-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(navigation): Added Drawer component
- Loading branch information
Showing
7 changed files
with
301 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { Component } from 'react'; | ||
import { | ||
Layout, | ||
Gutter, | ||
Heading, | ||
Drawer, | ||
Button, | ||
ToggleSwitch, | ||
Text, | ||
} from 'react-components-kit'; | ||
|
||
import Properties from './common/Properties'; | ||
import CodeBlock from './common/CodeBlock'; | ||
import { drawerExample } from './codeSnippets'; | ||
import { drawerProperties } from './compProperties'; | ||
|
||
class Navigation extends Component { | ||
state = { | ||
drawerOpen: false, | ||
drawerSide: 'left', | ||
}; | ||
|
||
toggleDrawer = () => { | ||
this.setState(prev => ({ drawerOpen: !prev.drawerOpen })); | ||
}; | ||
|
||
toggleMenuSide = () => { | ||
this.setState(prev => ({ | ||
drawerSide: prev.drawerSide === 'left' ? 'right' : 'left', | ||
})); | ||
}; | ||
|
||
render() { | ||
const { drawerOpen, drawerSide } = this.state; | ||
|
||
return ( | ||
<div> | ||
<Heading>Navigation</Heading> | ||
|
||
<Heading h2>Drawer</Heading> | ||
|
||
<Text p> | ||
You need to provide the menu content yourself as children | ||
of the Drawer component. | ||
</Text> | ||
|
||
<Gutter vertical /> | ||
|
||
<Layout column> | ||
<Button primary flat onClick={this.toggleDrawer}> | ||
Open drawer | ||
</Button> | ||
<Gutter vertical /> | ||
<Layout align='center'> | ||
<Text>Menu on right:</Text> | ||
<Gutter /> | ||
<ToggleSwitch | ||
isToggled={drawerSide === 'right'} | ||
onToggle={this.toggleMenuSide} | ||
/> | ||
</Layout> | ||
</Layout> | ||
|
||
<Drawer | ||
isOpen={drawerOpen} | ||
toggleDrawer={this.toggleDrawer} | ||
side={drawerSide} | ||
bg='#08ffb4' | ||
useGradient | ||
> | ||
<Layout column style={{ padding: 32 }}> | ||
<Layout.Box> | ||
Foo | ||
</Layout.Box> | ||
<Layout.Box> | ||
Bar | ||
</Layout.Box> | ||
<Layout.Box> | ||
Baz | ||
</Layout.Box> | ||
</Layout> | ||
</Drawer> | ||
|
||
<Gutter vertical /> | ||
|
||
<Layout> | ||
<CodeBlock code={drawerExample} /> | ||
</Layout> | ||
|
||
<Layout> | ||
<Properties properties={drawerProperties} /> | ||
</Layout> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Navigation; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { Component } from 'react'; | ||
import styled, { keyframes } from 'styled-components'; | ||
import color from 'color'; | ||
|
||
const ESC = 27; | ||
|
||
class Drawer extends Component { | ||
static propTypes = { | ||
isOpen: PropTypes.bool.isRequired, | ||
toggleDrawer: PropTypes.func.isRequired, | ||
side: PropTypes.oneOf(['left', 'right']), | ||
bg: PropTypes.string, | ||
useGradient: PropTypes.bool, | ||
children: PropTypes.any, | ||
}; | ||
|
||
static defaultProps = { | ||
bg: '#fff', | ||
side: 'right', | ||
useGradient: false, | ||
}; | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.isOpen) { | ||
window.addEventListener('keydown', this.handleKeyDown); | ||
} else { | ||
window.removeEventListener('keydown', this.handleKeyDown); | ||
} | ||
} | ||
|
||
handleKeyDown = ({ keyCode }) => { | ||
if (keyCode === ESC) this.props.toggleDrawer(); | ||
} | ||
|
||
render() { | ||
const { children, ...rest } = this.props; | ||
|
||
return ( | ||
<div> | ||
<Menu {...rest}> | ||
<Head side={rest.side}> | ||
<CloseButton onClick={this.props.toggleDrawer}> | ||
× | ||
</CloseButton> | ||
</Head> | ||
|
||
{children} | ||
</Menu> | ||
|
||
{rest.isOpen && <Backdrop onClick={this.props.toggleDrawer} />} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
const menuOnLeft = ({ side }) => side === 'left'; | ||
const menuOnRight = ({ side }) => side === 'right'; | ||
const getDir = ({ side }) => side === 'right' ? 1 : -1; | ||
|
||
const fadeIn = keyframes` | ||
from { opacity: 0; } | ||
to { opacity: 1; } | ||
`; | ||
|
||
const Head = styled.div` | ||
display: flex; | ||
justify-content: ${props => menuOnLeft(props) ? 'flex-end' : 'flex-start'}; | ||
padding: 4px; | ||
`; | ||
|
||
const CloseButton = styled.button` | ||
background: transparent; | ||
border: 0; | ||
font-size: 24px; | ||
font-family: Arial, Helvetica, sans-serif; | ||
opacity: 0.7; | ||
transition: all 0.2s ease; | ||
&:hover { | ||
transform: scale(1.2); | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
const Backdrop = styled.div` | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
background-color: rgba(0, 0, 0, 0.3); | ||
animation: ${fadeIn} 0.4s; | ||
z-index: 99; | ||
`; | ||
|
||
const Menu = styled.div` | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
${props => menuOnRight(props) && 'right: 0;'} | ||
${props => menuOnLeft(props) && 'left: 0;'} | ||
width: 360px; | ||
z-index: 100; | ||
display: flex; | ||
flex-direction: column; | ||
box-shadow: 0px 0px 12px rgba(0,0,0,0.5); | ||
opacity: ${props => props.isOpen ? 1 : 0}; | ||
transform: translateX(${props => props.isOpen ? 0 : getDir(props) * 360}px); | ||
transition: transform 0.4s cubic-bezier(0.2, 0.71, 0.14, 0.91); | ||
background: ${props => props.bg}; | ||
background: ${props => props.useGradient && `linear-gradient( | ||
to bottom, | ||
${props.bg} 0%, | ||
${color(props.bg).darken(0.2).hsl().string()} 100% | ||
)`}; | ||
@media (max-width: 400px) { | ||
width: 300px; | ||
transform: translateX(${props => props.isOpen ? 0 : getDir(props) * 300}px); | ||
} | ||
`; | ||
|
||
export default Drawer; |
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