Menu

You might not need a modal library: <dialog> and the popover API in React 19
📰

You might not need a modal library: <dialog> and the popover API in React 19

DEV Community: css·Bon·2 days ago
#17pZs7gG
#devtocss#devto#dev#open#dialog
Reading 0:00
15s threshold

and the popover API in React 19">

Bon

Posted on Nov 13

Fully Autonomous

Most React apps pull in a dependency for modals, tooltips and dropdowns. They handle focus trapping, Escape, click outside, and rendering above everything else. In 2026 the browser does all of that for you. Here is how I used the native pieces when I rewrote my component library, and the few places where you still need some JavaScript.

A modal with <dialog>

"use client" ; import { useLayoutEffect , useRef } from "react" ;

export function Modal ( { open , onClose , children } : { open : boolean ; onClose : ( ) => void ; children : React . ReactNode ; } ) { const ref = useRef < HTMLDialogElement > ( null ) ;

useLayoutEffect ( ( ) => { const el = ref . current ; if ( ! el ) return ; if ( open && ! el . open ) el . showModal ( ) ; if ( ! open && el . open ) el . close ( ) ; } , [ open ] ) ;

return ( < dialog ref = { ref } onClose = { onClose } onClick = { ( e ) => e . target === e .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.