|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + * A lookup popover's search input inside a modal Dialog must be focusable. |
| 9 | + * |
| 10 | + * Radix's FocusScope keeps a module-level stack: when the popover's scope |
| 11 | + * mounts it pauses the dialog's trap, so focus may travel into the portalled |
| 12 | + * popover (which lives at document.body, OUTSIDE the dialog's DOM). Stock |
| 13 | + * @radix-ui/react-focus-scope@1.1.16 has a race that silently breaks this |
| 14 | + * pact: the stack-add effect's cleanup schedules |
| 15 | + * `focusScopesStack.remove(scope)` in a `setTimeout(0)`. When the effect |
| 16 | + * re-runs for a still-mounted scope (any `container`/dep flicker), the re-run |
| 17 | + * `add`s the scope back and THEN the stale timeout evicts it. The dialog's |
| 18 | + * trap listeners stay alive but its scope is gone from the stack, so a later |
| 19 | + * popover pauses nothing and every focus into the popover's search input is |
| 20 | + * yanked straight back into the dialog — "lookup 无法搜索" in the production |
| 21 | + * console (objectui#3183). |
| 22 | + * |
| 23 | + * `patches/@radix-ui__react-focus-scope.patch` fixes the race by cancelling |
| 24 | + * the pending eviction when the effect re-runs for a live scope. The first |
| 25 | + * test drives the race deterministically (child key swap → new DOM node → |
| 26 | + * container ref reattach → effect re-run) and fails without the patch. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 30 | +import * as React from 'react'; |
| 31 | +import { render, cleanup, act, screen } from '@testing-library/react'; |
| 32 | +import { FocusScope } from '@radix-ui/react-focus-scope'; |
| 33 | +import { Dialog, DialogContent, DialogTitle } from '../ui/dialog'; |
| 34 | +import { Popover, PopoverTrigger, PopoverContent } from '../ui/popover'; |
| 35 | + |
| 36 | +afterEach(cleanup); |
| 37 | + |
| 38 | +/** Flush the focus-scope cleanup timers (the `setTimeout(0)` eviction path). */ |
| 39 | +const flushEvictionTimers = () => act(() => new Promise<void>((r) => setTimeout(r, 25))); |
| 40 | + |
| 41 | +/** |
| 42 | + * Minimal stand-in for "modal dialog with a portalled lookup popover": |
| 43 | + * a trapped outer scope (the dialog) and an optional untrapped inner scope |
| 44 | + * (the popover) that must pause it via the shared focus-scopes stack. |
| 45 | + * `contentKey` swaps the trapped scope's child element identity, forcing the |
| 46 | + * container ref to detach/reattach — the deterministic version of the |
| 47 | + * production dep flicker that re-runs the stack effect. |
| 48 | + */ |
| 49 | +function Harness({ contentKey, popoverOpen }: { contentKey: string; popoverOpen: boolean }) { |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <FocusScope trapped loop asChild> |
| 53 | + <div key={contentKey}> |
| 54 | + <button type="button">inside dialog</button> |
| 55 | + </div> |
| 56 | + </FocusScope> |
| 57 | + {popoverOpen && ( |
| 58 | + <FocusScope asChild trapped={false}> |
| 59 | + <div> |
| 60 | + <input placeholder="search records" /> |
| 61 | + </div> |
| 62 | + </FocusScope> |
| 63 | + )} |
| 64 | + </> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +describe('focus-scope stack race (patched @radix-ui/react-focus-scope)', () => { |
| 69 | + it('a re-run of the trapped scope effect must not evict it — the popover still pauses the dialog trap', async () => { |
| 70 | + const { rerender } = render(<Harness contentKey="a" popoverOpen={false} />); |
| 71 | + await flushEvictionTimers(); |
| 72 | + |
| 73 | + // Swap the child key: the trapped scope's container node is replaced, its |
| 74 | + // ref detaches/reattaches, and the stack effect cleans up + re-runs. |
| 75 | + // Un-patched, the stale cleanup timeout then evicts the re-added scope |
| 76 | + // while its trap listeners stay active and `paused` is still false. |
| 77 | + rerender(<Harness contentKey="b" popoverOpen={false} />); |
| 78 | + await flushEvictionTimers(); |
| 79 | + |
| 80 | + // Open the "popover". Its scope pauses the stack top — which, un-patched, |
| 81 | + // no longer contains the dialog scope, so the trap keeps yanking. |
| 82 | + rerender(<Harness contentKey="b" popoverOpen />); |
| 83 | + await flushEvictionTimers(); |
| 84 | + |
| 85 | + const inside = screen.getByRole('button', { name: 'inside dialog' }); |
| 86 | + const input = screen.getByPlaceholderText('search records'); |
| 87 | + act(() => { |
| 88 | + inside.focus(); |
| 89 | + }); |
| 90 | + act(() => { |
| 91 | + input.focus(); |
| 92 | + }); |
| 93 | + await flushEvictionTimers(); |
| 94 | + |
| 95 | + expect(document.activeElement).toBe(input); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sanity: without a popover the trapped scope still yanks outside focus back', async () => { |
| 99 | + render(<Harness contentKey="a" popoverOpen={false} />); |
| 100 | + await flushEvictionTimers(); |
| 101 | + |
| 102 | + const inside = screen.getByRole('button', { name: 'inside dialog' }); |
| 103 | + act(() => { |
| 104 | + inside.focus(); |
| 105 | + }); |
| 106 | + |
| 107 | + const outside = document.createElement('button'); |
| 108 | + outside.textContent = 'outside'; |
| 109 | + document.body.appendChild(outside); |
| 110 | + act(() => { |
| 111 | + outside.focus(); |
| 112 | + }); |
| 113 | + await flushEvictionTimers(); |
| 114 | + |
| 115 | + expect(document.activeElement).not.toBe(outside); |
| 116 | + outside.remove(); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('lookup popover search inside a modal dialog (integration)', () => { |
| 121 | + it('the popover search input inside a Dialog receives and keeps focus', async () => { |
| 122 | + render( |
| 123 | + <Dialog open> |
| 124 | + <DialogContent> |
| 125 | + <DialogTitle>host dialog</DialogTitle> |
| 126 | + <Popover open> |
| 127 | + <PopoverTrigger asChild> |
| 128 | + <button type="button">pick</button> |
| 129 | + </PopoverTrigger> |
| 130 | + <PopoverContent> |
| 131 | + <input placeholder="search records" /> |
| 132 | + </PopoverContent> |
| 133 | + </Popover> |
| 134 | + </DialogContent> |
| 135 | + </Dialog>, |
| 136 | + ); |
| 137 | + await flushEvictionTimers(); |
| 138 | + |
| 139 | + // Focus starts on an element INSIDE the dialog (the field trigger), then |
| 140 | + // moves into the portalled popover input — the real interaction shape. |
| 141 | + const trigger = screen.getByRole('button', { name: 'pick' }); |
| 142 | + const input = screen.getByPlaceholderText('search records'); |
| 143 | + act(() => { |
| 144 | + trigger.focus(); |
| 145 | + }); |
| 146 | + act(() => { |
| 147 | + input.focus(); |
| 148 | + }); |
| 149 | + await flushEvictionTimers(); |
| 150 | + |
| 151 | + expect(document.activeElement).toBe(input); |
| 152 | + }); |
| 153 | +}); |
0 commit comments