npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spatie-dom

v1.4.0

Published

A small DOM querying and manipulation library

Downloads

18

Readme

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.