Skip to content

Commit e8d8b9b

Browse files
committed
add additional case for $fixed-element-stacking-order
1 parent c278d64 commit e8d8b9b

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

polaris-migrator/src/migrations/replace-sass-z-index/replace-sass-z-index.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2942
const hasMoreThanOneArgument = (node: FunctionNode) => node.nodes.length > 1;
3043

3144
const 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}));

polaris-migrator/src/migrations/replace-sass-z-index/tests/replace-sass-z-index.input.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ $someElement: (
3434
.scenario-7 {
3535
z-index: calc(#{z-index(dev-ui, $fixed-element-stacking-order)} + 1);
3636
}
37+
38+
.scenario-8 {
39+
z-index: z-index(modal, $fixed-element-stacking-order);
40+
}

polaris-migrator/src/migrations/replace-sass-z-index/tests/replace-sass-z-index.output.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ $someElement: (
4242
.scenario-7 {
4343
z-index: calc(#{z-index(dev-ui, $fixed-element-stacking-order)} + 1);
4444
}
45+
46+
.scenario-8 {
47+
z-index: var(--p-z-11);
48+
}

polaris-migrator/src/migrations/replace-sass-z-index/tests/with-namespace.input.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,10 @@ $someElement: (
3939
#{legacy-polaris-v8.z-index(dev-ui, $fixed-element-stacking-order)} + 1
4040
);
4141
}
42+
43+
.scenario-8 {
44+
z-index: legacy-polaris-v8.z-index(
45+
modal,
46+
legacy-polaris-v8.$fixed-element-stacking-order
47+
);
48+
}

polaris-migrator/src/migrations/replace-sass-z-index/tests/with-namespace.output.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ $someElement: (
4949
#{legacy-polaris-v8.z-index(dev-ui, $fixed-element-stacking-order)} + 1
5050
);
5151
}
52+
53+
.scenario-8 {
54+
z-index: var(--p-z-11);
55+
}

0 commit comments

Comments
 (0)