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

classlist

v2.0.0

Published

Element#classList for older browsers.

Downloads

19,587

Readme

ClassList

This module aims to match the behaviour of Element#classList, without polyfilling it.

IE9 doesn't have the native classList, and other browsers have incomplete implementations. This module tries to bridge the gaps.

Installation

npm install classlist (or yarn add classlist)

Usage

This module is intended for use in the browser via a module bundler such as Webpack or Browserify. Sample usage:

var classList = require('classlist')

var element = document.querySelector('#id')
var list = classList(element)

// Add a class:
list.add('foo')

// Remove a class:
list.remove('bar')

// Toggle a class:
list.toggle('baz')

// Replace class 'foo' with 'bar'
list.replace('foo', 'bar')

// Check if the element has a class:
if (list.contains('bar')) {
    console.log('Element has class bar')
}

API

This module tries to follow the native API as closely as possible.

  • list = new ClassList(element)

    Constructor. Create a new ClassList object. new is optional.

    // Object style, "new"
    var ClassList = require('classlist')
    var list = new ClassList(element)
    
    // Function style, no "new"
    var classList = require('classlist')
    var list = classList(element)
  • list.item(index) or list[index]

    Returns the class at the given index. Example:

      var div = document.createElement('div')
      div.className = 'a b'
      var list = classList(div)
      list.item(0)  // 'a'
      list[1]       // 'b'
  • list.contains(class)

    Returns true if the element has the given class; false otherwise.

  • list.add(class1 [, class2 [, ... ]])

    Adds one or more classes to the element associated with list. If the class is already present on the element, it is not added again.

  • list.remove(class1 [, class2 [, ... ]])

    Removes one or more classes from the element, if present.

  • list.toggle(class [, force ])

    Toggles the given class; removes the class if it is present, or adds the class if it is not present.

    When force is true, the class is always added (i.e. it is equivalent to calling add). When force is false, the class is always removed (i.e. it is equivalent to calling remove). This can be useful when conditioning on an outcome; for example,

      list.toggle('not_empty', counter > 0)
    
      // is equivalent to:
      if (counter > 0) {
        list.add('not_empty')
      } else {
        list.remove('not_empty')
      }

    Returns true if the class is now present, or false otherwise.

  • list.replace(oldClass, newClass)

    If oldClass is present on the element, it is replaced by newClass. If oldClass is not present, this method has no effect.

    Returns true if the class was replaced, false otherwise.

Browser support

This module has been tested for compatibility with IE8+, and may work in even older browsers. If it doesn't, open an issue.

License

MIT