Skip to content
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
8 changes: 8 additions & 0 deletions next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const nextConfig: NextConfig = {
fullUrl: true,
},
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'images.unsplash.com',
},
],
},
};

export default nextConfig;
70 changes: 70 additions & 0 deletions src/components/ui/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use client';

import { DEFAULT_COLORS } from '@/constants/colors';
import { cn, getColorByString } from '@/utils/helper';
import { cva, VariantProps } from 'class-variance-authority';
import Image from 'next/image';
import { HTMLAttributes, useState } from 'react';

/**
* 커스터마이징 가능한 기본 공용 Avatar 컴포넌트입니다.
*
* ### Variants:
* - `size`:
* - `default`: width:38px, font-size:16px;
* - `md`: width:34px, font-size:16px;
* - `sm`: width:24px, font-size:12px;
*
* ### Default Variants (기본 스타일):
* - `size`: `default`
*
*
* @example
* <Avatar size="md" email="test@test.com" />
*
* 반응형으로 쓰고 싶을땐, classname을 추가하세요
* <Avatar email="test@test.com" className='w-8 md:w-10 lg:w-12' />
*/

const avatarVariants = cva(
//prettier-ignore
'relative inline-flex items-center justify-center aspect-square rounded-full overflow-hidden border-2 border-white leading-none',
{
variants: {
size: {
default: 'w-[38px] text-lg',
md: 'w-[34px] text-lg',
sm: 'w-6 text-xs',
},
},

defaultVariants: {
size: 'default',
},
},
);

interface AvatarProps extends HTMLAttributes<HTMLDivElement>, VariantProps<typeof avatarVariants> {
email: string;
profileImageUrl?: string;
}

export default function Avatar({ email, profileImageUrl, size, className, ...props }: AvatarProps) {
const [imgError, setImgError] = useState(false);
const colorCode = getColorByString(email, DEFAULT_COLORS);
const firstChar = email.charAt(0);

const isFallback = !profileImageUrl || imgError;

return (
<figure className={cn(avatarVariants({ size, className }))} {...props}>
{!isFallback ? (
<Image src={profileImageUrl} alt={email} fill onError={() => setImgError(true)} />
) : (
<span className='flex aspect-square w-full items-center justify-center font-semibold uppercase text-white' style={{ backgroundColor: colorCode }}>
{firstChar}
</span>
)}
</figure>
);
}
26 changes: 26 additions & 0 deletions src/components/ui/Avatar/StackAvatars.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Avatar from './Avatar';

interface StackAvatarsProps {
members: { email: string; profileImageUrl?: string }[];
visibleCount: number;
}

export default function StackAvatars({ members, visibleCount = 3 }: StackAvatarsProps) {
const chunkedMembers = members.slice(0, visibleCount);
const restMembersCount = members.length - visibleCount;

return (
<ul className='flex pl-2 leading-none'>
{chunkedMembers.map((member) => (
<li key={member.email} className='-ml-2'>
<Avatar email={member.email} profileImageUrl={member.profileImageUrl} />
</li>
))}
{restMembersCount > 0 && (
<li className='-ml-2'>
<span className='relative flex aspect-square h-full items-center justify-center rounded-full border-2 border-white bg-[#F4D7DA] font-medium text-[#D25B68]'>+{restMembersCount}</span>
</li>
)}
</ul>
);
}
3 changes: 3 additions & 0 deletions src/constants/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type HexColor = `#${string}`;

export const DEFAULT_COLORS: HexColor[] = ['#7AC555', '#760DDE', '#FFA500', '#76A5EA', '#E876EA'];
6 changes: 6 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

export function getColorByString(value: string, colorArray: string[]) {
const charCode = value.toLowerCase().charCodeAt(0);
const index = charCode % colorArray.length;
return colorArray[index];
}