Skip to content

Commit 9761391

Browse files
committed
Refine color function logic and types clean up
1 parent d950774 commit 9761391

File tree

2 files changed

+148
-172
lines changed

2 files changed

+148
-172
lines changed

polaris-migrator/src/migrations/replace-sass-color/color-maps.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

polaris-migrator/src/migrations/replace-sass-color/replace-sass-color.ts

Lines changed: 148 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
import type {FileInfo, API, Options} from 'jscodeshift';
22
import postcss, {Plugin} from 'postcss';
33
import valueParser from 'postcss-value-parser';
4-
import {colors as tokenColors} from '@shopify/polaris-tokens';
4+
import {colors as tokenColors, createVar} from '@shopify/polaris-tokens';
55

66
import {
77
NamespaceOptions,
88
namespace,
9-
isSassFunction,
109
getFunctionArgs,
1110
stripQuotes,
1211
StopWalkingFunctionNodes,
1312
} from '../../utilities/sass';
1413
import {isKeyOf} from '../../utilities/type-guards';
1514

16-
import {
17-
backgroundColorMap,
18-
borderColorMap,
19-
colorMap,
20-
fillColorMap,
21-
} from './color-maps';
22-
23-
const tokenColorsKeys = Object.keys(tokenColors);
24-
const maps = {
25-
colorMap,
26-
backgroundColorMap,
27-
borderColorMap,
28-
fillColorMap,
29-
};
30-
const propertyMap: {[key: string]: keyof typeof maps} = {
31-
color: 'colorMap',
32-
background: 'backgroundColorMap',
33-
'background-color': 'backgroundColorMap',
34-
border: 'borderColorMap',
35-
'border-color': 'borderColorMap',
36-
fill: 'fillColorMap',
37-
};
38-
3915
export default function replaceSassColors(
4016
file: FileInfo,
4117
_: API,
@@ -47,9 +23,6 @@ export default function replaceSassColors(
4723
}
4824

4925
const processed = Symbol('processed');
50-
const polarisCustomPropertyRegEx = new RegExp(
51-
String.raw`--p-(${tokenColorsKeys.join('|')})`,
52-
);
5326

5427
interface PluginOptions extends Options, NamespaceOptions {}
5528

@@ -62,9 +35,8 @@ const plugin = (options: PluginOptions = {}): Plugin => {
6235
// @ts-expect-error - Skip if processed so we don't process it again
6336
if (decl[processed]) return;
6437

65-
if (!isKeyOf(propertyMap, decl.prop)) return;
66-
const propertyMapKey = propertyMap[decl.prop];
67-
const replacementMap = maps[propertyMapKey];
38+
if (!isKeyOf(propertyMaps, decl.prop)) return;
39+
const replacementMap = propertyMaps[decl.prop];
6840
const parsed = valueParser(decl.value);
6941

7042
parsed.walk((node) => {
@@ -76,18 +48,16 @@ const plugin = (options: PluginOptions = {}): Plugin => {
7648

7749
// 1. Remove color() fallbacks
7850
if (node.value === 'var') {
79-
const {nodes} = node;
80-
const polarisCustomPropertyIndex = nodes.findIndex(
81-
(node) =>
82-
node.type === 'word' &&
83-
polarisCustomPropertyRegEx.test(node.value),
51+
const args = getFunctionArgs(node);
52+
const polarisCustomPropertyIndex = args.findIndex((arg) =>
53+
polarisCustomPropertyRegEx.test(arg),
8454
);
85-
const colorFnFallbackIndex = nodes.findIndex((node) =>
86-
isSassFunction(namespacedColor, node),
55+
const colorFnFallbackIndex = args.findIndex((arg) =>
56+
arg.startsWith(namespacedColor),
8757
);
8858

8959
if (polarisCustomPropertyIndex < colorFnFallbackIndex) {
90-
node.nodes = [nodes[0]];
60+
node.nodes = [node.nodes[0]];
9161
}
9262

9363
return StopWalkingFunctionNodes;
@@ -133,3 +103,142 @@ const plugin = (options: PluginOptions = {}): Plugin => {
133103
},
134104
};
135105
};
106+
107+
/*
108+
* See the legacy Sass API file for the original color palette
109+
* documentation/guides/legacy-polaris-v8-public-api.scss
110+
*/
111+
112+
const colorMap = {
113+
blue: {
114+
dark: '--p-interactive-hovered',
115+
base: '--p-interactive',
116+
},
117+
green: {
118+
dark: '--p-text-success',
119+
base: '--p-text-success',
120+
},
121+
yellow: {
122+
dark: '--p-text-warning',
123+
base: '--p-text-warning',
124+
},
125+
red: {
126+
dark: '--p-text-critical',
127+
base: '--p-text-critical',
128+
},
129+
ink: {
130+
base: '--p-text',
131+
light: '--p-text-subdued',
132+
lighter: '--p-text-subdued',
133+
lightest: '--p-text-subdued',
134+
},
135+
sky: {
136+
dark: '--p-text-subdued-on-dark',
137+
base: '--p-text-on-dark',
138+
light: '--p-text-on-dark',
139+
lighter: '--p-text-on-dark',
140+
},
141+
black: {
142+
base: '--p-text',
143+
},
144+
white: {
145+
base: '--p-text-on-dark',
146+
},
147+
};
148+
149+
const backgroundColorMap = {
150+
green: {
151+
light: '--p-surface-success',
152+
lighter: '--p-surface-success-subdued',
153+
},
154+
yellow: {
155+
light: '--p-surface-warning',
156+
lighter: '--p-surface-warning-subdued',
157+
},
158+
red: {
159+
light: '--p-surface-critical',
160+
lighter: '--p-surface-critical-subdued',
161+
},
162+
ink: {
163+
dark: '--p-surface-dark',
164+
base: '--p-surface-neutral-subdued-dark',
165+
},
166+
sky: {
167+
base: '--p-surface-neutral',
168+
light: '--p-surface-neutral-subdued',
169+
lighter: '--p-surface-subdued',
170+
},
171+
black: {
172+
base: '--p-surface-dark',
173+
},
174+
white: {
175+
base: '--p-surface',
176+
},
177+
};
178+
179+
const borderColorMap = {
180+
green: {
181+
dark: '--p-border-success',
182+
base: '--p-border-success',
183+
light: '--p-border-success-subdued',
184+
lighter: '--p-border-success-subdued',
185+
},
186+
yellow: {
187+
dark: '--p-border-warning',
188+
base: '--p-border-warning',
189+
light: '--p-border-warning-disabled',
190+
lighter: '--p-border-warning-subdued',
191+
},
192+
red: {
193+
dark: '--p-border-critical',
194+
base: '--p-border-critical',
195+
light: '--p-border-critical-subdued',
196+
lighter: '--p-border-critical-subdued',
197+
},
198+
ink: {
199+
lightest: '--p-border',
200+
},
201+
sky: {
202+
light: '--p-border-subdued',
203+
},
204+
};
205+
206+
const fillColorMap = {
207+
green: {
208+
dark: '--p-icon-success',
209+
base: '--p-icon-success',
210+
},
211+
yellow: {
212+
dark: '--p-icon-warning',
213+
base: '--p-icon-warning',
214+
},
215+
red: {
216+
dark: '--p-icon-critical',
217+
base: '--p-icon-critical',
218+
},
219+
ink: {
220+
base: '--p-icon',
221+
light: '--p-icon',
222+
lighter: '--p-icon-subdued',
223+
lightest: '--p-icon-disabled',
224+
},
225+
black: {
226+
base: '--p-icon',
227+
},
228+
white: {
229+
base: '--p-icon-on-dark',
230+
},
231+
};
232+
233+
const propertyMaps = {
234+
color: colorMap,
235+
background: backgroundColorMap,
236+
'background-color': backgroundColorMap,
237+
border: borderColorMap,
238+
'border-color': borderColorMap,
239+
fill: fillColorMap,
240+
};
241+
242+
const polarisCustomPropertyRegEx = new RegExp(
243+
Object.keys(tokenColors).map(createVar).join('|'),
244+
);

0 commit comments

Comments
 (0)