dom

Description#

A set of utilities to interact with the DOM, event listeners and similar low level tasks. For a more structured API (jQuery like) consider DOMtastic.

import { ... } from "@acanto/core-dom";

API#

escape#

Escape colons to allow use class names as .module:block__element

Parameters#

Returns string

\$

Sizzle/jQuery like DOM nodes shortcut for document.querySelector To avoid an extra function call we inline the above escape

Parameters#

Examples#

Basic DOM selection

const $container = $(".my-section:");
const $el = $("[data-some-attr]", $container);

Returns HTMLElement

\$\$#

Sizzle/jQuery like DOM nodes shortcut for document.querySelectorAll To avoid an extra function call we inline the above escape

Parameters#

Returns NodeList

forEach#

For each, iterate through a NodeList of HTMLElements

Parameters#

\$each#

Each shortcut, iterate through a NodeList of HTMLElements retrieved with the given selector (and optionally a parent container passed as thrid arguement)

Parameters#

toArray#

Node list to array

Parameters#

  • nodeList (NodeList | HTMLFormControlsCollection)

Returns (Array<HTMLElement> | Array<HTMLFormElement>)

setVendorCSS#

Set vendor CSS rule

Parameters#

isNodeList#

Is node list

Parameters#

  • nodes any The object to check

Returns boolean

on#

Shortcut for addEventListener

Parameters#

off#

Shortcut for removeEventListener

Parameters#

once#

One shot listener, it addEventListener and removes it first time is called with removeEventListener

Parameters#

createElement#

Shortcut for document.createElement, allowing to to create an HTML element with a given className directly (a very common use case)

Parameters#

Returns HTMLElement

isHidden#

Is element hidden?

Parameters#

Returns boolean

siblings#

  • See: @glidejs/glide/src/utils/dom (source)

Finds siblings nodes of the passed node.

Parameters#

Returns Array

exists#

  • See: @glidejs/glide/src/utils/dom (source)

Checks if passed node exist and is a valid element.

Parameters#

Returns Boolean

getDataAttr#

Get data attribute

Parameters#

Returns (string | null)

setDataAttr#

Set data attribute

Parameters#

  • element Element
  • attribute string The name of the data-{attr}
  • value (string | number | null)? The value to set, null or undefined will remove the attribute

activeEvents#

Listen: events delegation system

From: https://github.com/cferdinandi/events https://github.com/cferdinandi/events/blob/master/src/js/events/events.js

activeEvents#

Active events

ListenEvent#

Type: Object

Properties#

getIndex#

Get the index for the listener

Parameters#

Returns number The index of the listener

getRunTarget#

Check if the listener callback should run or not

Parameters#

Returns (window | Document | Element | false) If not false, run listener and pass the targeted element to use in the callback

eventHandler#

Handle listeners after event fires

Parameters#

event#

Type: Element

listen#

Listen an event

Parameters#

  • types string The event type or types (comma separated)
  • selector string The selector to run the event on
  • callback

unlisten#

Stop listening for an event

Parameters#

  • types string The event type or types (comma separated)
  • selector string The selector to remove the event from
  • callback Function The function to remove

listenOnce#

Listen an event, and automatically unlisten it after it's first run

Parameters#

  • types string The event type or types (comma separated)
  • selector string The selector to run the event on
  • callback Function The function to run when the event fires

getListeners#

Get an immutable copy of all active event listeners

Returns Object Active event listeners

listenLoaded#

Fires a callback when the DOM content is loaded

Parameters#

  • handler EventHandlerNonNull

listenResize#

Listen window resize event using lodash's debounce

Parameters#

  • handler EventHandlerNonNull
  • debounceOptions

Returns Function An automatic unbinding function to run to deregister the listener upon call

unbinder#

Unbind the previously attached scroll handler

unbinder#

Unbind the previously attached scroll handler

listenScroll#

Listen window scroll event using lodash's debounce

Parameters#

  • handler EventHandlerNonNull
  • debounceOptions

throttle#

Throttle for resize / scroll

Parameters#

Returns Function

emitEvent#

Emit event (use only if the targeted browser supports CustomEvents)

Parameters#

  • type string (optional, default "customEvent")
  • detail Object (optional, default {})

getHeight#

Get element height

Parameters#

Returns number

getOffset#

Get an element's distance from the top and left of the Document.

Parameters#

Returns {top: number, left: number} Distance from the top and left in pixels

getOffsetTop#

Get an element's distance from the top of the Document.

Parameters#

Returns number Distance from the top in pixels

getDocumentHeight#

Determine the document's height

Returns number

getScrollbarWidth#

Get scrollbar's current width

Parameters#

Returns number

isTotallyScrolled#

Is element totally scrolled

Parameters#

Returns boolean

isInViewport#

Determine if an element is in the viewport

(c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com

Parameters#

Returns boolean Returns true if element is in the viewport