Files
base/node_modules/bits-ui/dist/bits/toggle/toggle.svelte.js
T
eewing ec317eb17c
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m11s
INIT
2026-02-18 15:17:47 -06:00

40 lines
1.3 KiB
JavaScript

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";
}