-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
126 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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,48 @@ | ||
import { style } from '@vanilla-extract/css' | ||
import { tokens } from '../../theme' | ||
|
||
export const switchStyles = style({ | ||
boxSizing: 'border-box', | ||
width: '1.75rem', | ||
borderRadius: 9999, | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'flex-start', | ||
|
||
backgroundColor: tokens.colors.opaque3, | ||
borderWidth: 1, | ||
borderStyle: 'solid', | ||
borderColor: 'transparent', | ||
|
||
':focus-visible': { | ||
borderColor: tokens.colors.gray1000, | ||
}, | ||
|
||
selectors: { | ||
'&[data-state=checked], &[data-state=open]': { | ||
backgroundColor: tokens.colors.signalGreenElement, | ||
}, | ||
|
||
'&[disabled]': { | ||
backgroundColor: tokens.colors.opaque3, | ||
cursor: 'not-allowed', | ||
}, | ||
}, | ||
}) | ||
|
||
export const thumbStyles = style({ | ||
width: '1rem', | ||
height: '1rem', | ||
borderRadius: '100%', | ||
backgroundColor: tokens.colors.textNegative, | ||
|
||
transition: 'transform 100ms', | ||
transform: 'translateX(0)', | ||
willChange: 'transform', | ||
|
||
selectors: { | ||
'&[data-state=checked]': { | ||
transform: 'translateX(0.6rem)', | ||
}, | ||
}, | ||
}) |
This file contains 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,41 @@ | ||
import { action } from '@storybook/addon-actions' | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
import { useState, type ComponentProps } from 'react' | ||
import { Switch } from './Switch' | ||
|
||
type Controls = ComponentProps<typeof Switch> | ||
|
||
const meta: Meta<Controls> = { | ||
title: 'Switch', | ||
component: Switch, | ||
} | ||
export default meta | ||
|
||
type Story = StoryObj<Controls> | ||
|
||
export const Controlled: Story = { | ||
render: () => { | ||
const [checked, setIsChecked] = useState(true) | ||
|
||
const toggle = () => setIsChecked((isChecked) => !isChecked) | ||
|
||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Switch checked={checked} onCheckedChange={toggle} /> | ||
</div> | ||
) | ||
}, | ||
} | ||
|
||
export const Uncontrolled: Story = { | ||
render: (args: Controls) => { | ||
return ( | ||
<div style={{ maxWidth: '400px' }}> | ||
<Switch defaultChecked onCheckedChange={args.onCheckedChange} /> | ||
</div> | ||
) | ||
}, | ||
args: { | ||
onCheckedChange: action('onCheckedChange'), | ||
}, | ||
} |
This file contains 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,13 @@ | ||
import * as RadixSwitch from '@radix-ui/react-switch' | ||
import clsx from 'clsx' | ||
import { switchStyles, thumbStyles } from './Switch.css' | ||
|
||
export type SwitchProps = RadixSwitch.SwitchProps | ||
|
||
export const Switch = ({ className, ...props }: SwitchProps) => { | ||
return ( | ||
<RadixSwitch.Root className={clsx(switchStyles, className)} {...props}> | ||
<RadixSwitch.Thumb className={thumbStyles} /> | ||
</RadixSwitch.Root> | ||
) | ||
} |
This file contains 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 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