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

feat: lazy loading for userAvatar #1124

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 10 additions & 9 deletions src/components/UserAvatar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Component for displaying user avatar.

### PropTypes

| Name | Type | Required | Default | Description |
| :------------- | :--------------- | :------- | :------ | :--------------------------------------------------------- |
| imgUrl | `string` | | | Link to image |
| fallbackImgUrl | `string` | | | Link to fallback image |
| size | `UserAvatarSize` | | 'm' | Component size. Possible values: `xs`, `s`, `m`, `l`, `xl` |
| srcSet | `string` | | | `srcSet` attribute of the image |
| sizes | `string` | | | `sizes` attribute of the image |
| title | `string` | | | Tooltip text on hover |
| className | `string` | | | Class name |
| Name | Type | Required | Default | Description |
| :------------- | :--------------- | :------- | :------ | :------------------------------------------------------------------------------------------------------ |
| imgUrl | `string` | | | Link to image |
| fallbackImgUrl | `string` | | | Link to fallback image |
| size | `UserAvatarSize` | | 'm' | Component size. Possible values: `xs`, `s`, `m`, `l`, `xl` |
| srcSet | `string` | | | `srcSet` attribute of the image |
| sizes | `string` | | | `sizes` attribute of the image |
| title | `string` | | | Tooltip text on hover |
| className | `string` | | | Class name |
| loading | `eager \| lazy` | | | The loading attribute specifies whether a browser should load an image immediately or to defer loading. |
7 changes: 6 additions & 1 deletion src/components/UserAvatar/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ export interface UserAvatarProps {
sizes?: string;
title?: string;
className?: string;
loading?: 'eager' | 'lazy';
/** @deprecated Use appropriate component, like `<Button/>` instead */
onClick?: () => void;
}

const b = block('user-avatar');

export const UserAvatar = React.forwardRef<HTMLDivElement, UserAvatarProps>(
({imgUrl, fallbackImgUrl, size = 'm', srcSet, sizes, title, className, onClick}, ref) => {
(
{imgUrl, fallbackImgUrl, size = 'm', srcSet, sizes, title, className, loading, onClick},
ref,
) => {
const [isErrored, setIsErrored] = React.useState(false);

const handleError = React.useCallback(() => {
Expand All @@ -39,6 +43,7 @@ export const UserAvatar = React.forwardRef<HTMLDivElement, UserAvatarProps>(
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div className={b({size}, className)} title={title} onClick={onClick} ref={ref}>
<img
loading={loading}
className={b('figure')}
width={SIZES[size]}
height={SIZES[size]}
Expand Down
15 changes: 15 additions & 0 deletions src/components/UserAvatar/__stories__/UserAvatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ WithFallback.args = {
fallbackImgUrl: imgUrl,
size: 'xl',
};

export const LazyLoading: StoryFn<UserAvatarProps> = (args) => (
<div style={{overflow: 'scroll', height: '100px'}}>
{Array(10)
.fill(0)
.map((_, index) => {
return (
<div key={index} style={{padding: '2px'}}>
<UserAvatar imgUrl={`${args.imgUrl}?${index}`} loading="lazy" size="xl" />
</div>
);
})}
</div>
);
LazyLoading.args = {imgUrl};
Loading