import React, { useEffect } from 'react'; import styled from 'styled-components'; import v from '../../styles/Variables'; import Close from '../NavButton/Close'; import trapFocus from '../../utils/TrapFocus'; interface Props { /** * the color of the mask */ backgroundColor?: string; /** * any valid jsx */ children: React.ReactNode; /** * function to happen after the close button is clicked */ closeCb: () => void; /** * if the drawer is currently open or not */ drawerOpen: boolean; /** * class applied to the drawer */ theme?: string; /** * prevents the background page from scrolling */ preventScroll?: boolean; /** * title of the drawer */ title?: string; } interface Style { backgroundColor: string; } const BottomDrawer = ({ backgroundColor = v.colors.light, children, closeCb, drawerOpen = false, theme, preventScroll = true, title }: Props) => { // stop the body from being scrollable useEffect(() => { const elClassList = window.document.getElementsByTagName('html')[0].classList; setTimeout(() => { if (window.document && preventScroll === true && drawerOpen === true) { if (elClassList.contains('nwc--noscroll')) { elClassList.remove('nwc--noscroll'); } else { elClassList.add('nwc--noscroll'); } } }, 1500); if (drawerOpen === true) { const closeBtn = document.querySelector('.nwc--drawer .nwc--hamburger'); closeBtn!.focus(); } }, [drawerOpen, preventScroll]); useEffect(() => { document.addEventListener('keydown', (e) => { //trapFocus(e, id, drawerOpen); if (e.key === 'Escape') { window.document.getElementsByTagName('html')[0].classList.remove('nwc--noscroll'); closeCb(); } }); }, []); useEffect(() => { document.addEventListener('keydown', (e) => { trapFocus(e, 'bottomDrawer', drawerOpen); }); }, [drawerOpen]); return (
{ window.document.getElementsByTagName('html')[0].classList.remove('nwc--noscroll'); closeCb(); }} />
{children}
{ window.document.getElementsByTagName('html')[0].classList.remove('nwc--noscroll'); closeCb(); }} />
); }; export default BottomDrawer; /* styles */ const DrawerStyles = styled.nav