### Contribution Guide: Setup and Linking
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Steps to clone the repository, install dependencies, link the local library, and start development servers for both the main library and examples.
```bash
# 1. Clone repo
git clone https://github.com/eBay/nice-modal-react.git
# 2. Install deps
cd nice-modal-react
yarn
# 3. Make local repo as linked
yarn link
# 4. Start dev server
yarn dev
# 5. Install examples deps
cd example
yarn
# 6. Use local linked lib
yarn link @ebay/nice-modal-react
# 7. Start examples dev server
yarn start
```
--------------------------------
### Install NiceModal React
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Install the NiceModal React library using either yarn or npm.
```bash
# with yarn
yarn add @ebay/nice-modal-react
# or with npm
npm install @ebay/nice-modal-react
```
--------------------------------
### Create a Modal Component with NiceModal.create
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Create a modal component by wrapping a standard React component with NiceModal.create. This example uses Ant Design's Modal component. The modal is uncontrolled and can be hidden from within the component itself. Use `modal.remove()` to clean up the component from the tree.
```jsx
import { Modal } from 'antd';
import NiceModal, { useModal } from '@ebay/nice-modal-react';
export default NiceModal.create(({ name }: { name: string }) => {
// Use a hook to manage the modal state
const modal = useModal();
return (
modal.hide()}
visible={modal.visible}
onCancel={() => modal.hide()}
afterClose={() => modal.remove()}
>
Hello {name}!
);
});
```
--------------------------------
### Overriding Helper Properties
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Illustrates how to override properties provided by UI helper functions. The example shows overriding the `onOk` property for an Ant Design modal.
```jsx
const handleSubmit = () => {
doSubmit().then(() => {
modal.hide();
});
}
```
--------------------------------
### Declarative Context Access in Modals
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Shows how to use the declarative approach to access component tree context within a modal. This example uses Ant Design's Button and Modal components.
```jsx
export default function AntdSample() {
return (
<>
>
);
}
```
--------------------------------
### React Bootstrap Dialog Helper
Source: https://context7.com/ebay/nice-modal-react/llms.txt
The `bootstrapDialog` helper binds modal state and callbacks to React Bootstrap's `` component. Ensure `react-bootstrap` is installed and configured.
```tsx
import BootstrapModal from 'react-bootstrap/Modal';
import Button from 'react-bootstrap/Button';
import NiceModal, { useModal, bootstrapDialog } from '@ebay/nice-modal-react';
const MyBootstrapDialog = NiceModal.create(({ message }: { message: string }) => {
const modal = useModal();
return (
Confirmation
{message}
);
});
NiceModal.show(MyBootstrapDialog, { message: 'Are you sure you want to delete?' })
.then((confirmed) => confirmed && performDelete());
```
--------------------------------
### Testing Nice Modals with Testing Library
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Provides a basic test case for nice modals using `@testing-library/react`. It demonstrates rendering the provider, showing a modal, and asserting its visibility.
```jsx
import NiceModal from '@ebay/nice-modal-react';
import { render, act, screen } from '@testing-library/react';
import { MyNiceModal } from '../MyNiceModal';
test('My nice modal works!', () => {
render(
act(() => {
NiceModal.show(MyNiceModal);
});
expect(screen.getByRole('dialog')).toBeVisible();
});
```
--------------------------------
### Basic Modal State Management
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Demonstrates binding modal visibility and actions to a modal handler. Use this pattern for manual state management of modals.
```jsx
//...\nconst modal = useModal();
return (
modal.hide()}
onCancel={() => modal.hide()}
afterClose={() => modal.remove()}
>
Hello NiceModal!
);
//...
```
--------------------------------
### show Method
Source: https://github.com/ebay/nice-modal-react/blob/main/docs/api/interfaces/NiceModalHandler.html
Displays the modal. This method changes the 'visible' state to true and accepts optional arguments to pass as props to the modal component.
```APIDOC
## show Method
### Description
Shows the modal, changing its `visible` state to true. Accepts optional arguments to be passed as props to the modal component.
### Signature
`show(args?: Props): Promise`
### Parameters
#### Optional args: Props
An object passed to the modal component as props.
### Returns
A Promise that resolves with an unknown value. This promise typically resolves when the modal is closed or a result is returned.
```
--------------------------------
### NiceModal.create
Source: https://context7.com/ebay/nice-modal-react/llms.txt
Use NiceModal.create as a higher-order component to wrap your existing modal/dialog components. It integrates them with NiceModal, handling visibility and mounting/unmounting.
```APIDOC
## NiceModal.create — Wrap a modal component to integrate with NiceModal
`NiceModal.create` is a higher-order component that wraps your existing modal/dialog component. It ensures the component is not rendered at all while invisible, handles the `defaultVisible` and `keepMounted` props, and provides the modal's ID via context so that `useModal()` (with no arguments) works inside it.
```jsx
import { Modal } from 'antd';
import NiceModal, { useModal } from '@ebay/nice-modal-react';
// Ant Design modal — receives custom props (name) plus the modal handler via useModal()
const MyAntdModal = NiceModal.create(({ name }: { name: string }) => {
const modal = useModal();
return (
modal.hide()}
onCancel={() => modal.hide()}
afterClose={() => modal.remove()}
>
Hello {name}!
);
});
// Material UI modal — no custom props needed
const MyMuiDialog = NiceModal.create(() => {
const modal = useModal();
return (
);
});
```
```
--------------------------------
### show
Source: https://github.com/ebay/nice-modal-react/blob/main/docs/api/interfaces/NiceModalHandler.html
Shows the modal and returns a promise that resolves or rejects based on the modal's outcome.
```APIDOC
## show
### Description
Shows the modal. This method returns a Promise that will be resolved by the `resolve` method or rejected by the `reject` method when the modal interaction is complete.
### Method
`show(): Promise`
### Returns
`Promise` - A promise that resolves with data from the modal or rejects if the modal is cancelled or encounters an error.
```
--------------------------------
### Show Modal by Component Instance
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Use NiceModal.show with a component reference to display a modal. Arguments passed as the second argument will be forwarded as props to the modal component.
```js
import NiceModal from '@ebay/nice-modal-react';
import MyAntdModal from './my-antd-modal'; // created by above code
function App() {
const showAntdModal = () => {
// Show a modal with arguments passed to the component as props
NiceModal.show(MyAntdModal, { name: 'Nate' })
};
return (
Nice Modal Examples
);
}
```
--------------------------------
### show
Source: https://github.com/ebay/nice-modal-react/blob/main/docs/api/index.html
Displays a modal, either by providing the component directly or by its registered ID.
```APIDOC
## show
### Description
Displays a modal. This function can be used in two ways: by passing a React functional component directly, or by passing the string ID of a previously registered modal. It returns a Promise that resolves with the result of the modal interaction.
### Type parameters
#### T
- **T**: unknown - The type of the value resolved by the Promise when the modal is closed.
#### C
- **C**: unknown - The type of the props for the modal component when passed directly.
#### P
- **P**: Partial - The type of the arguments passed to the modal when using a component.
### Parameters
#### modal
- **modal** (React.FC) - The React functional component to display as a modal.
#### args
- **args** (P) - Optional. Arguments to be passed to the modal component.
### Returns
- **Promise** - A Promise that resolves with the return value of the modal.
```
```APIDOC
## show
### Description
Displays a modal identified by its string ID. It returns a Promise that resolves with the result of the modal interaction.
### Type parameters
#### T
- **T**: unknown - The type of the value resolved by the Promise when the modal is closed.
### Parameters
#### modal
- **modal** (string) - The unique identifier of the registered modal to display.
#### args
- **args** (Record) - Optional. Arguments to be passed to the modal.
### Returns
- **Promise** - A Promise that resolves with the return value of the modal.
```
--------------------------------
### NiceModal.show
Source: https://context7.com/ebay/nice-modal-react/llms.txt
Imperatively show a modal from anywhere in your application using NiceModal.show. It accepts a component reference or a registered string ID and returns a Promise for promise-based interactions.
```APIDOC
## NiceModal.show — Imperatively show a modal from anywhere
`NiceModal.show` displays a modal by passing either the component reference or a registered string ID. It returns a `Promise` that resolves when `modal.resolve()` is called inside the modal, enabling promise-based data flow from modal back to caller.
```jsx
import NiceModal from '@ebay/nice-modal-react';
import UserInfoModal from './UserInfoModal';
// Show by component reference — no prior registration needed
function UserList() {
const handleNewUser = () => {
NiceModal.show(UserInfoModal)
.then((newUser) => {
console.log('New user created:', newUser);
// { id: '1234567890', name: 'Alice', job: 'Engineer' }
})
.catch((err) => {
console.error('Modal rejected:', err.message);
});
};
const handleEditUser = (user) => {
NiceModal.show(UserInfoModal, { user })
.then((updatedUser) => {
console.log('User updated:', updatedUser);
});
};
return (
);
}
// Show by registered string ID
NiceModal.register('confirm-dialog', ConfirmDialog);
function SomeComponent() {
const handleDelete = () => {
NiceModal.show('confirm-dialog', { message: 'Delete this item?' })
.then(() => deleteItem())
.catch(() => console.log('Cancelled'));
};
return ;
}
```
```
--------------------------------
### show
Source: https://github.com/ebay/nice-modal-react/blob/main/docs/api/index.html
Shows a modal, either by providing the component directly or by its registered ID, and returns a promise that resolves with the modal's return value.
```APIDOC
## show
### Description
Shows a modal, either by providing the component directly or by its registered ID, and returns a promise that resolves with the modal's return value.
### Method
`show(modal: React.FC, args?: P): Promise`
### Type Parameters
- **T**: The type of the value the modal will resolve with.
- **C**: The type of the component's props.
- **P**: The type of the arguments passed to the modal, extending `Partial `.
### Parameters
#### Path Parameters
- **modal** (React.FC) - The modal component to show.
- **args** (P) - Optional. Arguments to pass to the modal.
### Returns
- `Promise`: A promise that resolves with the return value of the shown modal.
---
### Method
`show(modal: string, args?: Record): Promise`
### Type Parameters
- **T**: The type of the value the modal will resolve with.
### Parameters
#### Path Parameters
- **modal** (string) - The ID of the modal to show.
- **args** (Record) - Optional. Arguments to pass to the modal.
### Returns
- `Promise`: A promise that resolves with the return value of the shown modal.
---
### Method
`show(modal: string, args: P): Promise`
### Type Parameters
- **T**: The type of the value the modal will resolve with.
- **P**: The type of the arguments passed to the modal.
### Parameters
#### Path Parameters
- **modal** (string) - The ID of the modal to show.
- **args** (P) - Arguments to pass to the modal.
### Returns
- `Promise`: A promise that resolves with the return value of the shown modal.
```
--------------------------------
### UI Library Helper Integrations
Source: https://github.com/ebay/nice-modal-react/blob/main/README.md
Shows how to use helper functions from nice-modal-react to integrate with Material UI, Ant Design, and Bootstrap React components. These helpers simplify binding modal actions.
```jsx
import NiceModal, {
muiDialog,
muiDialogV5,
antdModal,
antdModalV5,
antdDrawer,
antdDrawerV5,
bootstrapDialog
} from '@ebay/nice-modal-react';
//...
const modal = useModal();
// For MUI