Skip to content

Commit 3d938fc

Browse files
laurkimkyledurand
andauthored
[Layout foundations] Refactor Box and update Toast to use Box (#7279)
### WHY are these changes introduced? Resolves #7134. [Storybook URL](https://5d559397bae39100201eedc1-bongofonfc.chromatic.com/?path=/story/all-components-toast--default). An earlier [prototype](#7096) explored adding `className` support to the `Box` component and keeping it as an internal component. This prototype explores keeping `Box` external without the `className` prop and replacing custom divs in `Toast` with the `Box` component. Even if we don't update `Toast` yet, there are some commits in this PR I'd like to keep that cleans up some of the types and prop descriptions in the `Box` component. ### WHAT is this pull request doing? - Cleans up types and updates prop descriptions in `Box` - Adds `color` and `maxWidth` support to `Box` - Updates `Toast` to use `Box` where custom `div` are being used <details> <summary>Toast original</summary> <img src="https://user-images.githubusercontent.com/26749317/192353275-5ca804db-0873-4e13-9061-59ee13425538.gif" alt="Toast original"> </details> <details> <summary>Toast refactored with Box</summary> <img src="https://user-images.githubusercontent.com/26749317/192353264-6eff870c-ddfb-4583-8d57-6a93f15daf94.gif" alt="Toast refactored with Box"> </details> <!-- ℹ️ Delete the following for small / trivial changes --> ### How to 🎩 🖥 [Local development instructions](https://github.com/Shopify/polaris/blob/main/README.md#local-development) 🗒 [General tophatting guidelines](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md) 📄 [Changelog guidelines](https://github.com/Shopify/polaris/blob/main/.github/CONTRIBUTING.md#changelog) <!-- Give as much information as needed to experiment with the component in the playground. --> <details> <summary>Copy-paste this code in <code>playground/Playground.tsx</code>:</summary> ```jsx import React, {useState, useCallback} from 'react'; import {Page, Frame, Toast, Button} from '../src'; export function Playground() { const [active, setActive] = useState(false); const toggleActive = useCallback(() => setActive((active) => !active), []); const toastMarkup = active ? ( <Toast content="It's toasty" onDismiss={toggleActive} /> ) : null; return ( <Frame> <Page title="Toast example using Box"> <Button onClick={toggleActive}>Show Toast (with 📦)</Button> {toastMarkup} </Page> </Frame> ); } ``` </details> ### 🎩 checklist - [ ] Tested on [mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing) - [ ] Tested on [multiple browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers) - [ ] Tested for [accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md) - [ ] Updated the component's `README.md` with documentation changes - [ ] [Tophatted documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md) changes in the style guide Co-authored-by: Kyle Durand <[email protected]>
1 parent be070cc commit 3d938fc

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
--pc-box-border-left: initial;
1111
--pc-box-border-right: initial;
1212
--pc-box-border-top: initial;
13+
--pc-box-color: initial;
1314
--pc-box-margin-bottom: initial;
1415
--pc-box-margin-left: initial;
1516
--pc-box-margin-right: initial;
1617
--pc-box-margin-top: initial;
18+
--pc-box-max-width: initial;
1719
--pc-box-padding-bottom: initial;
1820
--pc-box-padding-left: initial;
1921
--pc-box-padding-right: initial;
@@ -28,10 +30,12 @@
2830
border-left: var(--pc-box-border-left);
2931
border-right: var(--pc-box-border-right);
3032
border-top: var(--pc-box-border-top);
33+
color: var(--pc-box-color);
3134
margin-bottom: var(--pc-box-margin-bottom);
3235
margin-left: var(--pc-box-margin-left);
3336
margin-right: var(--pc-box-margin-right);
3437
margin-top: var(--pc-box-margin-top);
38+
max-width: var(--pc-box-max-width);
3539
padding-bottom: var(--pc-box-padding-bottom);
3640
padding-left: var(--pc-box-padding-left);
3741
padding-right: var(--pc-box-padding-right);

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

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {classNames, sanitizeCustomProperties} from '../../utilities/css';
55

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

8-
type ColorsTokenGroup = typeof colors;
9-
type ColorsTokenName = keyof ColorsTokenGroup;
8+
type ColorsTokenName = keyof typeof colors;
109
export type BackgroundColorTokenScale = Extract<
1110
ColorsTokenName,
1211
| 'background'
@@ -15,23 +14,20 @@ export type BackgroundColorTokenScale = Extract<
1514
| `surface-${string}`
1615
| 'backdrop'
1716
| 'overlay'
17+
| `action-${string}`
1818
>;
19+
type ColorTokenScale = Extract<ColorsTokenName, 'text' | `text-${string}`>;
1920

20-
type DepthTokenGroup = typeof depth;
21-
type DepthTokenName = keyof DepthTokenGroup;
21+
type DepthTokenName = keyof typeof depth;
2222
type ShadowsTokenName = Exclude<DepthTokenName, `shadows-${string}`>;
23-
2423
export type DepthTokenScale = ShadowsTokenName extends `shadow-${infer Scale}`
2524
? Scale
2625
: never;
2726

28-
type ShapeTokenGroup = typeof shape;
29-
type ShapeTokenName = keyof ShapeTokenGroup;
30-
27+
type ShapeTokenName = keyof typeof shape;
3128
type BorderShapeTokenScale = ShapeTokenName extends `border-${infer Scale}`
3229
? Scale
3330
: never;
34-
3531
type BorderTokenScale = Exclude<
3632
BorderShapeTokenScale,
3733
`radius-${string}` | `width-${string}`
@@ -58,10 +54,7 @@ interface BorderRadius {
5854
topRight: BorderRadiusTokenScale;
5955
}
6056

61-
type SpacingTokenGroup = typeof spacing;
62-
type SpacingTokenName = keyof SpacingTokenGroup;
63-
64-
// TODO: Bring this logic into tokens
57+
type SpacingTokenName = keyof typeof spacing;
6558
export type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
6659
? Scale
6760
: never;
@@ -73,53 +66,60 @@ interface Spacing {
7366
top: SpacingTokenScale;
7467
}
7568

69+
type Element = 'div' | 'span';
70+
7671
export interface BoxProps {
77-
as?: 'div' | 'span';
78-
/** Background color of the Box */
72+
/** HTML Element type */
73+
as?: Element;
74+
/** Background color */
7975
background?: BackgroundColorTokenScale;
80-
/** Border styling of the Box */
76+
/** Border style */
8177
border?: BorderTokenScale;
82-
/** Bottom border styling of the Box */
78+
/** Bottom border style */
8379
borderBottom?: BorderTokenScale;
84-
/** Left border styling of the Box */
80+
/** Left border style */
8581
borderLeft?: BorderTokenScale;
86-
/** Right border styling of the Box */
82+
/** Right border style */
8783
borderRight?: BorderTokenScale;
88-
/** Top border styling of the Box */
84+
/** Top border style */
8985
borderTop?: BorderTokenScale;
90-
/** Border radius of the Box */
86+
/** Border radius */
9187
borderRadius?: BorderRadiusTokenScale;
92-
/** Bottom left border radius of the Box */
88+
/** Bottom left border radius */
9389
borderRadiusBottomLeft?: BorderRadiusTokenScale;
94-
/** Bottom right border radius of the Box */
90+
/** Bottom right border radius */
9591
borderRadiusBottomRight?: BorderRadiusTokenScale;
96-
/** Top left border radius of the Box */
92+
/** Top left border radius */
9793
borderRadiusTopLeft?: BorderRadiusTokenScale;
98-
/** Top right border radius of the Box */
94+
/** Top right border radius */
9995
borderRadiusTopRight?: BorderRadiusTokenScale;
100-
/** Inner content of the Box */
96+
/** Inner content */
10197
children: ReactNode;
102-
/** Spacing outside of the Box */
98+
/** Color of children */
99+
color?: ColorTokenScale;
100+
/** Spacing outside of container */
103101
margin?: SpacingTokenScale;
104-
/** Bottom spacing outside of the Box */
102+
/** Bottom spacing outside of container */
105103
marginBottom?: SpacingTokenScale;
106-
/** Left side spacing outside of the Box */
104+
/** Left spacing outside of container */
107105
marginLeft?: SpacingTokenScale;
108-
/** Right side spacing outside of the Box */
106+
/** Right spacing outside of container */
109107
marginRight?: SpacingTokenScale;
110-
/** Top spacing outside of the Box */
108+
/** Top spacing outside of container */
111109
marginTop?: SpacingTokenScale;
112-
/** Spacing inside of the Box */
110+
/** Maximum width of container */
111+
maxWidth?: string;
112+
/** Spacing around children */
113113
padding?: SpacingTokenScale;
114-
/** Bottom spacing inside of the Box */
114+
/** Bottom spacing around children */
115115
paddingBottom?: SpacingTokenScale;
116-
/** Left side spacing inside of the Box */
116+
/** Left spacing around children */
117117
paddingLeft?: SpacingTokenScale;
118-
/** Right side spacing inside of the Box */
118+
/** Right spacing around children */
119119
paddingRight?: SpacingTokenScale;
120-
/** Top spacing inside of the Box */
120+
/** Top spacing around children */
121121
paddingTop?: SpacingTokenScale;
122-
/** Shadow on the Box */
122+
/** Shadow */
123123
shadow?: DepthTokenScale;
124124
}
125125

@@ -139,11 +139,13 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
139139
borderRadiusTopLeft,
140140
borderRadiusTopRight,
141141
children,
142+
color,
142143
margin,
143144
marginBottom,
144145
marginLeft,
145146
marginRight,
146147
marginTop,
148+
maxWidth,
147149
padding,
148150
paddingBottom,
149151
paddingLeft,
@@ -221,6 +223,7 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
221223
'--pc-box-border-radius-top-right': `var(--p-border-radius-${borderRadiuses.topRight})`,
222224
}
223225
: undefined),
226+
...(color ? {'--pc-box-color': `var(--p-${color})`} : undefined),
224227
...(margins.bottom
225228
? {'--pc-box-margin-bottom': `var(--p-space-${margins.bottom})`}
226229
: undefined),
@@ -233,6 +236,7 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
233236
...(margins.top
234237
? {'--pc-box-margin-top': `var(--p-space-${margins.top})`}
235238
: undefined),
239+
...(maxWidth ? {'--pc-box-max-width': `${maxWidth}px`} : undefined),
236240
...(paddings.bottom
237241
? {'--pc-box-padding-bottom': `var(--p-space-${paddings.bottom})`}
238242
: undefined),
@@ -256,8 +260,8 @@ export const Box = forwardRef<HTMLElement, BoxProps>(
256260
as,
257261
{
258262
className,
259-
style: sanitizeCustomProperties(style),
260263
ref,
264+
style: sanitizeCustomProperties(style),
261265
},
262266
children,
263267
);

0 commit comments

Comments
 (0)