chirashi

Minimalist DOM and events manager focused on performances

View the Project on GitHub

forEach

lib/core/forEach.js:26-37

Iterates over items and apply callback on each one.

Parameters

Examples

//esnext
import { forEach } from 'chirashi'

const items = forEach([0, 1, 2], (item, i) => console.log(`${i}: ${item + 1}`)) //returns: [0, 1, 2]
// logs:
//   0: 1
//   1: 2
//   2: 3
forEach(0, (item, i) => console.log(`${i}: ${item + 1}`)) //returns: [0]
//   0: 1
//es5
var items = Chirashi.forEach([0, 1, 2], function (item, i) { console.log(i+': '+(item + 1)) }) //returns: [0, 1, 2]
// logs:
//   0: 1
//   1: 2
//   2: 3
Chirashi.forEach(0, function (item, i) { console.log(i+': '+(item + 1)) }) //returns: [0]
// logs:
//   0: 1

Returns any iterable - items if it’s an array-like or an array containing items.

forEachCallback

lib/core/forEach.js:26-37

Callback to apply on item.

Parameters

forElements

lib/core/forElements.js:43-45

Iterates over dom elements and apply callback on each one.

Parameters

Examples

//esnext
import { createElement, append, forElements } from 'chirashi'
const sushi = createElement('.sushi')
const unagi = createElement('.unagi')
const yakitori = createElement('.yakitori')
const sashimi = createElement('.sashimi')
append(document.body, [sushi, unagi, yakitori, sashimi])
forElements('div', console.log) //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="yakitori"></div>, <div class="sashimi"></div>]
// logs:
// <div class="sushi"></div> 0
// <div class="unagi"></div> 1
// <div class="yakitori"></div> 2
// <div class="sashimi"></div> 3
forElements([yakitori, sashimi], console.log) //returns: [<div class="yakitori"></div>, <div class="sashimi"></div>]
// logs:
// <div class="yakitori"></div> 0
// <div class="sashimi"></div> 1
//es5
var sushi = Chirashi.createElement('.sushi')
var unagi = Chirashi.createElement('.unagi')
var yakitori = Chirashi.createElement('.yakitori')
var sashimi = Chirashi.createElement('.sashimi')
Chirashi.append(document.body, [sushi, unagi, yakitori, sashimi])
Chirashi.forElements('div', console.log) //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="yakitori"></div>, <div class="sashimi"></div>]
// logs:
// <div class="sushi"></div> 0
// <div class="unagi"></div> 1
// <div class="yakitori"></div> 2
// <div class="sashimi"></div> 3
Chirashi.forElements([yakitori, sashimi], console.log) //returns: [<div class="yakitori"></div>, <div class="sashimi"></div>]
// logs:
// <div class="yakitori"></div> 0
// <div class="sashimi"></div> 1

Returns Array iterable - The getElements’ result for chaining.

forElementsCallback

lib/core/forElements.js:43-45

Callback to apply on element.

Parameters

forIn

lib/core/forIn.js:28-34

Iterates over object’s keys and apply callback on each one.

Parameters

Examples

