RC-Dialog
rc-dialog is a foundational React UI component providing a highly customizable and accessible dialog (modal) implementation. It offers core functionalities like visibility control, masking, animation hooks, and keyboard interaction (e.g., Esc key to close). While version 10.0.0 is the latest stable release of `rc-dialog` as a standalone package, its active development has largely shifted to its successor, `@rc-component/dialog`. `rc-dialog` itself has not seen new releases or significant updates for over a year, suggesting it is now in a maintenance or deprecated state. Developers are generally advised to consider `@rc-component/dialog` for new projects or migrations due to its ongoing development and improved features, including enhanced focus management. This package prioritizes stateless design and easy customization via props like `prefixCls`, `className`, `classNames`, and `styles`, making it a flexible choice for various design systems.
Common errors
-
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
cause Often occurs when the `visible` prop is controlled by a state variable, and the `onClose` handler directly sets the state without a conditional check, leading to an infinite re-render loop if not managed carefully.fixEnsure that `onClose` or any visibility toggling function correctly updates the state to `false` (or the desired hidden state) and that there are no unintended side effects causing the dialog to immediately reopen. Example: `const onClose = useCallback(() => setVisible(false), []);` -
TypeError: Cannot read properties of undefined (reading 'focus') / Element not focusable
cause The dialog or its internal elements are attempting to set focus on an element that is not yet rendered, has been unmounted, or is not capable of being focused (e.g., a hidden element). This is common with `destroyOnClose` or asynchronous rendering.fixVerify that elements are present in the DOM before attempting to focus them. If using `destroyOnClose`, consider alternative strategies for preserving focus, or explicitly set focus to the trigger element in the `afterClose` callback. For custom focus handling, use `getContainer` to control where the dialog mounts, ensuring it's part of the accessible DOM tree. -
Module not found: Can't resolve 'rc-dialog'
cause The `rc-dialog` package is not correctly installed or the import path is incorrect. This can also happen in environments with strict module resolution if a CommonJS `require` is used where an ESM `import` is expected, or vice-versa.fixFirst, ensure the package is installed: `npm install rc-dialog` or `yarn add rc-dialog`. Double-check the import statement `import Dialog from 'rc-dialog';`. If using a bundler like Webpack, ensure its module resolution configuration (e.g., `resolve.extensions`) is set up to handle `.js`, `.jsx`, `.ts`, `.tsx` files correctly.
Warnings
- breaking The `rc-dialog` package itself has not had a significant release or update since version 10.0.0, published over a year ago. Active development, bug fixes, and new features (like improved focus management and semantic close) are now occurring in the `@rc-component/dialog` package. Migrating from `rc-dialog` to `@rc-component/dialog` is strongly recommended for new projects or to receive ongoing support and features.
- gotcha Focus management within modals, especially when dealing with nested dialogs or components that create their own focus traps (e.g., date pickers, select dropdowns), can lead to unexpected behavior where focus is incorrectly trapped or lost. This is a common issue for accessibility in UI libraries.
- gotcha The default styles for `rc-dialog` are provided via a separate CSS file (`rc-dialog/assets/index.css`). Failing to import this CSS will result in an unstyled, potentially unusable dialog, as the component itself only provides the DOM structure and logic.
- gotcha Using `require('rc-dialog')` (CommonJS) alongside `import ... from 'rc-dialog'` (ESM) in a mixed environment can lead to build tool issues or unexpected import resolutions, especially in modern React applications predominantly using ESM.
Install
-
npm install rc-dialog -
yarn add rc-dialog -
pnpm add rc-dialog
Imports
- Dialog
import { Dialog } from 'rc-dialog';import Dialog from 'rc-dialog';
- IDialogPropTypes
import type { IDialogPropTypes } from 'rc-dialog'; - DialogWrap
import type { DialogWrap } from 'rc-dialog';
Quickstart
import React, { useState, useCallback } from 'react';
import ReactDOM from 'react-dom/client';
import Dialog from 'rc-dialog';
import 'rc-dialog/assets/index.css'; // Don't forget to import the default styles
const App = () => {
const [visible, setVisible] = useState(false);
const [title, setTitle] = useState('Basic Dialog');
const showDialog = useCallback(() => {
setVisible(true);
}, []);
const onClose = useCallback(() => {
console.log('Dialog closed!');
setVisible(false);
}, []);
return (
<div>
<button onClick={showDialog}>Open Dialog</button>
<Dialog
title={title}
visible={visible}
onClose={onClose}
maskClosable={true} // Allow closing by clicking the mask
keyboard={true} // Allow closing by pressing Esc key
destroyOnClose={true} // Unmount children when dialog closes
className="my-custom-dialog"
style={{ width: 500 }}
>
<p>This is the content of the dialog.</p>
<p>You can put any React elements here.</p>
<p>Current version: 10.0.0</p>
<button onClick={() => alert('Button inside dialog clicked!')}>Click Me</button>
</Dialog>
</div>
);
};
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);