Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Avatar] Remove customer prop and experiemntal values #10283

Merged
merged 24 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6149589
[Avatar] Remove customer prop and experiemntal values
sam-b-rose Aug 30, 2023
2c5d5c8
[Avatar] Add v12 migration
sam-b-rose Aug 30, 2023
fe62b81
Update Avatar migration
sam-b-rose Aug 31, 2023
943787a
Apply v12 Avatar migration on polaris-react
sam-b-rose Aug 31, 2023
55d58ab
Apply v12 Avatar migration on polaris.shopify.com
sam-b-rose Aug 31, 2023
07c07ef
Apply v12 Avatar migration on stories
sam-b-rose Aug 31, 2023
9246e09
Update the migration guide
sam-b-rose Aug 31, 2023
0f8c1ac
revert changes to `removeJSXAttributes`
aveline Sep 5, 2023
8792c1c
Target opening element for removeJSXAttribute utility
sam-b-rose Sep 13, 2023
1a58fef
Move up the migration command in the guide
sam-b-rose Sep 13, 2023
035a93e
Fix migration mapping
sam-b-rose Sep 13, 2023
f32c326
Revert "Apply v12 Avatar migration on polaris-react"
sam-b-rose Sep 13, 2023
bc18c85
Revert "Apply v12 Avatar migration on polaris.shopify.com"
sam-b-rose Sep 13, 2023
fc8590c
Revert "Apply v12 Avatar migration on stories"
sam-b-rose Sep 13, 2023
7450aac
Apply migration to polaris-react and stories
sam-b-rose Sep 13, 2023
592c2d4
Apply migration to polaris.shopify.com
sam-b-rose Sep 13, 2023
a617b7d
Fix migration guide formatting and changeset
sam-b-rose Sep 13, 2023
16a5f85
Use t-shirt naming for Avatar sizes
sam-b-rose Sep 14, 2023
e383d36
Apply migration to polaris.shopify.com with updated mappings
sam-b-rose Sep 14, 2023
c3a1c57
Apply migration to polaris-react stories with updated mapping
sam-b-rose Sep 14, 2023
2c22fc4
Fix remaining Avatar size migrations
sam-b-rose Sep 14, 2023
3e58746
Map 2xl-experimental to xl
sam-b-rose Sep 14, 2023
c494e75
Fix migration and remove 2xl from stories
sam-b-rose Sep 14, 2023
f7fafd5
Fix Avatar tests
sam-b-rose Sep 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/hot-apples-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@shopify/polaris': major
---

Renamed `size` prop values for the Avatar component. See the following table for the new prop mappings.

