`preventOutsideDismiss` does not work correctly if toggled after dialog opens
-
What RMWC Version are you using [major.minor.patch]: 7.0.2
-
Name your build system [Webpack, Rollup...]: Next.js
-
Describe the bug with as much detail as possible:
The Dialog component's preventOutsideDismiss prop is supposed to prevent the dialog from closing when the scrim is clicked OR when the escape key is pressed. It works for the most part, but if the prop is initially false, and then later is toggled to true while the dialog is open, the dialog will still close when the escape key is pressed. (Clicking the scrim does correctly prevent a dismiss, but hitting escape does not.)
Code Sandbox: https://codesandbox.io/s/rmwc-sandbox-forked-d6nv14?file=/index.js
🕵️ Investigating the cause of the issue.... in Dialog/foundation.tsx.
It looks like the event listener is correctly being recreated when the prop updates: https://github.com/jamesmfriedman/rmwc/blob/a59d93fe568b3a5d30806d99e23553e30b502a32/src/dialog/foundation.tsx#L104
const handleDocumentKeydown = useMemo(() => {
const bound = foundation.handleDocumentKeydown.bind(foundation);
// Wrap the callback in a function that can
// short circuit the escape key if we are preventing outside dismiss
return (evt: KeyboardEvent) => {
if (evt.which === 27 && props.preventOutsideDismiss) return;
return bound(evt);
};
}, [foundation, props.preventOutsideDismiss]);
👍
but I believe the code causing the issue is here, a bit further down: https://github.com/jamesmfriedman/rmwc/blob/a59d93fe568b3a5d30806d99e23553e30b502a32/src/dialog/foundation.tsx#L153
useEffect(() => {
if (props.open) {
if (!foundation.isOpen()) {
document.addEventListener('keydown', handleDocumentKeydown);
foundation.open();
}
} else {
if (foundation.isOpen()) {
document.removeEventListener('keydown', handleDocumentKeydown);
foundation.close();
}
}
}, [props.open, foundation, handleDocumentKeydown]);
The effect is re-running when the handleDocumentKeydown function is recreated, BUT the new event listener is not being attached to the DOM because the open prop matches the dialog's open state.
At this point I'm unsure what the best fix is and I'm also unsure what a good work-around could be for me. For now, I'm just always setting preventOutsideDismiss to true in cases where I'd otherwise be setting it conditionally, but this is not a good UX.
Actually: also wondering if this would also be causing a bug wherein event listeners aren't being cleaned up properly. E.g. if handleDocumentKeydown is regenerated, but never attached. Later when the dialog closes, I think this code will be trying to remove the wrong function (it will be trying to remove the new one, but it's the old one that's attached).
if (foundation.isOpen()) {
document.removeEventListener('keydown', handleDocumentKeydown);
foundation.close();
}
Thanks for reporting this. If you feel like it you are welcome to try and make a PR that fixes this bug. If not we will look into it once #758 is done.
Closed as it has now been fixed https://github.com/jamesmfriedman/rmwc/pull/786