`useDialog` dialogs ignore Enter (confirm) and Escape (cancel)
What version of rsuite are you using?
6.2.4
What version of React are you using?
19.x
What version of TypeScript are you using (if any)?
5.x / 6.x
What browser are you using?
Chrome, Safari
Describe the Bug
Dialogs opened with useDialog() (alert, confirm, and prompt) no longer respond to the keyboard:
- Enter does not activate the OK / confirm action on
alertandconfirm. - Escape does not cancel or close
alert,confirm, orprompt.
This used to work. The only way to dismiss these dialogs now is to click a footer button. backdrop is "static" and the header close button is hidden, so there is no other dismiss path.
Modal itself still closes on Escape when the caller passes onClose (keyboard defaults to true). The regression is specific to the built-in dialog used by useDialog.
prompt still confirms on Enter while the text input is focused, because that input has its own onKeyDown. Escape still does nothing on a prompt.
Expected Behavior
Match the usual alert-dialog keyboard interaction (and the previous behavior):
| Dialog | Enter | Escape |
|---|---|---|
confirm |
Resolve true and close (same as OK) |
Resolve false and close (same as Cancel) |
alert |
Close (same as OK) | Close |
prompt |
Submit the input value (already works when the input is focused) | Cancel without submitting, same as Cancel |
Enter should not be stolen from a textarea (newline). Escape should only close the top-most dialog when several are open.
To Reproduce
import { CustomProvider, Button, useDialog } from 'rsuite';
import 'rsuite/dist/rsuite.min.css';
function Demo() {
const dialog = useDialog();
return (
<Button
appearance="primary"
onClick={async () => {
const ok = await dialog.confirm('Delete this item?');
console.log('result', ok);
}}
>
Open confirm
</Button>
);
}
export default function App() {
return (
<CustomProvider>
<Demo />
</CustomProvider>
);
}- Click Open confirm.
- Press Enter. The dialog stays open and the promise does not resolve.
- Press Escape. The dialog stays open.
- Click OK (resolves
true) or Cancel (resolvesfalse) to dismiss it.
The same Escape failure happens with dialog.alert() and dialog.prompt(). Enter on dialog.alert() also does nothing.
Cause
In src/useDialog/Dialog.tsx ([email protected]):
Escape.
onCloseis destructured off the props and never forwarded to<Modal>. The overlay listens for Escape (keyboarddefaults totrueinsrc/internals/Overlay/Modal) and callsonClose, but that prop isundefinedon this Modal, so the key is a no-op. Cancel only runs from the Cancel button (handleClose→handleCancel(false)).Enter.
handleInputKeyDownis attached only to the prompt<Input>.alertandconfirmhave no Enter handler. The OK button is nottype="submit", is not inside a<form>, and is not focused on open. ModalautoFocusmoves focus to the dialog container (tabIndex={-1}), not the primary button, so Enter does not activate OK.
Source: rsuite/rsuite