mirror of
https://github.com/hanzoai/svelte.git
synced 2026-10-01 00:41:47 +00:00
Extracted from @hanzo/ui pkg/ui/src/backends/svelte with its history. It lived inside a React component library, which meant every consumer of @hanzo/ui resolved svelte whether they rendered a single Svelte component or not, and the package declared peer svelte>=5 against a dev svelte ^4.2.20 — two answers to one question. Three runtime dependencies, all load-bearing: class-variance-authority for the variants the components already used, and clsx + tailwind-merge behind cn. The cn helper is vendored (17 lines) rather than imported back from @hanzo/ui, because depending on the package this was extracted from would undo the split. No design-system runtime, no icon pack, no CSS-in-JS. svelte-check: 50 files, 0 errors. Co-authored-by: Hanzo Dev <[email protected]>
52 lines
1.5 KiB
Svelte
52 lines
1.5 KiB
Svelte
<!--
|
|
Checkbox — accessible control (role=checkbox, keyboard via native button) with
|
|
the shadcn backend's data-state token styling. `bind:checked` is supported.
|
|
-->
|
|
<script lang="ts">
|
|
import { cn } from './core/cn'
|
|
import { createEventDispatcher } from 'svelte'
|
|
|
|
let className: string | undefined = undefined
|
|
export { className as class }
|
|
export let checked = false
|
|
export let disabled = false
|
|
|
|
const dispatch = createEventDispatcher<{ change: boolean }>()
|
|
|
|
function toggle() {
|
|
if (disabled) return
|
|
checked = !checked
|
|
dispatch('change', checked)
|
|
}
|
|
</script>
|
|
|
|
<button
|
|
type="button"
|
|
role="checkbox"
|
|
aria-checked={checked}
|
|
{disabled}
|
|
data-slot="checkbox"
|
|
data-state={checked ? 'checked' : 'unchecked'}
|
|
class={cn(
|
|
'peer flex size-4 shrink-0 items-center justify-center rounded-[4px] border border-input shadow-xs transition-shadow outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
|
|
className,
|
|
)}
|
|
on:click={toggle}
|
|
{...$$restProps}
|
|
>
|
|
{#if checked}
|
|
<svg
|
|
viewBox="0 0 24 24"
|
|
class="size-3.5"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="3"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
aria-hidden="true"
|
|
>
|
|
<path d="M20 6 9 17l-5-5" />
|
|
</svg>
|
|
{/if}
|
|
</button>
|