//esnext
import { forIn } from 'chirashi'
const californiaRoll = { name: 'California Roll', price: 4.25, recipe: ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed'] }
forIn(californiaRoll, (key, value) => {
  console.log(`${key} -> ${value}`)
}) //returns: { name: 'California Roll', price: 4.25, recipe: ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed'] }
// LOGS:
// name -> California Roll
// price -> 4.25
// recipe -> ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed']
//es5
var californiaRoll = { name: 'California Roll', price: 4.25, recipe: ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed'] }
Chirashi.forIn(californiaRoll, (key, value) => {
  console.log(key + ' -> ' + value)
}) //returns: { name: 'California Roll', price: 4.25, recipe: ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed'] }
// LOGS:
// name -> California Roll
// price -> 4.25
// recipe -> ['avocado', 'cucumber', 'crab', 'mayonnaise', 'sushi rice', 'seaweed']

Returns Object object - The iterable for chaining.

forInCallback

lib/core/forIn.js:36-38

Callback to apply on each key-value pair.

Parameters

getElement

lib/core/getElement.js:30-44

Get first dom element from iterable or selector.

Parameters

Examples

//esnext
import { createElement, append, getElement } from 'chirashi'
const sushi = createElement('.sushi')
const unagi = createElement('.unagi')
const yakitori = createElement('.yakitori')
const sashimi = createElement('.sashimi')
append(document.body, [sushi, unagi, yakitori, sashimi])
getElement('div') //returns: <div class="sushi"></div>
getElement('.yakitori, .sashimi') //returns: <div class="yakitori"></div>
getElement([sushi, unagi, '.sashimi', '.unknown']) //returns: <div class="sushi"></div>
getElement('.wasabi') //returns: undefined
//es5
var sushi = Chirashi.createElement('.sushi')
var unagi = Chirashi.createElement('.unagi')
var yakitori = Chirashi.createElement('.yakitori')
var sashimi = Chirashi.createElement('.sashimi')
Chirashi.append(document.body, [sushi, unagi, yakitori, sashimi])
Chirashi.getElement('div') //returns: <div class="sushi"></div>
Chirashi.getElement('.yakitori, .sashimi') //returns: <div class="yakitori"></div>
Chirashi.getElement([sushi, unagi, '.sashimi', '.unknown']) //returns: <div class="sushi"></div>
Chirashi.getElement('.wasabi') //returns: undefined

Returns (Window | Node | boolean) element - The dom element from input.

getElements

lib/core/getElements.js:33-54

Get dom element recursively from iterable or selector.

Parameters

Examples

//esnext
import { createElement, append, getElements } from 'chirashi'
const sushi = createElement('.sushi')
const unagi = createElement('.unagi')
const yakitori = createElement('.yakitori')
const sashimi = createElement('.sashimi')
append(document.body, [sushi, unagi, yakitori, sashimi])
getElements('div') //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="yakitori"></div>, <div class="sashimi"></div>]
getElements('.yakitori, .sashimi') //returns: [<div class="yakitori"></div>, <div class="sashimi"></div>]
getElements([sushi, unagi, '.sashimi', '.wasabi']) //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="sashimi"></div>]
getElements('.wasabi') //returns: []
//es5
var sushi = Chirashi.createElement('.sushi')
var unagi = Chirashi.createElement('.unagi')
var yakitori = Chirashi.createElement('.yakitori')
var sashimi = Chirashi.createElement('.sashimi')
Chirashi.append(document.body, [sushi, unagi, yakitori, sashimi])
Chirashi.getElements('div') //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="yakitori"></div>, <div class="sashimi"></div>]
Chirashi.getElements('.yakitori, .sashimi') //returns: [<div class="yakitori"></div>, <div class="sashimi"></div>]
Chirashi.getElements([sushi, unagi, '.sashimi', '.wasabi']) //returns: [<div class="sushi"></div>, <div class="unagi"></div>, <div class="sashimi"></div>]
Chirashi.getElements('.wasabi') //returns: []

Returns Array domElements - The array of dom elements from input.

isDomElement

lib/core/isDomElement.js:19-21

Test if element is a dom element. Doesn’t resolve selectors.

Parameters

Examples

//esnext
import { createElement, append, isDomElement } from 'chirashi'
const sushi = createElement('.sushi')
append(document.body, sushi)
isDomElement(window) //returns: true
isDomElement(sushi) //returns: true
isDomElement('.sushi') //returns: false
//es5
var sushi = Chirashi.createElement('.sushi')
Chirashi.append(document.body, sushi)
Chirashi.isDomElement(window) //returns: true
Chirashi.isDomElement(sushi) //returns: true
Chirashi.isDomElement('.sushi') //returns: false

Returns boolean isDomElement - true if element is an instance of Node or Window.

addClass

lib/dom/addClass.js:20-22

Iterates over classes and add it on each element of elements or ignore it if already set.

Parameters

Examples

//esnext
import { createElement, addClass } from 'chirashi'
const maki = createElement('.maki')
addClass(maki, 'wasabi') //returns: <div class="wasabi"></div>
addClass(maki, 'seaweed', 'cheese') //returns: <div class="wasabi seaweed cheese"></div>
addClass('.maki', 'avocado', 'salmon') //returns: <div class="wasabi seaweed cheese avocado salmon"></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.addClass(maki, 'wasabi') //returns: <div class="wasabi"></div>
Chirashi.addClass(maki, 'seaweed', 'cheese') //returns: <div class="wasabi seaweed cheese"></div>
Chirashi.addClass('.maki', 'avocado', 'salmon') //returns: <div class="wasabi seaweed cheese avocado salmon"></div>

Returns Array iterable - The getElements’ result for chaining.

append

lib/dom/append.js:22-30

Appends each node to a parent node.

Parameters

Examples

//esnext
import { createElement, append } from 'chirashi'
const maki = createElement('.maki')
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
const avocado = createElement('.avocado')
append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
var avocado = Chirashi.createElement('.avocado')
Chirashi.append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>

Returns (Node | boolean) node - The node for chaining or false if nodes can’t be appended.

children

lib/dom/children.js:18-20

Returns an element’s children as Array.

Parameters

Examples

//esnext
import { createElement, append, children } from 'chirashi'
const maki = createElement('.maki')
append(maki, ['.salmon', '.avocado'])
children(maki) //returns: [<div class="salmon"></div>, <div class="avocado"></div>]
//es5
const maki = Chirashi.createElement('.maki')
Chirashi.append(maki, ['.salmon', '.avocado'])
Chirashi.children(maki) //returns: [<div class="salmon"></div>, <div class="avocado"></div>]

Returns Array<Element> children - Element’s children as array.

clone

lib/dom/clone.js:21-23

Clones element.

Parameters

Examples

//esnext
import { createElement, append, clone } from 'chirashi'
const maki = createElement('.maki')
clone(maki) //returns: <div class="maki"></div>
const sushi = createElement('.sushi')
append(document.body, sushi)
clone('.sushi') //returns: <div class="sushi"></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.clone(maki) //returns: <div class="maki"></div>
var sushi = Chirashi.createElement('.sushi')
Chirashi.append(document.body, sushi)
Chirashi.clone('.sushi') //returns: <div class="sushi"></div>

Returns (Node | boolean) clone - element’s clone or false if element isn’t a node.

closest

lib/dom/closest.js:27-39

Get closest element matching the tested selector or tested element traveling up the DOM tree from element to limit.

Parameters

Examples

//esnext
import { createElement, append, closest } from 'chirashi'
const maki = createElement('.maki')
const cheese = createElement('.cheese')
append(maki, cheese)
append(cheese, '.avocado')
append(document.body, maki)
closest('.avocado', '.maki') //returns: <div class="maki"></div>
closest('.avocado', '.maki', '.cheese') //returns: false
//es5
var maki = Chirashi.createElement('.maki')
var cheese = Chirashi.createElement('.cheese')
Chirashi.append(maki, cheese)
Chirashi.append(cheese, '.avocado')
Chirashi.append(document.body, maki)
Chirashi.closest('.avocado', '.maki') //returns: <div class="maki"></div>
Chirashi.closest('.avocado', '.maki', '.cheese') //returns: false

Returns (Element | boolean) matchedElement - The matched element or false.

createElement

lib/dom/createElement.js:15-58

Creates a dom element from an HTML string, tag or css like selector with text between {} if needed.

Parameters

Examples

//esnext
import { createElement } from 'chirashi'
const maki = createElement('a#sushi.link[data-href="chirashijs.org"][data-link]{click me!}') //returns: <a class="link" id="sushi" data-href="chirashijs.org" data-link>click me!</a>
const greetings = createElement('<h1>Hello <strong>World</strong>!</h1>') //returns: <h1>Hello <strong>World</strong>!</h1>
//es5
var maki = Chirashi.createElement('a#sushi.link[data-href="chirashijs.org"][data-link]{click me!}') //returns: <a class="link" id="sushi" data-href="chirashijs.org" data-link>click me!</a>
var greetings = Chirashi.createElement('<h1>Hello <strong>World</strong>!</h1>') //returns: <h1>Hello <strong>World</strong>!</h1>

Returns Element element - The dom element created from the string.

empty

lib/dom/empty.js:17-19

Remove every child of elements.

Parameters

Examples

//esnext
import { createElement, append, empty } from 'chirashi'
const maki = createElement('.maki') //returns: <div class="maki"></div>
append(maki, '.cheese') //returns: <div class="maki"><div class="cheese"></div></div>
empty(maki) //returns: [<div class="maki"></div>]
//es5
const maki = Chirashi.createElement('.maki') //returns: <div class="maki"></div>
Chirashi.append(maki, '.cheese') //returns: <div class="maki"><div class="cheese"></div></div>
Chirashi.empty(maki) //returns: [<div class="maki"></div>]

Returns Array iterable - The getElements’ result for chaining.

filter

lib/dom/filter.js:28-28

Iterates over elements, returning an array of all elements matching tested selector.

Parameters

Examples

//esnext
import { createElement, append, filter } from 'chirashi'
const salmonMaki = createElement('.salmon.maki')
const tunaMaki = createElement('.tuna.maki')
const salmonSushi = createElement('.salmon.sushi')
const tunaSushi = createElement('.tuna.sushi')
append(document.body, [salmonMaki, tunaMaki, salmonSushi, tunaSushi])
filter('div', '.salmon') //returns: [<div class="salmon sushi"></div>, <div class="salmon maki"></div>]
filter([salmonMaki, tunaMaki, salmonSushi, tunaSushi], '.maki') //returns: [<div class="salmon maki"></div>, <div class="tuna maki"></div>]
filter('div', '.salmon') //returns: [<div class="salmon sushi"></div>, <div class="salmon maki"></div>]
//es5
var salmonMaki = Chirashi.createElement('.salmon.maki')
var tunaMaki = Chirashi.createElement('.tuna.maki')
var salmonSushi = Chirashi.createElement('.salmon.sushi')
var tunaSushi = Chirashi.createElement('.tuna.sushi')
Chirashi.append(document.body, [salmonMaki, tunaMaki, salmonSushi, tunaSushi])
Chirashi.filter('div', '.salmon') //returns: [<div class="salmon sushi"></div>, <div class="salmon maki"></div>]
Chirashi.filter([salmonMaki, tunaMaki, salmonSushi, tunaSushi], '.maki') //returns: [<div class="salmon maki"></div>, <div class="tuna maki"></div>]
Chirashi.filter('div', '.salmon') //returns: [<div class="salmon sushi"></div>, <div class="salmon maki"></div>]

Returns Array<Element> matching - The array of filtered elements.

find

lib/dom/find.js:27-27

Iterates over each element of elements and returns an array containing all elements’ children matching a selector.

Parameters

Examples

//esnext
import { createElement, append, find } from 'chirashi'
const maki = createElement('.maki')
append(maki, ['.salmon[data-fish][data-inside]', '.avocado[data-inside]'])
const roll = createElement('.roll')
append(roll, '.tuna[data-fish][data-inside]')
append(document.body, [maki, roll])
find('div', '[data-fish]') //returns: [<div class="salmon" data-fish data-inside></div>, <div class="tuna" data-fish data-inside></div>]
find(maki, '[data-inside]') //returns: [<div class="salmon" data-fish data-inside></div>, <div class="avocado" data-inside></div>]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, ['.salmon[data-fish][data-inside]', '.avocado[data-inside]'])
var roll = Chirashi.createElement('.roll')
Chirashi.append(roll, '.tuna[data-fish][data-inside]')
Chirashi.append(document.body, [maki, roll])
Chirashi.find('div', '[data-fish]') //returns: [<div class="salmon" data-fish data-inside></div>, <div class="tuna" data-fish data-inside></div>]
Chirashi.find(maki, '[data-inside]') //returns: [<div class="salmon" data-fish data-inside></div>, <div class="avocado" data-inside></div>]

Returns Array<Element> found - The array of elements’ descendants matching the selector.

findOne

lib/dom/findOne.js:27-29

Find the first element’s child matching the selector.

Parameters

Examples

//esnext
import { createElement, append, find } from 'chirashi'
const maki = createElement('.maki')
append(maki, ['.salmon[data-fish][data-inside]', '.avocado[data-inside]'])
const roll = createElement('.roll')
append(roll, '.tuna[data-fish][data-inside]')
append(document.body, [maki, roll])
findOne('div', '[data-fish]') //returns: <div class="salmon" data-fish data-inside></div>
findOne(maki, '[data-inside]') //returns: <div class="salmon" data-fish data-inside></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, ['.salmon[data-fish][data-inside]', '.avocado[data-inside]'])
var roll = Chirashi.createElement('.roll')
Chirashi.append(roll, '.tuna[data-fish][data-inside]')
Chirashi.append(document.body, [maki, roll])
Chirashi.findOne('div', '[data-fish]') //returns: <div class="salmon" data-fish data-inside></div>
Chirashi.findOne(maki, '[data-inside]') //returns: <div class="salmon" data-fish data-inside></div>
Returns **(Element null)** element - The first child of elements matching the selector or null.

getAttr

lib/dom/getAttr.js:16-16

Get value for named attribute of element.

Parameters

Examples

//esnext
import { createElement, getAttr } from 'chirashi'
const maki = createElement('.maki[data-fish="salmon"]')
getAttr(maki, 'data-fish') //returns: "salmon"
//es5
const maki = Chirashi.createElement('.maki[data-fish="salmon"]')
Chirashi.getAttr(maki, 'data-fish') //returns: "salmon"
Returns **(Object<string, (string | boolean)> string | boolean)** value - The value for the attribute if only one name, object of attributes’ name-value pairs or false if no element found.

getData

lib/dom/getData.js:16-16

Alias on getAttr prefixing name with ‘data-‘.

Parameters

Examples

//esnext
import { createElement, getData } from 'chirashi'
const maki = createElement('.maki[data-fish="salmon"]')
getData(maki, 'fish') //returns: "salmon"
//es5
const maki = Chirashi.createElement('.maki[data-fish="salmon"]')
Chirashi.getData(maki, 'fish') //returns: "salmon"
Returns **(Object<string, (string | boolean)> string | boolean)** value - The value for the data-attribute if only one name, object of data-attributes’ name-value pairs or false if no element found.

getHtml

lib/dom/getHtml.js:17-19

Get the inner html of an element.

Parameters

Examples

//esnext
import { createElement, setHtml, getHtml } from 'chirashi'
const maki = createElement('p.maki')
setHtml(maki, 'salmon')
getHtml(maki) //returns: "salmon"
//es5
var maki = createElement('p.maki')
setHtml(maki, 'salmon')
getHtml(maki) //returns: "salmon"
Returns **(string null)** innerHTML - The inner html of the element or null if no element found.

getProp

lib/dom/getProp.js:18-18

Get the value for the property name on the element.

Parameters

Examples

//esnext
import { createElement, append, getProp } from 'chirashi'
const maki = createElement('.maki')
append(maki, '.salmon')
getProp(maki, 'firstChild') //returns: <div class="salmon"></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, '.salmon')
Chirashi.getProp(maki, 'firstChild') //returns: <div class="salmon"></div>

Returns any value - The value for property on element if only one property, object of property-value pairs or false if no element found.

hasClass

lib/dom/hasClass.js:18-28

Iterates over classes and test if element has each.

Parameters

Examples

//esnext
import { createElement, hasClass } from 'chirashi'
const maki = createElement('.salmon.cheese.maki')
hasClass(maki, 'salmon', 'cheese') //returns: true
hasClass(maki, 'salmon', 'avocado') //returns: false
//es5
var maki = Chirashi.createElement('.salmon.cheese.maki')
Chirashi.hasClass(maki, 'salmon', 'cheese') //returns: true
Chirashi.hasClass(maki, 'salmon', 'avocado') //returns: false

Returns boolean hasClass - Is true if element has all classes.

indexInParent

lib/dom/indexInParent.js:19-28

Returns index of element in parent’s children.

Parameters

Examples

//esnext
import { createElement, append, indexInParent } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
indexInParent('.cheese') //returns: 1
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.indexInParent('.cheese') //returns: 1
Returns **(number null)** index - The position of element in his parent’s children or null if no element found.

insertAfter

lib/dom/insertAfter.js:21-21

Insert nodes after element in his parent.

Parameters

Examples

//esnext
import { createElement, append, insertAfter } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]'])
insertAfter('.salmon', ['.avocado', '.wasabi']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="wasabi"></div><div class="cheese" data-cheese="cream"></div></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]'])
Chirashi.insertAfter('.salmon', ['.avocado', '.wasabi']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="wasabi"></div><div class="cheese" data-cheese="cream"></div></div>

Returns (Node | undefined) element - The element for chaining or undefined if no element found or element has no parent.

insertBefore

lib/dom/insertBefore.js:21-21

Insert nodes before element in his parent.

Parameters

Examples

//esnext
import { createElement, append, insertBefore } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]'])
insertBefore('.cheese', ['.avocado', '.wasabi']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="wasabi"></div><div class="cheese" data-cheese="cream"></div></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]'])
Chirashi.insertBefore('.cheese', ['.avocado', '.wasabi']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="wasabi"></div><div class="cheese" data-cheese="cream"></div></div>

Returns (Node | undefined) element - The element for chaining or undefined if no element found or element has no parent.

next

lib/dom/next.js:21-23

Get the next sibling of element.

Parameters

Examples

//esnext
import { createElement, append, next } from 'chirashi'
const maki = createElement('.maki')
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
const avocado = createElement('.avocado')
append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
next(avocado) //returns: <div class="cheese" data-cheese="cream"></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
var avocado = Chirashi.createElement('.avocado')
Chirashi.append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.next(avocado) //returns: <div class="cheese" data-cheese="cream"></div>
Returns **(Node null)** nextElement - The element’s next sibling or null if no element found.

parent

lib/dom/parent.js:20-22

Returns the parent node of the element.

Parameters

Examples

//esnext
import { createElement, append, parent } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
parent('.salmon') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
//es5
var maki = Chirashi.createElement('.maki')
append(maki
Chirashi.append(document.body, maki)
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
Chirashi.parent('.salmon') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
Returns **(document | Element null)** parentElement - The parent node or null if no element found.

parents

lib/dom/parents.js:21-23

Returns an array of every element’s ancestors.

Parameters

Examples

//esnext
import { createElement, append, parents } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
parents('.salmon') //returns: [<div class="maki">...</div>, <body>...</body>, <html>...</html>, document]
//es5
var maki = Chirashi.createElement('.maki')
append(maki
Chirashi.append(document.body, maki)
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
Chirashi.parents('.salmon') //returns: [<div class="maki">...</div>, <body>...</body>, <html>...</html>, document]

Returns Array<Node> parents - Array of element’s ancestors.

prev

lib/dom/prev.js:21-23

Get the previous sibling of element.

Parameters

Examples

//esnext
import { createElement, append, prev } from 'chirashi'
const maki = createElement('.maki')
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
const avocado = createElement('.avocado')
append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
prev(avocado) //returns: <div class="salmon" data-fish="salmon"></div>
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
var avocado = Chirashi.createElement('.avocado')
Chirashi.append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.prev(avocado) //returns: <div class="salmon" data-fish="salmon"></div>
Returns **(Node null)** previousElement - The element’s previous sibling or null if no element found.

remove

lib/dom/remove.js:23-25

Iterates over elements and removes it from DOM.

Parameters

Examples

//esnext
import { createElement, append, remove } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
const avocado = createElement('.avocado')
append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
remove('.cheese') //returns: [<div class="cheese" data-cheese="cream"></div>]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, '.salmon[data-fish="salmon"]') //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div></div>
var avocado = Chirashi.createElement('.avocado')
Chirashi.append(maki, [avocado, '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="avocado"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.remove('.cheese') //returns: [<div class="cheese" data-cheese="cream"></div>]
Returns **(Array | NodeList HTMLCollection)** removedElements - The array or nodelist of removed dom elements.

removeAttr

lib/dom/removeAttr.js:20-22

Iterates over attributes and removes it from each element of elements.

Parameters

Examples

//esnext
import { createElement, append, removeAttr } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
removeAttr('.salmon', 'data-fish') //returns: [<div class="salmon"></div>]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.removeAttr('.salmon', 'data-fish') //returns: [<div class="salmon"></div>]

Returns Array iterable - The getElements’ result for chaining.

removeClass

lib/dom/removeClass.js:16-18

Iterates over classes and remove it from each element of elements.

Parameters

Examples

//esnext
import { createElement, removeClass } from 'chirashi'
const maki = createElement('.maki.salmon.cheese.wasabi') //returns: <div class="maki salmon cheese wasabi"></div>
removeClass(maki, 'cheese', 'wasabi') //returns: [<div class="maki salmon"></div>]
//es5
var maki = Chirashi.createElement('.maki.salmon.cheese.wasabi') //returns: <div class="maki salmon cheese wasabi"></div>
Chirashi.removeClass(maki, 'cheese', 'wasabi') //returns: [<div class="maki salmon"></div>]

Returns Array iterable - The getElements’ result for chaining.

removeData

lib/dom/removeData.js:21-25

Iterates over attributes and removes it from each element of elements.

Parameters

Examples

//esnext
import { createElement, append, removeData } from 'chirashi'
const maki = createElement('.maki')
append(document.body, maki)
append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
removeData('.salmon', 'fish') //returns: [<div class="salmon"></div>]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.append(document.body, maki)
Chirashi.append(maki, ['.salmon[data-fish="salmon"]', '.cheese[data-cheese="cream"]']) //returns: <div class="maki"><div class="salmon" data-fish="salmon"></div><div class="cheese" data-cheese="cream"></div></div>
Chirashi.removeData('.salmon', 'fish') //returns: [<div class="salmon"></div>]

Returns Array iterable - The getElements’ result for chaining.

setAttr

lib/dom/setAttr.js:21-21

Iterates over attributes as key value pairs and apply on each element of elements.

Parameters

Examples

//esnext
import { createElement, setAttr } from 'chirashi'
const maki = createElement('.maki')
setAttr(maki, {
  dataFish: 'salmon'
}) //returns: [<div class="maki" data-fish="salmon">]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.setAttr(maki, {
  dataFish: 'salmon'
}) //returns: [<div class="maki" data-fish="salmon">]

Returns Array iterable - The getElements’ result for chaining.

setData

lib/dom/setData.js:21-27

Iterates over data-attributes as key value pairs and apply on each element of elements.

Parameters

Examples

//esnext
import { createElement, setData } from 'chirashi'
const maki = createElement('.maki')
setData(maki, {
  fish: 'salmon'
}) //returns: [<div class="maki" data-fish="salmon">]
//es5
var maki = Chirashi.createElement('.maki')
Chirashi.setData(maki, {
  fish: 'salmon'
}) //returns: [<div class="maki" data-fish="salmon">]

Returns Array iterable - The getElements’ result for chaining.

setHtml

lib/dom/setHtml.js:18-20

Set the inner html of elements.

Parameters

Examples

//esnext
import { createElement, setHtml, getHtml } from 'chirashi'
const maki = createElement('p.maki')
setHtml(maki, 'salmon') //returns: [<p class="maki">salmon</p>]
getHtml(maki) //returns: "salmon"
//es5
var maki = createElement('p.maki')
setHtml(maki, 'salmon') //returns: [<p class="maki">salmon</p>]
getHtml(maki) //returns: "salmon"

Returns Array iterable - The getElements’ result for chaining.

setProp

lib/dom/setProp.js:18-20

Apply props as key value pairs on each element of elements.

Parameters

Examples

//esnext
import { createElement, setProp, getProp } from 'chirashi'
const maki = createElement('input.maki')
setProp(maki, { value: 'こんにちは世界' })
getProp(maki, 'value') //returns: こんにちは世界
//es5
var maki = Chirashi.createElement('input.maki')
Chirashi.setProp(maki, { value: 'こんにちは世界' })
Chirashi.getProp(maki, 'value') //returns: こんにちは世界

Returns Array iterable - The getElements’ result for chaining.

toggleClass

lib/dom/toggleClass.js:37-43

Iterates over classes and toggle it on each element of elements.

Parameters

Examples

//esnext
import { createElement, toggleClass, clone, setData, getData } from 'chirashi'
const maki = createElement('.wasabi.salmon.maki') //returns: <div class="maki salmon wasabi"></div>
const sushi = createElement('.salmon.sushi') //returns: <div class="sushi salmon"></div>
toggleClass([maki, sushi], 'wasabi') //returns: [<div class="maki salmon"></div>, <div class="sushi salmon wasabi"></div>]
const scdMaki = clone(maki)
setData(maki, { for: 'leonard' })
setData(scdMaki, { for: 'sheldon' })
toggleClass([maki, scdMaki], {
  cheese: element => {
    return getData(element, 'for') !== 'leonard'
  }
}) //returns: [<div class="maki salmon cheese" data-for="sheldon"></div>, <div class="maki salmon" data-for="leonard"></div>]
//es5
var maki = Chirashi.createElement('.wasabi.salmon.maki') //returns: <div class="wasabi salmon maki"></div>
var sushi = Chirashi.createElement('.salmon.sushi') //returns: <div class="salmon sushi"></div>
Chirashi.toggleClass([maki, sushi], 'wasabi') //returns: [<div class="maki salmon"></div>, <div class="sushi salmon wasabi"></div>]
var scdMaki = Chirashi.clone(maki)
Chirashi.setData(maki, { for: 'leonard' })
Chirashi.setData(scdMaki, { for: 'sheldon' })
Chirashi.toggleClass([maki, scdMaki], {
  cheese: function (element) {
    return Chirashi.getData(element, 'for') !== 'leonard'
  }
}) //returns: [<div class="maki salmon cheese" data-for="sheldon"></div>, <div class="maki salmon" data-for="leonard"></div>]

Returns Array iterable - The getElements’ result for chaining.

delegate

lib/events/delegate.js:50-61

Delegate events listener on delegate and execute callback when target matches selector (targets doesn’t have to be in the DOM).

Parameters

Examples

//esnext
import { createElement, append, delegate, trigger } from 'chirashi'
const off = delegate('.cheese, .wasabi', {
  click(e, target) => {
    console.log('clicked', target)
  },
  'mouseenter mousemove': (e, target) => {
    console.log('mouse in', target)
  }
})
const maki = createElement('a.cheese.maki')
const sushi = createElement('a.wasabi.sushi')
append(document.body, [maki, sushi])
trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
off('mouseenter mousemove') //remove mouseenter and mousemove listeners
off() //remove all listeners
//es5
var off = Chirashi.delegate('.cheese, .wasabi', {
  'click': function (e, target) {
    console.log('clicked', target)
  },
  'mouseenter mousemove': function(e, target) {
    console.log('mouse in', target)
  }
})
var maki = Chirashi.createElement('a.cheese.maki')
var sushi = Chirashi.createElement('a.wasabi.sushi')
Chirashi.append(document.body, [maki, sushi])
Chirashi.trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
Chirashi.trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
off('mouseenter mousemove') //remove mouseenter and mousemove listeners
off() //remove all listeners

Returns offCallback off - The unbind function.

offCallback

lib/events/delegate.js:74-79

Called to off one or all events.

Parameters

offCallback

lib/events/on.js:52-58

Called to remove one or all events listeners of one or all elements.

Parameters

delegateCallback

lib/events/delegate.js:74-79

Callback to execute on event using delegate.

Parameters

on

lib/events/on.js:52-58

Bind events listener on each element of elements.

Parameters

Examples

//esnext
import { createElement, append, on, trigger } from 'chirashi'
const maki = createElement('a.cheese.maki')
const sushi = createElement('a.wasabi.sushi')
append(document.body, [maki, sushi])
const off = on('.cheese, .wasabi', {
  click(e, target) {
    console.log('clicked', target)
  },
  'mouseenter mousemove': {
    handler: (e, target) => {
      console.log('mouse in', target)
    },
    passive: true
  }
})
trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
off(maki, 'click') //remove click event listener on maki
off() //remove all listeners from all elements
//es5
var off = Chirashi.bind('.cheese, .wasabi', {
  'click': function (e, target) {
    console.log('clicked', target)
  },
  'mouseenter mousemove': {
    handler: (e, target) => {
      console.log('mouse in', target)
    },
    passive: true
  }
})
var maki = Chirashi.createElement('a.cheese.maki')
var sushi = Chirashi.createElement('a.wasabi.sushi')
Chirashi.append(document.body, [maki, sushi])
Chirashi.trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
Chirashi.trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
off(maki, 'click') //remove click event listener on maki
off() //remove all listeners from all elements

Returns offCallback off - The unbinding function.

EventObject

lib/events/on.js:52-58

Options to bind event.

Properties

eventCallback

lib/events/on.js:52-58

Callback to execute on event.

Parameters

once

lib/events/once.js:71-84

Bind events listener on each element of elements and unbind after first triggered.

Parameters

Examples

//esnext
import { createElement, append, once, trigger } from 'chirashi'
const maki = createElement('a.cheese.maki')
const sushi = createElement('a.wasabi.sushi')
append(document.body, [maki, sushi])
const cancel = once('.cheese, .wasabi', {
  click(e, target) => {
    console.log('clicked', target)
  },
  'mouseenter mousemove': (e, target) => {
    console.log('mouse in', target)
  }
}, true, true)
trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
// click event listener was auto-removed from maki
trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
// click event listener was auto-removed from sushi
cancel() //remove all listeners from all elements
once('.cheese, .wasabi', {
  click(e, target) => {
    console.log('clicked', target)
  }
})
trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
// all events listeners were auto-removed from all elements
trigger(sushi, 'click') //simulate user's click
// won't log anything
//es5
var maki = Chirashi.createElement('a.cheese.maki')
var sushi = Chirashi.createElement('a.wasabi.sushi')
Chirashi.append(document.body, [maki, sushi])
var cancel = Chirashi.once('.cheese, .wasabi', {
  click: function (e, target) {
    console.log('clicked', target)
  },
  'mouseenter mousemove': function (e, target) {
    console.log('mouse in', target)
  }
}, true, true)
Chirashi.trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
// click event listener was auto-removed from maki
Chirashi.trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
// click event listener was auto-removed from sushi
cancel() //remove all listeners from all elements
Chirashi.once('.cheese, .wasabi', {
  click: function (e, target) {
    console.log('clicked', target)
  }
})
Chirashi.trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
// all events listeners were auto-removed from all elements
Chirashi.trigger(sushi, 'click') //simulate user's click
// won't log anything

Returns offCallback cancel - cancel function for unbinding.

ready

lib/events/ready.js:20-29

Execute callback when dom is ready.

Parameters

Examples

//esnext
import { ready } from 'chirashi'
ready(() => {
  console.log('Hello World!')
})
// Dom already complete or event fired.
// LOGS: "Hello World!"
//es5
Chirashi.ready(function () {
  console.log('Hello World!')
})
// Dom already complete or event fired.
// LOGS: "Hello World!"

trigger

lib/events/trigger.js:44-54

Trigger events on elements with data

Parameters

Examples

//esnext
import { createElement, append, on, trigger } from 'chirashi'
const maki = createElement('a.cheese.maki')
const sushi = createElement('a.wasabi.sushi')
append(document.body, [maki, sushi])
const listener = on('.cheese, .wasabi', {
  click(e, target) => {
    console.log('clicked', target)
  }
})
trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>
//es5
var listener = Chirashi.bind('.cheese, .wasabi', {
  'click': function (e, target) {
    console.log('clicked', target)
  }
})
var maki = Chirashi.createElement('a.cheese.maki')
var sushi = Chirashi.createElement('a.wasabi.sushi')
Chirashi.append(document.body, [maki, sushi])
Chirashi.trigger(maki, 'click') //simulate user's click
// LOGS: "clicked" <a class="maki cheese"></a>
Chirashi.trigger(sushi, 'click') //simulate user's click
// LOGS: "clicked" <a class="sushi wasabi"></a>

Returns Array iterable - The getElements’ result for chaining.

clearStyle

lib/styles/clearStyle.js:31-36

Clear inline style properties from elements.

Parameters

Examples

//esnext
import { createElement, setStyle, clearStyle } from 'chirashi'
const maki = createElement('a.cheese.maki')
setStyleProp(maki, {
  position: 'absolute',
  top: 10,
  width: 200,
  height: 200,
  background: 'red'
}) // returns: [<a class="cheese maki" style="position: 'absolute'; top: '10px'; width: '200px'; height: '200px'; background: 'red';"></a>]
clearStyle(maki, 'width', 'height', 'background') // returns: [<a class="cheese maki" style="position: 'absolute'; top: '10px';"></a>]
//es5
var maki = Chirashi.createElement('a.cheese.maki')
Chirashi.setStyleProp(maki, {
  position: 'absolute',
  top: 10,
  width: 200,
  height: 200,
  background: 'red'
}) // returns: [<a class="cheese maki" style="position: 'absolute'; top: '10px'; width: '200px'; height: '200px'; background: 'red';"></a>]
Chirashi.clearStyle(maki, 'width', 'height', 'background') // returns: [<a class="cheese maki" style="position: 'absolute'; top: '10px';"></a>]

Returns Array iterable - The getElements’ result for chaining.

clientRect

lib/styles/clientRect.js:56-72

Return the screen relative position of an element.

Parameters

Examples

esnext
import { setStyleProp, append, clientRect } from 'chirashi'

setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

append(document.body, '.poulp')

const poulp = setStyleProp('.poulp', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  background: 'red'
})

clientRect(poulp) // returns: { bottom: 300, height: 100, left: 240, right: 0, top: 200, width: 100 }
es5
Chirashi.setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

Chirashi.append(document.body, '.poulp')

var poulp = Chirashi.setStyleProp('.poulp', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  background: 'red'
})

Chirashi.clientRect(poulp) // returns: { bottom: 300, height: 100, left: 240, right: 0, top: 200, width: 100 }

Returns (Object | boolean) clientRect - Element’s screen position or false if no element found.

Returns number clientRect.bottom - Y-coordinate, relative to the viewport origin, of the bottom of the rectangle box.

Returns number clientRect.height - Height of the rectangle box (This is identical to bottom minus top).

Returns number clientRect.left - X-coordinate, relative to the viewport origin, of the left of the rectangle box.

Returns number clientRect.right - X-coordinate, relative to the viewport origin, of the right of the rectangle box.

Returns number clientRect.top - Y-coordinate, relative to the viewport origin, of the top of the rectangle box.

Returns number clientRect.width - Width of the rectangle box (This is identical to right minus left).

getComputedStyle

lib/styles/getComputedStyle.js:25-27

Get computed style of an element.

Parameters

Examples

//esnext
import { append, setStyleProp, getComputedStyle } from 'chirashi'
append(document.body, '.maki')
const maki = setStyleProp('.maki', {
  display: 'block',
  position: 'relative',
  top: 10
})
getComputedStyle(maki) //returns: { display: 'block', position: 'relative', top: '10px', [... other properties according to the browser] }
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.setStyleProp('.maki', {
  display: 'block',
  position: 'relative',
  top: 10
})
Chirashi.getComputedStyle(maki) //returns: { display: 'block', position: 'relative', top: '10px', [... other properties according to the browser] }

Returns Object<string, string> computedStyle - the computed style of the element.

getHeight

lib/styles/getHeight.js:32-34

Get element’s height in pixels.

Parameters

Examples

//esnext
import { append, setStyleProp, getHeight } from 'chirashi'
append(document.body, '.maki')
const maki = setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
getHeight(maki, true) //returns: 260
getHeight(maki) //returns: 220
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
Chirashi.getHeight(maki, true) //returns: 260
Chirashi.getHeight(maki) //returns: 220

Returns number height - The height in pixels.

getSize

lib/styles/getSize.js:33-38

Get element’s size in pixels.

Parameters

Examples

//esnext
import { append, setStyleProp, getSize } from 'chirashi'
append(document.body, '.maki')
const maki = setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
getSize(maki, true) //returns: { width: 260, height: 260 }
getSize(maki) //returns: { width: 220, height: 220 }
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
Chirashi.getSize(maki, true) //returns: { width: 260, height: 260 }
Chirashi.getSize(maki) //returns: { width: 220, height: 220 }

Returns number size - The size in pixels.

getStyleProp

lib/styles/getStyleProp.js:26-32

Get computed style props of an element. While getComputedStyle returns all properties, getStyleProp returns only needed and convert unitless numeric values or pixels values into numbers.

Parameters

Examples

//esnext
import { append, setStyleProp, getStyleProp } from 'chirashi'
append(document.body, '.maki')
const maki = setStyleProp('.maki', {
  display: 'block',
  position: 'relative',
  top: 10
})
getStyleProp(maki, 'display', 'top') //returns: { display: "block", top: 10 }
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.setStyleProp('.maki', {
  display: 'block',
  position: 'relative',
  top: 10
})
Chirashi.getStyleProp(maki, 'display', 'top') //returns: { display: "block", top: 10 }

Returns (string | number | Object<string, (string | number)>) computedStyle - Value of computed for provided prop if only one or parsed copy of element’s computed style if several.

getWidth

lib/styles/getWidth.js:32-34

Get element’s width in pixels.

Parameters

Examples

//esnext
import { append, setStyleProp, getWidth } from 'chirashi'
append(document.body, '.maki')
const maki = setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
getWidth(maki, true) //returns: 260
getWidth(maki) //returns: 220
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.setStyleProp('.maki', {
  display: 'block',
  border: '20px solid red',
  padding: 10,
  height: 200,
  width: 200
})
Chirashi.getWidth(maki, true) //returns: 260
Chirashi.getWidth(maki) //returns: 220

Returns number width - The width in pixels.

hide

lib/styles/hide.js:17-19

Hide each element of elements using visibility.

Parameters

Examples

//esnext
import { append, hide, getStyleProp }
append(document.body, '.maki')
const maki = hide('.maki')
getStyleProp(maki, 'visibility') // returns: "hidden"
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.hide('.maki')
Chirashi.getStyleProp(maki, 'visibility') // returns: "hidden"

Returns Array iterable - The getElements’ result for chaining.

offset

lib/styles/offset.js:45-52

Returns the top and left offset of an element. Offset is relative to web page.

Parameters

Examples

//esnext
import { setStyleProp, append, offset }
setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})
append(document.body, '.sushi')
const sushi = setStyleProp('.sushi', {
  display: 'block',
  width: 100,
  height: 100,
  position: 'absolute',
  top: 200,
  left: 240,
  background: 'red'
})
offset(sushi) // returns: { top: 200, left: 240 }
//es5
Chirashi.setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})
Chirashi.append(document.body, '.sushi')
var sushi = Chirashi.setStyleProp('.sushi', {
  display: 'block',
  width: 100,
  height: 100,
  position: 'absolute',
  top: 200,
  left: 240,
  background: 'red'
})
Chirashi.offset(sushi) // returns: { top: 200, left: 240 }

