custom-elements
Toggle switch custom element
last-toggle-switch
Custom element toggle-switch designed to act like a checkbox. Includes accessibility features.
Usage
Install via npm with npm install last-toggle-switch --save
.
If you're using a module bundler simply import "last-toggle-switch"
.
Alternatively you can include it via a script tag:
<script src="node_modules/last-toggle-switch/dist/ToggleSwitch.dist.js"></script>
Depending upon your browser support requirements, you may need to include one or more Web Component polyfills.
HTML
<toggle-switch>Label text</toggle-switch>
<!-- Can be checked or disabled by default, just like a regular input -->
<toggle-switch checked disabled>Label text</toggle-switch>
CSS
Here are the custom properties available, along with their initial values, which enable you to style the shadow DOM.
--toggle-switch-disabled-opacity: 0.5;
--toggle-switch-toggle-duration: 0.3s;
--toggle-switch-track-color: #cecece;
--toggle-switch-track-shadow: none;
--toggle-switch-track-border: none;
--toggle-switch-track-height: 75%;
--toggle-switch-knob-color: #333333;
--toggle-switch-knob-shadow: none;
--toggle-switch-knob-border: none;
JS
The element itself emits a change
event when the checked
property changes.
document.querySelector('toggle-switch').addEventListener('change', (event) => {
console.log(event)
})
Example implementation:
<script src="node_modules/last-toggle-switch/dist/ToggleSwitch.dist.js"></script>
<toggle-switch>Toggle Switch</toggle-switch>
<style>
toggle-switch {
--toggle-switch-track-color: #e8e2e2;
--toggle-switch-knob-color: rebeccapurple;
--toggle-switch-toggle-duration: 0.15s;
color: #262626;
transition: color 0.3s;
}
toggle-switch[checked] {
--toggle-switch-track-color: #dfc5e6;
}
toggle-switch:focus {
outline: none;
color: rebeccapurple;
}
</style>
Development
# installation
npm install
# dev mode
npm run dev
# build production
npm run build
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Dependencies
- @polymer/lit-element#^0.6.5
- Released
- 2019-01-15
- Maturity
- IMPORTED
- License
- Apache License 2.0
Compatibility
- Framework
- Browser
- Browser Independent
custom-elements - Vaadin Add-on Directory
Toggle switch custom elementView on GitHub
custom-elements version 1.0.2
### Dependencies
* @polymer/lit-element#^0.6.5
* @webcomponents/webcomponentsjs#^2.2.1
custom-elements version 1.0.3
### Dependencies
* @polymer/lit-element#^0.6.5
* @webcomponents/webcomponentsjs#^2.2.1
custom-elements version 1.0.4
### Dependencies
* @polymer/lit-element#^0.6.5
* @webcomponents/webcomponentsjs#^2.2.1
custom-elements version 1.0.5
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.10
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.6
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.7
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.8
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.9
### Dependencies
* @polymer/lit-element#^0.6.5
custom-elements version 1.0.11
### Dependencies
* @polymer/lit-element#^0.6.5