@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
5
Maintainers
Readme
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 atoggle-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, andfalse
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 readevent.detail
or the switch'sstate
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:
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 asinline-size = 2 * block-size
.--bg-off-color
: switch's background-color when in the OFF state. Default iswhite
.--bg-on-color
: switch's background-color when in the ON state. Default isblue
.--slider-off-color
: slider's color when in the OFF state. Default isblack
.--slider-on-color
: slider's color when in the ON state. Default iswhite
.--border-color
: switch's border color. Default is#ccc
.
toggle-switch { --block-size: 30px; --bg-off-color: lightgrey; }
Or you can reach the following parts of the component:
::part(container)
: the container is actually adiv
element, so style it as you style anydiv
element.::part(slider)
: the slider inside the container. It is actually adiv
element, so style it as you style anydiv
element.
::part(slider) { background-color: lightgray; }