Skip to content

Commit a4a28a7

Browse files
chazdeanlaurkim
authored andcommitted
[Layout foundations] Add breakpoint configuration to Tile component (#7286)
<!-- ☝️How to write a good PR title: - Prefix it with [ComponentName] (if applicable), for example: [Button] - Start with a verb, for example: Add, Delete, Improve, Fix… - Give as much context as necessary and as little as possible - Prefix it with [WIP] while it’s a work in progress --> ### WHY are these changes introduced? Fixes #7285 <!-- link to issue if one exists --> <!-- Context about the problem that’s being addressed. --> ### WHAT is this pull request doing? Adds ability for `Tile` to use breakpoints <details> <summary>Tile breakpoints example</summary> <img src="https://user-images.githubusercontent.com/59836805/192546149-89e03625-0d1f-433b-a756-b277df415c85.gif" alt="Description of what the gif shows"> </details> <!-- Summary of the changes committed. Before / after screenshots are appreciated for UI changes. Make sure to include alt text that describes the screenshot. If you include an animated gif showing your change, wrapping it in a details tag is recommended. Gifs usually autoplay, which can cause accessibility issues for people reviewing your PR: <details> <summary>Summary of your gif(s)</summary> <img src="..." alt="Description of what the gif shows"> </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 from 'react'; import {Page, Tile, Text} from '../src'; const styles = { background: 'var(--p-surface)', border: 'var(--p-border-base)', borderRadius: 'var(--p-border-radius-2)', padding: 'var(--p-space-4)', }; export function Playground() { const children = Array.from(Array(10)).map((ele, index) => ( <div key={index} style={styles}> <Text as="h2" variant="headingMd"> Sales </Text> <Text as="p" variant="bodyMd"> View a summary of your online store’s sales. </Text> </div> )); return ( <Page title="Playground"> <Tile columns={{xs: 2, sm: 3, md: 4, lg: 5, xl: 6}} gap={{xs: '2'}}> {children} </Tile> </Page> ); } ``` </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
1 parent 6188a0b commit a4a28a7

File tree

7 files changed

+696
-833
lines changed

7 files changed

+696
-833
lines changed
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
@import '../../styles/common';
22

33
.Tile {
4+
--pc-tile-xs: 6;
5+
--pc-tile-sm: var(--pc-tile-xs);
6+
--pc-tile-md: var(--pc-tile-sm);
7+
--pc-tile-lg: var(--pc-tile-md);
8+
--pc-tile-xl: var(--pc-tile-lg);
9+
--pc-tile-gap-xs: var(--p-space-4);
10+
--pc-tile-gap-sm: var(--pc-tile-gap-xs);
11+
--pc-tile-gap-md: var(--pc-tile-gap-sm);
12+
--pc-tile-gap-lg: var(--pc-tile-gap-md);
13+
--pc-tile-gap-xl: var(--pc-tile-gap-lg);
414
display: grid;
5-
grid-template-columns: var(--pc-tile-column-number);
6-
gap: var(--pc-tile-spacing);
15+
gap: var(--pc-tile-gap-xs);
16+
grid-template-columns: var(--pc-tile-xs);
17+
18+
@media #{$p-breakpoints-sm-up} {
19+
gap: var(--pc-tile-gap-sm);
20+
grid-template-columns: var(--pc-tile-sm);
21+
}
22+
23+
@media #{$p-breakpoints-md-up} {
24+
gap: var(--pc-tile-gap-md);
25+
grid-template-columns: var(--pc-tile-md);
26+
}
27+
28+
@media #{$p-breakpoints-lg-up} {
29+
gap: var(--pc-tile-gap-lg);
30+
grid-template-columns: var(--pc-tile-lg);
31+
}
32+
33+
@media #{$p-breakpoints-xl-up} {
34+
gap: var(--pc-tile-gap-xl);
35+
grid-template-columns: var(--pc-tile-xl);
36+
}
737
}

