Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@radix-ui/react-portal": "1.1.2",
"@radix-ui/react-radio-group": "1.2.1",
"@radix-ui/react-select": "2.1.2",
"@radix-ui/react-switch": "1.1.3",
"@radix-ui/react-slot": "1.1.0",
"@radix-ui/react-tabs": "1.1.1",
"@radix-ui/react-toast": "1.2.2",
Expand Down
56 changes: 56 additions & 0 deletions apps/web/vibes/soul/docs/switch.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: Switch
preview: switch-example
previewSize: xs
---

## Usage

{/* prettier-ignore-start */}

<CodeBlock lang="ts">{`
import { Switch } from '@/vibes/soul/form/switch';

function Usage() {
return (
<Switch
size="small"
label={{ on: 'On', off: 'Off' }}
/>
);
}
`}
</CodeBlock>

{/* prettier-ignore-end */}

## API Reference

### SwitchProps

| Prop | Type | Default |
| ----------------- | --------------------------------------------- | --------- |
| `name` | `string` | |
| `required` | `boolean` | |
| `variant` | `'primary' \| 'secondary' \| 'tertiary'` | |
| `size` | `'large' \| 'medium' \| 'small'` | |
| `labelPosition` | `'left' \| 'right' \| 'both'` | |
| `label` | `string \| { on: string; off: string }` | |
| `checked` | `boolean` | |
| `defaultChecked` | `boolean` | |
| `onCheckedChange` | `(checked: boolean) => void \| Promise<void>` | |
| `disabled` | `boolean` | |
| `loading` | `boolean` | |

### CSS Variables

This component supports various CSS variables for theming. Here's a comprehensive list.

```css
:root {
--switch-primary-background: hsl(var(--primary));
--switch-secondary-background: hsl(var(--foreground));
--switch-tertiary-background: hsl(var(--background));
--switch-tertiary-border: hsl(var(--contrast-200));
}
```
7 changes: 7 additions & 0 deletions apps/web/vibes/soul/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,13 @@ export const examples = [
files: ['examples/form/swatch-radio-group/index.tsx'],
component: lazy(() => import('./examples/form/swatch-radio-group')),
},
{
name: 'switch-example',
dependencies: [],
registryDependencies: ['switch'],
files: ['examples/form/switch/index.tsx'],
component: lazy(() => import('./examples/form/switch')),
},
{
name: 'textarea-example',
dependencies: [],
Expand Down
31 changes: 31 additions & 0 deletions apps/web/vibes/soul/examples/form/switch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Switch } from '@/vibes/soul/form/switch';

const variants = ['primary', 'secondary', 'tertiary'] as const;
const labelPositions = ['left', 'right', 'both'] as const;

export default function Preview() {
return (
<div className="p-10">
<div className="flex flex-row">
{labelPositions.map((pos) => (
<div key={pos} className="mb-4 flex flex-1 flex-col items-center">
<div className="mb-2 text-sm">
labelPosition: <span className="font-bold">{pos}</span>
</div>
{variants.map((variant) => (
<div key={variant} className="mb-2 flex justify-between">
<Switch
size="small"
label={{ on: 'On', off: 'Off' }}
variant={variant}
labelPosition={pos}
defaultChecked={true}
/>
</div>
))}
</div>
))}
</div>
</div>
);
}
231 changes: 231 additions & 0 deletions apps/web/vibes/soul/form/switch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import * as SwitchPrimitive from '@radix-ui/react-switch';
import { clsx } from 'clsx';
import { Loader2 } from 'lucide-react';
import { useId } from 'react';

import * as Skeleton from '@/vibes/soul/primitives/skeleton';

interface SwitchProps {
name?: string;
required?: boolean;
variant?: 'primary' | 'secondary' | 'tertiary';
size?: 'large' | 'medium' | 'small';
labelPosition?: 'left' | 'right' | 'both';
label?: string | { on: string; off: string };
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void | Promise<void>;
disabled?: boolean;
loading?: boolean;
}

