File size: 1,308 Bytes
04ec17f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import { attachRef } from "svelte-toolbelt";
import { createBitsAttrs, boolToStr, boolToEmptyStrOrUndef, boolToTrueOrUndef, } from "../../internal/attrs.js";
export const toggleAttrs = createBitsAttrs({
component: "toggle",
parts: ["root"],
});
export class ToggleRootState {
static create(opts) {
return new ToggleRootState(opts);
}
opts;
attachment;
constructor(opts) {
this.opts = opts;
this.attachment = attachRef(this.opts.ref);
this.onclick = this.onclick.bind(this);
}
onclick(_) {
if (this.opts.disabled.current)
return;
this.opts.pressed.current = !this.opts.pressed.current;
}
snippetProps = $derived.by(() => ({
pressed: this.opts.pressed.current,
}));
props = $derived.by(() => ({
[toggleAttrs.root]: "",
id: this.opts.id.current,
"data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
"aria-pressed": boolToStr(this.opts.pressed.current),
"data-state": getToggleDataState(this.opts.pressed.current),
disabled: boolToTrueOrUndef(this.opts.disabled.current),
onclick: this.onclick,
...this.attachment,
}));
}
export function getToggleDataState(condition) {
return condition ? "on" : "off";
}
|