Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

a-simple-switch

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

a-simple-switch

Vanilla JS/CSS Switch UI element

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
363
increased by26.04%
Maintainers
1
Weekly downloads
 
Created
Source

Simple Switch

npm version

Simple, accessible, performant implementation of the Switch UI element.

image

Features:

  • Vanilla JS/CSS: doesn't require any outside library to function
  • Accessible: works properly with screenreaders and the ARIA spec
  • Performant: uses proper layering and transitioning to ensure high performance

Statistics

Below are the gzipped sizes of the source files. All measured using an Apache server and Google Chrome.

FileSize (after gzip)
SimpleSwitch.css785 bytes
SimpleSwitch.min.js1.4 kilobytes

Installation

To install the Switch code, you will need to in some way include the Simple Switch Javascript and CSS into your page/build. Please follow the relevant instructions below for more information.

To get the latest minified/production ready files, please see the releases page.

Installing the Javascript

The Javascript is available as a NPM package, buildable through Webpack, or as a minified/uglified file that can be directly imported into the page.

The minified Javascript file is available on the releases page, within the release .zip file, under js/.


Alternatively, the Webpack package can be installed by running:

npm i --save a-simple-switch

And then importing it into your webpack build by saying:

import * as SimpleSwitch from "a-simple-switch";

Installing the CSS

You can install the CSS by downloading the compiled CSS file from the releases page, under css/.

Installing the SASS

You can import the relevant SASS file into your SASS build by either downloading the latest release from the releases page and grabbing the SASS file from the sass/ directory in the release, or you may directly import it from the source code:

@import "path/to/SimpleSwitch/src/sass/SimpleSwitch.scss";

Creating a Simple Switch

There are two ways to create a Simple Switch. On page load, the Simple Switch code will automatically detect checkboxes that are flagged as switches, and upgrade them, or you may manually instantiate a switch. See below for more details.

Automatically Creating Switches

To have a switch be automatically upgraded, simply add the data-type attribute to any checkbox-type input that you want upgraded, and set that attribute to the value of simple-switch. Then, at the end of your <body> tag, simply call SimpleSwitch.init() to initialize all of the switches marked as noted above.

Example:

<input type="checkbox" name="my-checkbox" data-type="simple-switch" />

Javascript Setup:

<!-- ^^ Rest of your page above ^^ -->
    <script type="text/javascript">
        SimpleSwitch.init();
    </script>
</body>

Manually Creating Switches

You may also manually instantiate a switch, which may be useful for lazily-loaded UI elements or parts of the page. The Switch class which handles upgrading and controlling Switches is available under the SimpleSwitch namespace, and takes one parameter, element, which is a direct reference to the HTMLElement checkbox to be upgraded.

Example:

<input type="checkbox" name="my-checkbox" id="my-checkbox" />
var myCheckbox = document.getElementById("my-checkbox");

new SimpleSwitch.Switch({
    element: myCheckbox
});

Keywords

FAQs

Package last updated on 11 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc