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

@vyvrhel/class-toggler

v1.1.0

Published

Javascript library for CSS class toggling

Downloads

15

Readme

Class Toggler

Simple javascript library for toggling element's CSS class by clicking another element(s). Toggling is controlled via data-attributes. Suitable for creating simply show/hide, more/less, tabs, accordion, dropdown or modal components without writing (repetitive) javascript code, all is done within your HTML template. Component visual behaviour is controlled by your stylesheets.

📋 Basic Usage

When toggling there are two types of HTML elements:

Toggle Button

  • Show/hides related content(s) when clicked.
  • Related content(s) is defined by data-toggle-target HTML attribut value.
  • Active state is identified by customizable CSS class (default -active-toggle).

Toggle Content

  • Element to be shown/hidden when related button(s) is clicked.
  • Related button(s) is defined by data-toggle-name HTML attribut value.
  • Hidden state is identified by customizable CSS class (default -hidden-toggle).

Basic Example

Reveals text and make the button green when you click on it:

<!-- Linking Class Toggler -->
<script src="class-toggler.min.js"></script>

<!-- Initialization of Class Toggler -->
<script>
  document.addEventListener('DOMContentLoaded', () => classToggler.init());
</script>

<!-- Styles for hidden content and active button -->
<style>
  .-active-toggle {
    color: green;
  }
  .-hidden-toggle {
    display: none;
  }
</style>

<!-- Toggle content -->
<div class="-hidden-toggle" data-toggle-name="toggle-name">
  <p>Text to be toggled.</p>
</div>

<!-- Toggle button -->
<button data-toggle-target="toggle-name">
  Toggle text!
</button>

(See live demos)

🚀 Initialization

  1. Link Class Toggler library in your HTML:
<script src="path-to-library/dist/class-toggler.min.js"></script>
  1. Initialize Class Toggler in your JS:
document.addEventListener('DOMContentLoaded', () => {
  classToggler.init();
});

or if using ES6 modules:

import classToggler from 'path-to-library/dist/class-toggler.esm.min.js';

document.addEventListener('DOMContentLoaded', () => {
  classToggler.init();
});

⚙ Options / Data Attributtes

Behaviour of each individual toggle can be set via these data attributes:

Toggle Button Options


data-toggle-target="name"

Targets related content(s) that has data-toggle-name="name".

(See More/Less demo)


data-toggle-class="class-name"

CSS class-name that will be assigned to active toggle button (default -active-toggle).

For example, when using BEM naming convention, your code should look like this:

<div class="Module">

  <button
    class="Module__button"
    data-toggle-target="module-text"
    data-toggle-class="Module__button--active"
  >Show text!</button>

  <p
    class="Module__text Module__text--hidden"
    data-toggle-name="module-text"
    data-toggle-class="Module__text--hidden"
  >Text lorem ipsum dolor.</p>

</div>

(See More/Less demo)


data-toggle-group="group-name"

Each group (togglers with the same group-name) can have maximum of one active toggler. Turning one toggler on will turn off the rest of togglers in group.

(See Accordion demo)


data-toggle-tabs="tabs-name"

Each tabs group (togglers with the same tabs-name) has always just one active toggler. Turning one toggler on will turn off the rest of togglers in tabs. Active toggler can't be turned off by clicking it.

(See Tabs demo)


data-toggle-abort="event-1 event-2"

Automatically hides toggle for these possible events:

  • escape - Hide when escape key is pressed.
  • clickout - Hide when clicking outside the toggle content.

(See Modal demo)


data-toggle-match="media-query"

Binds toggle function on toggler only for matching media-query (e.g. (min-width: 30em)), otherwise default action will be fired (opening the <a>'s link, submitting <button>'s form...).

(See Media query demo)

Toggle Content Options


data-toggle-name="name"

Targets related button(s) that has data-toggle-target="name".

(See More/Less demo)


data-toggle-class="class-name"

CSS class-name that will be assigned to hidden toggle content (default -hidden-toggle). See toggle button option example.

(See More/Less demo)


data-toggle-focus="css-selector"

Element targeted by css-selector (e.g. #form-field) will be focused when content is shown.

(See Focused content demo)

👂 Events

ct.button.on

Fired at toggle button after it is turned on.

document.querySelector('#button').addEventListener('ct.button.on', () => {
  console.log('Button turned on!');
});

ct.button.off

Fired at toggle button after it is turned off.

document.querySelector('#button').addEventListener('ct.button.off', () => {
  console.log('Button turned off!');
});

ct.content.shown

Fired at toggle content after it is shown.

document.querySelector('#content').addEventListener('ct.content.shown', () => {
  console.log('Content shown!');
});

ct.content.hidden

Fired at toggle content after it is hidden.

document.querySelector('#content').addEventListener('ct.content.hidden', () => {
  console.log('Content hidden!');
});

ct.toggled

Fired at document when any toggle button is clicked.

document.addEventListener('ct.toggled', () => {
  console.log('Toggle button click!');
});

🛠 Configuration

Class Toggler can be initialized with these options:

  • classActive - CSS class for active toggle button (default -active-toggle)
  • classHidden - CSS class for hidden toggle content (default -hidden-toggle)
  • attrTarget - HTML data attribute used for toggle targeting (default data-toggle-target),
  • attrName - HTML attribute used for toggle naming (default data-toggle-name),
  • attrClass - HTML attribute defining toggled CSS class (default data-toggle-class),
  • attrFocus - HTML attribute used for focusing toggle content (default data-toggle-focus),
  • attrAbort - HTML attribute used for toggle aborting (default data-toggle-abort),
  • attrMatch - HTML attribute used for toggle media query (default data-toggle-match),
  • attrGroup - HTML attribute used for toggle grouping (default data-toggle-group),
  • attrTabs - HTML attribute used for toggle grouping in tabs (default data-toggle-tabs),
  • eventNamespace - Namespace for Class Toggler events (default ct),

Example of initialization with custom configuration:

document.addEventListener('DOMContentLoaded', () => {
  classToggler.init({
    classActive: 'your-active-class',
    classHidden: 'your-hidden-class',
  });
});