Skip to content

Commit

Permalink
[Avatar] Add shape parameter (#6103)
Browse files Browse the repository at this point in the history
* Add shape param to Avatar

* Add example to polaris.shopify.com page

* Revert "Add example to polaris.shopify.com page"

This reverts commit 3d2768f.

* update fonts

* revert font size changes (unneeded)

* update font size

* Update .changeset/yellow-worms-behave.md

Co-authored-by: Kyle Durand <kyledurand@users.noreply.github.com>

* use variationName for square css class (per code review)

* add a test

* Improve documentation to provide proper usage instructions

* Update .changeset/yellow-worms-behave.md

Co-authored-by: Chloe Rice <chloerice@users.noreply.github.com>

Co-authored-by: Kyle Durand <kyledurand@users.noreply.github.com>
Co-authored-by: Chloe Rice <chloerice@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 16, 2022
1 parent 7a7bc0c commit a622544
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/yellow-worms-behave.md
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Added a `shape` prop to `Avatar`
4 changes: 4 additions & 0 deletions polaris-react/src/components/Avatar/Avatar.scss
Expand Up @@ -24,6 +24,10 @@
}
}

.shapeSquare {
border-radius: var(--p-border-radius-05);
}

.hidden {
visibility: hidden;
}
Expand Down
12 changes: 11 additions & 1 deletion polaris-react/src/components/Avatar/Avatar.tsx
Expand Up @@ -9,6 +9,8 @@ import styles from './Avatar.scss';

type Size = 'extraSmall' | 'small' | 'medium' | 'large';

type Shape = 'square' | 'round';

enum Status {
Pending = 'PENDING',
Loaded = 'LOADED',
Expand All @@ -23,6 +25,11 @@ export interface AvatarProps {
* @default 'medium'
*/
size?: Size;
/**
* Shape of avatar
* @default 'round'
*/
shape?: Shape;
/** The name of the person */
name?: string;
/** Initials of person to display */
Expand All @@ -44,6 +51,7 @@ export function Avatar({
initials,
customer,
size = 'medium',
shape = 'round',
accessibilityLabel,
}: AvatarProps) {
const i18n = useI18n();
Expand Down Expand Up @@ -96,6 +104,7 @@ export function Avatar({
size && styles[variationName('size', size)],
!customer && styles[variationName('style', styleClass(nameString))],
hasImage && status === Status.Loaded && styles.imageHasLoaded,
shape && styles[variationName('shape', shape)],
);

const imageClassName = classNames(
Expand Down Expand Up @@ -129,7 +138,8 @@ export function Avatar({
y="50%"
dy={verticalOffset}
fill="currentColor"
fontSize="20"
fontSize={shape === 'square' ? '15.5' : '20'}
fontWeight={shape === 'square' ? '600' : '400'}
textAnchor="middle"
>
{initials}
Expand Down
8 changes: 8 additions & 0 deletions polaris-react/src/components/Avatar/README.md
Expand Up @@ -115,6 +115,14 @@ function ExtraSmallAvatarExample() {
}
```

### Square avatar

Use a `square` shape when the avatar represents a non-person entity like an app, channel, or store.

```jsx
<Avatar name="Shop One" shape="square" />
```

---

## Related components
Expand Down
10 changes: 10 additions & 0 deletions polaris-react/src/components/Avatar/tests/Avatar.test.tsx
Expand Up @@ -129,4 +129,14 @@ describe('<Avatar />', () => {
});
});
});

describe('shape', () => {
it('renders a square background when square is passed to shape', () => {
const avatar = mountWithApp(<Avatar initials="DL" shape="square" />);

expect(avatar).toContainReactComponent('span', {
className: expect.stringContaining('shapeSquare'),
});
});
});
});

0 comments on commit a622544

Please sign in to comment.