Skip to content

Commit 3292df6

Browse files
avelinelaurkim
authored andcommitted
[Layout foundations] Refactor token types (#7296)
### WHY are these changes introduced? Fixes #7277 ### WHAT is this pull request doing? Pulls in token scales from `polaris-tokens` instead of adhoc creating them and duplicating the effort in various component files
1 parent a4a28a7 commit 3292df6

File tree

9 files changed

+78
-89
lines changed

9 files changed

+78
-89
lines changed

.changeset/three-games-cough.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@shopify/polaris': patch
3+
'@shopify/polaris-tokens': minor
4+
---
5+
6+
Refactored token types in primitive Layout components
7+
Exposed `DepthShadowAlias` type

polaris-react/src/components/AlphaCard/AlphaCard.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1+
import type {
2+
BreakpointsAlias,
3+
ColorsTokenName,
4+
DepthShadowAlias,
5+
ShapeBorderRadiusScale,
6+
SpacingSpaceScale,
7+
} from '@shopify/polaris-tokens';
18
import React from 'react';
29

310
import {useBreakpoints} from '../../utilities/breakpoints';
4-
// These should come from polaris-tokens eventually, see #7164
5-
import {
6-
BackgroundColorTokenScale,
7-
BorderRadiusTokenScale,
8-
Box,
9-
DepthTokenScale,
10-
SpacingTokenScale,
11-
} from '../Box';
11+
import {Box} from '../Box';
1212

1313
type CardElevationTokensScale = Extract<
14-
DepthTokenScale,
14+
DepthShadowAlias,
1515
'card' | 'transparent'
1616
>;
1717

1818
type CardBackgroundColorTokenScale = Extract<
19-
BackgroundColorTokenScale,
19+
ColorsTokenName,
2020
'surface' | 'surface-subdued'
2121
>;
2222

23-
type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
24-
2523
export interface AlphaCardProps {
2624
/** Elements to display inside card */
2725
children?: React.ReactNode;
2826
backgroundColor?: CardBackgroundColorTokenScale;
2927
hasBorderRadius?: boolean;
3028
elevation?: CardElevationTokensScale;
31-
padding?: SpacingTokenScale;
32-
roundedAbove?: Breakpoint;
29+
padding?: SpacingSpaceScale;
30+
roundedAbove?: BreakpointsAlias;
3331
}
3432

3533
export const AlphaCard = ({
@@ -41,7 +39,7 @@ export const AlphaCard = ({
4139
roundedAbove,
4240
}: AlphaCardProps) => {
4341
const breakpoints = useBreakpoints();
44-
const defaultBorderRadius = '2' as BorderRadiusTokenScale;
42+
const defaultBorderRadius = '2' as ShapeBorderRadiusScale;
4543

4644
let hasBorderRadius = !roundedAbove && hasBorderRadiusProp;
4745

polaris-react/src/components/AlphaStack/AlphaStack.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
import React from 'react';
2-
import type {spacing} from '@shopify/polaris-tokens';
2+
import type {SpacingSpaceScale} from '@shopify/polaris-tokens';
33

44
import {classNames} from '../../utilities/css';
55

66
import styles from './AlphaStack.scss';
77

8-
type SpacingTokenGroup = typeof spacing;
9-
type SpacingTokenName = keyof SpacingTokenGroup;
10-
11-
// TODO: Bring this logic into tokens
12-
type Spacing = SpacingTokenName extends `space-${infer Scale}` ? Scale : never;
13-
148
type Align = 'start' | 'end' | 'center';
159

1610
export interface AlphaStackProps {
1711
/** Elements to display inside stack */
1812
children?: React.ReactNode;
1913
/** Adjust spacing between elements */
20-
spacing?: Spacing;
14+
spacing?: SpacingSpaceScale;
2115
/** Adjust vertical alignment of elements */
2216
align?: Align;
2317
}

polaris-react/src/components/Bleed/Bleed.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
import React from 'react';
2-
import type {spacing} from '@shopify/polaris-tokens';
2+
import type {SpacingSpaceScale} from '@shopify/polaris-tokens';
33

44
import {sanitizeCustomProperties} from '../../utilities/css';
55

66
import styles from './Bleed.scss';
77

8-
type SpacingTokenName = keyof typeof spacing;
9-
10-
// TODO: Bring this logic into tokens
11-
type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
12-
? Scale
13-
: never;
14-
158
interface Spacing {
16-
bottom: SpacingTokenScale;
17-
left: SpacingTokenScale;
18-
right: SpacingTokenScale;
19-
top: SpacingTokenScale;
9+
bottom: SpacingSpaceScale;
10+
left: SpacingSpaceScale;
11+
right: SpacingSpaceScale;
12+
top: SpacingSpaceScale;
2013
}
2114

2215
export interface BleedProps {
2316
/** Elements to display inside tile */
2417
children: React.ReactNode;
25-
spacing?: SpacingTokenScale;
26-
horizontal?: SpacingTokenScale;
27-
vertical?: SpacingTokenScale;
28-
top?: SpacingTokenScale;
29-
bottom?: SpacingTokenScale;
30-
left?: SpacingTokenScale;
31-
right?: SpacingTokenScale;
18+
spacing?: SpacingSpaceScale;
19+
horizontal?: SpacingSpaceScale;
20+
vertical?: SpacingSpaceScale;
21+
top?: SpacingSpaceScale;
22+
bottom?: SpacingSpaceScale;
23+
left?: SpacingSpaceScale;
24+
right?: SpacingSpaceScale;
3225
}
3326

3427
export const Bleed = ({

polaris-react/src/components/Box/Box.tsx

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import React, {createElement, forwardRef, ReactNode} from 'react';
2-
import type {colors, depth, shape, spacing} from '@shopify/polaris-tokens';
2+
import type {
3+
ColorsTokenName,
4+
DepthShadowAlias,
5+
ShapeTokenName,
6+
SpacingSpaceScale,
7+
} from '@shopify/polaris-tokens';
38

49
import {classNames, sanitizeCustomProperties} from '../../utilities/css';
510

611
import styles from './Box.scss';
712

8-
type ColorsTokenName = keyof typeof colors;
9-
export type BackgroundColorTokenScale = Extract<
13+
type BackgroundColorTokenScale = Extract<
1014
ColorsTokenName,
1115
| 'background'
1216
| `background-${string}`
@@ -18,13 +22,6 @@ export type BackgroundColorTokenScale = Extract<
1822
>;
1923
type ColorTokenScale = Extract<ColorsTokenName, 'text' | `text-${string}`>;
2024

21-
type DepthTokenName = keyof typeof depth;
22-
type ShadowsTokenName = Exclude<DepthTokenName, `shadows-${string}`>;
23-
export type DepthTokenScale = ShadowsTokenName extends `shadow-${infer Scale}`
24-
? Scale
25-
: never;
26-
27-
type ShapeTokenName = keyof typeof shape;
2825
type BorderShapeTokenScale = ShapeTokenName extends `border-${infer Scale}`
2926
? Scale
3027
: never;
@@ -40,7 +37,7 @@ interface Border {
4037
top: BorderTokenScale;
4138
}
4239

43-
export type BorderRadiusTokenScale = Extract<
40+
type BorderRadiusTokenScale = Extract<
4441
BorderShapeTokenScale,
4542
`radius-${string}`
4643
> extends `radius-${infer Scale}`
@@ -54,16 +51,11 @@ interface BorderRadius {
5451
topRight: BorderRadiusTokenScale;
5552
}
5653

57-
type SpacingTokenName = keyof typeof spacing;
58-
export type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
59-
? Scale
60-
: never;
61-
6254
interface Spacing {
63-
bottom: SpacingTokenScale;
64-
left: SpacingTokenScale;
65-
right: SpacingTokenScale;
66-
top: SpacingTokenScale;
55+
bottom: SpacingSpaceScale;
56+
left: SpacingSpaceScale;
57+
right: SpacingSpaceScale;
58+
top: SpacingSpaceScale;
6759
}
6860

6961
type Element = 'div' | 'span';
@@ -98,29 +90,29 @@ export interface BoxProps {
9890
/** Color of children */
9991
color?: ColorTokenScale;
10092
/** Spacing outside of container */
101-
margin?: SpacingTokenScale;
93+
margin?: SpacingSpaceScale;
10294
/** Bottom spacing outside of container */
103-
marginBottom?: SpacingTokenScale;
95+
marginBottom?: SpacingSpaceScale;
10496
/** Left spacing outside of container */
105-
marginLeft?: SpacingTokenScale;
97+
marginLeft?: SpacingSpaceScale;
10698
/** Right spacing outside of container */
107-
marginRight?: SpacingTokenScale;
99+
marginRight?: SpacingSpaceScale;
108100
/** Top spacing outside of container */
109-
marginTop?: SpacingTokenScale;
101+
marginTop?: SpacingSpaceScale;
110102
/** Maximum width of container */
111103
maxWidth?: string;
112104
/** Spacing around children */
113-
padding?: SpacingTokenScale;
105+
padding?: SpacingSpaceScale;
114106
/** Bottom spacing around children */
115-
paddingBottom?: SpacingTokenScale;
107+
paddingBottom?: SpacingSpaceScale;
116108
/** Left spacing around children */
117-
paddingLeft?: SpacingTokenScale;
109+
paddingLeft?: SpacingSpaceScale;
118110
/** Right spacing around children */
119-
paddingRight?: SpacingTokenScale;
111+
paddingRight?: SpacingSpaceScale;
120112
/** Top spacing around children */
121-
paddingTop?: SpacingTokenScale;
113+
paddingTop?: SpacingSpaceScale;
122114
/** Shadow */
123-
shadow?: DepthTokenScale;
115+
shadow?: DepthShadowAlias;
124116
}
125117

126118
export const Box = forwardRef<HTMLElement, BoxProps>(

polaris-react/src/components/Columns/Columns.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import React from 'react';
2-
import type {spacing} from '@shopify/polaris-tokens';
2+
import type {
3+
BreakpointsAlias,
4+
SpacingSpaceScale,
5+
} from '@shopify/polaris-tokens';
36

47
import {sanitizeCustomProperties} from '../../utilities/css';
58

69
import styles from './Columns.scss';
710

8-
type Breakpoints = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
9-
type SpacingName = keyof typeof spacing;
10-
type SpacingScale = SpacingName extends `space-${infer Scale}` ? Scale : never;
11-
1211
type Columns = {
13-
[Breakpoint in Breakpoints]?: number | string;
12+
[Breakpoint in BreakpointsAlias]?: number | string;
1413
};
1514

1615
type Gap = {
17-
[Breakpoint in Breakpoints]?: SpacingScale;
16+
[Breakpoint in BreakpointsAlias]?: SpacingSpaceScale;
1817
};
1918

2019
export interface ColumnsProps {

polaris-react/src/components/Inline/Inline.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import React from 'react';
2-
import type {spacing} from '@shopify/polaris-tokens';
2+
import type {SpacingSpaceScale} from '@shopify/polaris-tokens';
33

44
import {elementChildren} from '../../utilities/components';
55

66
import styles from './Inline.scss';
77

8-
type SpacingTokenGroup = typeof spacing;
9-
type SpacingTokenName = keyof SpacingTokenGroup;
10-
11-
// TODO: Bring this logic into tokens
12-
type Spacing = SpacingTokenName extends `space-${infer Scale}` ? Scale : never;
13-
148
const AlignY = {
159
top: 'start',
1610
center: 'center',
@@ -26,7 +20,7 @@ export interface InlineProps {
2620
/** Wrap stack elements to additional rows as needed on small screens (Defaults to true) */
2721
wrap?: boolean;
2822
/** Adjust spacing between elements */
29-
spacing?: Spacing;
23+
spacing?: SpacingSpaceScale;
3024
/** Adjust vertical alignment of elements */
3125
alignY?: keyof typeof AlignY;
3226
/** Adjust horizontal alignment of elements */

polaris-tokens/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export type {
1515

1616
export type {ColorsTokenGroup, ColorsTokenName} from './token-groups/colors';
1717

18-
export type {DepthTokenGroup, DepthTokenName} from './token-groups/depth';
18+
export type {
19+
DepthTokenGroup,
20+
DepthTokenName,
21+
DepthShadowAlias,
22+
} from './token-groups/depth';
1923

2024
export type {
2125
FontTokenGroup,

polaris-tokens/src/token-groups/depth.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ export const depth = {
4444

4545
export type DepthTokenGroup = TokenGroup<typeof depth>;
4646
export type DepthTokenName = keyof DepthTokenGroup;
47+
48+
// temporary until shadows prefix is removed
49+
type ShadowsTokenName = Exclude<DepthTokenName, `shadows-${string}`>;
50+
51+
// e.g. "transparent" | "faint" | "base" | "deep" | ...
52+
export type DepthShadowAlias = ShadowsTokenName extends `shadow-${infer Scale}`
53+
? Scale
54+
: never;

0 commit comments

Comments
 (0)