Returns (Object | boolean) offset - Offset object or false if no element found.

Returns Object.top top - Top offset in pixels.

Returns Object.left left - Left offset in pixels.

position

lib/styles/position.js:78-83

Return the top and left position of an element. Position is relative to parent.

Parameters

Examples

//esnext
import { append, setStyleProp, position } from 'chirashi'

setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

append(document.body, '.maki')
append('.maki', '.salmon')

setStyleProp('.maki', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  borderRadius: '50%',
  background: 'black'
})

const salmon = setStyleProp('.salmon', {
  display: 'block',
  position: 'absolute',
  top: 20,
  left: 10,
  width: 10,
  height: 10,
  borderRadius: '50%',
  background: 'pink'
})

position(salmon) // returns: { top: 20, left: 10 }
//es5
Chirashi.setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

Chirashi.append(document.body, '.maki')
Chirashi.append('.maki', '.salmon')

Chirashi.setStyleProp('.maki', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  borderRadius: '50%',
  background: 'black'
})

var salmon = Chirashi.setStyleProp('.salmon', {
  display: 'block',
  position: 'absolute',
  top: 20,
  left: 10,
  width: 10,
  height: 10,
  borderRadius: '50%',
  background: 'pink'
})

Chirashi.position(salmon) // returns: { top: 20, left: 10 }

