2021-01-04 13:30:23 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
import { translate } from '../../../base/i18n';
|
2021-02-04 13:24:25 +00:00
|
|
|
import { Icon, IconArrowUpWide, IconArrowDownWide } from '../../../base/icons';
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the drawer should have a button that expands its size or not.
|
|
|
|
*/
|
|
|
|
canExpand: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The component(s) to be displayed within the drawer menu.
|
|
|
|
*/
|
|
|
|
children: React$Node,
|
|
|
|
|
|
|
|
/**
|
|
|
|
Whether the drawer should be shown or not.
|
|
|
|
*/
|
|
|
|
isOpen: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
Function that hides the drawer.
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
onClose: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
|
|
|
t: Function
|
2021-01-04 13:30:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that displays the mobile friendly drawer on web.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
function Drawer({
|
|
|
|
canExpand,
|
|
|
|
children,
|
|
|
|
isOpen,
|
2021-06-10 12:48:44 +00:00
|
|
|
onClose,
|
|
|
|
t }: Props) {
|
2021-01-04 13:30:23 +00:00
|
|
|
const [ expanded, setExpanded ] = useState(false);
|
|
|
|
const drawerRef: Object = useRef(null);
|
|
|
|
|
|
|
|
/**
|
2021-06-30 16:12:12 +00:00
|
|
|
* Closes the drawer when clicking or touching outside of it.
|
2021-01-04 13:30:23 +00:00
|
|
|
*
|
2021-06-30 16:12:12 +00:00
|
|
|
* @param {Event} event - Mouse down/start touch event object.
|
2021-01-04 13:30:23 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-30 16:12:12 +00:00
|
|
|
function handleOutsideClickOrTouch(event: Event) {
|
2021-01-04 13:30:23 +00:00
|
|
|
if (drawerRef.current && !drawerRef.current.contains(event.target)) {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-06-30 16:12:12 +00:00
|
|
|
window.addEventListener('mousedown', handleOutsideClickOrTouch);
|
|
|
|
window.addEventListener('touchstart', handleOutsideClickOrTouch);
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
return () => {
|
2021-06-30 16:12:12 +00:00
|
|
|
window.removeEventListener('mousedown', handleOutsideClickOrTouch);
|
|
|
|
window.removeEventListener('touchstart', handleOutsideClickOrTouch);
|
2021-01-04 13:30:23 +00:00
|
|
|
};
|
|
|
|
}, [ drawerRef ]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles the menu state between expanded/collapsed.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function toggleExpanded() {
|
|
|
|
setExpanded(!expanded);
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
/**
|
|
|
|
* KeyPress handler for accessibility.
|
|
|
|
*
|
|
|
|
* @param {React.KeyboardEventHandler<HTMLDivElement>} e - The key event to handle.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function onKeyPress(e) {
|
|
|
|
if (e.key === ' ' || e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
toggleExpanded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:30:23 +00:00
|
|
|
return (
|
|
|
|
isOpen ? (
|
|
|
|
<div
|
|
|
|
className = { `drawer-menu${expanded ? ' expanded' : ''}` }
|
|
|
|
ref = { drawerRef }>
|
|
|
|
{canExpand && (
|
|
|
|
<div
|
2021-06-10 12:48:44 +00:00
|
|
|
aria-expanded = { expanded }
|
|
|
|
aria-label = { expanded ? t('toolbar.accessibilityLabel.collapse')
|
|
|
|
: t('toolbar.accessibilityLabel.expand') }
|
2021-01-04 13:30:23 +00:00
|
|
|
className = 'drawer-toggle'
|
2021-06-10 12:48:44 +00:00
|
|
|
onClick = { toggleExpanded }
|
|
|
|
onKeyPress = { onKeyPress }
|
|
|
|
role = 'button'
|
|
|
|
tabIndex = { 0 }>
|
2021-03-11 12:59:49 +00:00
|
|
|
<Icon
|
|
|
|
size = { 24 }
|
|
|
|
src = { expanded ? IconArrowDownWide : IconArrowUpWide } />
|
2021-01-04 13:30:23 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
export default translate(Drawer);
|