JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 99
  • Score
    100M100P100Q65847F
  • License LGPL-3.0-or-later

Permanently apply javascript modifiers to your dynamic DOM

Package Exports

  • @itrocks/build
  • @itrocks/build/build.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@itrocks/build) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version npm downloads GitHub issues discord

build

Apply JavaScript code to your dynamic DOM, automatically executed each time a new matching element is added.

Installation

npm i @itrocks/build

Usage

To execute a function each time a new element is added to the DOM:

import { build } from './node_modules/@itrocks/build/build.js'
build<HTMLAnchorElement>('a', anchor => console.log('DOM + anchor', anchor))

This will display "DOM + anchor" in your console for every anchor already present in the DOM.

If you later add another anchor:

document.body.append(document.createElement('a'))

"DOM + anchor" will appear in your console again.

Demo, Testing and Development

git clone https://github.com/itrocks-ts/build
cd build
npm install
npm run build

To test, serve demo/build.html with a local web server and open your browser's console to see it in action.

API

build

Executes a callback each time a matching element is added to the DOM.

build(callback)
build(event)
build(selector, callback)
build(selector, event, callback)
build(selector, type, callback)

Parameters

  • selector: A string with one or more CSS selectors to match.
    For complex multiple-selectors, you may use a CSS selectors chain.
    Defaults to the ALWAYS constant, triggering the callback for any element added to the DOM.
  • type: A case-sensitive string representing the event type to listen for.
    Defaults to the CALL constant, which calls the callback immediately when an element is added to the DOM.
  • event: A BuildEvent object that configures the build event manager. Properties include selector, type, callback, args, priority.
    • args: Additional arguments to pass to the callback.
    • priority: Defaults to 1000. If multiple build events match the same element, you can prioritize them by setting different values.
    • Other properties correspond to build() parameters.

  • callback: The function to execute.
    callback(element, ...args)
    • element: The DOM element that triggered the callback.
    • args: Additional arguments associated with the build event.

Constants

  • ALWAYS: A universal selector that applies to any element added to the DOM.
  • CALL: A special event type that calls a callback immediately upon adding an element to the DOM.

CSS selectors chain

This feature avoids repeating CSS paths within complex CSS selector strings.

Example

This selector string with repeated CSS path body > main:

'body > main > header > h2, body > main > h2'

Is equivalent to:

['body > main', '> header > h2, > h2']