-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6261102
commit b770cb3
Showing
11 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
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,46 @@ | ||
import styled from "styled-components"; | ||
|
||
export const Handle = styled.div` | ||
background-color: ${({ theme }) => theme.toggle.background}; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
height: 32px; | ||
left: 4px; | ||
position: absolute; | ||
top: 4px; | ||
transition: left 200ms ease-in; | ||
width: 32px; | ||
z-index: 1; | ||
`; | ||
|
||
export const Input = styled.input` | ||
cursor: pointer; | ||
opacity: 0; | ||
height: 100%; | ||
position: absolute; | ||
width: 100%; | ||
z-index: 3; | ||
&:checked + ${Handle} { | ||
left: calc(100% - 36px); | ||
} | ||
&:focus + ${Handle} { | ||
box-shadow: ${({ theme }) => theme.shadows.focus}; | ||
} | ||
`; | ||
|
||
const StyledToggle = styled.div<{ checked: boolean }>` | ||
align-items: center; | ||
background-color: ${({ theme, checked }) => theme.colors[checked ? "success" : "input"]}; | ||
border-radius: 24px; | ||
box-shadow: inset 0px 2px 2px -1px rgba(74, 74, 104, 0.1); | ||
cursor: pointer; | ||
display: inline-flex; | ||
height: 40px; | ||
position: relative; | ||
transition: background-color 200ms; | ||
width: 72px; | ||
`; | ||
|
||
export default StyledToggle; |
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,15 @@ | ||
import React, { useState } from "react"; | ||
import Toggle from "./index"; | ||
|
||
export default { | ||
title: "Toggle", | ||
component: Toggle, | ||
}; | ||
|
||
export const Default: React.FC = () => { | ||
const [isChecked, setIsChecked] = useState(false); | ||
|
||
const toggle = () => setIsChecked(!isChecked); | ||
|
||
return <Toggle checked={isChecked} onChange={toggle} />; | ||
}; |
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,15 @@ | ||
import React, { InputHTMLAttributes } from "react"; | ||
import StyledToggle, { Input, Handle } from "./StyledToggle"; | ||
|
||
const Toggle: React.FC<InputHTMLAttributes<HTMLInputElement>> = ({ checked, ...props }) => { | ||
const isChecked = !!checked; | ||
|
||
return ( | ||
<StyledToggle checked={isChecked}> | ||
<Input checked={checked} {...props} type="checkbox" /> | ||
<Handle /> | ||
</StyledToggle> | ||
); | ||
}; | ||
|
||
export default Toggle; |
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,9 @@ | ||
import { ToggleTheme } from "./types"; | ||
|
||
export const light: ToggleTheme = { | ||
background: "#FFFFFF", | ||
}; | ||
|
||
export const dark: ToggleTheme = { | ||
background: "#2B223E", | ||
}; |
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,3 @@ | ||
export type ToggleTheme = { | ||
background: string; | ||
}; |
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
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
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
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
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
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