Returns (Object | boolean) offset - Offset object or false if no element found.

Returns Object.top top - Top position in pixels.

Returns Object.left left - Left position in pixels.

screenPosition

lib/styles/screenPosition.js:52-59

Return the screen relative position of an element.

Parameters

Examples

esnext
import { setStyleProp, append, screenPosition } from 'chirashi'

setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

append(document.body, '.poulp')

const poulp = setStyleProp('.poulp', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  background: 'red'
})

screenPosition(poulp) // returns: { top: 200, left: 240 }
es5
Chirashi.setStyleProp([document.documentElement, document.body], {
  position: 'relative',
  margin: 0,
  padding: 0
})

Chirashi.append(document.body, '.poulp')

var poulp = Chirashi.setStyleProp('.poulp', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  background: 'red'
})

Chirashi.screenPosition(poulp) // returns: { top: 200, left: 240 }

Returns (Object | boolean) screenPosition - Element’s screen position or false if no element found.

Returns Object.top top - Y-coordinate, relative to the viewport origin, of the top of the rectangle box.

Returns Object.left left - X-coordinate, relative to the viewport origin, of the left of the rectangle box.

setCssVariable

lib/styles/setCssVariable.js:27-27

Set the provided css variables to elements.

Parameters

Examples

//esnext
import { append, setCssVariable } from 'chirashi'

