Provides some simple and regularily used frontend helpers for JavaScript.
Reads, transforms and validates an attribute from an HTML element.
import { readAttribute } from '@joinbox/ui-tools';
const element = document.querySelector('div.className');
readAttribute(element, 'attributeName', { transform: (value) => parseInt(value, 10) });
Syntax: readAttribute(element, attribute, additionalArguments)
;
-
element
(HTMLElement
, required): Element to read attribute from -
attribute
(string
, required): Name of the attribute to read -
additionalArguments
(object
, optional): Object with properties-
transform
(function
, optional), takesvalue
as its only parameter and should return the transformed value. Defaults to(value) => value
. In order to check if a boolean attribute is present, use(value) => value !== null
. -
validate
(function
, optional), takesvalue
as its only parameter (that is the attribute's value after thetransform
function has been applied) and should returntrue
if the value is valid, elsefalse
. Defaults to(value) => value
. -
expectation
(string
, optional): expected value for the (transformed) value; will be printed in the error message ifvalidate
returnsfalse
.
-
*
: Transformed and validated value
Error
if validate
function returns false
Creates a debounced function. Primarily needed for performant scroll (and window resize) operations.
import { debounce } from '@joinbox/ui-tools';
const callbackFunction = () => { console.log('update'); };
const debouncedFunction = debounce(callbackFunction, 200);
// Will only print a console.log *after* the window has *not* been resized for 200ms
window.addEventListener('resize', debouncedFunction);
Syntax: debounce(callback, timeout)
-
callback
(function
, required): The function that will be executed after the debounce function has not been called fortimeout
ms. -
timeout
(number
, required, in ms). The inactivity timeout in ms after which the `callback`` function will be executed.
undefined
Only executes a given function once for a given element; the execution state (executed or not) is
stored on an HTML element and only depends on the provided name
, not the function
. Needed
especially to implement Drupal behaviors.
import { once } from '@joinbox/ui-tools';
const executeOnlyOnce = () => { console.log('executing'); };
const element = document.querySelector('.executing-element');
once(element, 'example-executer', executeOnlyOnce);
// Will *not* execute executeOnlyOnce because it has been executed for this element before
once(element, 'example-executer', executeOnlyOnce);
// Will *not* execute because the name has been used before
once(element, 'example-executer', () => { console.log('nope') });
// Will execute as executeOnlyOnce has not been executed for this element before
once(document.querySelector('body'), 'example-executer', executeOnlyOnce);
Syntax: once(element, name, function)
-
element
(HTMLElement
, required): The HTML element for which thefunction
should be executed once -
name
(string
, required): Name under which the execution state function (executed or not) will be stored on the providedelement
-
function
(function
, required): Function that shall only be executed once
undefined
Gets dimensions of an element (by calling getBoundingClientRect()
) on load, window resize
and (optionally) intersection. Returns them as an object that updates its values whenever any of
the events described happens.
import { measureElement } from '@joinbox/ui-tools';
const element = document.querySelector('div.className');
const dimensions = measureElement({ element, updateOnIntersection: true });
Syntax: measureElement({ element, updateOnIntersection })
;
-
element
(HTMLElement
, required): Element to measure -
updateOnIntersection
(boolean
, optional): Iftrue
, dimensions will update whenever the element becomes visible. Defaults tofalse
.
object
: Object with same properties as DOMRect
with values that reflect the element's current dimensions.
No specific errors