-
Notifications
You must be signed in to change notification settings - Fork 6
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-fe: Switch Toggle 컴포넌트 구현 #230
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
82 changes: 82 additions & 0 deletions
82
frontend/src/components/common/ToggleSwitch/ToggleSwitch.stories.tsx
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,82 @@ | ||
import { useArgs } from '@storybook/preview-api'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import ToggleSwitch from './index'; | ||
|
||
const meta: Meta<typeof ToggleSwitch> = { | ||
title: 'Components/ToggleSwitch', | ||
component: ToggleSwitch, | ||
parameters: { | ||
layout: 'centered', | ||
docs: { | ||
description: { | ||
component: 'ToggleSwitch 컴포넌트는 스위치를 토글할 수 있는 기능을 제공합니다.', | ||
}, | ||
}, | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
isChecked: { | ||
description: '스위치의 체크 상태를 나타냅니다.', | ||
control: { type: 'boolean' }, | ||
table: { | ||
type: { summary: 'boolean' }, | ||
}, | ||
}, | ||
isDisabled: { | ||
description: '스위치의 활성화 상태를 나타냅니다.', | ||
control: { type: 'boolean' }, | ||
table: { | ||
type: { summary: 'boolean' }, | ||
}, | ||
}, | ||
onChange: { | ||
description: '스위치 상태 변경 시 호출되는 콜백 함수입니다.', | ||
action: 'changed', | ||
table: { | ||
type: { summary: '() => void' }, | ||
}, | ||
}, | ||
}, | ||
decorators: [ | ||
(Child, context) => { | ||
const [args, updateArgs] = useArgs(); | ||
|
||
const handleChange = () => { | ||
updateArgs({ isChecked: !context.args.isChecked }); | ||
}; | ||
|
||
return ( | ||
<Child | ||
args={{ | ||
...args, | ||
onChange: handleChange, | ||
}} | ||
/> | ||
); | ||
}, | ||
], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
isChecked: false, | ||
}, | ||
}; | ||
|
||
export const Disabled: Story = { | ||
args: { | ||
isChecked: false, | ||
isDisabled: true, | ||
}, | ||
}; | ||
|
||
Default.parameters = { | ||
docs: { | ||
description: { | ||
story: 'ToggleSwitch 컴포넌트의 기본 상태입니다.', | ||
}, | ||
}, | ||
}; |
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,20 @@ | ||
import S, { StyleProps } from './style'; | ||
|
||
interface ToggleSwitchProps extends StyleProps { | ||
onChange: () => void; | ||
} | ||
|
||
export default function ToggleSwitch({ isChecked, isDisabled, onChange }: ToggleSwitchProps) { | ||
return ( | ||
<S.Switch | ||
isChecked={isChecked} | ||
isDisabled={isDisabled} | ||
onClick={onChange} | ||
> | ||
<S.Knob | ||
isChecked={isChecked} | ||
isDisabled={isDisabled} | ||
/> | ||
</S.Switch> | ||
); | ||
} |
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,60 @@ | ||
import { css } from '@emotion/react'; | ||
import styled from '@emotion/styled'; | ||
|
||
export interface StyleProps { | ||
isChecked: boolean; | ||
isDisabled: boolean; | ||
} | ||
|
||
const Switch = styled.div<StyleProps>` | ||
width: 5rem; | ||
--parent-width: 5rem; | ||
aspect-ratio: 5/3; | ||
|
||
background-color: ${({ isChecked, theme }) => | ||
isChecked ? theme.baseColors.purplescale[600] : theme.baseColors.grayscale[100]}; | ||
outline: 0.2rem solid | ||
${({ isChecked, theme }) => (isChecked ? theme.baseColors.purplescale[600] : theme.baseColors.grayscale[700])}; | ||
border-radius: 99rem; | ||
|
||
display: flex; | ||
align-items: center; | ||
|
||
padding: 0.3rem; | ||
--parent-padding: 0.3rem; | ||
|
||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
|
||
${({ isDisabled, theme }) => | ||
isDisabled && | ||
css({ | ||
backgroundColor: theme.baseColors.grayscale[400], | ||
outline: `0.2rem solid ${theme.baseColors.grayscale[500]}`, | ||
})} | ||
`; | ||
|
||
const Knob = styled.div<StyleProps>` | ||
height: 100%; | ||
aspect-ratio: 1/1; | ||
background-color: ${({ isChecked, theme }) => | ||
isChecked ? theme.baseColors.grayscale[200] : theme.baseColors.grayscale[700]}; | ||
border-radius: 50%; | ||
|
||
transform: ${({ isChecked }) => | ||
isChecked ? 'translate(calc(var(--parent-width) - var(--parent-padding)*2 - 100%))' : 'translate(0)'}; | ||
transition: transform 0.2s; | ||
Comment on lines
+45
to
+47
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. 👍 |
||
|
||
${({ isDisabled, theme }) => | ||
isDisabled && | ||
css({ | ||
backgroundColor: theme.baseColors.grayscale[700], | ||
})} | ||
`; | ||
|
||
const S = { | ||
Switch, | ||
Knob, | ||
}; | ||
|
||
export default S; |
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.
사소한 건데 변수는 잘 보이게 최상단으로 끌어 올려주는 거 어떨까요? 가독성에 대한 거라서 필수는 아닙니다.