@@ -2,17 +2,19 @@ import * as React from 'react';
22import EventManager from './EventManager' ;
33import { DEFAULT_CONTEXT_NAME } from './constants' ;
44import type {
5+ BottomSheetInstance ,
56 SheetID ,
67 SheetIds ,
7- SheetProps ,
88 SheetPayload ,
9+ SheetProps ,
910 SheetReturnValue ,
10- BottomSheetInstance ,
1111} from './types' ;
1212
1313let keys : string [ ] = [ ] ;
1414let contexts : string [ ] = [ ] ;
1515let instances : Record < string , BottomSheetInstance > = { } ;
16+ let zIndexOrders : Record < string , number > = { } ;
17+ let zIndexCounter = 0 ;
1618let sheetRegistry : Record <
1719 string ,
1820 Record < string , React . ComponentType < SheetProps > >
@@ -33,26 +35,26 @@ function showSheet<Id extends SheetIds>(
3335 id : SheetID < Id > ,
3436 ...args : SheetPayload < Id > extends never
3537 ? [
36- options ?: {
37- /**
38- * Provide `context` of the `SheetManagerProvider` where you want to show the bottom sheet.
39- */
40- context ?: string ;
41- } ,
42- ]
38+ options ?: {
39+ /**
40+ * Provide `context` of the `SheetManagerProvider` where you want to show the bottom sheet.
41+ */
42+ context ?: string ;
43+ } ,
44+ ]
4345 : [
44- options : {
45- /**
46- * Any data to pass to the `BottomSheet`.
47- */
48- payload : SheetPayload < Id > ;
49-
50- /**
51- * Provide `context` of the `SheetManagerProvider` where you want to show the bottom sheet.
52- */
53- context ?: string ;
54- } ,
55- ]
46+ options : {
47+ /**
48+ * Any data to pass to the `BottomSheet`.
49+ */
50+ payload : SheetPayload < Id > ;
51+
52+ /**
53+ * Provide `context` of the `SheetManagerProvider` where you want to show the bottom sheet.
54+ */
55+ context ?: string ;
56+ } ,
57+ ]
5658) : Promise < SheetReturnValue < Id > | undefined > ;
5759function showSheet ( ...args : any [ ] ) {
5860 const [ id , options = { } ] = args ;
@@ -81,26 +83,26 @@ function hideSheet<Id extends SheetIds>(
8183 id : SheetID < Id > ,
8284 ...args : SheetReturnValue < Id > extends never
8385 ? [
84- options ?: {
85- /**
86- * Provide `context` of the `SheetManagerProvider` where you want to hide the bottom sheet.
87- */
88- context ?: string ;
89- } ,
90- ]
86+ options ?: {
87+ /**
88+ * Provide `context` of the `SheetManagerProvider` where you want to hide the bottom sheet.
89+ */
90+ context ?: string ;
91+ } ,
92+ ]
9193 : [
92- options : {
93- /**
94- * Return some data to the caller on closing the `BottomSheet`.
95- */
96- value : SheetReturnValue < Id > ;
97-
98- /**
99- * Provide `context` of the `SheetManagerProvider` where you want to hide the bottom sheet.
100- */
101- context ?: string ;
102- } ,
103- ]
94+ options : {
95+ /**
96+ * Return some data to the caller on closing the `BottomSheet`.
97+ */
98+ value : SheetReturnValue < Id > ;
99+
100+ /**
101+ * Provide `context` of the `SheetManagerProvider` where you want to hide the bottom sheet.
102+ */
103+ context ?: string ;
104+ } ,
105+ ]
104106) : Promise < SheetReturnValue < Id > | undefined > ;
105107function hideSheet ( ...args : any [ ] ) {
106108 const [ id , options = { } ] = args ;
@@ -227,7 +229,9 @@ const SheetManager = {
227229 * Hide all the opened BottomSheets.
228230 */
229231 hideAll ( ) : void {
230- for ( const key of keys ) {
232+ // Iterate over a snapshot to avoid issues if the array is mutated during iteration
233+ const snapshot = [ ...keys ] ;
234+ for ( const key of snapshot ) {
231235 const [ id , context ] = key . split ( '.' ) ;
232236
233237 if ( id && context ) {
@@ -242,6 +246,8 @@ export const PrivateSheetManager = {
242246 keys = [ ] ;
243247 contexts = [ ] ;
244248 instances = { } ;
249+ zIndexOrders = { } ;
250+ zIndexCounter = 0 ;
245251 sheetRegistry = {
246252 [ DEFAULT_CONTEXT_NAME ] : { } ,
247253 } ;
@@ -269,6 +275,8 @@ export const PrivateSheetManager = {
269275 const key = makeKey ( id , context ) ;
270276
271277 instances [ key ] = instance ;
278+ // Track show order so the latest opened sheet always has highest zIndex
279+ zIndexOrders [ key ] = DEFAULT_Z_INDEX + ( ++ zIndexCounter ) ;
272280
273281 if ( keys . indexOf ( key ) === - 1 ) {
274282 keys . push ( key ) ;
@@ -300,13 +308,8 @@ export const PrivateSheetManager = {
300308 getSheetComponent,
301309 getSheetZIndex ( id : string , context : string ) : number {
302310 const key = makeKey ( id , context ) ;
303- const index = keys . indexOf ( key ) ;
304-
305- if ( index > - 1 ) {
306- return DEFAULT_Z_INDEX + index + 1 ;
307- }
308-
309- return DEFAULT_Z_INDEX ;
311+ const zIndex = zIndexOrders [ key ] ?? DEFAULT_Z_INDEX ;
312+ return zIndex ;
310313 } ,
311314} as const ;
312315
0 commit comments