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

fix(Avatar): Support for Non-Square Input Image for Avatar component #2337

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Changes from 3 commits
Commits
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
22 changes: 19 additions & 3 deletions modules/react/avatar/lib/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useState} from 'react';
import {Property} from 'csstype';
import {styled, focusRing, hideMouseFocus} from '@workday/canvas-kit-react/common';
import isPropValid from '@emotion/is-prop-valid';
import {borderRadius, colors} from '@workday/canvas-kit-react/tokens';
@@ -35,6 +36,13 @@ export interface AvatarProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
* Will render an `div` tag instead of a `button` when defined.
*/
as?: 'div';
/**
* The object-fit CSS property sets how the content of a replaced element,
* such as an <img> or <video>, should be resized to fit its container.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can maybe add something like If you image is not a square, you can use this property to ensure the image is rendered properly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

* See [object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit).
* If your image is not a square, you can use this property to ensure the image is rendered properly.
*/
objectFit?: Property.ObjectFit;
}

/**
@@ -107,15 +115,16 @@ const StyledIcon = styled(SystemIconCircle)<{isImageLoaded: boolean}>(
})
);

const StyledImage = styled('img')<{isLoaded: boolean}>(
const StyledImage = styled('img')<{isLoaded: boolean; objectFit?: Property.ObjectFit}>(
{
width: '100%',
height: '100%',
borderRadius: borderRadius.circle,
transition: fadeTransition,
},
({isLoaded}) => ({
({isLoaded, objectFit}) => ({
opacity: isLoaded ? 1 : 0,
objectFit,
})
);

@@ -127,6 +136,7 @@ export const Avatar: AvatarOverload = React.forwardRef(
altText = 'Avatar',
url,
onClick,
objectFit,
...elemProps
}: AvatarProps,
ref: React.Ref<HTMLButtonElement>
@@ -164,7 +174,13 @@ export const Avatar: AvatarOverload = React.forwardRef(
</StyledStack>
{url && (
<StyledStack size={size}>
<StyledImage src={url} alt={altText} onLoad={loadImage} isLoaded={imageLoaded} />
<StyledImage
src={url}
alt={altText}
onLoad={loadImage}
isLoaded={imageLoaded}
objectFit={objectFit}
/>
</StyledStack>
)}
</StyledContainer>
12 changes: 12 additions & 0 deletions modules/react/avatar/stories/stories.tsx
Original file line number Diff line number Diff line change
@@ -57,6 +57,18 @@ storiesOf('Components/Indicators/Avatar', module)
<h3>Extra Small</h3>
<Avatar as="div" size={Avatar.Size.xs} url={testAvatar} />
</div>
))
.add('Non-Square Image', () => (
<div className="story">
<h3>Original Rectangle Image</h3>
<img alt="" src="https://placekitten.com/g/450/200" />
<h3>Using Object Fit on a Rectangle Image</h3>
<Avatar as="div" size={200} url="https://placekitten.com/g/450/200" objectFit="contain" />
<h3>Original Square Image</h3>
<img alt="" src="https://placekitten.com/g/450/450" />
<h3>using Object Fit on a Square Image</h3>
mannycarrera4 marked this conversation as resolved.
Show resolved Hide resolved
<Avatar as="div" size={200} url="https://placekitten.com/g/450/450" />
</div>
));

storiesOf('Components/Indicators/Avatar/Avatar Button', module)