···
const hasPriority = usePriority(ref);
-
if (!hasPriority) return;
function onKey(event: KeyboardEvent) {
-
event.defaultPrevented ||
-
event.code !== 'Escape'
-
event.preventDefault();
function onClick(event: MouseEvent | TouchEvent) {
-
contains(ref.current, event.target) ||
-
document.addEventListener('mousedown', onClick);
-
document.addEventListener('touchstart', onClick);
document.addEventListener('keydown', onKey);
-
document.removeEventListener('mousedown', onClick);
-
document.removeEventListener('touchstart', onClick);
document.removeEventListener('keydown', onKey);
}, [ref, hasPriority, onDismiss]);
···
const hasPriority = usePriority(ref);
+
if (!ref.current || !hasPriority) return;
function onKey(event: KeyboardEvent) {
+
if (event.defaultPrevented || event.isComposing) return;
+
if (event.code === 'Escape') {
+
// The current dialog can be dismissed by pressing escape if it either has focus
+
const active = document.activeElement;
+
if (hasPriority || (active && contains(ref.current, active))) {
+
event.preventDefault();
function onClick(event: MouseEvent | TouchEvent) {
+
const { target } = event;
+
if (contains(ref.current, target) || event.defaultPrevented) {
+
document.addEventListener('mousedown', onClick);
+
document.addEventListener('touchstart', onClick);
document.addEventListener('keydown', onKey);
+
document.removeEventListener('mousedown', onClick);
+
document.removeEventListener('touchstart', onClick);
document.removeEventListener('keydown', onKey);
}, [ref, hasPriority, onDismiss]);