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

docs: useSwitch 스타일링방식 vanilla-extract로 변경/README 업데이트 #23

Merged
merged 2 commits into from
Jun 2, 2024
Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ function App() {

`removeValue (function)`: A function that removes values related to the current key from the local storage.

### useSwitch

You can declaratively manage a Switch (or Toggle) component using useSwitch.

#### Function Arguments

You can determine the initial on/off state of the toggle through `defaultValue`.

#### Return Values

`isOn` (default `false`) : Indicates the state of the switch. If the switch is on, it returns `true`; otherwise, it returns `false`.

`on` : Turns the switch to the on state.

`off` : Turns the switch to the off state.

`toggle` : Reverses the current state of the switch.

## Animation

The animation of this package is based on ClassName by default.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

62 changes: 0 additions & 62 deletions src/stories/useSwitch/Switch.css

This file was deleted.

68 changes: 68 additions & 0 deletions src/stories/useSwitch/Switch.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ComplexStyleRule, style } from '@vanilla-extract/css';

/* The switch - the box around the slider */
export const switchLabel = style({
position: 'relative',
display: 'inline-block',
width: 60,
height: 34,
});

export const switchCirble: ComplexStyleRule = {
position: 'absolute',
content: '',
height: 26,
width: 26,
left: 4,
bottom: 4,
backgroundColor: 'white',
WebkitTransition: '.4s',
transition: '.4s',
};

/* Hide default HTML checkbox */
export const switchInput = style({
opacity: 0,
width: 0,
height: 0,
});

/* The slider */
export const baseSlider = style({
position: 'absolute',
cursor: 'pointer',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: '#ccc',
WebkitTransition: '.4s',
transition: '.4s',
'::before': {
...switchCirble,
},
});

export const checkedSlider = style({
backgroundColor: '#2196F3',
position: 'absolute',
cursor: 'pointer',
top: 0,
left: 0,
right: 0,
bottom: 0,
'::before': {
...switchCirble,
transform: 'translateX(26px)',
WebkitTransform: 'translateX(26px)',
msTransform: 'translateX(26px)',
},
});

/* Rounded sliders */
export const round = style({
borderRadius: 34,
'::before': {
borderRadius: '50%',
},
});
9 changes: 4 additions & 5 deletions src/stories/useSwitch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import useSwitch from '../../useSwitch/useSwitch';
import React, { ChangeEventHandler } from 'react';

import './Switch.css';
import { baseSlider, checkedSlider, round, switchInput, switchLabel } from './Switch.css';

export default function Switch() {
const sw = useSwitch(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

구조 분해 할당도 좋을 것 같네요.

Expand All @@ -11,9 +10,9 @@ export default function Switch() {
};

return (
<label className="switch">
<input type="checkbox" checked={sw.isOn} onChange={handleChangeSwitch} />
<span className="slider round"></span>
<label className={switchLabel}>
<input type="checkbox" checked={sw.isOn} onChange={handleChangeSwitch} className={switchInput} />
<span className={`${sw.isOn ? checkedSlider : baseSlider} ${round}`}></span>
check
</label>
);
Expand Down
Loading