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(xdesign-story): Component Switch 구현 #59

Open
wants to merge 6 commits into
base: new-design-system
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions services/xdesign-story/src/components/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import styled from '@emotion/styled';
import { useRef, useState } from 'react';
import { Switch } from '.';

export default {
title: 'Components/Switch',
component: Switch,
};

const Background = styled.div`
display: flex;
`;

export const Default: React.VFC = () => {
const [isOn, setIsOn] = useState<boolean>(false);
const ref = useRef(null);
const onClick = () => {
setIsOn(!isOn);
};

return (
<Background>
<Switch isOn={isOn} onClick={onClick} disabled={false} ref={ref} />
<Switch isOn={false} onClick={onClick} disabled={true} />
<Switch isOn={true} onClick={onClick} disabled={true} />
</Background>
);
};
55 changes: 55 additions & 0 deletions services/xdesign-story/src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useRef } from 'react';
import styled from '@emotion/styled';
import { SwitchProps } from './Switch.types';

const SWITCH_WIDTH = 56;
const SWITCH_HEIGHT = 32;
const SWITCH_PADDING = 1;
const SWITCH_TRANSITION_TIME = '0.1s';

const SwitchContainer = styled.button<SwitchProps>`
position: relative;
cursor: ${(props) => (props.disabled ? 'not-allowed' : 'pointer')};
opacity: ${(props) => (props.disabled ? 0.4 : 1)};
width: ${SWITCH_WIDTH}px;
height: ${SWITCH_HEIGHT}px;
border-radius: ${SWITCH_HEIGHT}px;
border: none;
display: flex;
justify-content: center;
align-items: center;
transition: ${SWITCH_TRANSITION_TIME};
background-color: ${(props) =>
props.isOn ? props.theme.color.lightTheme.Secondary : props.theme.color.lightTheme.Outline};
> .circle {
position: absolute;
top: ${SWITCH_PADDING}px;
left: ${(props) =>
props.isOn
? `${SWITCH_WIDTH - SWITCH_HEIGHT + SWITCH_PADDING}px`
: `${SWITCH_PADDING}px`};
width: ${SWITCH_HEIGHT - SWITCH_PADDING * 2}px;
height: ${SWITCH_HEIGHT - SWITCH_PADDING * 2}px;
border-radius: 50%;
background: white;
transition: ${SWITCH_TRANSITION_TIME};
}
`;

export const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(function Switch(props, ref) {
const handleContainerClick = () => {
if (!props.disabled) {
props.onClick();
if (typeof ref !== 'function') {
ref?.current?.click();
}
}
};

return (
<SwitchContainer onClick={handleContainerClick} disabled={props.disabled} isOn={props.isOn}>
<div className="circle" />
<input type="checkbox" style={{ display: 'none' }} ref={ref} />
</SwitchContainer>
);
});
10 changes: 10 additions & 0 deletions services/xdesign-story/src/components/Switch/Switch.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface SwitchProps {
/** on/off 여부*/
isOn: boolean;

/** 클릭 이벤트 */
onClick: () => void;

/** 활성화/비활성화 여부 */
disabled: boolean;
}
2 changes: 2 additions & 0 deletions services/xdesign-story/src/components/Switch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Switch } from './Switch';
export type { SwitchProps } from './Switch.types';
Loading