Skip to content
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

[FEAT] Switch 컴포넌트, 스토리북 제작 #20

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ yarn-error.log*
next-env.d.ts

*storybook.log
storybook-static
179 changes: 178 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prepare": "husky && husky install"
},
"dependencies": {
"@radix-ui/react-switch": "^1.1.2",
"@storybook/addon-a11y": "^8.4.7",
"@tanstack/react-query": "^5.62.15",
"axios": "^1.7.9",
Expand Down
1 change: 1 addition & 0 deletions src/shared/lib/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ export const COLORS = {

export type Colors = keyof typeof colors;
export type IconColor = keyof typeof COLORS;
export type SwitchColor = keyof typeof COLORS;
Copy link
Collaborator

@ujinsim ujinsim Jan 26, 2025

Choose a reason for hiding this comment

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

피그마 상에는 다른 색상은 사용되고 있지 않은 것으로 확인했는데, 다른 색상의 사용 활용에 대해 전달받으신 부분이 있는지 궁금합니다 !

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PR에도 남긴 부분인데, 디자인팀과는 primary 단일 색상으로 의견을 맞췄어요.
하지만 다른 색상을 적용해야하는 경우가 존재할 수 있다고 판단했고, 이 경우에 대응이 쉽도록 color 속성 + defualt 처리했어요 :)

export const colorNames = Object.keys(colors) as Colors[];
31 changes: 31 additions & 0 deletions src/shared/ui/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Meta, StoryObj } from '@storybook/react';

import { COLORS } from '@/shared/lib/colors';

import { Switch } from '.';

const meta = {
title: 'components/common/Switch',
component: Switch,
tags: ['autodocs'],
} satisfies Meta<typeof Switch>;

export default meta;
type Story = StoryObj<typeof Switch>;

export const Basic: Story = {
args: {
color: 'primary',
},
argTypes: {
color: {
control: { type: 'select' },
table: {
type: { summary: 'SwitchColor' },
defaultValue: { summary: 'primary' },
},
options: Object.keys(COLORS),
},
},
render: (args) => <Switch {...args} />,
};
23 changes: 23 additions & 0 deletions src/shared/ui/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as SwitchPrimitives from '@radix-ui/react-switch';

import { COLORS, SwitchColor } from '@/shared/lib/colors';

type Props = React.ComponentProps<typeof SwitchPrimitives.Root> & {
/**
* color of the switch.
* @default 'primary'
*/
color?: SwitchColor;
};

export function Switch({ color = 'primary', ...props }: Props) {
return (
<SwitchPrimitives.Root
{...props}
className="group relative 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"
keemsebin marked this conversation as resolved.
Show resolved Hide resolved
style={color && ({ '--switch-color': COLORS[color] } as React.CSSProperties)}
>
<SwitchPrimitives.Thumb className="h-5 w-5 -translate-x-1.5 rounded-full bg-white shadow-inner duration-100 group-data-[state=checked]:translate-x-3.5" />
</SwitchPrimitives.Root>
);
}
1 change: 1 addition & 0 deletions src/shared/ui/Switch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Switch } from './Switch';
Loading