2
0

switch.svelte 1.0 KB

123456789101112131415161718192021222324252627
  1. <script lang="ts">
  2. import { Switch as SwitchPrimitive, type WithoutChildrenOrChild } from "bits-ui";
  3. import { cn } from "$lib/utils.js";
  4. let {
  5. ref = $bindable(null),
  6. checked = $bindable(false),
  7. class: className,
  8. ...restProps
  9. }: WithoutChildrenOrChild<SwitchPrimitive.RootProps> = $props();
  10. </script>
  11. <SwitchPrimitive.Root
  12. bind:ref
  13. bind:checked
  14. class={cn(
  15. "focus-visible:ring-ring focus-visible:ring-offset-background data-[state=checked]:bg-primary data-[state=unchecked]:bg-input 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-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
  16. className
  17. )}
  18. {...restProps}
  19. >
  20. <SwitchPrimitive.Thumb
  21. class={cn(
  22. "bg-background pointer-events-none block size-4 rounded-full shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0"
  23. )}
  24. />
  25. </SwitchPrimitive.Root>