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

Avatar 컴포넌트 구현 #21

Merged
merged 15 commits into from
Oct 31, 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
26 changes: 26 additions & 0 deletions src/components/common/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CSSProperties } from 'react'
import Image from 'next/image'

export interface AvatarProps {
src: string
width: number
height: number
alt: string
style?: CSSProperties
}

const Avatar = ({ src, width, height, alt }: AvatarProps) => {
return (
<div className="inline-block">
<Image
src={src}
width={width}
height={height}
alt={alt}
className="border-slate3 rounded-full border object-cover"
/>
</div>
)
}

export default Avatar
59 changes: 59 additions & 0 deletions src/components/common/AvatarGroup/AvatarGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react'
import { AvatarProps } from '../Avatar/Avatar'
import {
DEFAULT_LIMIT,
DEFAULT_SIZE,
MARGIN_LEFT_DIVIDE,
PADDING_LEFT_DIVIDE,
} from './constants'

export interface AvatarGroupProps {
children: React.ReactNode
size?: number
limit?: number
}

const AvatarGroup = ({
children,
size = DEFAULT_SIZE,
limit = DEFAULT_LIMIT,
}: AvatarGroupProps) => {
const allAvatars = React.Children.toArray(children)
const avatars = React.Children.toArray(children)
.filter((element): element is React.ReactElement<AvatarProps> => {
return true
})
.slice(0, limit)
.map((avatar, index, avatars) => {
return React.cloneElement(avatar, {
style: {
marginLeft: -size / MARGIN_LEFT_DIVIDE,
zIndex: avatars.length - index,
},
})
})

return (
<div className="flex items-center gap-0.5">
<div
className="flex"
style={{ paddingLeft: size / PADDING_LEFT_DIVIDE }}>
{avatars.map((avatar) => (
<div
className="flex"
key={avatar.key}
style={avatar.props.style}>
{avatar}
</div>
))}
</div>
{allAvatars.length > limit && (
<div className="text-slate6 text-xs font-normal">
+{allAvatars.length - limit}명
</div>
)}
</div>
)
}

export default AvatarGroup
4 changes: 4 additions & 0 deletions src/components/common/AvatarGroup/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const MARGIN_LEFT_DIVIDE = 7
export const PADDING_LEFT_DIVIDE = 7
export const DEFAULT_SIZE = 60
export const DEFAULT_LIMIT = 3
2 changes: 2 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { default as Providers } from './Providers/providers'
export { default as ThemeButton } from './ThemeButton/themeButton'
export { default as Avatar } from './common/Avatar/Avatar'
export { default as AvatarGroup } from './common/AvatarGroup/AvatarGroup'