Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.

Add hasPriority exception to click/touch events in useDialogDismiss

Changed files
+11 -12
src
+11 -12
src/useDialogDismiss.ts
···
return;
}
-
event.preventDefault();
-
onDismiss();
}
-
if (hasPriority) {
-
document.addEventListener('mousedown', onClick);
-
document.addEventListener('touchstart', onClick);
-
}
-
document.addEventListener('keydown', onKey);
return () => {
-
if (hasPriority) {
-
document.removeEventListener('mousedown', onClick);
-
document.removeEventListener('touchstart', onClick);
-
}
-
document.removeEventListener('keydown', onKey);
};
}, [ref, hasPriority, onDismiss]);
···
return;
}
+
// The current dialog can be dismissed by pressing outside of it if it either has
+
// focus or it has priority
+
const active = document.activeElement;
+
if (hasPriority || (active && contains(ref.current, active))) {
+
event.preventDefault();
+
onDismiss();
+
}
}
+
document.addEventListener('mousedown', onClick);
+
document.addEventListener('touchstart', onClick);
document.addEventListener('keydown', onKey);
return () => {
+
document.removeEventListener('mousedown', onClick);
+
document.removeEventListener('touchstart', onClick);
document.removeEventListener('keydown', onKey);
};
}, [ref, hasPriority, onDismiss]);