-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add Switch form component #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jordanarldt
wants to merge
1
commit into
makeswift:main
Choose a base branch
from
jordanarldt:ja/add-switch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = ({ | ||
| 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> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a
classNameprop?