Skip to content

Commit

Permalink
[Avatar] Add back customer prop (#10584)
Browse files Browse the repository at this point in the history
Added back the `customer` prop to support the gray + customer
icon combination.
  • Loading branch information
sam-b-rose committed Sep 20, 2023
1 parent b3c83f9 commit 08a0f2f
Show file tree
Hide file tree
Showing 50 changed files with 126 additions and 107 deletions.
1 change: 0 additions & 1 deletion documentation/guides/migrating-from-v11-to-v12.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Polaris v12.0.0 ([full release notes](https://github.com/Shopify/polaris/release

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

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

| Before | After |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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 customer size="unknown" />
<Avatar customer size="xs" />
<Avatar customer size="sm" />
<Avatar customer size="md" />
<Avatar customer size="lg" />
<Avatar size="xl" />
<Avatar size="xl" />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
hasImportSpecifier,
normalizeImportSourcePaths,
} from '../../utilities/imports';
import {removeJSXAttributes, replaceJSXAttributes} from '../../utilities/jsx';
import {replaceJSXAttributes} from '../../utilities/jsx';

export interface MigrationOptions extends Options {
relative?: boolean;
Expand Down Expand Up @@ -45,8 +45,6 @@ export default function transformer(

// 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);
});
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 name="Farrah" size="sm" />,
prefix: <Avatar customer 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 size="md" name={name} />;
const media = <Avatar customer 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 size="md" name={name} />;
const media = <Avatar customer size="md" name={name} />;

return (
<ResourceList.Item id={id} url={url} media={media}>
Expand Down
6 changes: 3 additions & 3 deletions polaris-react/src/components/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function All() {
</Text>
<InlineStack gap="2" blockAlign="center">
{sizes.map((size) => (
<Avatar key={size} size={size} />
<Avatar customer key={size} size={size} />
))}
</InlineStack>
</BlockStack>
Expand Down Expand Up @@ -189,11 +189,11 @@ export function ExtraSmallInContext() {
items={[
{
content: 'Chet Baker',
prefix: <Avatar size="xs" name="Chet Baker" />,
prefix: <Avatar customer size="xs" name="Chet Baker" />,
},
{
content: 'Farrah Fawcett',
prefix: <Avatar size="xs" name="Farrah Fawcett" />,
prefix: <Avatar customer size="xs" name="Farrah Fawcett" />,
},
]}
/>
Expand Down
36 changes: 21 additions & 15 deletions polaris-react/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface AvatarProps {
name?: string;
/** Initials of person to display */
initials?: string;
/** Whether the avatar is for a customer */
customer?: boolean;
/** URL of the avatar image which falls back to initials if the image fails to load */
source?: string;
/** Callback fired when the image fails to load */
Expand All @@ -68,6 +70,7 @@ export function Avatar({
source,
onError,
initials,
customer,
size = 'md',
accessibilityLabel,
}: AvatarProps) {
Expand Down Expand Up @@ -112,7 +115,9 @@ export function Avatar({
styles.Avatar,
size && styles[variationName('size', size)],
hasImage && status === Status.Loaded && styles.imageHasLoaded,
!source && styles[variationName('style', styleClass(nameString))],
!customer &&
!source &&
styles[variationName('style', styleClass(nameString))],
);

const textClassName = classNames(
Expand Down Expand Up @@ -158,20 +163,21 @@ export function Avatar({
</>
);

const avatarBody = !initials ? (
avatarPath
) : (
<text
className={textClassName}
x="50%"
y="50%"
dy={verticalOffset}
fill="currentColor"
textAnchor="middle"
>
{initials}
</text>
);
const avatarBody =
customer || !initials ? (
avatarPath
) : (
<text
className={textClassName}
x="50%"
y="50%"
dy={verticalOffset}
fill="currentColor"
textAnchor="middle"
>
{initials}
</text>
);

const svgMarkup = hasImage ? null : (
<span className={styles.Initials}>
Expand Down
6 changes: 3 additions & 3 deletions polaris-react/src/components/Avatar/tests/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ describe('<Avatar />', () => {

describe('customer', () => {
it('renders an inline svg', () => {
const avatar = mountWithApp(<Avatar />);
const avatar = mountWithApp(<Avatar customer />);
expect(avatar).toContainReactComponentTimes('svg', 1);
});

it('does not render a customer Avatar if a source is provided', () => {
const src = 'image/path/';
const avatar = mountWithApp(<Avatar source={src} />);
const avatar = mountWithApp(<Avatar customer source={src} />);
expect(avatar).not.toContainReactComponent('svg');
});

it('does not apply a style class', () => {
const src = 'image/path/';
const avatar = mountWithApp(<Avatar source={src} />);
const avatar = mountWithApp(<Avatar customer source={src} />);
expect(avatar).toContainReactComponent('span', {
className: 'Avatar sizeMd',
});
Expand Down
14 changes: 7 additions & 7 deletions polaris-react/src/components/Filters/Filters.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function WithAResourceList() {
]}
renderItem={(item) => {
const {id, url, name, location} = item;
const media = <Avatar size="md" name={name} />;
const media = <Avatar customer size="md" name={name} />;

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

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

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

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

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

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

return (
<ResourceList.Item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function WithAResourceList() {
]}
renderItem={(item) => {
const {id, url, name, location} = item;
const media = <Avatar size="md" name={name} />;
const media = <Avatar customer size="md" name={name} />;

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

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

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

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

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

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

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

return (
<ResourceList.Item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export function All() {
{
value: 'avatar_extra_small',
label: 'Avatar extra small',
media: <Avatar name="Hello World" size="sm" />,
media: <Avatar name="Hello World" size="xs" />,
},
{
value: 'avatar_small',
Expand Down
20 changes: 15 additions & 5 deletions polaris-react/src/components/ResourceItem/ResourceItem.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export function SelectableWithMedia() {
<ResourceItem
id={id}
url={url}
media={<Avatar size="md" name={name} source={avatarSource} />}
media={
<Avatar customer size="md" name={name} source={avatarSource} />
}
accessibilityLabel={`View details for ${name}`}
name={name}
>
Expand Down Expand Up @@ -142,7 +144,9 @@ export function WithMedia() {
<ResourceItem
id={id}
url={url}
media={<Avatar size="md" name={name} source={avatarSource} />}
media={
<Avatar customer size="md" name={name} source={avatarSource} />
}
accessibilityLabel={`View details for ${name}`}
name={name}
>
Expand Down Expand Up @@ -183,7 +187,9 @@ export function WithShortcutActions() {
<ResourceItem
id={id}
url={url}
media={<Avatar size="md" name={name} source={avatarSource} />}
media={
<Avatar customer size="md" name={name} source={avatarSource} />
}
shortcutActions={[
{content: 'View latest order', url: latestOrderUrl},
]}
Expand Down Expand Up @@ -230,7 +236,9 @@ export function WithPersistedShortcutActions() {
<ResourceItem
id={id}
url={url}
media={<Avatar size="md" name={name} source={avatarSource} />}
media={
<Avatar customer size="md" name={name} source={avatarSource} />
}
persistActions
shortcutActions={shortcutActions}
accessibilityLabel={`View details for ${name}`}
Expand Down Expand Up @@ -273,7 +281,9 @@ export function WithVerticalAlignment() {
verticalAlignment="center"
id={id}
url={url}
media={<Avatar size="md" name={name} source={avatarSource} />}
media={
<Avatar customer size="md" name={name} source={avatarSource} />
}
accessibilityLabel={`View details for ${name}`}
name={name}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ describe('<ResourceItem />', () => {
it('includes an <Avatar /> if one is provided', () => {
const wrapper = mountWithApp(
<ResourceListContext.Provider value={mockDefaultContext}>
<ResourceItem id={itemId} url={url} media={<Avatar />} />
<ResourceItem id={itemId} url={url} media={<Avatar customer />} />
</ResourceListContext.Provider>,
);
expect(wrapper).toContainReactComponent(Avatar);
Expand Down
Loading

0 comments on commit 08a0f2f

Please sign in to comment.