| Before | After |
| ------------------------- | ----------- |
| `size="extraSmall"` | `size="xs"` |
| `size="small"` | `size="sm"` |
| `size="medium"` | `size="md"` |
| `size="large"` | `size="lg"` |
| `size="xl-experimental"` | `size="xl"` |
| `size="2xl-experimental"` | `size="xl"` |
16 changes: 16 additions & 0 deletions documentation/guides/migrating-from-v11-to-v12.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ Polaris v12.0.0 ([full release notes](https://github.com/Shopify/polaris/release

## Quick migration guide

**Avatar**

`npx @shopify/polaris-migrator v12-react-avatar-component <path>`

- Remove the `customer` prop
- Replace the `size` prop with the new mapping below

| Before | After |
| ------------------------- | ----------- |
| `size="extraSmall"` | `size="xs"` |
| `size="small"` | `size="sm"` |
| `size="medium"` | `size="md"` |
| `size="large"` | `size="lg"` |
| `size="xl-experimental"` | `size="xl"` |
| `size="2xl-experimental"` | `size="xl"` |

**Badge**

`npx @shopify/polaris-migrator react-rename-component-prop <path> --componentName="Badge" --from="status" --to="tone"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type {API, FileInfo, Options} from 'jscodeshift';
import type {
API,
FileInfo,
Options,
ArrayExpression,
ASTPath,
} from 'jscodeshift';

import {POLARIS_MIGRATOR_COMMENT} from '../../utilities/constants';
import {
Expand Down Expand Up @@ -69,15 +75,12 @@ export default function transformer(
return true;
})
.find(j.ArrayExpression)
.replaceWith((nodePath) => {
.replaceWith((nodePath: ASTPath<ArrayExpression>) => {
const arrayOfBreadcrumbs = nodePath.node.elements;

if (arrayOfBreadcrumbs.length === 0) {
removeJSXAttributes(
j,
nodePath.parentPath.parentPath.parentPath.parentPath,
'breadcrumbs',
);
const element = j(nodePath).closest(j.JSXElement).paths()[0];
removeJSXAttributes(j, element, 'breadcrumbs');
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {check} from '../../../utilities/check';

const transform = 'v12-react-avatar-component';
const fixtures = ['v12-react-avatar-component'];

for (const fixture of fixtures) {
check(__dirname, {
fixture,
transform,
options: {
relative: fixture.includes('relative') ? true : undefined,
},
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import {Avatar} from '@shopify/polaris';

export function App() {
return (
<>
<Avatar customer size="unknown" />
<Avatar customer size="extraSmall" />
<Avatar customer size="small" />
<Avatar customer size="medium" />
<Avatar customer size="large" />
<Avatar size="xl-experimental" />
<Avatar size="2xl-experimental" />
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import {Avatar} from '@shopify/polaris';

export function App() {
return (
<>
<Avatar size="unknown" />
<Avatar size="xs" />
<Avatar size="sm" />
<Avatar size="md" />
<Avatar size="lg" />
<Avatar size="xl" />
<Avatar size="xl" />
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type {API, FileInfo, Options} from 'jscodeshift';

import {
getImportSpecifierName,
hasImportDeclaration,
hasImportSpecifier,
normalizeImportSourcePaths,
} from '../../utilities/imports';
import {removeJSXAttributes, replaceJSXAttributes} from '../../utilities/jsx';

export interface MigrationOptions extends Options {
relative?: boolean;
}

export default function transformer(
file: FileInfo,
{jscodeshift: j}: API,
options: MigrationOptions,
) {
const source = j(file.source);

if (
!options.relative &&
!hasImportDeclaration(j, source, '@shopify/polaris')
) {
return file.source;
}

const sourcePaths = normalizeImportSourcePaths(j, source, {
relative: options.relative,
from: 'Avatar',
to: 'Avatar',
});

if (!sourcePaths) return;
if (
!hasImportSpecifier(j, source, 'Avatar', sourcePaths.from) &&
!hasImportSpecifier(j, source, 'AvatarProps', sourcePaths.from)
) {
return;
}

const localElementName =
getImportSpecifierName(j, source, 'Avatar', sourcePaths.from) || 'Avatar';

// Find all JSX elements with the name 'Avatar'
source.findJSXElements(localElementName).forEach((element) => {
// Remove the 'customer' prop
removeJSXAttributes(j, element, 'customer');
// Replace the 'size' prop value with the new size
replaceJSXAttributes(j, element, 'size', 'size', sizeMapping);
});

return source.toSource();
}

// Define the mapping of old sizes to new sizes
const sizeMapping = {
extraSmall: 'xs',
small: 'sm',
medium: 'md',
large: 'lg',
'xl-experimental': 'xl',
// 2xl-experimental is not supported in the new Avatar component
'2xl-experimental': 'xl',
};
18 changes: 13 additions & 5 deletions polaris-migrator/src/utilities/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function removeJSXAttributes(
element: ASTPath<JSXElement>,
attributeName: string,
) {
const jsxAttributes = element.value.attributes?.filter(
const jsxAttributes = element.value.openingElement?.attributes?.filter(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === attributeName,
);

Expand Down Expand Up @@ -97,10 +97,18 @@ export function replaceJSXAttributes(
j(attribute)
.find(j.StringLiteral)
.forEach((literal) => {
literal.node.value =
typeof newAttributeValue === 'string'
? newAttributeValue
: newAttributeValue[literal.node.value];
const isStringLiteral = typeof newAttributeValue === 'string';

if (isStringLiteral) {
literal.node.value = newAttributeValue;
return;
}

const value = literal.node.value as string;

if (value in newAttributeValue) {
literal.node.value = newAttributeValue[literal.node.value];
}
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export function WithAPrefixAndASuffix() {
},
{
content: 'Or there',
prefix: <Avatar customer name="Farrah" size="small" />,
prefix: <Avatar name="Farrah" size="sm" />,
suffix: <Icon source={ChevronRightMinor} />,
},
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function Default(_, context) {
]}
renderItem={(item) => {
const {id, url, name, location} = item;
const media = <Avatar customer size="medium" name={name} />;
const media = <Avatar size="md" name={name} />;

return (
<ResourceList.Item id={id} url={url} media={media}>
Expand Down Expand Up @@ -121,7 +121,7 @@ export function WithI18n(_, context) {
]}
renderItem={(item) => {
const {id, url, name, location} = item;
const media = <Avatar customer size="medium" name={name} />;
const media = <Avatar size="md" name={name} />;

return (
<ResourceList.Item id={id} url={url} media={media}>
Expand Down
40 changes: 16 additions & 24 deletions polaris-react/src/components/Avatar/Avatar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

.Avatar {
// stylelint-disable -- Polaris component custom properties
--pc-avatar-extra-small-size: 20px;
--pc-avatar-small-size: 24px;
--pc-avatar-medium-size: 28px;
--pc-avatar-large-size: 32px;
--pc-avatar-xl-size-experimental: 40px;
--pc-avatar-2xl-size-experimental: 54px;
--pc-avatar-xs-size: 20px;
--pc-avatar-sm-size: 24px;
--pc-avatar-md-size: 28px;
--pc-avatar-lg-size: 32px;
--pc-avatar-xl-size: 40px;
// stylelint-enable
position: relative;
display: block;
overflow: hidden;
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
min-width: var(--pc-avatar-extra-small-size);
min-width: var(--pc-avatar-xs-size);
max-width: 100%;
background: var(--p-color-avatar-background-experimental);
color: var(--p-color-avatar-color-experimental);
Expand Down Expand Up @@ -48,48 +47,41 @@
visibility: hidden;
}

.sizeExtraSmall {
.sizeXs {
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
width: var(--pc-avatar-extra-small-size);
width: var(--pc-avatar-xs-size);
// stylelint-disable-next-line -- custom overrides
border-radius: 4px;
}

.sizeSmall {
.sizeSm {
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
width: var(--pc-avatar-small-size);
width: var(--pc-avatar-sm-size);
// stylelint-disable-next-line -- custom overrides
border-radius: 6px;
}

.sizeMedium {
.sizeMd {
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
width: var(--pc-avatar-medium-size);
width: var(--pc-avatar-md-size);
// stylelint-disable-next-line -- custom overrides
border-radius: 6px;
}

.sizeLarge {
.sizeLg {
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
width: var(--pc-avatar-large-size);
width: var(--pc-avatar-lg-size);
// stylelint-disable-next-line -- custom overrides
border-radius: 8px;
}

.sizeXl-experimental {
.sizeXl {
// stylelint-disable-next-line -- custom overrides
width: var(--pc-avatar-xl-size-experimental);
width: var(--pc-avatar-xl-size);
// stylelint-disable-next-line -- custom overrides
border-radius: 8px;
}

.size2xl-experimental {
// stylelint-disable-next-line -- custom overrides
width: var(--pc-avatar-2xl-size-experimental);
// stylelint-disable-next-line -- custom overrides
border-radius: 10px;
}

.styleOne {
background: var(--p-color-avatar-style-one-background-experimental);
color: var(--p-color-avatar-style-one-color-experimental);
Expand Down
Loading