/**
* This component supports various CSS variables for theming. Here's a comprehensive list,
* along with their default values:
*
* ```css
* :root {
* --switch-primary-background: hsl(var(--primary));
* --switch-secondary-background: hsl(var(--foreground));
* --switch-tertiary-background: hsl(var(--background));
* --switch-tertiary-border: hsl(var(--contrast-200));
* }
* ```
*/
export const Switch = ({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a className prop?

name,
variant = 'primary',
size = 'medium',
labelPosition = 'right',
label,
disabled,
loading,
checked,
defaultChecked,
onCheckedChange,
}: SwitchProps) => {
const id = useId();
const hasLabel = label != null && label !== '';

return (
<div className="group/switch flex items-center gap-2">
{(labelPosition === 'left' || labelPosition === 'both') && hasLabel && (
<SwitchLabel
id={id}
label={label}
size={size}
state={labelPosition === 'both' ? 'off' : undefined}
/>
)}
<SwitchPrimitive.Root
aria-busy={loading}
checked={checked}
defaultChecked={defaultChecked}
className={clsx(
'w-12 rounded-full border border-contrast-200 p-[3px] transition-colors duration-100 focus-visible:outline-none focus-visible:ring-2 data-[disabled]:cursor-not-allowed [&:not([data-loading])]:data-[disabled]:bg-contrast-100',
)}
data-loading={loading ? '' : undefined}
disabled={disabled || loading}
id={id}
name={name}
onCheckedChange={onCheckedChange}
>
<SwitchPrimitive.Thumb
className={clsx(
'relative block h-5 w-5 overflow-hidden rounded-full transition-transform duration-100 data-[state=checked]:translate-x-full data-[disabled]:bg-contrast-200 data-[state=unchecked]:bg-contrast-200',
{
primary: 'bg-[var(--switch-primary-background,hsl(var(--primary)))]',
secondary: 'bg-[var(--switch-secondary-background,hsl(var(--foreground)))]',
tertiary:
'border border-[var(--switch-tertiary-border,hsl(var(--contrast-200)))] bg-[var(--switch-tertiary-background,hsl(var(--background)))]',
}[variant],
)}
>
<span
className={clsx(
'absolute inset-0 grid place-content-center transition-all duration-300 ease-in-out',
loading ? 'translate-y-0 opacity-100' : 'translate-y-full opacity-0',
)}
>
<Loader2
className={clsx('animate-spin', variant === 'tertiary' && 'text-foreground')}
size={16}
/>
</span>
</SwitchPrimitive.Thumb>
</SwitchPrimitive.Root>
{(labelPosition === 'right' || labelPosition === 'both') && hasLabel && (
<SwitchLabel
id={id}
label={label}
loading={loading}
size={size}
state={labelPosition === 'both' ? 'on' : undefined}
/>
)}
</div>
);
};

interface LabelProps {
id: string;
label: string | { on: string; off: string };
size: 'large' | 'medium' | 'small';
state?: 'off' | 'on';
loading?: boolean;
}

function SwitchLabel({ id, label, size = 'medium', state, loading }: LabelProps) {
const baseClass =
'[&:not([data-loading])]:group-has-[[data-disabled]]/switch:text-contrast-400 font-semibold select-none';

if (typeof label === 'string') {
return (
<label
className={clsx(
baseClass,
{
small: 'text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
data-loading={loading ? '' : undefined}
htmlFor={id}
>
{label}
</label>
);
}

if (state) {
return (
<label
className={clsx(
baseClass,
{
small: 'text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
data-loading={loading ? '' : undefined}
htmlFor={id}
>
{label[state]}
</label>
);
}

return (
<div className="leading-[0]">
<label
className={clsx(
'group-has-[[data-state=unchecked]]/switch:invisible group-has-[[data-state=checked]]/switch:block',
baseClass,
{
small: 'mb-[-1px] text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
data-loading={loading ? '' : undefined}
htmlFor={id}
>
{label.on}
</label>
<label
className={clsx(
'group-has-[[data-state=checked]]/switch:invisible group-has-[[data-state=unchecked]]/switch:block',
baseClass,
{
small: 'mt-[-1px] text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
data-loading={loading ? '' : undefined}
htmlFor={id}
>
{label.off}
</label>
</div>
);
}

export function SwitchSkeleton({
size = 'medium',
labelPosition = 'right',
}: Pick<SwitchProps, 'size' | 'labelPosition'>) {
return (
<div className="flex items-center gap-2">
{(labelPosition === 'left' || labelPosition === 'both') && (
<Skeleton.Text
characterCount={6}
className={clsx(
'rounded',
{
small: 'text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
/>
)}
<Skeleton.Box className="h-6 w-12 rounded-full p-[3px]" />
{(labelPosition === 'right' || labelPosition === 'both') && (
<Skeleton.Text
characterCount={6}
className={clsx(
'rounded',
{
small: 'text-sm',
medium: 'text-base',
large: 'text-lg',
}[size],
)}
/>
)}
</div>
);
}
6 changes: 6 additions & 0 deletions apps/web/vibes/soul/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ const formPages: [Page, ...Page[]] = [
file: 'docs/swatch-radio-group.mdx',
component: 'swatch-radio-group',
},
{
title: 'Switch',
slug: 'switch',
file: 'docs/switch.mdx',
component: 'switch',
},
{
title: 'Textarea',
slug: 'textarea',
Expand Down
Loading