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

toggled.js

v1.1.1

Published

adds or removes body classes when something is clicked and retains state

Downloads

5

Readme

toggled.js

demonstration | GitHub | npm | donate | @craigbuckler | craigbuckler.com

Toggles a class to the HTML body when an element is clicked.

Please use the code as you wish. Tweet me @craigbuckler if you find it useful and donate toward development if you use it commercially.

  • easily add a toggle switch to any HTML element
  • state is remembered between page loads
  • works in all modern browsers
  • no external dependencies - works with any framework
  • fast and lightweight: 630 bytes of JavaScript

Usage

The page must load the JavaScript. It can be placed anywhere but, typically, it is loaded just before the closing </body> tag:

<script src="https://cdn.jsdelivr.net/npm/toggled.js/dist/toggled.js"></script>

The CDN URL is shown above but you can also npm i toggled.js to install via npm and use a bundler.

Add a data-toggled="NAME" attribute to any HTML element. When that element is clicked, the NAME class is added to or removed from the HTML body.

<button data-toggled="buttonsize">CLICK ME</button>

When the button is clicked for the first time, a buttonsize class is added to the HTML body. CSS or JavaScript can respond to the change, e.g. change the font size:

.buttonsize button {
  font-size: 2em;
}

The class is removed when the button is clicked for a second time and the state is retained between page loads.

Advanced options:

  • Any clickable element can be used as a toggle.
  • Any number of space-separated classes can be defined, e.g. data-toggled="NAME1 NAME2 NAME3".
  • Set data-toggled="" to apply the element's classes, e.g. when <button class="one two" data-toggled="">button</button> is clicked, the classes one and two are toggled on the HTML body.

Version history

v1.1.1, 2 July 2019

  • fixed bug which stored null

v1.1.0, 24 June 2019

  • data-toggled="" option to apply element's classes to the body
  • simpler and faster processing

v1.0.0, 23 June 2019

  • initial release