Skip to content

Commit ddb9c2e

Browse files
committed
feat: 컴포넌트 switch 구현
1 parent ced808c commit ddb9c2e

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Switch } from '.';
2+
import { useState } from 'react';
3+
4+
export default {
5+
title: 'components/Switch',
6+
component: Switch,
7+
};
8+
9+
export const Default = () => {
10+
const [isOn1, setIsOn1] = useState<boolean>(false);
11+
const [isOn2, setIsOn2] = useState<boolean>(false);
12+
13+
return (
14+
<>
15+
<h3>{`${isOn1}`}</h3>
16+
<Switch isOn={isOn1} onClick={() => setIsOn1(!isOn1)} isActive={true} />
17+
<br />
18+
<h3>비활성화되었을 시 40%의 alpha 값을 가집니다.</h3>
19+
<Switch isOn={isOn2} onClick={() => setIsOn2(!isOn2)} isActive={false} />
20+
<br />
21+
<Switch isOn={!isOn2} onClick={() => setIsOn2(!isOn2)} isActive={false} />
22+
</>
23+
);
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import styled from '@emotion/styled';
2+
import { color } from '@/foundations';
3+
import { SwitchProps } from './Switch.types';
4+
5+
interface SwitchContainerProps {
6+
isActive: boolean;
7+
}
8+
9+
const SwitchContainer = styled.div<SwitchContainerProps>`
10+
position: relative;
11+
cursor: pointer;
12+
opacity: ${(props) => (props.isActive ? 1 : 0.4)};
13+
> .wrapper {
14+
width: 56px;
15+
height: 32px;
16+
border-radius: 30px;
17+
background: ${color.lightTheme.Outline};
18+
display: flex;
19+
justify-content: center;
20+
align-items: center;
21+
}
22+
> .switch--checked {
23+
background: ${color.lightTheme.Secondary};
24+
transition: 0.5s;
25+
}
26+
> .circle {
27+
position: absolute;
28+
top: 1px;
29+
left: 1px;
30+
width: 30px;
31+
height: 30px;
32+
border-radius: 50%;
33+
background: white;
34+
transition: 0.5s;
35+
}
36+
> .switch--checked {
37+
left: 25px;
38+
transition: 0.5s;
39+
}
40+
`;
41+
42+
export const Switch = ({ isOn, onClick, isActive }: SwitchProps) => {
43+
return (
44+
<SwitchContainer onClick={isActive ? onClick : () => {}} isActive={isActive}>
45+
<div className={`wrapper ${isOn ? 'switch--checked' : null}`} />
46+
<div className={`circle ${isOn ? 'switch--checked' : null}`} />
47+
</SwitchContainer>
48+
);
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { HTMLAttributes } from 'react';
2+
3+
export interface SwitchProps extends HTMLAttributes<HTMLElement> {
4+
isOn: boolean;
5+
onClick: () => void;
6+
isActive: boolean;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { Switch } from './Switch';
2+
export type { SwitchProps } from './Switch.types';

0 commit comments

Comments
 (0)