append(document.body, '.maki')

setCssVariable('.maki', {
  opacity: 0.5,
  textColor: 'blue'
}) // returns: [<div class="maki" style="--opacity: 0.5; --text-color: 'rgb(0,0,255)'"></div>]
//es5
Chirashi.append(document.body, '.maki')

Chirashi.setCssVariable('.maki', {
  opacity: 0.5,
  textColor: 'blue'
}) // returns: [<div class="maki" style="--opacity: 0.5; --text-color: 'rgb(0,0,255)'"></div>]

Returns Array iterable - The getElements’ result for chaining.

setHeight

lib/styles/setHeight.js:21-23

Set the provided height to elements.

Parameters

Examples

//esnext
import { append, setHeight } from 'chirashi'

append(document.body, '.maki')

setHeight('.maki', 20) // returns: [<div class="maki" style="height: 20px;"></div>]
setHeight('.maki', '100%') // returns: [<div class="maki" style="height: 100%;"></div>]
//es5
Chirashi.append(document.body, '.maki')

Chirashi.setHeight('.maki', 20) // returns: [<div class="maki" style="height: 20px;"></div>]
Chirashi.setHeight('.maki', '100%') // returns: [<div class="maki" style="height: 100%;"></div>]

Returns Array iterable - The getElements’ result for chaining.

