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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | 6x 6x 16x 16x 16x 16x 16x 16x 16x 16x 14x 14x 14x 14x 14x 16x 14x 14x 14x 14x 14x 14x 4x 14x 2x 6x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 16x 14x 14x 14x 2x 12x 16x 14x 14x 1x 16x 14x 14x 14x 16x 14x 14x 14x 14x 14x 14x 14x 14x 14x 2x 2x 4x 2x 16x 14x | // @flow strict
import React, { useState, useRef, useEffect, useReducer } from 'react';
import MDCDialogFoundation from '@material/dialog/foundation';
import * as util from '@material/dialog/util';
import cx from 'classnames';
import { FocusTrap } from 'focus-trap';
import { closest, matches } from '@material/dom/ponyfill';
import type { Node } from 'react';
import { mtClassNamesReducer } from '../reducers';
const noOp = () => {};
type Props = {
children?: Node,
name: string,
title?: string,
open?: boolean,
autoStackButtons?: boolean,
onClosing?: () => void,
onClosed?: () => void,
onOpening?: () => void,
onOpened?: () => void,
className?: string,
};
const Dialog = ({
children,
className,
name,
title,
open = false,
autoStackButtons,
onOpening = noOp,
onOpened = noOp,
onClosing = noOp,
onClosed = noOp,
...rest
}: Props) => {
const foundationRef = useRef<?MDCDialogFoundation>();
const rootRef = useRef<?HTMLDivElement>();
const buttons = useRef<Array<HTMLElement>>([]);
const defaultButton = useRef<?HTMLElement>();
const content = useRef<?HTMLElement>();
const [reversed, setReversed] = useState<boolean>(false);
const [mtClassName, mtClassNameDistpatch] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
useEffect(() => {
const root = rootRef.current;
Eif (root) {
buttons.current = [].slice.call(
root.querySelectorAll(MDCDialogFoundation.strings.BUTTON_SELECTOR),
);
defaultButton.current = root.querySelector(
`[${MDCDialogFoundation.strings.BUTTON_DEFAULT_ATTRIBUTE}]`,
);
content.current = root.querySelector(
MDCDialogFoundation.strings.CONTENT_SELECTOR,
);
}
}, []);
useEffect(() => {
const root = rootRef.current;
const initialFocus = document.querySelector(
`[${MDCDialogFoundation.strings.INITIAL_FOCUS_ATTRIBUTE}]`,
);
let container;
Eif (root) {
container = root.querySelector(
MDCDialogFoundation.strings.CONTAINER_SELECTOR,
);
}
const focusTrap = util.createFocusTrapInstance(
container,
FocusTrap,
initialFocus,
);
const foundation = new MDCDialogFoundation({
addBodyClass: mClassName =>
document.body && document.body.classList.add(mClassName),
addClass: mClassName =>
mtClassNameDistpatch({ type: 'add', className: mClassName }),
areButtonsStacked: () => util.areTopsMisaligned(buttons.current),
clickDefaultButton: () =>
defaultButton.current && defaultButton.current.click(),
eventTargetMatches: (target, selector) =>
target ? matches(target, selector) : false,
getActionFromEvent: (evt: Event) => {
if (!evt.target) {
return '';
}
const element = closest(
evt.target,
`[${MDCDialogFoundation.strings.ACTION_ATTRIBUTE}]`,
);
return (
element &&
element.getAttribute(MDCDialogFoundation.strings.ACTION_ATTRIBUTE)
);
},
getInitialFocusEl: () => initialFocus,
hasClass: mClassName => root && root.classList.contains(mClassName),
isContentScrollable: () => util.isScrollable(content.current),
releaseFocus: () => focusTrap.deactivate(),
removeBodyClass: mClassName =>
document.body && document.body.classList.remove(mClassName),
removeClass: mClassName =>
mtClassNameDistpatch({ type: 'remove', className: mClassName }),
trapFocus: () => focusTrap.activate(),
});
foundationRef.current = foundation;
foundation.init();
const handleClick = (...handleOpeningRest) =>
foundation.handleClick(...handleOpeningRest);
const handleKeydown = (...handleOpeningRest) =>
foundation.handleKeydown(...handleOpeningRest);
Eif (root) {
root.addEventListener('click', handleClick);
root.addEventListener('keydown', handleKeydown);
}
return () => {
Eif (root) {
root.removeEventListener('click', handleClick);
root.removeEventListener('keydown', handleKeydown);
}
foundation.destroy();
foundationRef.current = null;
};
}, []);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
if (open) {
foundation.open();
} else {
foundation.close();
}
}
}, [open]);
useEffect(() => {
const foundation = foundationRef.current;
if (foundation && autoStackButtons !== undefined) {
foundation.setAutoStackButtons(autoStackButtons);
}
}, [autoStackButtons]);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
foundation.adapter_.reverseButtons = () => setReversed(!reversed);
}
}, [reversed]);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
foundation.adapter_.notifyClosed = onClosed;
foundation.adapter_.notifyOpened = onOpened;
const LAYOUT_EVENTS = ['resize', 'orientationchange'];
const handleDocumentKeydown = (...handleDocumentKeydownRest) =>
foundation.handleDocumentKeydown(...handleDocumentKeydownRest);
const handleLayout = () => foundation.layout();
foundation.adapter_.notifyOpening = (...onOpeningRest) => {
onOpening(...onOpeningRest);
LAYOUT_EVENTS.forEach(evtType =>
window.addEventListener(evtType, handleLayout),
);
document.addEventListener('keydown', handleDocumentKeydown);
};
foundation.adapter_.notifyClosing = (...onClosingRest) => {
onClosing(...onClosingRest);
LAYOUT_EVENTS.forEach(evtType =>
window.removeEventListener(evtType, handleLayout),
);
document.removeEventListener('keydown', handleDocumentKeydown);
};
}
}, [onClosed, onClosing, onOpened, onOpening]);
return (
<div
aria-modal="true"
role="alertdialog"
{...(rest: mixed)}
className={cx('mdc-dialog', className, ...Array.from(mtClassName))}
ref={rootRef}
aria-labelledby={`${name}-title`}
aria-describedby={`${name}-content`}
id={name}
>
<div className="mdc-dialog__container">
<div className="mdc-dialog__surface">
{title != null && (
<h2 className="mdc-dialog__title" id={`${name}-title`}>
{title}
</h2>
)}
{React.Children.map(children, child => {
return (
<child.type {...child.props} name={name} reversed={reversed} />
);
})}
</div>
</div>
<div className="mdc-dialog__scrim" />
</div>
);
};
export default Dialog;
|