JavaScript Toggle Button

The LemonadeJS Toggle component provides a reactive and customizable solution for managing binary states in your web applications. With support for Material Icons and text labels, it offers a clean interface for toggle functionality. As a framework-agnostic JavaScript plugin, it integrates seamlessly with Vue, React, and Angular.

Documentation

Installation

npm install @lemonadejs/toggle

Settings

AttributeDescription
text?: StringThe text label displayed next to the toggle.
icon?: StringMaterial icon name (e.g., ‘mic’, ‘videocam’).
value?: BooleanThe current toggle state.
name?: StringThe attribute name assigned to the input element.
disabled?: BooleanDisables the functionality of the toggle.
onchange?: FunctionCallback when the toggle state changes.

Instance Properties

PropertyDescription
text: StringThe text label displayed next to the toggle.
icon: StringMaterial icon name.
value: BooleanCurrent toggle state.
name: StringThe name attribute of the input element.
disabled: BooleanWhether the toggle is disabled.
checked: BooleanCurrent checked state of the underlying checkbox (read-only).
el: HTMLElementThe root DOM element.

Examples

JavaScript Toggle Example

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

<div id='root'></div>

<script>
function Component() {
    this.value = false;
    return render => render`<div>
        <p>Value: ${this.value}</p>
        <Toggle text="Enable notifications" icon="notifications" :bind="this.value" />
    </div>`
}
lemonade.render(Component, document.getElementById('root'));
</script>
</html>

Basic HTML Example

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

<div id='root'></div>

<script>
Toggle(document.getElementById('root'), {
    text: 'Microphone',
    icon: 'mic'
});
</script>
</html>

Render as Web Component

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

<lm-toggle text="Camera" icon="videocam" value="true"></lm-toggle><br>
<lm-toggle text="Microphone" icon="mic" value="false"></lm-toggle>

</html>

Toggle with onchange Event

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/toggle/dist/style.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />

<div id='root'></div>

<script>
Toggle(document.getElementById('root'), {
    text: 'Dark mode',
    icon: 'dark_mode',
    onchange: function(instance, value) {
        console.log('Toggle changed to:', value);
    }
});
</script>
</html>