|
| 1 | +/* |
| 2 | + * Copyright 2026 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +// Regression tests for https://github.com/adobe/react-spectrum/issues/10093 |
| 14 | +// Verifies that overlays close when a scrollable ancestor scrolls, both in |
| 15 | +// light DOM and inside a shadow DOM (where scroll events have composed: false). |
| 16 | +// |
| 17 | +// Uses ComboBox which sets isNonModal: true so its Popover registers a |
| 18 | +// window.addEventListener('scroll', ...) via useCloseOnScroll — the same |
| 19 | +// hook that is affected by the shadow DOM scroll propagation bug. |
| 20 | + |
| 21 | +import {Button} from '../src/Button'; |
| 22 | +import {ComboBox} from '../src/ComboBox'; |
| 23 | +import {createRoot} from 'react-dom/client'; |
| 24 | +import {enableShadowDOM} from 'react-stately/private/flags/flags'; |
| 25 | +import {expect, it} from 'vitest'; |
| 26 | +import {Input} from '../src/Input'; |
| 27 | +import {Label} from '../src/Label'; |
| 28 | +import {ListBox, ListBoxItem} from '../src/ListBox'; |
| 29 | +import {Popover} from '../src/Popover'; |
| 30 | +import React from 'react'; |
| 31 | +import {User} from '@react-aria/test-utils'; |
| 32 | + |
| 33 | +function TestComboBox() { |
| 34 | + return ( |
| 35 | + <ComboBox aria-label="Favorite Animal"> |
| 36 | + <Label>Favorite Animal</Label> |
| 37 | + <Input /> |
| 38 | + <Button>▼</Button> |
| 39 | + <Popover> |
| 40 | + <ListBox> |
| 41 | + <ListBoxItem id="cat">Cat</ListBoxItem> |
| 42 | + <ListBoxItem id="dog">Dog</ListBoxItem> |
| 43 | + <ListBoxItem id="kangaroo">Kangaroo</ListBoxItem> |
| 44 | + </ListBox> |
| 45 | + </Popover> |
| 46 | + </ComboBox> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function makeScrollableContainer() { |
| 51 | + let scrollable = document.createElement('div'); |
| 52 | + scrollable.style.cssText = 'height: 100px; overflow-y: auto;'; |
| 53 | + let inner = document.createElement('div'); |
| 54 | + inner.style.height = '500px'; |
| 55 | + scrollable.appendChild(inner); |
| 56 | + let mountPoint = document.createElement('div'); |
| 57 | + inner.appendChild(mountPoint); |
| 58 | + return {scrollable, mountPoint}; |
| 59 | +} |
| 60 | + |
| 61 | +it('overlay closes when a scrollable light DOM ancestor scrolls', async () => { |
| 62 | + let testUtilUser = new User(); |
| 63 | + let {scrollable, mountPoint} = makeScrollableContainer(); |
| 64 | + document.body.appendChild(scrollable); |
| 65 | + |
| 66 | + let root = createRoot(mountPoint); |
| 67 | + root.render(<TestComboBox />); |
| 68 | + await new Promise<void>(resolve => setTimeout(resolve, 100)); |
| 69 | + |
| 70 | + let comboboxTester = testUtilUser.createTester('ComboBox', {root: scrollable}); |
| 71 | + await comboboxTester.open(); |
| 72 | + |
| 73 | + // ComboBox listbox renders into document.body via portal. |
| 74 | + expect(comboboxTester.getListbox()).not.toBeNull(); |
| 75 | + |
| 76 | + // Scroll the ancestor that contains the trigger — window capturing listener should close the overlay. |
| 77 | + scrollable.dispatchEvent(new Event('scroll')); |
| 78 | + await new Promise<void>(resolve => setTimeout(resolve, 100)); |
| 79 | + |
| 80 | + expect(comboboxTester.getListbox()).toBeNull(); |
| 81 | + |
| 82 | + root.unmount(); |
| 83 | + document.body.removeChild(scrollable); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('Shadow DOM', () => { |
| 87 | + /** |
| 88 | + * EnableShadowDOM must be called before mounting. |
| 89 | + * |
| 90 | + * Cannot be turned off, so should be called after light-dom tests. |
| 91 | + */ |
| 92 | + enableShadowDOM(); |
| 93 | + |
| 94 | + it('overlay closes when a scrollable shadow DOM ancestor scrolls', async () => { |
| 95 | + let testUtilUser = new User(); |
| 96 | + let outerHost = document.createElement('div'); |
| 97 | + document.body.appendChild(outerHost); |
| 98 | + let shadowRoot = outerHost.attachShadow({mode: 'open'}); |
| 99 | + |
| 100 | + let {scrollable, mountPoint} = makeScrollableContainer(); |
| 101 | + shadowRoot.appendChild(scrollable); |
| 102 | + |
| 103 | + let root = createRoot(mountPoint); |
| 104 | + root.render(<TestComboBox />); |
| 105 | + await new Promise<void>(resolve => setTimeout(resolve, 100)); |
| 106 | + |
| 107 | + let comboboxTester = testUtilUser.createTester('ComboBox', {root: scrollable}); |
| 108 | + await comboboxTester.open(); |
| 109 | + |
| 110 | + // Listbox renders into document.body via portal even in shadow DOM mode. |
| 111 | + expect(comboboxTester.getListbox()).not.toBeNull(); |
| 112 | + |
| 113 | + // Scroll inside the shadow root. |
| 114 | + // Without the fix, window never sees this event (composed: false). |
| 115 | + // With the fix (getEventTargets + addEvent), the shadow root listener closes the overlay. |
| 116 | + scrollable.dispatchEvent(new Event('scroll')); |
| 117 | + await new Promise<void>(resolve => setTimeout(resolve, 100)); |
| 118 | + |
| 119 | + expect(comboboxTester.getListbox()).toBeNull(); |
| 120 | + |
| 121 | + root.unmount(); |
| 122 | + document.body.removeChild(outerHost); |
| 123 | + }); |
| 124 | +}); |
0 commit comments