setSize

lib/styles/setSize.js:20-22

Set the provided size to elements.

Parameters

Examples

//esnext
import { append, setSize } from 'chirashi'

append(document.body, '.maki')

setSize('.maki', 20, '100%') // returns: [<div class="maki" style="width: 20px; height: 100%;"></div>]
//es5
Chirashi.append(document.body, '.maki')

Chirashi.setSize('.maki', 20, '100%') // returns: [<div class="maki" style="width: 20px; height: 100%;"></div>]

Returns Array iterable - The getElements’ result for chaining.

setStyleProp

lib/styles/setStyleProp.js:76-76

Set the provided style to elements.

Parameters

Examples

//esnext
import { append, setStyleProp } from 'chirashi'

append(document.body, '.maki')
append('.maki', '.salmon')

setStyleProp('.maki', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  borderRadius: '50%',
  background: 'black'
}) // returns: [<div class="maki" style="display: block; position: absolute; top: 200px; left: 240px; width: 100px; height: 100px; border-radius: 50%; background: black;"><div class="salmon"></div></div>]

const salmon = setStyleProp('.salmon', {
  display: 'block',
  position: 'absolute',
  top: 20,
  left: 10,
  width: 10,
  height: 10,
  borderRadius: '50%',
  background: 'pink'
}) // returns: [<div class="salmon" style="display: block; position: absolute; top: 20px; left: 10px; width: 10px; height: 10px; border-radius: 50%; background: pink;"></div>]
//es5
Chirashi.append(document.body, '.maki')
Chirashi.append('.maki', '.salmon')

