add switch and checkbox components

This commit is contained in:
Justin Edmund 2025-09-16 01:34:06 -07:00
parent 205e1045a6
commit 1b0294a683
4 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<!-- Checkbox Component -->
<script lang="ts">
import { Checkbox as CheckboxPrimitive } from 'bits-ui';
import { Check, Minus } from 'lucide-svelte';
import { cn } from '$lib/utils.js';
import styles from './checkbox.module.scss';
import type { HTMLButtonAttributes } from 'svelte/elements';
interface Props extends Omit<HTMLButtonAttributes, 'value'> {
checked?: boolean | 'indeterminate';
disabled?: boolean;
required?: boolean;
name?: string;
value?: string;
onCheckedChange?: (checked: boolean | 'indeterminate') => void;
class?: string;
variant?: 'default' | 'bound';
size?: 'small' | 'medium' | 'large';
}
let {
checked = $bindable(false),
disabled = false,
required = false,
name,
value,
onCheckedChange,
class: className,
variant = 'default',
size = 'medium',
...restProps
}: Props = $props();
$: if (onCheckedChange && checked !== undefined) {
onCheckedChange(checked);
}
const sizeClasses = {
small: styles.small,
medium: styles.medium,
large: styles.large
};
const variantClasses = {
default: '',
bound: styles.bound
};
</script>
<CheckboxPrimitive.Root
bind:checked
{disabled}
{required}
{name}
{value}
class={cn(
styles.checkbox,
sizeClasses[size],
variantClasses[variant],
className
)}
{...restProps}
>
<CheckboxPrimitive.Indicator class={styles.indicator}>
{#if checked === 'indeterminate'}
<Minus class={styles.icon} />
{:else}
<Check class={styles.icon} />
{/if}
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>

View file

@ -0,0 +1,99 @@
@use 'themes/spacing';
@use 'themes/colors';
@use 'themes/layout';
@use 'themes/typography';
.checkbox {
background-color: var(--input-bg);
border: 2px solid var(--separator-bg);
border-radius: layout.$item-corner-small;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
transition: all 0.18s ease-out;
&:hover:not(:disabled) {
background-color: var(--input-bg-hover);
border-color: var(--separator-bg-hover);
}
&:focus,
&:focus-visible {
outline: none;
border-color: colors.$blue;
box-shadow: 0 0 0 2px rgba(colors.$blue, 0.2);
}
&[data-state='checked'],
&[data-state='indeterminate'] {
background-color: var(--accent-blue);
border-color: var(--accent-blue);
&:hover:not(:disabled) {
background-color: var(--accent-blue-hover);
border-color: var(--accent-blue-hover);
}
}
&:disabled {
cursor: not-allowed;
opacity: 0.5;
}
&.bound {
background-color: var(--input-bound-bg);
&:hover:not(:disabled) {
background-color: var(--input-bound-bg-hover);
}
&[data-state='checked'],
&[data-state='indeterminate'] {
background-color: var(--accent-blue);
border-color: var(--accent-blue);
}
}
}
// Size variations
.small {
width: 16px;
height: 16px;
.icon {
width: 12px;
height: 12px;
}
}
.medium {
width: 20px;
height: 20px;
.icon {
width: 14px;
height: 14px;
}
}
.large {
width: 24px;
height: 24px;
.icon {
width: 18px;
height: 18px;
}
}
.indicator {
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.icon {
stroke-width: 3;
}

View file

@ -0,0 +1,46 @@
<!-- Switch Component -->
<script lang="ts">
import { Switch as SwitchPrimitive } from 'bits-ui';
import { cn } from '$lib/utils.js';
import styles from './switch.module.scss';
import type { HTMLButtonAttributes } from 'svelte/elements';
interface Props extends Omit<HTMLButtonAttributes, 'value'> {
checked?: boolean;
disabled?: boolean;
required?: boolean;
name?: string;
value?: string;
onCheckedChange?: (checked: boolean) => void;
class?: string;
thumbClass?: string;
}
let {
checked = $bindable(false),
disabled = false,
required = false,
name,
value,
onCheckedChange,
class: className,
thumbClass,
...restProps
}: Props = $props();
$: if (onCheckedChange && checked !== undefined) {
onCheckedChange(checked);
}
</script>
<SwitchPrimitive.Root
bind:checked
{disabled}
{required}
{name}
{value}
class={cn(styles.switch, className)}
{...restProps}
>
<SwitchPrimitive.Thumb class={cn(styles.thumb, thumbClass)} />
</SwitchPrimitive.Root>

View file

@ -0,0 +1,54 @@
@use 'themes/spacing';
@use 'themes/colors';
@use 'themes/layout';
.switch {
$height: 34px;
background: colors.$grey-70;
border-radius: calc($height / 2);
border: none;
padding-left: spacing.$unit-half;
padding-right: spacing.$unit-half;
position: relative;
width: 58px;
height: $height;
cursor: pointer;
transition: background-color 100ms ease-out;
&:focus,
&:focus-visible {
box-shadow: 0 0 0 2px var(--accent-blue-focus);
outline: none;
}
&[data-state='checked'] {
background: var(--accent-blue);
}
&:disabled {
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
.thumb {
background: colors.$grey-80;
cursor: not-allowed;
}
}
}
.thumb {
background: colors.$grey-100;
border-radius: 13px;
display: block;
height: 26px;
width: 26px;
transition: transform 100ms;
transform: translateX(0px);
cursor: pointer;
&[data-state='checked'] {
background: colors.$grey-100;
transform: translateX(24px);
}
}