Overview
Reusable JavaScript toggle switch component with custom labels, gradient thumb styling, state events and CSS variable customization.
Features
- Custom ON / OFF labels
- Reusable ToggleSwitch class
- onChange callback support
- Size and color control using CSS variables
- Animated metal-style switch UI
Quick Start
- Open the template folder.
- Run
ToggleSwitch.htmlin browser. - Import the component in your project.
- Initialize with a container selector.
Usage Example
import {ToggleSwitch} from "./ToggleSwitch.js";
const sw = new ToggleSwitch(".switchContainer");
sw.onChange((state)=>{
console.log(state);
});
Custom Labels
sw.label = {
on:"AUTO",
off:"MANUAL"
};
CSS Variables
:root{
--switchWidth:120px;
--switchHeight:50px;
--bgColor:#444;
}
API
-
new ToggleSwitch(selector)Creates a new switch instance -
onChange(callback)Runs callback when state changes labelUpdates visible switch labels