Skip to content

Commit f113bba

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

File tree

2 files changed

+145
-170
lines changed

2 files changed

+145
-170
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: 145 additions & 37 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,
@@ -48,7 +24,7 @@ export default function replaceSassColors(
4824

4925
const processed = Symbol('processed');
5026
const polarisCustomPropertyRegEx = new RegExp(
51-
String.raw`--p-(${tokenColorsKeys.join('|')})`,
27+
Object.keys(tokenColors).map(createVar).join('|'),
5228
);
5329

5430
interface PluginOptions extends Options, NamespaceOptions {}
@@ -62,9 +38,8 @@ const plugin = (options: PluginOptions = {}): Plugin => {
6238
// @ts-expect-error - Skip if processed so we don't process it again
6339
if (decl[processed]) return;
6440

65-
if (!isKeyOf(propertyMap, decl.prop)) return;
66-
const propertyMapKey = propertyMap[decl.prop];
67-
const replacementMap = maps[propertyMapKey];
41+
if (!isKeyOf(propertyMaps, decl.prop)) return;
42+
const replacementMap = propertyMaps[decl.prop];
6843
const parsed = valueParser(decl.value);
6944

7045
parsed.walk((node) => {
@@ -76,18 +51,16 @@ const plugin = (options: PluginOptions = {}): Plugin => {
7651

7752
// 1. Remove color() fallbacks
7853
if (node.value === 'var') {
79-
const {nodes} = node;
80-
const polarisCustomPropertyIndex = nodes.findIndex(
81-
(node) =>
82-
node.type === 'word' &&
83-
polarisCustomPropertyRegEx.test(node.value),
54+
const args = getFunctionArgs(node);
55+
const polarisCustomPropertyIndex = args.findIndex((arg) =>
56+
polarisCustomPropertyRegEx.test(arg),
8457
);
85-
const colorFnFallbackIndex = nodes.findIndex((node) =>
86-
isSassFunction(namespacedColor, node),
58+
const colorFnFallbackIndex = args.findIndex((arg) =>
59+
arg.startsWith(namespacedColor),
8760
);
8861

8962
if (polarisCustomPropertyIndex < colorFnFallbackIndex) {
90-
node.nodes = [nodes[0]];
63+
node.nodes = [node.nodes[0]];
9164
}
9265

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

0 commit comments

Comments
 (0)