Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 5x 5x 5x 10x 10x 10x 10x 10x 10x 10x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 5x 5x 7x 5x 5x 10x 7x 7x 7x 2x 2x 7x 7x 2x 2x 7x 7x 2x 7x 7x 10x 7x 5x 5x 10x 7x 5x 5x 10x | // @flow strict
import React, { useRef, useEffect, useReducer } from 'react';
import cx from 'classnames';
import * as util from '@material/drawer/util';
import MDCDismissibleDrawerFoundation from '@material/drawer/dismissible/foundation';
import MDCModalDrawerFoundation from '@material/drawer/modal/foundation';
import MDCListFoundation from '@material/list/foundation';
import createFocusTrap from 'focus-trap';
import { mtClassNamesReducer } from '../reducers';
const { SCRIM_SELECTOR } = MDCDismissibleDrawerFoundation.strings;
const noOp = () => {};
type Props = {
className?: string,
dismissible?: boolean,
modal?: boolean,
onClose?: () => void,
onOpen?: () => void,
open?: boolean,
};
const Drawer = ({
className,
dismissible = false,
modal = false,
onClose = noOp,
onOpen = noOp,
open = false,
...rest
}: Props) => {
const foundationRef = useRef<
?MDCModalDrawerFoundation | ?MDCDismissibleDrawerFoundation,
>();
const rootRef = useRef<?HTMLElement>();
const scrimRef = useRef<?HTMLElement>();
const focusTrapRef = useRef();
const previousFocusRef = useRef();
const [mtClassName, mtClassNameDispatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
useEffect(() => {
let foundation;
let handleKeyDown;
let handleTransitionEnd;
const root = rootRef.current;
if (modal || dismissible) {
const Foundation = modal
? MDCModalDrawerFoundation
: MDCDismissibleDrawerFoundation;
foundation = new Foundation({
addClass: mClassName =>
mtClassNameDispatch({ type: 'add', className: mClassName }),
removeClass: mClassName =>
mtClassNameDispatch({ type: 'remove', className: mClassName }),
hasClass: mClassName =>
rootRef.current && rootRef.current.classList.contains(mClassName),
elementHasClass: (element, mClassName) =>
element.classList.contains(mClassName),
saveFocus: () => {
previousFocusRef.current = document.activeElement;
},
restoreFocus: () => {
const previousFocus = previousFocusRef.current;
if (
previousFocus &&
previousFocus.focus &&
rootRef.current &&
rootRef.current.contains(document.activeElement)
) {
previousFocus.focus();
}
},
focusActiveNavigationItem: () => {
if (rootRef.current) {
const activeNavItemEl = rootRef.current.querySelector(
`.${MDCListFoundation.cssClasses.LIST_ITEM_ACTIVATED_CLASS}`,
);
if (activeNavItemEl) {
activeNavItemEl.focus();
}
}
},
trapFocus: () =>
focusTrapRef.current && focusTrapRef.current.activate(),
releaseFocus: () =>
focusTrapRef.current && focusTrapRef.current.deactivate(),
});
foundation.init();
foundationRef.current = foundation;
Eif (root) {
handleKeyDown = (...args) => foundation.handleKeydown(...args);
handleTransitionEnd = (...args) =>
foundation.handleTransitionEnd(...args);
root.addEventListener('keydown', handleKeyDown);
root.addEventListener('transitionend', handleTransitionEnd);
}
}
return () => {
if (root && handleKeyDown && handleTransitionEnd) {
root.removeEventListener('keydown', handleKeyDown);
root.removeEventListener('transitionend', handleTransitionEnd);
}
if (foundation) {
foundation.destroy();
foundationRef.current = null;
}
};
}, [dismissible, modal]);
useEffect(() => {
const root = rootRef.current;
const foundation = foundationRef.current;
let handleScrimClick;
if (root && modal) {
// flowlint-next-line unclear-type:off
scrimRef.current = ((root.parentNode: any): HTMLElement).querySelector(
SCRIM_SELECTOR,
);
focusTrapRef.current = util.createFocusTrapInstance(
rootRef.current,
createFocusTrap,
);
}
const scrim = scrimRef.current;
if (scrim && foundation) {
handleScrimClick = (...args) => foundation.handleScrimClick(...args);
scrim.addEventListener('click', handleScrimClick);
}
return () => {
if (handleScrimClick && scrim) {
scrim.removeEventListener('click', handleScrimClick);
}
scrimRef.current = null;
focusTrapRef.current = null;
};
}, [modal]);
useEffect(() => {
if (foundationRef.current) {
foundationRef.current.adapter_.notifyClose = onClose;
foundationRef.current.adapter_.notifyOpen = onOpen;
}
}, [onClose, onOpen]);
useEffect(() => {
if (foundationRef.current) {
Iif (open) {
foundationRef.current.open();
} else {
foundationRef.current.close();
}
}
}, [open]);
return (
<aside
{...rest}
className={cx(
className,
'mdc-drawer',
{
'mdc-drawer--dismissible': dismissible,
'mdc-drawer--modal': modal,
},
...Array.from(mtClassName),
)}
ref={rootRef}
/>
);
};
export default Drawer;
|