diff --git a/src/components/Sidebar.js b/src/components/Sidebar.js index dc867a45..d6a2014c 100644 --- a/src/components/Sidebar.js +++ b/src/components/Sidebar.js @@ -1,6 +1,7 @@ import React from 'react' import Color from 'color' import styled from 'styled-components' +import PropTypes from 'prop-types' import { ArrowDown } from 'components/icons' import { useDisclosureState, @@ -78,8 +79,15 @@ const DisclosureContent = styled(ReakitDisclosureContent)` } ` -const Sidebar = ({ sidebarIcon, children, ...props }) => { - const disclosure = useDisclosureState({ animated: true, visible: true }) +const Sidebar = ({ + sidebarIcon, + visible = true, + onChange = () => {}, + children, + ...props +}) => { + const disclosure = useDisclosureState({ animated: true, visible }) + const [toggled, setToggled] = React.useState(false) const openingIcon = sidebarIcon || ( @@ -88,23 +96,36 @@ const Sidebar = ({ sidebarIcon, children, ...props }) => { const closeIconRef = React.useRef(null) React.useEffect(() => { - if (!disclosure.visible) { - openIconRef?.current?.focus() - } else { - closeIconRef?.current?.focus() + if (toggled) { + if (!disclosure.visible) { + openIconRef?.current?.focus() + } else { + closeIconRef?.current?.focus() + } + onChange(disclosure.visible) } }, [openIconRef, disclosure.visible]) return ( {!disclosure.visible && ( - + setToggled(true)} + {...disclosure} + > {openingIcon} )} {disclosure.visible && ( - disclosure.hide()}> + { + disclosure.hide() + setToggled(true) + }} + > )} @@ -114,4 +135,30 @@ const Sidebar = ({ sidebarIcon, children, ...props }) => { ) } +Sidebar.propTypes = { + /** + * The icon you want to display to open the sidebar. Arrow by default + */ + sidebarIcon: PropTypes.node, + /** + * The initial state of the sidebar (open or closed) + */ + visible: PropTypes.bool, + /** + * Action to trigger on toggle change + */ + onChange: PropTypes.func, + /** + * Content of the sidebar + */ + children: PropTypes.node, +} + +Sidebar.defaultProps = { + sidebarIcon: , + visible: true, + onChange: null, + children: null, +} + export default Sidebar diff --git a/src/stories/Sidebar.stories.js b/src/stories/Sidebar.stories.js index fd11085d..50fa3700 100644 --- a/src/stories/Sidebar.stories.js +++ b/src/stories/Sidebar.stories.js @@ -36,3 +36,13 @@ WithIcon.args = { ), } + +export const DefaultClosed = Template.bind({}) +DefaultClosed.args = { + visible: false, + children: ( + + I’m a sidebar + + ), +}