-
Notifications
You must be signed in to change notification settings - Fork 0
[REFACTOR] switch 컴포넌트 radix 의존성 제거 #126
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,23 +1,60 @@ | ||||||
| import * as SwitchPrimitives from '@radix-ui/react-switch'; | ||||||
| import { useState } from 'react'; | ||||||
|
|
||||||
| import { COLORS, SwitchColor } from '@/shared/lib/colors'; | ||||||
|
|
||||||
| type Props = React.ComponentProps<typeof SwitchPrimitives.Root> & { | ||||||
| type Props = Omit<React.ComponentProps<'button'>, 'onClick'> & { | ||||||
| /** | ||||||
| * color of the switch. | ||||||
| * @default 'primary' | ||||||
| */ | ||||||
| color?: SwitchColor; | ||||||
| checked?: boolean; | ||||||
| defaultChecked?: boolean; | ||||||
| onCheckedChange?: (checked: boolean) => void; | ||||||
| }; | ||||||
|
|
||||||
| export function Switch({ color = 'primary', ...props }: Props) { | ||||||
| export function Switch({ | ||||||
| color = 'primary', | ||||||
| checked, | ||||||
| defaultChecked = false, | ||||||
| onCheckedChange, | ||||||
| className, | ||||||
| disabled, | ||||||
| ...props | ||||||
| }: Props) { | ||||||
| const [uncontrolledChecked, setUncontrolledChecked] = useState(defaultChecked); | ||||||
| const isChecked = checked ?? uncontrolledChecked; | ||||||
|
|
||||||
| const handleClick = () => { | ||||||
| if (disabled) return; | ||||||
|
|
||||||
| const next = !isChecked; | ||||||
| if (checked === undefined) { | ||||||
| setUncontrolledChecked(next); | ||||||
| } | ||||||
| onCheckedChange?.(next); | ||||||
| }; | ||||||
|
|
||||||
| return ( | ||||||
| <SwitchPrimitives.Root | ||||||
| <button | ||||||
| {...props} | ||||||
| className="group flex h-3.5 w-7 items-center rounded-full shadow-lg data-[state=checked]:bg-[var(--switch-color)] data-[state=unchecked]:bg-gray-500" | ||||||
| style={color && ({ '--switch-color': COLORS[color] } as React.CSSProperties)} | ||||||
| type="button" | ||||||
| role="switch" | ||||||
| disabled={disabled} | ||||||
| onClick={handleClick} | ||||||
| aria-checked={isChecked} | ||||||
| data-state={isChecked ? 'checked' : 'unchecked'} | ||||||
| className={[ | ||||||
| 'group flex h-3.5 w-7 items-center rounded-full shadow-lg', | ||||||
| 'data-[state=checked]:bg-[var(--switch-color)]', | ||||||
| 'data-[state=unchecked]:bg-gray-500', | ||||||
| className, | ||||||
| ] | ||||||
| .filter(Boolean) | ||||||
| .join(' ')} | ||||||
| style={{ '--switch-color': COLORS[color] } as React.CSSProperties} | ||||||
| > | ||||||
| <SwitchPrimitives.Thumb className="h-5 w-5 -translate-x-1.5 rounded-full bg-white shadow-inner drop-shadow-md duration-100 group-data-[state=checked]:translate-x-3.5" /> | ||||||
| </SwitchPrimitives.Root> | ||||||
| <span className="h-5 w-5 -translate-x-1.5 rounded-full bg-white shadow-inner drop-shadow-md duration-100 group-data-[state=checked]:translate-x-3.5" /> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. transition 클래스 누락으로 애니메이션이 동작하지 않습니다.
🔎 트랜지션 수정 제안- <span className="h-5 w-5 -translate-x-1.5 rounded-full bg-white shadow-inner drop-shadow-md duration-100 group-data-[state=checked]:translate-x-3.5" />
+ <span className="h-5 w-5 -translate-x-1.5 rounded-full bg-white shadow-inner drop-shadow-md transition-transform duration-100 group-data-[state=checked]:translate-x-3.5" />📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| </button> | ||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ); | ||||||
| } | ||||||
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.
🧹 Nitpick | 🔵 Trivial
선택적 개선: 명시적인 disabled 및 focus 스타일 고려.
현재는 네이티브
disabled속성과 브라우저 기본 포커스 스타일에 의존하고 있습니다. 디자인 시스템의 일관성을 위해 명시적인 스타일을 추가하는 것을 고려해보세요:disabled:opacity-50 disabled:cursor-not-allowed- 비활성 상태 시각적 피드백focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2- 접근성 향상을 위한 포커스 표시🔎 선택적 스타일링 개선 제안
className={[ 'group flex h-3.5 w-7 items-center rounded-full shadow-lg', 'data-[state=checked]:bg-[var(--switch-color)]', 'data-[state=unchecked]:bg-gray-500', + 'disabled:opacity-50 disabled:cursor-not-allowed', + 'focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-300', className, ]🤖 Prompt for AI Agents