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

ADCIO-4651) refactor: refactor switch component #111

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions src/component/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState } from 'react';

import type { Meta, StoryFn } from '@storybook/react';
import { Switch } from './Switch';
import { hstack, vstack } from '../../../styled-system/patterns';

export default {
component: Switch,
parameters: {
design: {
type: 'figma',
url: 'https://www.figma.com/file/tN7Q8dJZqVsjo2FWflOKfu/CDS(Corca-Design-System)?type=design&node-id=435-15115&mode=design&t=bhY2SksBs7AqptYx-0',
},
},
} as Meta<typeof Switch>;

const Template: StoryFn<typeof Switch> = args => <Switch {...args} />;
export const Basic = Template.bind({});
Basic.args = {
disabled: false,
checked: true,
};

const containerStyle = vstack({ gap: 20, flexDirection: 'column' });
const wrapperStyle = hstack({ gap: 15 });

export function Active() {
const [active, setActive1] = useState(false);
const [disabled1, setdDisabled1] = useState(true);
const [disabled2, setdDisabled2] = useState(false);

return (
<div className={containerStyle}>
<div className={wrapperStyle}>
<Switch checked={active} onChange={setActive1} />
</div>
<div className={wrapperStyle}>
<Switch checked={disabled1} disabled onChange={setdDisabled1} />
<Switch checked={disabled2} disabled onChange={setdDisabled2} />
</div>
</div>
);
}
148 changes: 148 additions & 0 deletions src/component/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { forwardRef } from 'react';
import { cx, sva } from '../../../styled-system/css';

export interface SwitchProps {
checked: boolean;
onChange: (checked: boolean) => void;
disabled?: boolean;
}

export const Switch = forwardRef<HTMLLabelElement, SwitchProps>(function Switch(
{ checked, onChange, disabled = false, ...props },
ref,
) {
const classes = switchSlot({ disabled, checked });

return (
<label ref={ref} {...props} className={cx(classes.root)}>
<input
type="checkbox"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
type="checkbox"
type="checkbox"
role="switch"

Copy link
Collaborator

Choose a reason for hiding this comment

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

์œ„์™€ ๊ฐ™์€ ์ ‘๊ทผ์„ฑ์ด๋ž‘ ๊ธฐํƒ€ ์ธํ„ฐํŽ˜์ด์Šค ๋•Œ๋ฌธ์—
https://react-spectrum.adobe.com/react-aria/Switch.html๋ฅผ ๋ž˜ํ•‘์—์„œ ์“ฐ๋Š”๊ฒŒ ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์™œ๋‚˜๋ฉด ํƒญ ์ธ๋ฑ์Šค, ํฌ์ปค์Šค, ํ‚ค๋ณด๋“œ ๋“ฑ ์‹ค์ œ๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์‹ ๊ฒฝ์“ธ๊ป˜ ๋งŽ๊ฑฐ๋“ ์š”.

https://www.jollyui.dev/docs/components/switch ๋ฅผ ๋ณด๋ฉด react-aria-component๋กœ ์–ด๋–ป๊ฒŒ ์ปค์Šคํ…€ํ•˜๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-07-30 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 8 58 29

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋„ ๋ณต์žกํ•˜๊ณ  cds์—๋งŒ ์žˆ๋Š” ํŠน์ˆ˜ํ•œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋Œ€๋ถ€๋ถ„ react-aria ์“ฐ๋ฉด ์–ด๋–จ๊นŒ ์‹ถ์–ด์š”

Copy link
Member Author

Choose a reason for hiding this comment

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

๋‹ค๋“ค ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž ๊ทธ๋ ‡๊ฒŒ ์ˆ˜์ • ํ›„ ๋ฆฌ๋ทฐ ์š”์ฒญ ๋“œ๋ฆด๊ฒŒ์š”~

checked={checked}
onChange={() => onChange(!checked)}
disabled={disabled}
className={cx(classes.checkbox)}
/>
<div className={cx(classes.slider)}>
<span className={cx(classes.text, classes.textOn)}>ON</span>
<span className={cx(classes.text, classes.textOff)}>OFF</span>
</div>
</label>
);
});

Switch.displayName = 'Switch';

const SWITCH_WIDTH = 38;
const SWITCH_HEIGHT = 18;
const SWITCH_CIRCLE_SIZE = 12;
const SWITCH_CIRCLE_GAP = 3;

const switchSlot = sva({
slots: ['root', 'checkbox', 'slider', 'text', 'textOn', 'textOff'],
base: {
root: {
position: 'relative',
width: `${SWITCH_WIDTH}px`,
height: `${SWITCH_HEIGHT}px`,
userSelect: 'none',
display: 'inline-block',
cursor: 'pointer',
},
checkbox: {
opacity: 0,
width: 0,
height: 0,
},
slider: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
borderRadius: '10px',
display: 'flex',
alignItems: 'center',
transition: 'background 0.2s ease',
'&::before': {
content: '""',
zIndex: 10,
width: `${SWITCH_CIRCLE_SIZE}px`,
height: `${SWITCH_CIRCLE_SIZE}px`,
backgroundColor: 'etc.white',
borderRadius: '50%',
transition: 'transform 0.2s ease, background-color 0.2s ease',
transform: `translateX(${SWITCH_CIRCLE_GAP}px)`,
},
},
text: {
position: 'absolute',
fontSize: '9px',
fontWeight: 500,
fontFamily: 'Pretendard Variable, Pretendard',
lineHeight: '1.2',
},
textOn: {
left: '5px',
},
textOff: {
right: '3.5px',
},
},
variants: {
checked: {
true: {
slider: {
backgroundColor: 'main.black',
'&::before': {
transform: `translateX(${SWITCH_WIDTH - SWITCH_CIRCLE_SIZE - SWITCH_CIRCLE_GAP}px)`,
},
},
textOn: {
opacity: 1,
},
textOff: {
opacity: 0,
},
},
false: {
slider: {
backgroundColor: 'grey.40',
'&::before': {
transform: `translateX(${SWITCH_CIRCLE_GAP}px)`,
},
},
textOn: {
opacity: 0,
},
textOff: {
opacity: 1,
},
},
},
disabled: {
true: {
slider: {
backgroundColor: 'grey.50',
cursor: 'not-allowed',
'&::before': {
backgroundColor: 'grey.60',
},
},
text: {
color: 'grey.60',
},
},
false: {
slider: {
backgroundColor: 'grey.40',
},
textOn: {
color: 'etc.white',
},
textOff: {
color: 'grey.50',
},
},
},
},
});
7 changes: 7 additions & 0 deletions src/component/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export { Button } from './Button/button';
export type { IButtonProps } from './Button/button';

export { Badge } from './Badge/Badge';
export type { BadgeProps } from './Badge/Badge';

export { Thumbnail } from './Thumbnail/Thumbnail';
export type { ThumbnailProps } from './Thumbnail/Thumbnail';

export { Switch } from './Switch/Switch';
export type { SwitchProps } from './Switch/Switch';