Checkbox and Switch
The Radix Indicator and Thumb parts are kept, and the form behavior is built in: with a name the control renders a hidden native input, so it appears in…
The Radix Indicator and Thumb parts are kept, and the form behavior is built in: with a name the control renders a
hidden native input, so it appears in FormData, honors required and resets with its form.
src/ui/toggles.tsxtsx
import { Checkbox as CheckboxPrimitive, mergeElementProps, Switch as SwitchPrimitive } from "@shadow-js/primitives";
import type { CheckboxProps, SwitchProps } from "@shadow-js/primitives";
import { CheckIcon } from "./icons";
export function Checkbox(props: CheckboxProps<"button">) {
return (
<CheckboxPrimitive
{...mergeElementProps(
{
class:
"peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
},
props,
)}
>
<CheckboxPrimitive.Indicator class="flex items-center justify-center text-current">
<CheckIcon class="h-3.5 w-3.5" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive>
);
}
export function Switch(props: SwitchProps<"button">) {
return (
<SwitchPrimitive
{...mergeElementProps(
{
class:
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
},
props,
)}
>
<SwitchPrimitive.Thumb class="pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0" />
</SwitchPrimitive>
);
}