Hudini Toggle Component
daisyUI-style switch â pill-shaped track + sliding handle with per-state icons that cross-fade.
Toggle Component
A daisyUI-style switch. Same contract as Checkbox â uncontrolled state, onChange callback, disabled/readOnly, getValue() for form serialization. The visual is a pill-shaped track with a handle that slides between left (off) and right (on).
Two icons â onIcon and offIcon â ride inside the handle. When the toggle flips, the handle slides while the icons cross-fade (outgoing â 0, incoming â 1).
Features
- đ¯ Same API as Checkbox:
toggle,setChecked,getValue,disable,readOnly,onChange,onClick,name - đ¨ Two icons:
onIcon(default'check') +offIcon(default'xmark'). Passnullto disable either - đĢ Cross-fade during slide: outgoing icon fades to 0 while incoming fades to 1, all in sync with the handle motion
- đī¸ Two track colors:
color(on) +offColor(off) â perfect for dark-mode toggles etc. - đ§ą Composable: reports its size so Row / Column / Stack lay it out correctly
Props
checked- Initial state (defaultfalse)label- Optional text to the rightonIcon/offIcon- FA icon per state. Passnullto disableiconColor- Default'slate-700'(contrast on white handle)color/offColor- Track color per statehandleColor- Default'white'labelColor,size,borderRadius,labelGapdisabled,readOnly,nameonChange-(checked, name?) => voidonClick- Fires on any click
Live Example
Loading game...
Usage Example
import Phaser from 'phaser';
import {
createTheme,
HUDINI_KEY,
HudiniPlugin,
withHudini,
Toggle,
} from 'hudini';
class SettingsScene extends Phaser.Scene {
create(): void {
// 1. Default toggle â check / xmark icons that cross-fade
const sound = new Toggle({
scene: this, x: 200, y: 100,
label: 'Sound',
checked: true,
name: 'sound',
onChange: (v) => console.log('sound:', v),
});
// 2. Dark-mode toggle â moon (on) + sun (off), different track colors
const darkMode = new Toggle({
scene: this, x: 200, y: 160,
label: 'Dark mode',
onIcon: 'moon',
offIcon: 'sun',
color: 'indigo-600',
offColor: 'yellow-400',
});
// 3. Read the current values later (form-style)
const submit = () => ({
sound: sound.getValue(),
dark: darkMode.getValue(),
});
}
}