Files
eewing ec317eb17c
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m11s
INIT
2026-02-18 15:17:47 -06:00

57 lines
1.3 KiB
Svelte

<script lang="ts">
import { boxWith, mergeProps } from "svelte-toolbelt";
import type { SwitchRootProps } from "../types.js";
import { SwitchRootState } from "../switch.svelte.js";
import SwitchInput from "./switch-input.svelte";
import { createId } from "../../../internal/create-id.js";
import { noop } from "../../../internal/noop.js";
const uid = $props.id();
let {
child,
children,
ref = $bindable(null),
id = createId(uid),
disabled = false,
required = false,
checked = $bindable(false),
value = "on",
name = undefined,
type = "button",
onCheckedChange = noop,
...restProps
}: SwitchRootProps = $props();
const rootState = SwitchRootState.create({
checked: boxWith(
() => checked,
(v) => {
checked = v;
onCheckedChange?.(v);
}
),
disabled: boxWith(() => disabled ?? false),
required: boxWith(() => required),
value: boxWith(() => value),
name: boxWith(() => name),
id: boxWith(() => id),
ref: boxWith(
() => ref,
(v) => (ref = v)
),
});
const mergedProps = $derived(mergeProps(restProps, rootState.props, { type }));
</script>
{#if child}
{@render child({ props: mergedProps, ...rootState.snippetProps })}
{:else}
<button {...mergedProps}>
{@render children?.(rootState.snippetProps)}
</button>
{/if}
<SwitchInput />