This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roman Chubarkin
committed
Jun 22, 2021
1 parent
23445f0
commit 30c0160
Showing
6 changed files
with
158 additions
and
0 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
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,43 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { IconProfile } from 'static/icons'; | ||
|
||
import { useImageLoad } from './use-image-load'; | ||
|
||
const AvatarContainer = styled.div` | ||
--local-size: calc((var(--woly-component-level) + 2) * 2 * var(--woly-const-m)); | ||
width: var(--local-size); | ||
height: var(--local-size); | ||
& > * { | ||
width: 100%; | ||
height: 100%; | ||
border-radius: 50%; | ||
} | ||
`; | ||
|
||
interface AvatarProps { | ||
alt?: string; | ||
src?: string; | ||
srcSet?: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
export const Avatar: React.FC<AvatarProps> = ({ alt, src, srcSet, children: childrenProp }) => { | ||
const loadFailed = useImageLoad({ src, srcSet }); | ||
const hasImg = src || srcSet; | ||
let children = null; | ||
|
||
if (hasImg && !loadFailed) { | ||
children = <img alt={alt} src={src} srcSet={srcSet} />; | ||
} else if (childrenProp) { | ||
children = childrenProp; | ||
} else { | ||
// render fallback if image loading failed or no src attributes / children provided | ||
children = <IconProfile />; | ||
} | ||
|
||
return <AvatarContainer>{children}</AvatarContainer>; | ||
}; |
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,73 @@ | ||
--- | ||
name: avatar | ||
category: atoms | ||
package: woly | ||
--- | ||
|
||
import {Avatar} from 'ui' | ||
import {Playground, block} from 'lib/playground' | ||
|
||
`Avatar` shows user avatar | ||
|
||
### Example | ||
|
||
<Playground> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</Playground> | ||
|
||
### Sizes | ||
|
||
<Playground> | ||
<block.N> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.N> | ||
<block.S> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.S> | ||
<block.M> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.M> | ||
<block.L> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.L> | ||
<block.XL> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.XL> | ||
<block.H> | ||
<Avatar src="https://image.flaticon.com/icons/png/512/168/168726.png" /> | ||
</block.H> | ||
</Playground> | ||
|
||
### Fallback | ||
|
||
<Playground> | ||
<Avatar src="http://broken.url/image" /> | ||
</Playground> | ||
|
||
### Custom child component | ||
|
||
<Playground> | ||
<Avatar> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
backgroundColor: 'black', | ||
color: 'white', | ||
fontSize: '20px', | ||
}} | ||
> | ||
RC | ||
</div> | ||
</Avatar> | ||
</Playground> | ||
|
||
### Props | ||
|
||
| Name | Type | Default | Description | | ||
| ---------- | ----------------- | ----------- | ---------------------------------------- | | ||
| `alt` | `string` | `''` | text description of the image | | ||
| `children` | `React.ReactNode` | `undefined` | use if no src attributes provided | | ||
| `src` | `string` | `''` | avatar src | | ||
| `srcSet` | `string` | `''` | avatar src set for multiple screen sizes | |
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,35 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export function useImageLoad({ src, srcSet }: { src?: string; srcSet?: string }) { | ||
const [failed, setFailed] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!src && !srcSet) { | ||
return; | ||
} | ||
|
||
setFailed(false); | ||
|
||
const image = new Image(); | ||
image.src = src ?? ''; | ||
image.srcset = srcSet ?? ''; | ||
|
||
const onLoad = () => { | ||
setFailed(false); | ||
}; | ||
|
||
const onError = () => { | ||
setFailed(true); | ||
}; | ||
|
||
image.addEventListener('load', onLoad); | ||
image.addEventListener('error', onError); | ||
|
||
return () => { | ||
image.removeEventListener('load', onLoad); | ||
image.removeEventListener('error', onError); | ||
}; | ||
}, [src, srcSet]); | ||
|
||
return failed; | ||
} |
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