This package aims to provide some convenient, practical helpers for DOM manipulation and interaction, querying elements and implementing safe continuation patterns to facilitate a slightly more functional programming style in vanilla web projects.
All functions are exported from one index file. Import the functions you want to use:
If you have suggestions how to improve this package, or
want to contribute by adding new useful functions you're absolutely welcome to do so. File an issue, or even better, submit a PR. If you like this package and think it is useful please leave a star on Github 😃
-
Returns a promise that resolves as soon as possible after the DOM content is loaded. If the document.readyState
is
'interactive'
or 'complete'
at call-time, the returned promise resolves immediately, otherwise it resolves upon the DOMContentLoaded event.
-
Curried function that first takes a list of classes, then returns a new function that takes the element to add those classes to.
-
Adds classes to an element for a given amount of milliseconds.
-
Adds classes to an element for a given amount of animation frames.
-
Takes a positive (>= 0) integer n
and a callback function, and executes the callback function after n animation frames have passed.
-
Returns a Promise<void>
that resolves after n animation frames have passed. Like deferFrames
but 'portable', so that many callbacks can subscribe to the 'event' of n frames having passed.
-
Takes a CSS display value and returns a function that takes elements. When applied to an element the returned function assigns the given display value to the given element's style.display
property.
-
Takes a boolean condition and a CSS display value, and returns a function that takes elements. The returned function will set style.display
onto given elements to either given value or to 'none'
based on the given cond boolean.
-
Takes a predicate function for elements and a CSS display value, and returns a function that takes elements. The returned function will set style.display
onto given elements to either given value or to 'none'
based on the result of applying the predicate to those elements.
-
Get the value of the content attribute for the first (and presumably only) <meta>
element with given name as the value for its name attribute. Optionally takes a transformer to deserialize string values.
-
Hide the given element through the style.display
property. This is a specialisation of display()
that always sets display to 'none'
.
-
Takes an HTML string or null
, and returns a function that takes Element
objects. Sets innerHTML
of given elements to the given string, or to an empty string if given null
.
-
Takes a string or null
, and returns a function that takes HTMLElement
objects. Sets innerText
of given elements to the given string, or to an empty string if given null
.
-
Takes a callback that is executed as soon as possible after the DOM content is loaded. If the document.readyState
is either 'interactive'
or 'complete'
at call-time, the callback is called immediately, otherwise it is called upon the DOMContentLoaded event.
-
Takes a callback that is executed as soon as possible after the window is loaded. If the document.readyState
is 'complete'
at call-time, the callback is called immediately, otherwise it is called upon the window load event.
-
Takes events and calls their .preventDefault()
method.
-
Calls querySelectorAll
with given selector on given scope, or on document
by default when the scope is omitted. Returns an array containing the found elements.
-
Takes an element as scope for CSS selector queries. Returns a function that takes selectors to query elements for within the set scope. The returned function finds all elements matching given selector and returns them in an array.
-
Calls querySelector
with given selector
on given scope
, or on document
by default when the scope is omitted. Returns the first element matching given selector if any exists, or null
otherwise. Attempts to parse the given CSS selector to determine the element type.
-
Takes an element as scope for CSS selector queries. Returns a function that takes selectors to query elements for within the set scope. The returned function finds the first element matching given selector and returns it. Returns null
if no matching element exists.
-
Read dataset values. Takes a dataset key and optionally a transformer for the corresponding value, and returns a new function that takes the element to read the dataset key from.
-
Removes given element from the DOM if it's currently in it.
-
Curried function that first takes a list of classes, then returns a new function that takes the element to remove those classes from.
-
Removes classes from an element for a given amount of milliseconds.
-
Removes classes from an element for a given amount of animation frames.
-
Takes a string name and returns a new function that takes a string content, and then updates the <meta>
tag with the given name if it exists, or otherwise creates a new one. The meta element to which the content value was set is returned for reference. When a new element is created it will be appended to the end of <head>
.
-
Shows the given element by unsetting any inline style.display
value. This is a specialisation of display()
that always unsets inline display.
Note
This function assumes that given elements are shown by default - ie. there's no CSS rule that has set the element's display to 'none'
.
-
Takes a boolean condition, and returns a function that takes elements. The returned function will unset style.display
onto a given element if the given condition is true
. If the condition is false
, style.display
is set to 'none'
.
Note
This function assumes that given elements are shown by default - ie. there's no CSS rule that has set the element's display to 'none'
.
-
Takes a predicate function for elements and returns a function that takes elements to conditionally show depending on the result of applying the predicate function to given elements.
Note
This function assumes that given elements are shown by default - ie. there's no CSS rule that has set the element's display to 'none'
.
-
Takes an object of style attribute values, and returns a new function that takes an element to apply those styles to.
-
Curried function that first takes a list of classes, then returns a new function that takes the element on which to toggle those classes. The second function optionally takes the second argument force: boolean
to use on the native DOMTokenList.toggle()
method. Note that the value for force will be the same for all classes that are toggled.
-
Takes an array of selectors and a callback function. When for all selectors an element is found, the callback is called with each found element in order. Optionally takes a scope as third argument to use for the element search.
-
Takes an array of selectors. Returns a promise that will only resolve when for all selectors an element is found. The promise value is an array of the elements in the order of the selector array. Optionally takes a scope as third argument to use for the element search.
This function is useful as an alternative for touchAll
in async functions. When awaited it'll block all further execution of the function when not all elements are found.
-
Finds the first element within the set scope that matches selector. If found the element is applied to the given callback function, and the function's return value will be propagated as return value of touchElement
. If no element is found, the callback is not invoked, and null
is returned from touchElement
.
-
Finds the first element within the set scope that matches a given selector. If found the returned promise resolves with the element. If no element is found, the promise will never resolve. Like touchElement
but 'portable', so that many callbacks can subscribe to the 'event' of the element being found.
-
Returns a promise that resolves as soon as possible after the window is loaded. If the document.readyState
is 'complete'
at call-time, the returned promise resolves immediately, otherwise it resolves upon the window load event.
-
Write dataset values. Takes a key, and returns a new function that takes the value, which in turn returns the last function that takes the element to write the key-value pair to.