Skip to content

Commit

Permalink
feat(sidebar): allow open from right or left with animation
Browse files Browse the repository at this point in the history
use createPortal to place the sidebar
  • Loading branch information
aneurysmjs committed Jul 28, 2019
1 parent 0e980fb commit d36b8f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
Binary file modified src/app/assets/svg/.DS_Store
Binary file not shown.
45 changes: 33 additions & 12 deletions src/app/components/shared/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow strict
import React, { Fragment, useEffect } from 'react';
import React, { Fragment, useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
import type { Node } from 'react';

import Icon from '@/components/base/Icon/Icon';
Expand All @@ -22,34 +23,52 @@ const Sidebar = ({
title = 'sidebar',
children,
}: PropsType) => {
const asideRef = useRef<null>(null);

const toggleSidebar = (): void => {
// avoid 'sketchy' null
if (asideRef.current) {
asideRef.current.classList.toggle(`sidebar--open-${side}`);
}
};

const closeSidebar = (): void => {
toggleSidebar();
setTimeout(onClose, 100);
};

useEffect(() => {
const handle = (evt: KeyboardEvent): void => {
// @link https://stackoverflow.com/questions/53834672/flow-type-keydown-event
const handleKeyDown = (evt: KeyboardEvent): void => {
if (isOpen && evt.keyCode === KEYBOARD.ESCAPE_KEY) {
onClose();
}
};

document.addEventListener('keydown', handle);
if (isOpen) {
setTimeout(toggleSidebar, 100);
}

document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('keydown', handle);
document.removeEventListener('keydown', handleKeyDown);
};
});

return isOpen
? (
? createPortal(
<Fragment>
<div
onKeyPress={() => {}}
role="presentation"
onClick={onClose}
onClick={closeSidebar}
className="sidebar__overlay"
/>
<aside
className="sidebar"
style={{
[side]: isOpen ? 0 : '-300px',
}}
// $FlowFixMe
ref={asideRef}
className={`sidebar sidebar--${side}`}
>
<header className="sidebar__header">
<h3 className="sidebar__title">{ title }</h3>
Expand All @@ -58,7 +77,7 @@ const Sidebar = ({
tabIndex="-1"
role="button"
onKeyPress={() => {}}
onClick={onClose}
onClick={closeSidebar}
>
<Icon path="icons/close" />
</span>
Expand All @@ -67,7 +86,9 @@ const Sidebar = ({
{children}
</div>
</aside>
</Fragment>
</Fragment>,
// $FlowFixMe
document.body,
) : null;
};

Expand Down
18 changes: 17 additions & 1 deletion src/app/components/shared/Sidebar/Sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ $sidebar-width: px-to-rem(300);
top: 0;
height: 100%;
padding: 1rem;
transition: left, right .3s, transform .3s;
transition: left .3s, right .3s, transform .3s;
width: $sidebar-width;
z-index: 10;

@include modifier(left) {
left: #{-$sidebar-width};
}

@include modifier(right) {
right: #{-$sidebar-width};
}

@include modifier(open-left) {
left: 0;
}

@include modifier(open-right) {
right: 0;
}

@include element(overlay) {
background: rgba(0, 0, 0, .6);
height: 100%;
Expand Down

0 comments on commit d36b8f1

Please sign in to comment.