polaris-react/src/components/Tile/Tile.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ const children = Array.from(Array(4)).map((ele, index) => (
2626

2727
export function Default() {
2828
return (
29-
<Tile spacing="1" columns="2">
29+
<Tile columns={{xs: 2}} gap={{xs: '2'}}>
3030
{children}
3131
</Tile>
3232
);
3333
}
3434

3535
export function LargeSpacing() {
3636
return (
37-
<Tile spacing="10" columns="2">
37+
<Tile columns={{xs: 2}} gap={{xs: '10'}}>
3838
{children}
3939
</Tile>
4040
);
@@ -53,7 +53,7 @@ export function ManyColumns() {
5353
));
5454

5555
return (
56-
<Tile spacing="3" columns="5">
56+
<Tile columns={{xs: 5}} gap={{xs: '2'}}>
5757
{children}
5858
</Tile>
5959
);
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
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 styles from './Tile.scss';
58

6-
type SpacingTokenName = keyof typeof spacing;
7-
8-
// TODO: Bring this logic into tokens
9-
type Spacing = SpacingTokenName extends `space-${infer Scale}` ? Scale : never;
10-
11-
type Columns =
12-
| '1'
13-
| '2'
14-
| '3'
15-
| '4'
16-
| '5'
17-
| '6'
18-
| '7'
19-
| '8'
20-
| '9'
21-
| '10'
22-
| '11'
23-
| '12';
9+
type Columns = {
10+
[Breakpoint in BreakpointsAlias]?: number | string;
11+
};
12+
13+
type Gap = {
14+
[Breakpoint in BreakpointsAlias]?: SpacingSpaceScale;
15+
};
2416

2517
export interface TileProps {
2618
/** Elements to display inside tile */
2719
children: React.ReactNode;
2820
/** Adjust spacing between elements */
29-
spacing: Spacing;
21+
gap?: Gap;
3022
/** Adjust number of columns */
31-
columns: Columns;
23+
columns?: Columns;
3224
}
3325

34-
export const Tile = ({children, spacing, columns}: TileProps) => {
26+
export const Tile = ({children, gap, columns}: TileProps) => {
3527
const style = {
36-
'--pc-tile-column-number': `repeat(${columns}, 1fr)`,
37-
'--pc-tile-spacing': `var(--p-space-${spacing})`,
28+
'--pc-tile-gap-xs': gap?.xs ? `var(--p-space-${gap?.xs})` : undefined,
29+
'--pc-tile-gap-sm': gap?.sm ? `var(--p-space-${gap?.sm})` : undefined,
30+
'--pc-tile-gap-md': gap?.md ? `var(--p-space-${gap?.md})` : undefined,
31+
'--pc-tile-gap-lg': gap?.lg ? `var(--p-space-${gap?.lg})` : undefined,
32+
'--pc-tile-gap-xl': gap?.xl ? `var(--p-space-${gap?.xl})` : undefined,
33+
'--pc-tile-xs': formatColumns(columns?.xs),
34+
'--pc-tile-sm': formatColumns(columns?.sm),
35+
'--pc-tile-md': formatColumns(columns?.md),
36+
'--pc-tile-lg': formatColumns(columns?.lg),
37+
'--pc-tile-xl': formatColumns(columns?.xl),
3838
} as React.CSSProperties;
3939

4040
return (
@@ -43,3 +43,9 @@ export const Tile = ({children, spacing, columns}: TileProps) => {
4343
</div>
4444
);
4545
};
46+
47+
function formatColumns(columns?: number | string) {
48+
if (!columns) return undefined;
49+
50+
return typeof columns === 'number' ? `repeat(${columns}, 1fr)` : columns;
51+
}

polaris-react/src/components/Tile/tests/Tile.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Children = () => <p>This is a tile</p>;
88
describe('<Tile />', () => {
99
it('renders children', () => {
1010
const tile = mountWithApp(
11-
<Tile spacing="1" columns="1">
11+
<Tile columns={{xs: 2, sm: 2, md: 2, lg: 2, xl: 2}} gap={{xs: '2'}}>
1212
<Children />
1313
</Tile>,
1414
);
@@ -18,15 +18,16 @@ describe('<Tile />', () => {
1818

1919
it('uses custom properties when passed in', () => {
2020
const tile = mountWithApp(
21-
<Tile spacing="1" columns="2">
21+
<Tile columns={{xs: 2, lg: 2}} gap={{xs: '2'}}>
2222
<Children />
2323
</Tile>,
2424
);
2525

2626
expect(tile).toContainReactComponent('div', {
2727
style: {
28-
'--pc-tile-column-number': 'repeat(2, 1fr)',
29-
'--pc-tile-spacing': 'var(--p-space-1)',
28+
'--pc-tile-xs': 'repeat(2, 1fr)',
29+
'--pc-tile-lg': 'repeat(2, 1fr)',
30+
'--pc-tile-gap-xs': 'var(--p-space-2)',
3031
} as React.CSSProperties,
3132
});
3233
});

polaris.shopify.com/pages/examples/tile-with-columns.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const children = Array.from(Array(8)).map((ele, index) => (
2424
function TileWithColumnsExample() {
2525
return (
2626
<div style={{width: '500px'}}>
27-
<Tile columns="4" spacing="2">
27+
<Tile columns={{xs: 4}} gap={{xs: '2'}}>
2828
{children}
2929
</Tile>
3030
</div>

polaris.shopify.com/pages/examples/tile-with-spacing.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const children = Array.from(Array(2)).map((ele, index) => (
2424
function TileWithSpacingExample() {
2525
return (
2626
<div style={{width: '500px'}}>
27-
<Tile columns="1" spacing="5">
27+
<Tile columns={{xs: 1}} gap={{xs: '5'}}>
2828
{children}
2929
</Tile>
3030
</div>

0 commit comments

Comments
 (0)