Description
I want to add a <dialog>
element with some content and add an onClick
event handler such that when the user clicks anywhere on the backdrop the <dialog>
closes but if the users clicks on the content within the <dialog>
it remains open. This was straight forwards to implement so I did:
const handleSidebarBackdrop = (event: React.MouseEvent<HTMLDialogElement, MouseEvent>) => {
if (sidebarDialogRef.current && event.target === sidebarDialogRef.current) {
sidebarDialogRef.current.close();
setSidebarOpen(false);
}
};
// ...
<dialog onClick={handleSidebarBackdrop} ref={sidebarDialogRef}>
I immediately get two warnings:
Visible, non-interactive elements with click handlers must have at least one keyboard listener.eslint[jsx-a11y/click-events-have-key-events](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/click-events-have-key-events.md)
Non-interactive elements should not be assigned mouse or keyboard event listeners.eslint[jsx-a11y/no-noninteractive-element-interactions](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-noninteractive-element-interactions.md)
When I check MDN <dialog>
page it says: "The HTML element represents a modal or non-modal dialog box or other interactive component, such as a dismissible alert, inspector, or subwindow."
Why is <dialog>
treated as non-interactive when it obviously is interactive? Also I can add onKeyDown
event handler to e.g. close the <dialog>
on escape key presses to get rid of the first warning but the <dialog>
already handles escape key presses natively so this is redundant.
Can someone explain why this is happening?