1. spatie-dom
A small DOM querying and manipulation library
spatie-dom
Package: spatie-dom
Created by: spatie
Last modified: Sun, 26 Jun 2022 22:37:51 GMT
Version: 1.4.0
License: MIT
Downloads: 30
Repository: https://github.com/spatie/spatie-dom

Install

npm install spatie-dom
yarn add spatie-dom

spatie-dom

Latest Version on NPM
Software License
Build Status

A small DOM querying and manipulation library.

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Install

You can install the package via yarn:

 yarn add spatie-dom

Usage

Querying the DOM

The DOM can be queried with query and queryAll, which are wrappers around querySelector and querySelectorAll.

Querying an element in document:

 <div id="app"></div>
 const app = query('#app'); // Returns a `HTMLElement`

Querying a collection of elements:

queryAll returns a plain array instead of the usual NodeList collection

 <div id="main">
    <article></article>
    <article></article>
</div>
 const articles = queryAll('#main > article'); // Returns an array `Array<HTMLElement>`

query and queryAll also accept a scope as their second argument (by default, the scope is document).

 <div id="main">
    <h1>Header</h1>
</div>
 const main = query('#main'); // Returns a `HTMLElement`
const header = query('h1', main); // Also returns a `HTMLElement`

Retrieving 'props'

Props are DOM attributes that exist to be consumed by scripts. Props behave just like attributes, except they get parsed as JSON if prefixed by a :.

This syntax is heavily based on what Vue uses for component props

 <div
    id="component"
    my-prop="foo"
    :config='{ "url": "bar" }'
></div>
 import { query, prop, props } from  'spatie-dom';

const el = query('#component');

prop(el, 'myProp'); // 'foo'
prop(el, 'config'); // { url: 'bar' }

props(el); // { myProp: 'foo', config: { url: 'bar' }}

Firing events based on the DOM state

The whenReady function calls a function:

  • immediately if the DOM is loaded;
  • otherwise after the document DOMContentLoaded event
 import { whenReady } from  'spatie-dom';

whenReady(() => console.log('Ready!'));

The whenLoaded function calls a function:

  • immediately if the DOM and all subresources (scripts, images,...) are loaded;
  • otherwise after the window load event
 import { whenLoaded } from  'spatie-dom';

whenLoaded(() => console.log('Loaded!'));

Reading the DOM

There are several functions to read data from the dom.

With attribute you can retrieve an attribute, and with data you can retrieve a data attribute.

 <div id="element" data-foo="bar"></div>
 import { attribute, data, query };

const el = query('#element');

// Retrieve an attribute
attribute('id', el); // 'element'

// Retrieve an attribute with a fallback value
attribute('class', el, 'active'); // 'active'

// Retrieve a data attribute
data('foo', el); // 'bar'

// Retrieve a data attribute with a fallback value
data('baz', el, 'qux'); // 'qux'

Full API

Attribute

 function attribute(name: string, el: HTMLElement, fallback: string = ''): string

Data

 function data(name: string, el: HTMLElement, fallback: string = ''): string

On

 function on(event: string, subject: HTMLElement, handler: Function): string

Props

 function prop(el: HTMLElement, name: string, fallback: any = null): any;

function props(el: HTMLElement): Object;

Query

 function query(selector: string): HTMLElement | null;
function query(selector: string, scope: HTMLElement | Document): HTMLElement | null;

function queryAll(selector: string): Array<HTMLElement>;
function queryAll(selector: string, scope: HTMLElement | Document): Array<HTMLElement>;

When

 function whenReady(callback: Function): void

function whenLoaded(callback: Function): void

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

 $ npm run test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact Sebastian De Deyne instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code