Chirashi.setStyleProp('.maki', {
  display: 'block',
  position: 'absolute',
  top: 200,
  left: 240,
  width: 100,
  height: 100,
  borderRadius: '50%',
  background: 'black'
}) // returns: [<div class="maki" style="display: block; position: absolute; top: 200px; left: 240px; width: 100px; height: 100px; border-radius: 50%; background: black;"><div class="salmon"></div></div>]

const salmon = Chirashi.setStyleProp('.salmon', {
  display: 'block',
  position: 'absolute',
  top: 20,
  left: 10,
  width: 10,
  height: 10,
  borderRadius: '50%',
  background: 'pink'
}) // returns: [<div class="salmon" style="display: block; position: absolute; top: 20px; left: 10px; width: 10px; height: 10px; border-radius: 50%; background: pink;"></div>]

Returns Array iterable - The getElements’ result for chaining.

setWidth

lib/styles/setWidth.js:21-23

Set the provided width to elements.

Parameters

Examples

//esnext
import { append, setWidth } from 'chirashi'

append(document.body, '.maki')

setWidth('.maki', 20) // returns: [<div class="maki" style="width: 20px;"></div>]
setWidth('.maki', '100%') // returns: [<div class="maki" style="width: 100%;"></div>]
//es5
Chirashi.append(document.body, '.maki')

