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

rivet-switch

v0.3.2

Published

An accessible switch component for Rivet

Downloads

21

Readme

rivet-switch

An accessible switch component for Rivet

Download Rivet switch | View the demo

Notes about usage

Switch components are based on their real-life counterparts — physical switches. Physical switches close or open a circuit to create states of "on" or "off", respectively. As a result, switch components should feature the text "On" and "Off", with any additional context being provided by a label.

Switches do not behave the same as checkboxes. Checkboxes are generally used in the context of a form and separate the act of selection from the execution of the chosen selection. Switches are typically standalone components which combine selection and execution into a single step. For these reasons, switches should not be used within a form.

For more information please see this article Checkbox vs Toggle Switch.

Getting started

The Rivet switch add-on requires the use of the core Rivet CSS. You can find out more about how to get started in the Rivet documentation. Once you are using Rivet, you can download the Rivet switch source files and include them in your project.

Note: closest() polyfill

The Rivet Switch uses the .closest() JavaScript method which works in all modern web browsers, but requires a small polyfill to add support for Internet Explorer 11. The main Rivet JavaScript file (rivet.js) already includes the closest() polyfill so if you are using it on your page there is no need to add it.

/**
 * Element.closest() polyfill
 * https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
 */
if (!Element.prototype.closest) {
  if (!Element.prototype.matches) {
    Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
  }

  Element.prototype.closest = function (s) {
    var el = this;
    var ancestor = this;
    if (!document.documentElement.contains(el)) return null;
    do {
      if (ancestor.matches(s)) return ancestor;
      ancestor = ancestor.parentElement;
    } while (ancestor !== null);
    return null;
  };
}

1. Include the CCS and JavaScript in your page

<link rel="stylesheet" href="dist/css/rivet-switch.min.css">
<script src="dist/js/rivet-switch.min.js"></script>

2. Add the markup to your HTML

The Rivet switch markup uses a HTML <button> element. To use the switch add the following markup to your page.

<button class="rvt-switch" data-switch="email-switch" role="switch" aria-checked="false">
  <span class="rvt-switch__on">On</span>
  <span class="rvt-switch__off">Off</span>
</button>

3. Initialize the add-on

Lastly, you'll need to initialize somewhere right before the closing </body> tag of you page.

<script>
  Switch.init();
</script>

JavaScript API

The Rivet switch component exposes a handful of methods you can use to programmatically control the component. The .init() method must be called somewhere in your document after the rivet-switch.js script is included. The init() method attaches and event listener to the document that listens for clicks on buttons with the data-switch attribute. With that in mind you should be able to dynamically add switches to the DOM without having the re-initialize the component.

Methods

| Method| Description| |--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Switch.init() | Initializes the switch component | | Switch.on(button, callback) | Accepts a switch button element [data-switch] and an optional callback function that is run after the switch is toggled on. | | Switch.off(button, callback) | Accepts a switch button element [data-switch] and an optional callback function that is run after the switch is toggled off. |

Custom events

The switch component emmits custom events when it is toggled on or off. You can listen for these events in your own scripts and respond to them as needed.

|Event|Description| |----|------| |switchOn|Emitted when the switch is toggled on via the Switch.on() method. The value of the switch data-switch attibute is also passed along via the custom event's detail property and is available to use in your scripts as event.detail.name()| |switchOff|Emitted when the switch is toggled on via the Switch.off() method. The value of the switch data-switch attibute is also passed along via the custom event's detail property and is available to use in your scripts as event.detail.name()|

Custom event example

// Listen for a custom "switchOn" event
document.addEventListener('switchOn', event => {
  if (event.detail.name() == 'email-switch') {
    alert('Okay, we\'ll send you some emails!')
  }
  // Maybe send some data via an AJAX request, etc...
}, false);

Working with source files

  1. To work with the switch source files first clone or download this repo: https://github.com/indiana-university/rivet-switch.git
  2. Install the dependencies using NPM: npm install
  3. Start the development server by running npm start in your terminal. The demo will open in a new browser window at http://localhost:3000 and the server will watch for changes to all .scss and .js files.