@@ -97,8 +97,9 @@ export const machine = createMachine({
97
97
98
98
exit : [ "cleanupObservers" , "cleanupAutoResetRef" ] ,
99
99
100
- initialState ( ) {
101
- return "closed"
100
+ initialState ( { prop } ) {
101
+ const value = prop ( "value" ) || prop ( "defaultValue" )
102
+ return value ? "open" : "closed"
102
103
} ,
103
104
104
105
on : {
@@ -113,21 +114,30 @@ export const machine = createMachine({
113
114
actions : [ "focusTopLevelEl" ] ,
114
115
} ,
115
116
"TRIGGER.ENTER" : {
116
- actions : [ "clearCloseRefs" ] ,
117
+ actions : [ "clearPreviousValue" , " clearCloseRefs"] ,
117
118
} ,
118
119
"TRIGGER.CLICK" : [
119
120
{
120
- target : "closed" ,
121
121
guard : and ( "isItemOpen" , "isRootMenu" ) ,
122
+ target : "closed" ,
122
123
actions : [ "setPreviousValue" , "clearValue" , "setClickCloseRef" ] ,
123
124
} ,
125
+ {
126
+ guard : and ( "wasItemOpen" , "isRootMenu" ) ,
127
+ target : "open" ,
128
+ actions : [ "setPreviousValue" , "setValueOnNextTick" ] ,
129
+ } ,
124
130
{
125
131
reenter : true ,
126
132
target : "open" ,
127
133
actions : [ "setPreviousValue" , "setValue" ] ,
128
134
} ,
129
135
] ,
130
136
"TRIGGER.MOVE" : [
137
+ {
138
+ guard : "isItemOpen" ,
139
+ actions : [ "setPreviousValue" , "setValue" , "setPointerMoveOpenedRef" ] ,
140
+ } ,
131
141
{
132
142
guard : "isSubmenu" ,
133
143
target : "open" ,
@@ -145,7 +155,7 @@ export const machine = createMachine({
145
155
146
156
states : {
147
157
closed : {
148
- entry : [ "cleanupObservers" , "propagateClose" , "clearPreviousValue" ] ,
158
+ entry : [ "cleanupObservers" , "propagateClose" ] ,
149
159
on : {
150
160
"TRIGGER.LEAVE" : {
151
161
actions : [ "clearPointerMoveRef" ] ,
@@ -192,15 +202,11 @@ export const machine = createMachine({
192
202
} ,
193
203
"CONTENT.DISMISS" : {
194
204
target : "closed" ,
195
- actions : [ "focusTriggerIfNeeded " , "clearValue" , "clearPointerMoveRef" ] ,
205
+ actions : [ "focusTrigger " , "clearValue" , "clearPointerMoveRef" ] ,
196
206
} ,
197
207
"CONTENT.ENTER" : {
198
208
actions : [ "restoreTabOrder" ] ,
199
209
} ,
200
- "TRIGGER.MOVE" : {
201
- guard : "isSubmenu" ,
202
- actions : [ "setValue" ] ,
203
- } ,
204
210
"ROOT.CLOSE" : {
205
211
// clear the previous value so indicator doesn't animate
206
212
actions : [ "clearPreviousValue" , "cleanupObservers" ] ,
@@ -214,11 +220,11 @@ export const machine = createMachine({
214
220
on : {
215
221
"CLOSE.DELAY" : {
216
222
target : "closed" ,
217
- actions : [ "setPreviousValue" , " clearValue"] ,
223
+ actions : [ "clearValue" ] ,
218
224
} ,
219
225
"CONTENT.DISMISS" : {
220
226
target : "closed" ,
221
- actions : [ "focusTriggerIfNeeded " , "clearValue" , "clearPointerMoveRef" ] ,
227
+ actions : [ "focusTrigger " , "clearValue" , "clearPointerMoveRef" ] ,
222
228
} ,
223
229
"CONTENT.ENTER" : {
224
230
target : "open" ,
@@ -230,8 +236,8 @@ export const machine = createMachine({
230
236
231
237
implementations : {
232
238
guards : {
233
- isOpen : ( { context } ) => context . get ( "value" ) !== null ,
234
239
isItemOpen : ( { context, event } ) => context . get ( "value" ) === event . value ,
240
+ wasItemOpen : ( { context, event } ) => context . get ( "previousValue" ) === event . value ,
235
241
isRootMenu : ( { context } ) => context . get ( "parent" ) == null ,
236
242
isSubmenu : ( { context } ) => context . get ( "parent" ) != null ,
237
243
} ,
@@ -386,6 +392,11 @@ export const machine = createMachine({
386
392
setValue ( { context, event } ) {
387
393
context . set ( "value" , event . value )
388
394
} ,
395
+ setValueOnNextTick ( { context, event } ) {
396
+ raf ( ( ) => {
397
+ context . set ( "value" , event . value )
398
+ } )
399
+ } ,
389
400
clearValue ( { context } ) {
390
401
context . set ( "value" , null )
391
402
} ,
@@ -410,7 +421,7 @@ export const machine = createMachine({
410
421
tabbableEls [ 0 ] ?. focus ( )
411
422
} )
412
423
} ,
413
- focusTriggerIfNeeded ( { context, scope } ) {
424
+ focusTrigger ( { context, scope } ) {
414
425
if ( context . get ( "value" ) == null ) return
415
426
const contentEl = dom . getContentEl ( scope , context . get ( "value" ) ! )
416
427
if ( ! contains ( contentEl , scope . getActiveElement ( ) ) ) return
0 commit comments