classattr
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

ClassAttr

A classList-like API that's purely based on reading/writing the class attribute.

WHY?!

Because apparently touching the classList API for a node causes a DOMTokenList to be allocated for that node indefinitely. Like you may be asking the classList API to delete a class from a node that perhaps doesn't even have classes to begin with, and that will still allocate a DOMTokenList for that node, which sounds like nonsense to me.

Also sometimes the classList API pointlessly triggers MutationObservers even when it doesn't actually change any class, which sounds like nonsense to me also.

Those problems is what this library is meant to address, maybe you don't need it.

Install

npm install --save classattr

Usage

It works just like the classList API.

import ClassAttr from 'classattr';

const element = document.body;

ClassAttr.add ( element, 'foo', 'bar', 'baz' );

ClassAttr.contains ( element, 'foo' ); // => true
ClassAttr.contains ( element, 'foo2' ); // => false

ClassAttr.remove ( element, 'bar' );

ClassAttr.toggle ( element, 'baz' );
ClassAttr.toggle ( element, 'baz' );
ClassAttr.toggle ( element, 'baz', true );
ClassAttr.toggle ( element, 'baz', false );

License

MIT © Fabio Spampinato

Readme

Keywords

Package Sidebar

Install

npm i classattr

Weekly Downloads

10

Version

2.0.1

License

none

Unpacked Size

10.6 kB

Total Files

12

Last publish

Collaborators

  • fabiospampinato