@@ -9,6 +9,204 @@ import Combobox from './story/_Combobox.vue'
99import ComboboxObject from './story/_ComboboxObject.vue'
1010import ComboboxTagsInput from './story/_ComboboxTagsInput.vue'
1111
12+ const PersistentCombobox = defineComponent ( {
13+ components : { ComboboxAnchor, ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot, ComboboxTrigger, ComboboxViewport } ,
14+ props : {
15+ bodyLock : Boolean ,
16+ onEscapeKeyDown : Function ,
17+ onInteractOutside : Function ,
18+ } ,
19+ template : `<ComboboxRoot :unmount-on-hide="false">
20+ <ComboboxAnchor>
21+ <ComboboxInput data-testid="persistent-input" />
22+ <ComboboxTrigger data-testid="persistent-trigger">Open</ComboboxTrigger>
23+ </ComboboxAnchor>
24+ <ComboboxPortal>
25+ <ComboboxContent
26+ data-testid="persistent-content"
27+ :body-lock="bodyLock"
28+ @escape-key-down="onEscapeKeyDown"
29+ @interact-outside="onInteractOutside"
30+ >
31+ <ComboboxViewport>
32+ <ComboboxItem value="Apple">Apple</ComboboxItem>
33+ </ComboboxViewport>
34+ <input data-testid="option-state" aria-label="Option state">
35+ </ComboboxContent>
36+ </ComboboxPortal>
37+ </ComboboxRoot>` ,
38+ } )
39+
40+ const PersistentComboboxWithInputInside = defineComponent ( {
41+ components : { ComboboxContent, ComboboxInput, ComboboxItem, ComboboxPortal, ComboboxRoot, ComboboxTrigger, ComboboxViewport } ,
42+ template : `<ComboboxRoot :unmount-on-hide="false">
43+ <ComboboxTrigger data-testid="inside-trigger">Open</ComboboxTrigger>
44+ <ComboboxPortal>
45+ <ComboboxContent data-testid="inside-content">
46+ <ComboboxInput data-testid="inside-input" />
47+ <ComboboxViewport>
48+ <ComboboxItem value="Apple">Apple</ComboboxItem>
49+ </ComboboxViewport>
50+ </ComboboxContent>
51+ </ComboboxPortal>
52+ </ComboboxRoot>` ,
53+ } )
54+
55+ describe ( 'given a Combobox with unmountOnHide=false' , ( ) => {
56+ let wrapper : VueWrapper < InstanceType < typeof PersistentCombobox > >
57+
58+ beforeEach ( ( ) => {
59+ document . body . innerHTML = ''
60+ wrapper = mount ( PersistentCombobox , { attachTo : document . body } )
61+ } )
62+
63+ afterEach ( async ( ) => {
64+ wrapper ?. unmount ( )
65+ await nextTick ( )
66+ } )
67+
68+ it ( 'keeps the same hidden content node and subtree state across close and reopen' , async ( ) => {
69+ const trigger = wrapper . find ( '[data-testid="persistent-trigger"]' )
70+ const content = document . querySelector ( '[data-testid="persistent-content"]' ) as HTMLElement
71+ const statefulInput = document . querySelector ( '[data-testid="option-state"]' ) as HTMLInputElement
72+
73+ expect ( content . style . display ) . toBe ( 'none' )
74+ expect ( content . dataset . state ) . toBe ( 'closed' )
75+ expect ( document . querySelector ( '[role="combobox"]' ) ?. getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' )
76+ statefulInput . value = 'preserved'
77+
78+ await trigger . trigger ( 'click' )
79+ await nextTick ( )
80+ expect ( content . style . display ) . not . toBe ( 'none' )
81+
82+ statefulInput . focus ( )
83+ statefulInput . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
84+ await vi . waitFor ( ( ) => expect ( content . style . display ) . toBe ( 'none' ) )
85+ expect ( document . activeElement ) . toBe ( trigger . element )
86+
87+ await trigger . trigger ( 'click' )
88+ await nextTick ( )
89+ expect ( document . querySelector ( '[data-testid="persistent-content"]' ) ) . toBe ( content )
90+ expect ( ( document . querySelector ( '[data-testid="option-state"]' ) as HTMLInputElement ) . value ) . toBe ( 'preserved' )
91+ } )
92+
93+ it ( 'does not activate focus or dismissal behavior while closed' , async ( ) => {
94+ const onEscapeKeyDown = vi . fn ( )
95+ const onInteractOutside = vi . fn ( )
96+ await wrapper . setProps ( { onEscapeKeyDown, onInteractOutside } )
97+
98+ expect ( document . activeElement ) . toBe ( document . body )
99+ document . body . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
100+ document . body . dispatchEvent ( new Event ( 'pointerdown' , { bubbles : true } ) )
101+ await sleep ( 1 )
102+
103+ expect ( onEscapeKeyDown ) . not . toHaveBeenCalled ( )
104+ expect ( onInteractOutside ) . not . toHaveBeenCalled ( )
105+ } )
106+
107+ it ( 'releases body scroll lock when hidden' , async ( ) => {
108+ await wrapper . setProps ( { bodyLock : true } )
109+ const trigger = wrapper . find ( '[data-testid="persistent-trigger"]' )
110+
111+ expect ( document . body . style . overflow ) . not . toBe ( 'hidden' )
112+ await trigger . trigger ( 'click' )
113+ await nextTick ( )
114+ expect ( document . body . style . overflow ) . toBe ( 'hidden' )
115+
116+ await wrapper . setProps ( { bodyLock : false } )
117+ await nextTick ( )
118+ expect ( document . body . style . overflow ) . not . toBe ( 'hidden' )
119+
120+ await wrapper . setProps ( { bodyLock : true } )
121+ await nextTick ( )
122+ expect ( document . body . style . overflow ) . toBe ( 'hidden' )
123+
124+ document . activeElement ?. dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
125+ await vi . waitFor ( ( ) => expect ( document . body . style . overflow ) . not . toBe ( 'hidden' ) )
126+ } )
127+
128+ it ( 'removes persistent portal content when the root unmounts' , ( ) => {
129+ const content = document . querySelector ( '[data-testid="persistent-content"]' ) as HTMLElement
130+ wrapper . unmount ( )
131+ expect ( document . body . contains ( content ) ) . toBe ( false )
132+ } )
133+ } )
134+
135+ describe ( 'given a persistent Combobox with its input inside content' , ( ) => {
136+ let wrapper : VueWrapper < InstanceType < typeof PersistentComboboxWithInputInside > >
137+
138+ beforeEach ( ( ) => {
139+ document . body . innerHTML = ''
140+ wrapper = mount ( PersistentComboboxWithInputInside , { attachTo : document . body } )
141+ } )
142+
143+ afterEach ( ( ) => wrapper ?. unmount ( ) )
144+
145+ it ( 'does not focus hidden content on mount and restores the trigger on close' , async ( ) => {
146+ const trigger = wrapper . find ( '[data-testid="inside-trigger"]' )
147+ expect ( document . activeElement ) . toBe ( document . body )
148+
149+ await trigger . trigger ( 'click' )
150+ await vi . waitFor ( ( ) => expect ( document . activeElement ?. getAttribute ( 'data-testid' ) ) . toBe ( 'inside-input' ) )
151+
152+ await document . activeElement ?. dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
153+ await vi . waitFor ( ( ) => expect ( document . activeElement ) . toBe ( trigger . element ) )
154+ } )
155+ } )
156+
157+ describe ( 'given two persistent Comboboxes' , ( ) => {
158+ const TwoPersistentComboboxes = defineComponent ( {
159+ components : { PersistentCombobox } ,
160+ props : { onSecondEscapeKeyDown : Function } ,
161+ template : `<div>
162+ <PersistentCombobox data-testid="first" />
163+ <PersistentCombobox data-testid="second" :on-escape-key-down="onSecondEscapeKeyDown" />
164+ </div>` ,
165+ } )
166+
167+ let wrapper : VueWrapper < InstanceType < typeof TwoPersistentComboboxes > >
168+
169+ afterEach ( async ( ) => {
170+ wrapper ?. unmount ( )
171+ await nextTick ( )
172+ } )
173+
174+ it ( 'lets the open Combobox handle Escape when a hidden one mounted after it' , async ( ) => {
175+ document . body . innerHTML = ''
176+ const onSecondEscapeKeyDown = vi . fn ( )
177+ wrapper = mount ( TwoPersistentComboboxes , { attachTo : document . body , props : { onSecondEscapeKeyDown } } )
178+ await nextTick ( )
179+ const triggers = wrapper . findAll ( '[data-testid="persistent-trigger"]' )
180+ const contents = document . querySelectorAll < HTMLElement > ( '[data-testid="persistent-content"]' )
181+
182+ await triggers [ 0 ] . trigger ( 'click' )
183+ await nextTick ( )
184+ expect ( contents [ 0 ] . style . display ) . not . toBe ( 'none' )
185+ expect ( contents [ 1 ] . style . display ) . toBe ( 'none' )
186+
187+ document . activeElement ?. dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
188+ await vi . waitFor ( ( ) => expect ( contents [ 0 ] . style . display ) . toBe ( 'none' ) )
189+
190+ expect ( onSecondEscapeKeyDown ) . not . toHaveBeenCalled ( )
191+ } )
192+ } )
193+
194+ describe ( 'given a default Combobox mount policy' , ( ) => {
195+ it ( 'continues to unmount content when closed' , async ( ) => {
196+ document . body . innerHTML = ''
197+ const wrapper = mount ( Combobox , { attachTo : document . body } )
198+ expect ( document . querySelector ( '[role="listbox"]' ) ) . toBeNull ( )
199+
200+ await wrapper . find ( 'button' ) . trigger ( 'click' )
201+ await nextTick ( )
202+ expect ( document . querySelector ( '[role="listbox"]' ) ) . not . toBeNull ( )
203+
204+ document . activeElement ?. dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } ) )
205+ await vi . waitFor ( ( ) => expect ( document . querySelector ( '[role="listbox"]' ) ) . toBeNull ( ) )
206+ wrapper . unmount ( )
207+ } )
208+ } )
209+
12210describe ( 'given default Combobox' , ( ) => {
13211 let wrapper : VueWrapper < InstanceType < typeof Combobox > >
14212 let valueBox : DOMWrapper < HTMLInputElement >
@@ -804,6 +1002,33 @@ describe('given combobox handleBlur with deferred close', () => {
8041002 } )
8051003} )
8061004
1005+ describe ( 'given a Combobox with popper content' , ( ) => {
1006+ it ( 'forwards popper prop changes after mount' , async ( ) => {
1007+ const side = ref < 'bottom' | 'top' > ( 'bottom' )
1008+ const wrapper = mount ( defineComponent ( {
1009+ setup ( ) {
1010+ return ( ) => h ( ComboboxRoot , { open : true } , {
1011+ default : ( ) => [
1012+ h ( ComboboxAnchor , null , { default : ( ) => h ( ComboboxInput ) } ) ,
1013+ h ( ComboboxContent , { position : 'popper' , side : side . value } , {
1014+ default : ( ) => h ( ComboboxViewport , null , {
1015+ default : ( ) => h ( ComboboxItem , { value : 'Apple' } , { default : ( ) => 'Apple' } ) ,
1016+ } ) ,
1017+ } ) ,
1018+ ] ,
1019+ } )
1020+ } ,
1021+ } ) , { attachTo : document . body } )
1022+
1023+ const content = ( ) => document . querySelector < HTMLElement > ( '[role="listbox"]' ) ?. closest ( '[data-side]' )
1024+ await vi . waitFor ( ( ) => expect ( content ( ) ?. dataset . side ) . toBe ( 'bottom' ) )
1025+
1026+ side . value = 'top'
1027+ await vi . waitFor ( ( ) => expect ( content ( ) ?. dataset . side ) . toBe ( 'top' ) )
1028+ wrapper . unmount ( )
1029+ } )
1030+ } )
1031+
8071032describe ( 'comboboxContent with popper positioning' , ( ) => {
8081033 const getSlotRenderCount = vi . fn ( ( ) => ( { value : 0 } ) )
8091034
0 commit comments