Skip to content

Commit 99f310d

Browse files
committed
fix(emcn): a modal over a native surface accepts keys as soon as focus lands
1 parent 0379df3 commit 99f310d

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎apps/desktop/e2e/browser-page-dialogs.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ createRoot(document.getElementById('root')).render(createElement(Fixture));`,
315315
}
316316
void url
317317
}, form)
318-
for (let attempt = 0; attempt < 8; attempt++) {
318+
for (let attempt = 0; attempt < 12; attempt++) {
319319
await shell.evaluate(() => {
320320
;(globalThis as unknown as { escapeDiag: unknown[] }).escapeDiag.length = 0
321321
})

‎packages/emcn/src/components/modal/modal.test.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,15 @@ describe('native-surface modal preparation', () => {
153153
expect(readyLayers.contentLayer.className).toContain('pointer-events-none')
154154
expect(readyLayers.dialog).not.toBeNull()
155155
expect(readyLayers.dialog?.style.visibility).not.toBe('hidden')
156-
expect(document.activeElement).toBe(
157-
document.querySelector<HTMLInputElement>('[aria-label="Modal field"]')
158-
)
156+
const modalField = document.querySelector<HTMLInputElement>('[aria-label="Modal field"]')
157+
expect(document.activeElement).toBe(modalField)
158+
// The control that now holds focus takes the very next key; no frame has to pass first.
159+
const modalKeyDown = vi.fn()
160+
modalField?.addEventListener('keydown', modalKeyDown)
161+
const firstKey = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key: 'x' })
162+
modalField?.dispatchEvent(firstKey)
163+
expect(firstKey.defaultPrevented).toBe(false)
164+
expect(modalKeyDown).toHaveBeenCalledOnce()
159165
})
160166

161167
it('fails closed when a registered preparation rejects', async () => {

‎packages/emcn/src/components/modal/modal.tsx‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import * as React from 'react'
2121
import * as DialogPrimitive from '@radix-ui/react-dialog'
2222
import { usePathname } from 'next/navigation'
23+
import { flushSync } from 'react-dom'
2324
import { cn } from '../../lib/cn'
2425
import { focusFirstTextInput, focusFirstTextInputIn } from './auto-focus'
2526

@@ -79,9 +80,21 @@ export interface NativeSurfaceOcclusionPrepareDetail {
7980

8081
interface 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+
8598
const 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
*/
164177
function 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

Comments
 (0)