-
Notifications
You must be signed in to change notification settings - Fork 4
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
Nick Grato
committed
Dec 22, 2022
1 parent
6947ad9
commit 0e9f7f0
Showing
8 changed files
with
238 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
dist | ||
node_modules | ||
node_modules | ||
|
||
# VS code local settings | ||
/.vscode |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@use '../../styles/boilerplate.scss'as *; | ||
|
||
.wrapper { | ||
display: flex; | ||
align-items: center; | ||
|
||
&:hover { | ||
.handle_shadow { | ||
background: rgba(0, 0, 0, 0.1); | ||
} | ||
} | ||
|
||
button { | ||
border: 0px; | ||
background-color: transparent; | ||
position: relative; | ||
cursor: pointer; | ||
} | ||
|
||
button.disabled { | ||
cursor: not-allowed; | ||
} | ||
|
||
label { | ||
@include font-size(16); | ||
color: $color-text-main; | ||
line-height: 24px; | ||
font-weight: 600; | ||
margin-left: 12px; | ||
} | ||
} | ||
|
||
.track { | ||
height: 16px; | ||
width: 40px; | ||
background: #e0e0e0; // need to add this to theme. | ||
border-radius: 8px; | ||
} | ||
|
||
@mixin handle { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
position: absolute; | ||
height: 24px; | ||
width: 24px; | ||
border-radius: 40px; | ||
top: -3px; | ||
left: -4px; | ||
transition: 500ms background ease-in-out, 500ms transform ease-in-out; | ||
} | ||
|
||
.handle { | ||
&_on { | ||
@include handle; | ||
transform: translateX(130%); | ||
background: $color-interaction-primary; | ||
} | ||
|
||
&_off { | ||
@include handle; | ||
transform: translateX(-5%); | ||
background: #676767; // need to add this to theme. | ||
} | ||
|
||
&_shadow { | ||
position: absolute; | ||
height: 35px; | ||
width: 35px; | ||
border-radius: 40px; | ||
top: -5px; | ||
left: -5px; | ||
background: rgba(0, 0, 0, 0); | ||
transition: 250ms background ease-in-out; | ||
} | ||
} | ||
|
||
.icon { | ||
stroke: #ffffff; | ||
} |
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,76 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import Icon, { IconT } from '../Icon'; | ||
import styles from './Switch.module.scss'; | ||
|
||
export type SwitchPropsT = { | ||
label?: string; | ||
defaultState?: boolean; | ||
disabled?: boolean; | ||
onChange: Function; | ||
icon: IconT; | ||
iconOn: IconT; // Note: to use "iconOn", "icon" needs to have a value. | ||
classProp?: string; | ||
}; | ||
|
||
const Switch = ({ | ||
label = 'switch', | ||
defaultState = false, | ||
disabled, | ||
icon, | ||
iconOn, | ||
onChange, | ||
classProp = '', | ||
}: SwitchPropsT) => { | ||
const [isOn, setIsOn] = useState(defaultState); | ||
const [currentIcon, setCurrentIcon] = useState<IconT | undefined>( | ||
defaultState === true && iconOn !== undefined ? iconOn : icon | ||
); | ||
|
||
useEffect(() => { | ||
typeof onChange === 'function' && onChange(isOn); | ||
}, [isOn]); | ||
|
||
useEffect(() => { | ||
// If "on" find out if we put iconOn up or not | ||
if (isOn) { | ||
if (icon && iconOn) { | ||
setCurrentIcon(iconOn); | ||
return; | ||
} | ||
setCurrentIcon(icon); | ||
return; | ||
} | ||
|
||
// if "off" Icon is the default icon | ||
icon && setCurrentIcon(icon); | ||
}, [isOn, icon, iconOn]); | ||
|
||
const handleSwitchClick = () => { | ||
if (disabled) return; | ||
setIsOn((state) => !state); | ||
}; | ||
|
||
return ( | ||
<div className={`${classProp} ${styles.wrapper}`}> | ||
<button | ||
aria-label={label} | ||
role="switch" | ||
type="button" | ||
onClick={handleSwitchClick} | ||
className={disabled && styles.disabled} | ||
> | ||
<div className={styles.track}></div> | ||
<div className={isOn ? styles.handle_on : styles.handle_off}> | ||
<div className={styles.handle_shadow}></div> | ||
{currentIcon && ( | ||
<Icon classProp={styles.icon} size={16} name={currentIcon} /> | ||
)} | ||
</div> | ||
</button> | ||
|
||
{label && <label>{label}</label>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Switch; |
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 @@ | ||
export { default } from './Switch'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
import { Switch } from '../components'; | ||
|
||
export default { | ||
title: 'Example/Switch', | ||
component: Switch, | ||
} as ComponentMeta<typeof Switch>; | ||
|
||
const onChange = (value: any) => { | ||
console.log('onchange value', value); | ||
}; | ||
|
||
const Template: ComponentStory<typeof Switch> = (args) => ( | ||
<> | ||
<main data-theme="light" style={{ padding: '20px' }}> | ||
<h3>Light Theme</h3> | ||
<Switch | ||
{...args} | ||
onChange={onChange} | ||
icon="x" | ||
iconOn="check" | ||
disabled={true} | ||
/> | ||
</main> | ||
<main data-theme="dark" style={{ background: '#000000', padding: '20px' }}> | ||
<h3 style={{ color: '#ffffff' }}>Dark Theme</h3> | ||
<Switch {...args} /> | ||
</main> | ||
</> | ||
); | ||
|
||
export const Main = Template.bind({}); | ||
Main.args = { | ||
label: 'Switch me!', | ||
}; |