Giter VIP home page Giter VIP logo

classattr's Introduction

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

classattr's People

Contributors

fabiospampinato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.