-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add images, icons loading skeletons and fallback items
- Loading branch information
Showing
9 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { AppImage } from './AppImage'; | ||
|
||
export default { | ||
title: 'shared/AppImage', | ||
component: AppImage, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof AppImage>; | ||
|
||
const Template: ComponentStory<typeof AppImage> = (args) => <AppImage {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
ImgHTMLAttributes, memo, ReactElement, useLayoutEffect, useState, | ||
} from 'react'; | ||
|
||
interface AppImageProps extends ImgHTMLAttributes<HTMLImageElement>{ | ||
className?: string; | ||
fallback?: ReactElement; | ||
errorFallback?: ReactElement; | ||
} | ||
|
||
export const AppImage = memo((props: AppImageProps) => { | ||
const { | ||
className, | ||
fallback, | ||
errorFallback, | ||
src, | ||
alt = 'image', | ||
...otherProps | ||
} = props; | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [hasError, setHasError] = useState(false); | ||
|
||
useLayoutEffect(() => { | ||
const img = new Image(); | ||
img.src = src ?? ''; | ||
img.onload = () => { | ||
setIsLoading(false); | ||
}; | ||
img.onerror = () => { | ||
setIsLoading(false); | ||
setHasError(true); | ||
}; | ||
}, [src]); | ||
|
||
if (isLoading && fallback) { | ||
return fallback; | ||
} | ||
if (hasError && errorFallback) { | ||
return errorFallback; | ||
} | ||
return ( | ||
<img | ||
src={src} | ||
alt={alt} | ||
className={className} | ||
{...otherProps} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AppImage } from './AppImage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import { CSSProperties, useMemo } from 'react'; | ||
import { classNames, Mods } from '@/shared/lib/classNames/classNames'; | ||
import cls from './Avatar.module.scss'; | ||
import { AppImage } from '../AppImage'; | ||
import UserIcon from '../../assets/icons/user-filled.svg'; | ||
import { Icon } from '../Icon'; | ||
import { Skeleton } from '../Skeleton'; | ||
|
||
export interface AvatarProps { | ||
className?: string; | ||
src?: string; | ||
size?: number; | ||
alt?: string; | ||
fallbackInverted?: boolean; | ||
} | ||
|
||
export const Avatar = (props: AvatarProps) => { | ||
const { | ||
className, | ||
src, | ||
size, | ||
size = 100, | ||
alt, | ||
fallbackInverted, | ||
} = props; | ||
const mods: Mods = {}; | ||
|
||
const styles = useMemo<CSSProperties>(() => ({ | ||
width: size || 100, | ||
height: size || 100, | ||
width: size, | ||
height: size, | ||
}), [size]); | ||
|
||
const fallback = <Skeleton width={size} height={size} border="50%" />; | ||
const errorFallback = <Icon width={size} height={size} Svg={UserIcon} inverted={fallbackInverted} />; | ||
|
||
return ( | ||
<img | ||
<AppImage | ||
src={src} | ||
style={styles} | ||
className={classNames(cls.Avatar, mods, [className])} | ||
alt={alt} | ||
fallback={fallback} | ||
errorFallback={errorFallback} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
.Icon { | ||
fill: var(--primary-color); | ||
color: var(--primary-color); | ||
} | ||
|
||
.inverted { | ||
fill: var(--inverted-primary-color); | ||
color: var(--inverted-primary-color); | ||
} |