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

zw-toggler

v0.0.1

Published

A small helper library that allows toggling classes of HTML elements without writing much JS code. For example, to toggle class `block--red` on an element when some button is clicked, you should write the following code:

Downloads

5

Readme

What is it?

A small helper library that allows toggling classes of HTML elements without writing much JS code. For example, to toggle class block--red on an element when some button is clicked, you should write the following code:

<div class="block">
  <button data-toggle="block--red" data-toggle-parent=".block">
    Toggle color
  </button>
</div>

If you click the button, the closest parent element that matches selector in data-toggle-parent attribute will get the class specified in data-toggle attribute. Next time you click the button, block--red class will be removed from the element. To specify element receiving classes, use data-toggle-target and data-toggle-parent attributes.

Installation

npm i --save zw-toggler

Usage

const Toggler = require('zw-toggler');

Toggler.init();

Toggler.init

Call this static member function to initialize Toggler. All element with appropriate attributes now are going to react on events and toggle classes.

Toggler.uninit

Call this static member function to stop Toggler. Now event listeners are removed and togglers will no more toggle classes.

data-toggle

Elements with this attribute will react on click and toggle class specified by this attribute.

data-toggle-focus

Same as data-toggle, but toggles classes when elements receives or loses focus instead of click. Note that it toggles the class, not adds it when the toggler has focus and removes on losing. E.g., if the target element already has the class, it is going to be removed when toggler gets focus, and vise versa.

data-toggle-hover

Same as data-toggle-focus, but with hover instead of focus.

data-toggle-target

Defines selector used to find elements that receive classes. It will only match children of the element found by data-toggle-parent selector (let's call it "target root"). If no data-toggle-target attribute exists, the target root itself will receive classes. But if both data-toggle-parent and data-toggle-target attributes are missing, the toggler element itself will receive classes.

data-toggle-parent

Defines selector for target root. Target root should only be a parent of the toggler element. So, the closest parent element of the toggler is considered to be a target root. If no data-toggle-parent exists, e.g., no target root defined, target root is considered to be a document element.

Examples

Toggle foo class on button:

<div class="grandparent">
  <div class="parent">
    <button data-toggle="foo">Click me</button>
  </div>
</div>

Toggle foo class on .grandparent element:

<div class="grandparent">
  <div class="parent">
    <button data-toggle="foo" data-toggle-parent=".grandparent">Click me</button>
  </div>
</div>

Toggle foo class on .panel element:

<div class="grandparent">
  <div class="parent">
    <button data-toggle="foo" data-toggle-parent=".grandparent" data-toggle-target=".panel">Click me</button>
  </div>
  <div class="panel"></div>
</div>

Toggle foo class on .footer element:

<div class="grandparent">
  <div class="parent">
    <button data-toggle="foo" data-toggle-target=".footer">Click me</button>
  </div>
  <div class="panel"></div>
</div>
<div class="footer"></div>