|
| 1 | +import React from "react"; |
| 2 | +import propTypes from "prop-types"; |
| 3 | +import styled from "styled-components"; |
| 4 | +import { blockSizes } from "../common/system"; |
| 5 | + |
| 6 | +const StyledAvatar = styled.div` |
| 7 | + display: inline-block; |
| 8 | + box-sizing: border-box; |
| 9 | + object-fit: contain; |
| 10 | + height: calc(${props => blockSizes.md} - 2px); |
| 11 | + width: calc(${props => blockSizes.md} - 2px); |
| 12 | + border-radius: ${({ square }) => (square ? 0 : "50%")}; |
| 13 | + overflow: hidden; |
| 14 | + ${({ noBorder, theme }) => |
| 15 | + !noBorder && |
| 16 | + ` |
| 17 | + border-top: 2px solid ${theme.borderDark}; |
| 18 | + border-left: 2px solid ${theme.borderDark}; |
| 19 | + border-bottom: 2px solid ${theme.borderLightest}; |
| 20 | + border-right: 2px solid ${theme.borderLightest}; |
| 21 | + background: ${theme.material}; |
| 22 | + `} |
| 23 | + ${({ src }) => |
| 24 | + !src && |
| 25 | + ` |
| 26 | + display: flex; |
| 27 | + align-items: center; |
| 28 | + justify-content: space-around; |
| 29 | + font-weight: bold; |
| 30 | + font-size: 1rem; |
| 31 | + `} |
| 32 | +`; |
| 33 | +const SlyledAvatarIMG = styled.img` |
| 34 | + display: block; |
| 35 | + object-fit: contain; |
| 36 | + width: 100%; |
| 37 | + height: 100%; |
| 38 | +`; |
| 39 | +// src takes priority over children |
| 40 | + |
| 41 | +const Avatar = ({ children, noBorder, square, src, alt, ...otherProps }) => { |
| 42 | + return ( |
| 43 | + <StyledAvatar noBorder={noBorder} square={square} {...otherProps}> |
| 44 | + {src ? <SlyledAvatarIMG src={src} alt={alt} /> : children} |
| 45 | + </StyledAvatar> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +Avatar.defaultProps = { |
| 50 | + square: false, |
| 51 | + noBorder: false, |
| 52 | + src: undefined, |
| 53 | + children: null, |
| 54 | + alt: "" |
| 55 | +}; |
| 56 | +Avatar.propTypes = { |
| 57 | + square: propTypes.bool, |
| 58 | + noBorder: propTypes.bool, |
| 59 | + children: propTypes.node, |
| 60 | + src: propTypes.string, |
| 61 | + alt: propTypes.string |
| 62 | +}; |
| 63 | +export default Avatar; |
0 commit comments