Skip to content

Commit

Permalink
Merge pull request vczb#24 from yamitrvg12/main
Browse files Browse the repository at this point in the history
create avatar component
  • Loading branch information
vczb authored Oct 3, 2022
2 parents 2f8673c + 05ca710 commit 85464e4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/packages/Avatar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { BaseHTMLAttributes } from 'react'
import * as S from './styles'

export type AvatarProps = {
children: React.ReactNode
src?: string
alt?: string
size?: 'xsmall' | 'small' | 'medium' | 'large'
variant?: 'circle' | 'square' | 'rounded'
} & BaseHTMLAttributes<HTMLDivElement>

const Avatar = ({
children,
src,
alt,
size,
variant,
...props
}: AvatarProps) => (
<S.Wrapper size={size} variant={variant} {...props}>
{src ? <S.Img src={src} alt={alt} /> : children}
</S.Wrapper>
)

export default Avatar
17 changes: 17 additions & 0 deletions src/packages/Avatar/stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'
import { Story, Meta } from '@storybook/react/types-6-0'
import Avatar, { AvatarProps } from '.'

export default {
title: 'Avatar',
component: Avatar,
args: {
children: 'OP',
src: 'https://picsum.photos/id/237/300/300',
alt: 'Random image',
size: 'medium',
variant: 'circle'
}
} as Meta

export const Default: Story<AvatarProps> = (args) => <Avatar {...args} />
40 changes: 40 additions & 0 deletions src/packages/Avatar/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import styled, { css, DefaultTheme } from 'styled-components'
import { AvatarProps } from '.'

const variantShape = {
circle: () => css`
border-radius: 50%;
`,
square: () => css`
border-radius: 0;
`,
rounded: (theme: DefaultTheme) => css`
border-radius: ${theme.border.radius};
`
}

export const Wrapper = styled.div<Pick<AvatarProps, 'size' | 'variant'>>`
${({ theme, size = 'medium', variant = 'circle' }) => css`
border-radius: 50%;
align-items: center;
font-size: ${theme.font.sizes.large};
width: ${theme.avatarSizes[size]};
height: ${theme.avatarSizes[size]};
justify-content: center;
overflow: hidden;
position: relative;
display: inline-flex;
color: ${theme.colors.base.white};
background-color: ${theme.colors.neutral.dark};
${!!variant && variantShape[variant](theme)}}
`}
`

export const Img = styled.img`
${() => css`
width: 100%;
height: 100%;
text-align: center;
object-fit: cover;
`}
`
1 change: 1 addition & 0 deletions src/packages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { default as GridFluid } from './GridFluid'
export { default as TextContent } from './TextContent'
export { default as Divider } from './Divider'
export { default as Card } from './Card'
export { default as Avatar } from './Avatar'
6 changes: 6 additions & 0 deletions src/styles/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@ export default {
shadows: {
default: 'rgb(0 0 0 / 15%) 0px 2rem 8rem 0',
focus: `0 0 0.5rem ${colors.secondary.medium}`
},
avatarSizes: {
large: '6rem',
medium: '4rem',
small: '3rem',
xsmall: '2rem'
}
} as const

0 comments on commit 85464e4

Please sign in to comment.