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

@imadkurdi/toggle-switch

v1.0.0

Published

This is a toggle-switch component: a switch having ON & OFF states. It is a standard custom web component written in pure vanilla JS, so it could be used in any framework.

Downloads

85

Readme

toggle-switch component

Example demonstrating the use of the toggle-switch component

This is a toggle-switch component: a switch having ON & OFF states. It is a standard custom web component written in pure vanilla JS, so it could be used in any framework.

With the package you will find a complete functional example.html Note that, as a developer, you need not to take any action to control the switch itself, it works automatically as expected.

Usage:

Use it like any standard tag:

  • In html: <toggle-switch></toggle-switch>
  • In JS: document.createElement("toggle-switch")

Attributes:

  • disabled: if present <toggle-switch disabled></toggle-switch>, the switch will be disabled: meaning it will not respond to user actions. Please note that a disabled switch could be in ON or OFF state.

Properties:

  • state: it is a boolean property of a JS object representing a toggle-switch tag. This property does not have a corresponding attribute. It is a read/write property. true value indicates the ON state of the switch, and false value indicates the OFF state of the switch.

Events:

  • toggle-switch-changed: This event enables developers to take actions when a user changes the state of the switch. To learn about the switch's new state after the user changed it, you should read event.detail or the switch's state property. It's value indicates the final state of the switch.
    document.querySelector("toggle-switch")
       .addEventListener("toggle-switch-clicked", e => { console.log(e.detail); });

Styling:

  1. Define in an element's selector block one or more of the following custom properties:

    • --block-size: this controls the block-size (height) of the component (not including the borders). Default is 14px. Note that the inline-size is calculated internally from the block-size as inline-size = 2 * block-size.
    • --bg-off-color: switch's background-color when in the OFF state. Default is white.
    • --bg-on-color: switch's background-color when in the ON state. Default is blue.
    • --slider-off-color: slider's color when in the OFF state. Default is black.
    • --slider-on-color: slider's color when in the ON state. Default is white.
    • --border-color: switch's border color. Default is #ccc.
    toggle-switch { 
       --block-size: 30px;
       --bg-off-color: lightgrey;
    }
  2. Or you can reach the following parts of the component:

    • ::part(container): the container is actually a div element, so style it as you style any div element.
    • ::part(slider): the slider inside the container. It is actually a div element, so style it as you style any div element.
    ::part(slider) { background-color: lightgray; }