@@ -20,16 +20,33 @@ const zIndexMap = {
2020 overlay : '--p-z-2' ,
2121} ;
2222
23- const isValidElement = (
24- element : unknown ,
25- ) : element is keyof typeof zIndexMap => {
26- return Object . keys ( zIndexMap ) . includes ( element as string ) ;
23+ const fixedElementStackingOrder = {
24+ 'global-ribbon' : '--p-z-3' ,
25+ 'top-bar' : '--p-z-4' ,
26+ 'context-bar' : '--p-z-5' ,
27+ 'small-screen-loading-bar' : '--p-z-6' ,
28+ 'nav-backdrop' : '--p-z-7' ,
29+ nav : '--p-z-8' ,
30+ 'skip-to-content' : '--p-z-9' ,
31+ backdrop : '--p-z-10' ,
32+ modal : '--p-z-11' ,
33+ toast : '--p-z-12' ,
2734} ;
2835
36+ function isValidElement <
37+ MapType extends typeof zIndexMap | typeof fixedElementStackingOrder ,
38+ > ( element : unknown , mapObj : MapType ) : element is keyof typeof mapObj {
39+ return Object . keys ( mapObj ) . includes ( element as string ) ;
40+ }
41+
2942const hasMoreThanOneArgument = ( node : FunctionNode ) => node . nodes . length > 1 ;
3043
3144const plugin = ( options : PluginOptions = { } ) : Plugin => {
3245 const namespacedZIndex = namespace ( 'z-index' , options ) ;
46+ const namespacedFixedElementStackingOrder = namespace (
47+ '$fixed-element-stacking-order' ,
48+ options ,
49+ ) ;
3350 return {
3451 postcssPlugin : 'replace-sass-z-index' ,
3552 Declaration ( decl ) {
@@ -40,7 +57,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
4057
4158 if ( ! hasSassFunction ( namespacedZIndex , parsedValue ) ) return ;
4259
43- let containsSecondArgument = false ;
60+ let containsUnknownSecondArgument = false ;
4461
4562 parsedValue . walk ( ( node : Node ) => {
4663 if ( ! isSassFunction ( namespacedZIndex , node ) ) return ;
@@ -49,14 +66,33 @@ const plugin = (options: PluginOptions = {}): Plugin => {
4966 // We assume they're passing in a custom map
5067 // In this case its unlikely this will resolve to a polaris token value
5168 // transform legacy zIndex usage to map-get and move on.
52- containsSecondArgument = true ;
53- node . value = 'map-get' ;
69+
70+ const [ key , _ , map ] = node . nodes ;
71+ if (
72+ map . value === namespacedFixedElementStackingOrder &&
73+ isValidElement ( key . value , fixedElementStackingOrder )
74+ ) {
75+ const fixedElementStackingOrderToken =
76+ fixedElementStackingOrder [ key . value ] ;
77+ node . value = 'var' ;
78+ node . nodes = [
79+ {
80+ type : 'word' ,
81+ value : fixedElementStackingOrderToken ,
82+ sourceIndex : node . nodes [ 0 ] ?. sourceIndex ?? 0 ,
83+ sourceEndIndex : fixedElementStackingOrderToken . length ,
84+ } ,
85+ ] ;
86+ } else {
87+ containsUnknownSecondArgument = true ;
88+ node . value = 'map-get' ;
89+ node . nodes . reverse ( ) ;
90+ }
5491 // map-get arguments are in the reverse order to z-index arguments.
5592 // map-get expects the map object first, and the key second.
56- node . nodes . reverse ( ) ;
5793 } else {
5894 const element = node . nodes [ 0 ] ?. value ?? '' ;
59- if ( ! isValidElement ( element ) ) return ;
95+ if ( ! isValidElement < typeof zIndexMap > ( element , zIndexMap ) ) return ;
6096 const zIndexCustomProperty = zIndexMap [ element ] ;
6197
6298 node . value = 'var' ;
@@ -71,7 +107,7 @@ const plugin = (options: PluginOptions = {}): Plugin => {
71107 }
72108 } ) ;
73109
74- if ( hasNumericOperator ( parsedValue ) || containsSecondArgument ) {
110+ if ( hasNumericOperator ( parsedValue ) || containsUnknownSecondArgument ) {
75111 // Insert comment if the declaration value contains calculations
76112 // or if the invocation of zIndex has more than one argument
77113 decl . before ( postcss . comment ( { text : POLARIS_MIGRATOR_COMMENT } ) ) ;
0 commit comments