Chirashi.setWidth('.maki', 20) // returns: [<div class="maki" style="width: 20px;"></div>]
Chirashi.setWidth('.maki', '100%') // returns: [<div class="maki" style="width: 100%;"></div>]

Returns Array iterable - The getElements’ result for chaining.

show

lib/styles/show.js:17-19

Show each element of elements using visibility.

Parameters

Examples

//esnext
import { append, show, getStyleProp }
append(document.body, '.maki')
const maki = show('.maki')
getStyleProp(maki, 'visibility') // returns: "visible"
//es5
Chirashi.append(document.body, '.maki')
var maki = Chirashi.show('.maki')
Chirashi.getStyleProp(maki, 'visibility') // returns: "visible"

Returns Array iterable - The getElements’ result for chaining.

transform

lib/styles/transform.js:36-38

Compute and apply 3d transform matrix from provided transformation to each element of elements.

Parameters

Examples

//esnext
import { createElement, setHtml, transform } from 'chirashi'
const wasabiPea = createElement('p')
setHtml(wasabiPea, 'Wasabi')
transform(wasabiPea, {}) // returns: [<p style="transform: "matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {x: 5, y: 6, z: 7}) // returns: [<p style="transform: "matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,5,6,7,1)">Wasabi</p>]
transform(wasabiPea, {scale: 2}) // returns: [<p style="transform: "matrix3d(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {scale: {x: 2, y: 3, z: 4}}) // returns: [<p style="transform: "matrix3d(2,0,0,0,0,3,0,0,0,0,4,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {rotate: 45}) // returns: [<p style="transform: "matrix3d(0.53,0.85,0,0,-0.85,0.53,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {rotate: {x: 45, y: 20, z: 15}}) // returns: [<p style="transform: "matrix3d(-0.31,0.65,-0.91,0,-0.65,-0.4,0.85,0,0.91,-0.85,0.21,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {skew: 45}) // returns: [<p style="transform: "matrix3d(1,1.62,0,0,1.62,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {skew: {x: 25, y: 45}}) // returns: [<p style="transform: "matrix3d(1,1.62,0,0,-0.13,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
transform(wasabiPea, {x: 5, y: 6, z: 7, scale: {x: 2, y: 3}, rotate: {x: 45, y: 20, z: 15}, skew: {x: 25, y: 45}}) // returns: [<p style="transform: "matrix3d(-0.62,2.27,-0.91,0,-0.78,-1.2,0.85,0,0.91,-0.85,0.21,0,5,6,7,1)">Wasabi</p>]
//es5
var wasabiPea = Chirashi.createElement('p')
Chirashi.setHtml(wasabiPea, 'Wasabi')
Chirashi.transform(wasabiPea, {}) // returns: [<p style="transform: "matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {x: 5, y: 6, z: 7}) // returns: [<p style="transform: "matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,5,6,7,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {scale: 2}) // returns: [<p style="transform: "matrix3d(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {scale: {x: 2, y: 3, z: 4}}) // returns: [<p style="transform: "matrix3d(2,0,0,0,0,3,0,0,0,0,4,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {rotate: 45}) // returns: [<p style="transform: "matrix3d(0.53,0.85,0,0,-0.85,0.53,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {rotate: {x: 45, y: 20, z: 15}}) // returns: [<p style="transform: "matrix3d(-0.31,0.65,-0.91,0,-0.65,-0.4,0.85,0,0.91,-0.85,0.21,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {skew: 45}) // returns: [<p style="transform: "matrix3d(1,1.62,0,0,1.62,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {skew: {x: 25, y: 45}}) // returns: [<p style="transform: "matrix3d(1,1.62,0,0,-0.13,1,0,0,0,0,1,0,0,0,0,1)">Wasabi</p>]
Chirashi.transform(wasabiPea, {x: 5, y: 6, z: 7, scale: {x: 2, y: 3}, rotate: {x: 45, y: 20, z: 15}, skew: {x: 25, y: 45}}) // returns: [<p style="transform: "matrix3d(-0.62,2.27,-0.91,0,-0.78,-1.2,0.85,0,0.91,-0.85,0.21,0,5,6,7,1)">Wasabi</p>]

Returns Array iterable - The getElements’ result for chaining.

Returns function domElements.chrshPush - Methods to push dom elements into the array. Accepts same input as getElements.

Transformation

lib/styles/transform.js:75-103

Properties