ModalProvider
The provider component that renders all active modals.
Usage
Place ModalProvider at the root of your app:
tsx
import { ModalProvider } from "@hirotoshioi/hiraku-radix-ui";
function App() {
return (
<>
<YourApp />
<ModalProvider/>
</>
);
}tsx
import { ModalProvider } from "@hirotoshioi/hiraku-base-ui";
function App() {
return (
<>
<YourApp />
<ModalProvider/>
</>
);
}Props
tsx
interface ModalProviderProps {
exitDuration?: number; // default: 300
}exitDuration
How long (in ms) a modal stays mounted after close() so its close animation can play. When it ends, the modal is removed and openAsync(), ref.result and onDidClose() resolve.
tsx
<ModalProvider exitDuration={200} />tsx
// Only used by custom wrapper components
<ModalProvider exitDuration={200} />- Radix UI - Radix does not report when the close animation ends, so every modal waits
exitDurationms. Match it to your Content's exit animation. - Base UI - built-in wrappers (
dialog,sheet,alert-dialog) finish closing when Base UI'sonOpenChangeComplete(false)fires, i.e. when the actual exit animation ends, whatever its length.exitDurationis only used as a timer for custom wrapper components.
A per-modal value overrides the provider's:
tsx
const slowSheet = createSheet(SlowSheet, { exitDuration: 500 });How It Works
ModalProvider:
- Subscribes to the modal store
- Renders all active modal instances
- Wraps each modal with the appropriate Root component for your integration
- Provides the modal's context so components can call
useModalContext() - Keeps closing modals mounted until their close animation ends (see exitDuration)
- Passes dismiss events back to the store
With Next.js App Router
tsx
// app/layout.tsx
import { ModalProvider } from "@hirotoshioi/hiraku-radix-ui";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
{children}
<ModalProvider/>
</body>
</html>
);
}