2020import * as React from 'react'
2121import * as DialogPrimitive from '@radix-ui/react-dialog'
2222import { usePathname } from 'next/navigation'
23+ import { flushSync } from 'react-dom'
2324import { cn } from '../../lib/cn'
2425import { focusFirstTextInput , focusFirstTextInputIn } from './auto-focus'
2526
@@ -79,9 +80,21 @@ export interface NativeSurfaceOcclusionPrepareDetail {
7980
8081interface NativeSurfaceOcclusionPreparation {
8182 completion : Promise < void >
83+ /** Re-enables keyboard input. Idempotent; `cancel` also releases it. */
84+ releaseInteraction : ( ) => void
8285 cancel : ( ) => void
8386}
8487
88+ interface NativeSurfaceOcclusionPreparationOptions {
89+ /**
90+ * Keeps keyboard input blocked after preparation settles until the caller
91+ * calls `releaseInteraction`, instead of releasing it on the next frame. A
92+ * surface that moves focus into itself on readiness releases right after
93+ * that focus lands, so a focused control never drops its first key.
94+ */
95+ holdInteraction ?: boolean
96+ }
97+
8598const PENDING_NATIVE_INTERACTION_EVENTS = [
8699 'beforeinput' ,
87100 'compositionstart' ,
@@ -162,7 +175,8 @@ function acquirePendingNativeInteractionLock(): () => void {
162175 * than exposing an unoccluded native surface underneath it.
163176 */
164177function requestNativeSurfaceOcclusionPreparation (
165- kind : NativeSurfaceOcclusionPrepareDetail [ 'kind' ]
178+ kind : NativeSurfaceOcclusionPrepareDetail [ 'kind' ] ,
179+ { holdInteraction = false } : NativeSurfaceOcclusionPreparationOptions = { }
166180) : NativeSurfaceOcclusionPreparation | null {
167181 const preparations : Promise < unknown > [ ] = [ ]
168182 const detail : NativeSurfaceOcclusionPrepareDetail = {
@@ -178,12 +192,13 @@ function requestNativeSurfaceOcclusionPreparation(
178192 const releaseInteractionLock = acquirePendingNativeInteractionLock ( )
179193 let releaseFrame : number | null = null
180194 const completion = Promise . all ( preparations ) . then ( ( ) => undefined )
181- // Keep keyboard input blocked through the first ready paint. The modal's
182- // layout effect can move focus into its real content before interaction is
183- // re-enabled; rejected preparation deliberately remains fail-closed until
184- // the owning surface unmounts and calls `cancel`.
195+ // Keep keyboard input blocked through the first ready paint, or with
196+ // `holdInteraction` until the caller has moved focus and releases it.
197+ // Rejected preparation deliberately remains fail-closed until the owning
198+ // surface unmounts and calls `cancel`.
185199 void completion . then (
186200 ( ) => {
201+ if ( holdInteraction ) return
187202 releaseFrame = requestAnimationFrame ( ( ) => {
188203 releaseFrame = null
189204 releaseInteractionLock ( )
@@ -193,6 +208,7 @@ function requestNativeSurfaceOcclusionPreparation(
193208 )
194209 return {
195210 completion,
211+ releaseInteraction : releaseInteractionLock ,
196212 cancel : ( ) => {
197213 if ( releaseFrame !== null ) cancelAnimationFrame ( releaseFrame )
198214 releaseFrame = null
@@ -340,14 +356,21 @@ function NativeSurfaceModalGate({ barrierClaimedRef, onReadyChange }: NativeSurf
340356 let active = true
341357
342358 onReadyChange ( false )
343- const preparation = requestNativeSurfaceOcclusionPreparation ( 'modal' )
359+ const preparation = requestNativeSurfaceOcclusionPreparation ( 'modal' , {
360+ holdInteraction : true ,
361+ } )
344362 barrierClaimedRef . current = preparation !== null
345363 if ( ! preparation ) {
346364 onReadyChange ( true )
347365 } else {
348366 void preparation . completion . then (
349367 ( ) => {
350- if ( active ) onReadyChange ( true )
368+ if ( ! active ) return
369+ // Commit readiness now so ModalContent's layout effect moves focus into
370+ // the dialog, then accept keys: a focused control must take the very
371+ // next key (Escape on a Leave prompt), never drop it to the barrier.
372+ flushSync ( ( ) => onReadyChange ( true ) )
373+ preparation . releaseInteraction ( )
351374 } ,
352375 ( ) => {
353376 // A failed native-surface transition is unsafe to reveal through.
0 commit comments