Skip to content

Commit 15352ad

Browse files
sam-b-roseaveline
andauthored
[Avatar] Remove customer prop and experiemntal values (Shopify#10283)
### WHY are these changes introduced? Fixes Shopify#9977 ### WHAT is this pull request doing? - [x] Remove `customer` boolean prop (make the default) - [x] Remove `-experimental` from `size` prop - [x] Update Avatar stories - [x] Update Avatar documentation examples - [x] Update `v12` migration guide - [x] Write migration for `Avatar` prop changes - [x] remove `customer` prop - [x] `size` prop mapping ### 🎩 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: Sam Rose <11774595+samrose3@users.noreply.github.com> Co-authored-by: aveline <aveline@users.noreply.github.com>
1 parent 54ac269 commit 15352ad

File tree

65 files changed

+336
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+336
-266
lines changed

.changeset/hot-apples-press.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@shopify/polaris': major
3+
---
4+
5+
Renamed `size` prop values for the Avatar component. See the following table for the new prop mappings.
6+
7+
| Before | After |
8+
| ------------------------- | ----------- |
9+
| `size="extraSmall"` | `size="xs"` |
10+
| `size="small"` | `size="sm"` |
11+
| `size="medium"` | `size="md"` |
12+
| `size="large"` | `size="lg"` |
13+
| `size="xl-experimental"` | `size="xl"` |
14+
| `size="2xl-experimental"` | `size="xl"` |

documentation/guides/migrating-from-v11-to-v12.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ Polaris v12.0.0 ([full release notes](https://github.com/Shopify/polaris/release
88

99
## Quick migration guide
1010

11+
**Avatar**
12+
13+
`npx @shopify/polaris-migrator v12-react-avatar-component <path>`
14+
15+
- Remove the `customer` prop
16+
- Replace the `size` prop with the new mapping below
17+
18+
| Before | After |
19+
| ------------------------- | ----------- |
20+
| `size="extraSmall"` | `size="xs"` |
21+
| `size="small"` | `size="sm"` |
22+
| `size="medium"` | `size="md"` |
23+
| `size="large"` | `size="lg"` |
24+
| `size="xl-experimental"` | `size="xl"` |
25+
| `size="2xl-experimental"` | `size="xl"` |
26+
1127
**Badge**
1228

1329
`npx @shopify/polaris-migrator react-rename-component-prop <path> --componentName="Badge" --from="status" --to="tone"`

polaris-migrator/src/migrations/v11-react-update-page-breadcrumbs/transform.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type {API, FileInfo, Options} from 'jscodeshift';
1+
import type {
2+
API,
3+
FileInfo,
4+
Options,
5+
ArrayExpression,
6+
ASTPath,
7+
} from 'jscodeshift';
28

39
import {POLARIS_MIGRATOR_COMMENT} from '../../utilities/constants';
410
import {
@@ -69,15 +75,12 @@ export default function transformer(
6975
return true;
7076
})
7177
.find(j.ArrayExpression)
72-
.replaceWith((nodePath) => {
78+
.replaceWith((nodePath: ASTPath<ArrayExpression>) => {
7379
const arrayOfBreadcrumbs = nodePath.node.elements;
7480

7581
if (arrayOfBreadcrumbs.length === 0) {
76-
removeJSXAttributes(
77-
j,
78-
nodePath.parentPath.parentPath.parentPath.parentPath,
79-
'breadcrumbs',
80-
);
82+
const element = j(nodePath).closest(j.JSXElement).paths()[0];
83+
removeJSXAttributes(j, element, 'breadcrumbs');
8184
return;
8285
}
8386

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {check} from '../../../utilities/check';
2+
3+
const transform = 'v12-react-avatar-component';
4+
const fixtures = ['v12-react-avatar-component'];
5+
6+
for (const fixture of fixtures) {
7+
check(__dirname, {
8+
fixture,
9+
transform,
10+
options: {
11+
relative: fixture.includes('relative') ? true : undefined,
12+
},
13+
});
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar customer size="unknown" />
8+
<Avatar customer size="extraSmall" />
9+
<Avatar customer size="small" />
10+
<Avatar customer size="medium" />
11+
<Avatar customer size="large" />
12+
<Avatar size="xl-experimental" />
13+
<Avatar size="2xl-experimental" />
14+
</>
15+
);
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Avatar} from '@shopify/polaris';
3+
4+
export function App() {
5+
return (
6+
<>
7+
<Avatar size="unknown" />
8+
<Avatar size="xs" />
9+
<Avatar size="sm" />
10+
<Avatar size="md" />
11+
<Avatar size="lg" />
12+
<Avatar size="xl" />
13+
<Avatar size="xl" />
14+
</>
15+
);
16+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type {API, FileInfo, Options} from 'jscodeshift';
2+
3+
import {
4+
getImportSpecifierName,
5+
hasImportDeclaration,
6+
hasImportSpecifier,
7+
normalizeImportSourcePaths,
8+
} from '../../utilities/imports';
9+
import {removeJSXAttributes, replaceJSXAttributes} from '../../utilities/jsx';
10+
11+
export interface MigrationOptions extends Options {
12+
relative?: boolean;
13+
}
14+
15+
export default function transformer(
16+
file: FileInfo,
17+
{jscodeshift: j}: API,
18+
options: MigrationOptions,
19+
) {
20+
const source = j(file.source);
21+
22+
if (
23+
!options.relative &&
24+
!hasImportDeclaration(j, source, '@shopify/polaris')
25+
) {
26+
return file.source;
27+
}
28+
29+
const sourcePaths = normalizeImportSourcePaths(j, source, {
30+
relative: options.relative,
31+
from: 'Avatar',
32+
to: 'Avatar',
33+
});
34+
35+
if (!sourcePaths) return;
36+
if (
37+
!hasImportSpecifier(j, source, 'Avatar', sourcePaths.from) &&
38+
!hasImportSpecifier(j, source, 'AvatarProps', sourcePaths.from)
39+
) {
40+
return;
41+
}
42+
43+
const localElementName =
44+
getImportSpecifierName(j, source, 'Avatar', sourcePaths.from) || 'Avatar';
45+
46+
// Find all JSX elements with the name 'Avatar'
47+
source.findJSXElements(localElementName).forEach((element) => {
48+
// Remove the 'customer' prop
49+
removeJSXAttributes(j, element, 'customer');
50+
// Replace the 'size' prop value with the new size
51+
replaceJSXAttributes(j, element, 'size', 'size', sizeMapping);
52+
});
53+
54+
return source.toSource();
55+
}
56+
57+
// Define the mapping of old sizes to new sizes
58+
const sizeMapping = {
59+
extraSmall: 'xs',
60+
small: 'sm',
61+
medium: 'md',
62+
large: 'lg',
63+
'xl-experimental': 'xl',
64+
// 2xl-experimental is not supported in the new Avatar component
65+
'2xl-experimental': 'xl',
66+
};

polaris-migrator/src/utilities/jsx.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function removeJSXAttributes(
3939
element: ASTPath<JSXElement>,
4040
attributeName: string,
4141
) {
42-
const jsxAttributes = element.value.attributes?.filter(
42+
const jsxAttributes = element.value.openingElement?.attributes?.filter(
4343
(attr) => attr.type === 'JSXAttribute' && attr.name.name === attributeName,
4444
);
4545

@@ -97,10 +97,18 @@ export function replaceJSXAttributes(
9797
j(attribute)
9898
.find(j.StringLiteral)
9999
.forEach((literal) => {
100-
literal.node.value =
101-
typeof newAttributeValue === 'string'
102-
? newAttributeValue
103-
: newAttributeValue[literal.node.value];
100+
const isStringLiteral = typeof newAttributeValue === 'string';
101+
102+
if (isStringLiteral) {
103+
literal.node.value = newAttributeValue;
104+
return;
105+
}
106+
107+
const value = literal.node.value as string;
108+
109+
if (value in newAttributeValue) {
110+
literal.node.value = newAttributeValue[literal.node.value];
111+
}
104112
});
105113
});
106114
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export function WithAPrefixAndASuffix() {
374374
},
375375
{
376376
content: 'Or there',
377-
prefix: <Avatar customer name="Farrah" size="small" />,
377+
prefix: <Avatar name="Farrah" size="sm" />,
378378
suffix: <Icon source={ChevronRightMinor} />,
379379
},
380380
]}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function Default(_, context) {
5858
]}
5959
renderItem={(item) => {
6060
const {id, url, name, location} = item;
61-
const media = <Avatar customer size="medium" name={name} />;
61+
const media = <Avatar size="md" name={name} />;
6262

6363
return (
6464
<ResourceList.Item id={id} url={url} media={media}>
@@ -121,7 +121,7 @@ export function WithI18n(_, context) {
121121
]}
122122
renderItem={(item) => {
123123
const {id, url, name, location} = item;
124-
const media = <Avatar customer size="medium" name={name} />;
124+
const media = <Avatar size="md" name={name} />;
125125

126126
return (
127127
<ResourceList.Item id={id} url={url} media={media}>

0 commit comments

Comments
 (0)