You don't need a library for a React modal
The native dialog element gives you focus trapping, Escape to close and a styleable backdrop for free. A complete React modal in about 20 lines.
Ask a React developer for a modal in 2026 and you’ll get npx shadcn add dialog before you finish the sentence. That gives you a well-built component, a Radix or Base UI primitive underneath it, a portal, a focus scope and a handful of files in your repo.
For a lot of projects that is more than the job needs. The browser has shipped a modal for years. It’s called <dialog>, and it does most of what those libraries were invented to do.
What the browser gives you
Open a <dialog> with showModal() and you get all of this without writing any of it:
- Top layer rendering. The dialog sits above everything else on the page. No portal, no
z-index: 9999, no fighting with a parent that hasoverflow: hidden. - A focus trap. The rest of the page becomes inert. Tab stays inside the dialog and screen readers can’t wander out of it.
- Escape to close.
- Focus restore. When the dialog closes, focus goes back to the button that opened it.
- A backdrop. The
::backdroppseudo-element covers the page behind it, and you can style it.
These are exactly the parts people get wrong when they build a modal out of a div.
The HTML version
<button id="open">Open Modal</button>
<dialog id="confirm" aria-labelledby="confirm-title" closedby="any">
<h2 id="confirm-title">Delete this project?</h2>
<p>This can't be undone.</p>
<form method="dialog">
<button>Cancel</button>
<button value="delete">Delete</button>
<button class="close" aria-label="Close">×</button>
</form>
</dialog>
<script>
const dialog = document.getElementById('confirm');
document.getElementById('open').onclick = () => dialog.showModal();
</script>
Two things here do a lot of work.
<form method="dialog"> closes the dialog when it’s submitted, so the buttons need no click handlers. The value of the button that was pressed ends up in dialog.returnValue. The close icon in the corner is one more button in that form. It gets an aria-label, because ”×” on its own doesn’t tell a screen reader user what the button does.
closedby="any" closes the dialog when you click outside it. It’s the newest part of this setup, so check browser support against your audience. Without it the dialog still closes with Escape and with its buttons, which is a fine fallback.
Here is that markup running on this page, with the styles from the next section. Open it, then try Tab, Escape and a click outside the dialog.
Styling it
dialog {
border: 0;
border-radius: 12px;
padding: 24px;
max-width: min(480px, calc(100vw - 32px));
}
dialog::backdrop {
background: rgb(10 20 40 / 0.6);
backdrop-filter: blur(2px);
}
dialog .close {
position: absolute;
top: 12px;
right: 12px;
width: 32px;
height: 32px;
border: 0;
border-radius: 50%;
background: none;
color: inherit;
font-size: 1.5rem;
line-height: 1;
cursor: pointer;
}
dialog .close:hover {
background: rgb(10 20 40 / 0.08);
}
/* Stop the page behind it from scrolling */
html:has(dialog:modal) {
overflow: hidden;
}
That’s the whole stylesheet. The browser centers the dialog for you. A modal dialog is position: fixed, so the absolutely positioned close button anchors to its corner without any extra wrapper.
One gotcha: if you want display: flex or grid on the dialog, put it on dialog[open]. Set it on plain dialog and you override the browser rule that hides a closed dialog, so your modal is suddenly always visible.
The React component
React thinks in state, and <dialog> wants a method call. A small effect bridges the two:
import { useEffect, useRef, type ReactNode } from 'react';
type ModalProps = {
open: boolean;
onClose: () => void;
children: ReactNode;
};
export function Modal({ open, onClose, children }: ModalProps) {
const ref = useRef<HTMLDialogElement>(null);
useEffect(() => {
const dialog = ref.current;
if (!dialog) return;
if (open && !dialog.open) dialog.showModal();
if (!open && dialog.open) dialog.close();
}, [open]);
return (
<dialog ref={ref} onClose={onClose} closedby="any">
{children}
</dialog>
);
}
Using it looks like any other controlled component:
const [open, setOpen] = useState(false);
<button onClick={() => setOpen(true)}>Open Modal</button>
<Modal open={open} onClose={() => setOpen(false)}>
<h2>Delete this project?</h2>
<button onClick={() => setOpen(false)}>Cancel</button>
</Modal>
The onClose prop matters more than it looks. The browser can close the dialog on its own, through Escape or a click on the backdrop. The close event is how your state hears about it. Leave it out and open stays true while the dialog is gone, and the next click on your button does nothing.
Don’t be tempted to skip the effect and pass open straight to the element. <dialog open> shows the dialog as a non-modal one. You lose the backdrop, the focus trap and the top layer, which were the reasons to use it.
A little animation
dialog {
transition: opacity 0.2s, translate 0.2s;
}
@starting-style {
dialog[open] {
opacity: 0;
translate: 0 8px;
}
}
@starting-style tells the browser where the transition starts when the dialog first appears. That covers the entrance. Exit animations are possible too, with transition-behavior: allow-discrete on display and overlay, but support is less even. I usually ship the entrance only. A modal that closes instantly feels fast, and nobody has ever complained.
What it costs in the bundle
To put a number on it, I built the same confirm dialog in an empty Vite and React 19 app and compared the production JavaScript. The shadcn/ui versions are exactly what the CLI generates, once on Radix and once on Base UI.
| Build | JS, gzipped | Added by the modal |
|---|---|---|
| React app without a modal | 68.53 kB | |
Native <dialog> | 68.76 kB | 0.23 kB |
| shadcn/ui Dialog on Radix | 95.30 kB | 26.77 kB |
| shadcn/ui Dialog on Base UI | 102.82 kB | 34.29 kB |
The native version adds about 230 bytes, which is the component from this post and nothing else. The Radix version adds almost 27 kB, close to 40% on top of React and ReactDOM. Before compression the gap is 0.7 kB against 74 kB.
Where does it go? In the Radix build, about a third is cn, the helper that merges Tailwind classes. Most of the rest is Radix itself: a focus scope, a dismissable layer, scroll locking and presence tracking for exit animations. Those are JavaScript versions of what showModal() does natively.
This is the worst case for the library, a page with one modal and nothing else. cn and most of those internals are shared, so the second and the tenth shadcn/ui component cost far less. And 27 kB won’t make or break an app. It is still a hundred times more code than the native version, for something that opens and closes.
The CSS is harder to compare fairly, because shadcn/ui needs Tailwind and the native version doesn’t. The dialog styles in this post come to under 0.4 kB gzipped.
Measured in November 2026 with Vite 8.4, React 19.3 and version 4.23 of the shadcn CLI.
When I still reach for a library
The native element isn’t always the answer.
- The project already uses shadcn/ui. Then use its Dialog. Consistency beats saving a dependency you already have.
- You need more than a modal. Comboboxes, nested menus and popovers with collision detection are hard. That’s where Radix and Base UI earn their place.
- Polished exit animations in every browser. Libraries keep the element mounted until the animation finishes. Doing that by hand costs you the simplicity you came for.
- Old browsers. If your analytics still show meaningful traffic from browsers that predate
<dialog>, this isn’t for you.
For a confirm dialog, a newsletter signup or an image lightbox on a site without a component library, I write the 20 lines. There is less to update and less to debug. And the accessibility comes from the browser, which gets it right more often than I do.