1- import { ReactNode } from "react" ;
2- import { Root as DialogRoot , DialogPortal , DialogContent } from "@radix-ui/react-dialog" ;
1+ import { ReactNode , useEffect } from "react" ;
2+ import {
3+ Root as DialogRoot ,
4+ DialogPortal ,
5+ DialogContent ,
6+ DialogTitle ,
7+ DialogDescription ,
8+ } from "@radix-ui/react-dialog" ;
39import { Shadow } from "./Shadow" ;
410import { twMerge } from "tailwind-merge" ;
511
@@ -10,6 +16,25 @@ export type Props = {
1016} ;
1117
1218export function Modal ( { open, onOpenChange, children } : Props ) {
19+ // Focus trapping doesn't seem to completely work with our iframe approach,
20+ // so tabbing until you get to the document body means Escape doesn't work.
21+ // We'll patch this behavior for now with our own listener.
22+ useEffect ( ( ) => {
23+ function onKeyDown ( event : KeyboardEvent ) {
24+ if ( event . defaultPrevented ) return ;
25+
26+ if ( event . key === "Escape" && open ) {
27+ event . preventDefault ( ) ;
28+ onOpenChange ?.( false ) ;
29+ }
30+ }
31+
32+ window . addEventListener ( "keydown" , onKeyDown ) ;
33+ return ( ) => {
34+ window . removeEventListener ( "keydown" , onKeyDown ) ;
35+ } ;
36+ } , [ onOpenChange , open ] ) ;
37+
1338 return (
1439 < DialogRoot open = { open } onOpenChange = { onOpenChange } >
1540 { /* This intentionally does not use `<DialogTrigger>` because it doesn't play nicely with `<Shadow>` trigger (our primary use case). */ }
@@ -28,14 +53,10 @@ export function Modal({ open, onOpenChange, children }: Props) {
2853 ) }
2954 >
3055 < div >
31- < DialogContent
32- className = "outline-none w-full max-w-[26rem] mx-auto"
33- // TODO description
34- aria-describedby = { undefined }
35- onOpenAutoFocus = { ( event ) => {
36- event . preventDefault ( ) ;
37- } }
38- >
56+ < DialogContent className = "outline-none w-full max-w-[26rem] mx-auto" >
57+ < DialogTitle className = "sr-only" > EntryKit</ DialogTitle >
58+ < DialogDescription className = "sr-only" > Sign in to this app</ DialogDescription >
59+
3960 { children }
4061 </ DialogContent >
4162 </ div >
0 commit comments