-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat/Avatar] add Avatar demo component (#27)
* add dependendy @radix-ui/react-avatar * add default avatar component * add avatar demo component
- Loading branch information
Showing
11 changed files
with
234 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui'; | ||
import { TypographyH4 } from '@/components/ui/typography'; | ||
import { cn } from '@/lib'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
const SAMPLE_AVATAR = [ | ||
{ | ||
src: 'https://images.unsplash.com/photo-1511485977113-f34c92461ad9?ixlib=rb-1.2.1&w=128&h=128&dpr=2&q=80', | ||
alt: 'sample-avatar1', | ||
fallback: 'sample', | ||
size: 'default', | ||
description: 'Default', | ||
}, | ||
{ | ||
src: 'https://images.unsplash.com/photo-1634926878768-2a5b3c42f139?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1356&q=80', | ||
alt: 'sample-avatar2', | ||
fallback: 'sample', | ||
size: 'lg', | ||
description: 'Large', | ||
}, | ||
]; | ||
|
||
const FALLBACK_AVATAR = [ | ||
{ | ||
src: '', | ||
alt: 'sample-avatar1/fallback', | ||
fallback: 'FB', | ||
size: 'default', | ||
description: 'Default', | ||
}, | ||
{ | ||
src: '', | ||
alt: 'sample-avatar2/fallback', | ||
fallback: 'FB', | ||
size: 'lg', | ||
description: 'Large', | ||
}, | ||
]; | ||
|
||
export default function AvatarDemo() { | ||
const [isHovered, setIsHovered] = useState(false); | ||
|
||
const handleIsHovered = useCallback( | ||
(hover: boolean) => setIsHovered(hover), | ||
[] | ||
); | ||
// 호버 상태면 fallback | ||
// 호버가 아닐땐 avatar | ||
|
||
return ( | ||
<div className='flex items-center justify-center gap-10'> | ||
{!isHovered && | ||
SAMPLE_AVATAR.map(({ src, alt, fallback, size, description }, idx) => ( | ||
<div | ||
key={`${alt}/${idx}`} | ||
className='flex h-[180px] w-[90px] flex-col items-center justify-start gap-y-2 font-bold' | ||
> | ||
<TypographyH4 className='flex-1 font-light text-foreground/60'> | ||
{description} | ||
</TypographyH4> | ||
<div className={cn('flex flex-1 items-center')}> | ||
<Avatar | ||
onMouseEnter={() => handleIsHovered(true)} | ||
onMouseLeave={() => handleIsHovered(false)} | ||
size={size as 'default' | 'lg'} | ||
> | ||
<AvatarImage src={src} alt={alt} /> | ||
<AvatarFallback>{fallback}</AvatarFallback> | ||
</Avatar> | ||
</div> | ||
</div> | ||
))} | ||
{isHovered && | ||
FALLBACK_AVATAR.map( | ||
({ src, alt, fallback, size, description }, idx) => ( | ||
<div | ||
key={`${alt}/${idx}`} | ||
className='flex h-[180px] w-[90px] flex-col items-center justify-start gap-y-2 font-bold' | ||
> | ||
<TypographyH4 className='flex-1 font-light text-foreground/60'> | ||
{description} | ||
</TypographyH4> | ||
<div className={cn('flex flex-1 items-center')}> | ||
<Avatar | ||
onMouseEnter={() => handleIsHovered(true)} | ||
onMouseLeave={() => handleIsHovered(false)} | ||
size={size as 'default' | 'lg'} | ||
> | ||
<AvatarImage src={src} alt={alt} /> | ||
<AvatarFallback>{fallback}</AvatarFallback> | ||
</Avatar> | ||
</div> | ||
</div> | ||
) | ||
)} | ||
</div> | ||
); | ||
} |
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
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
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,67 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import * as AvatarPrimitive from '@radix-ui/react-avatar'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
interface AvatarSize { | ||
size: 'default' | 'lg'; | ||
} | ||
|
||
const Avatar = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root> & AvatarSize | ||
>(({ className, children, size, ...props }, ref) => { | ||
return ( | ||
<AvatarPrimitive.Root | ||
ref={ref} | ||
className={cn( | ||
'rounded-full border-orange-500 border-2 flex justify-center items-center select-none align-middle overflow-hidden bg-blackA3', | ||
`h-[${size === 'default' ? 45 : 90}px] w-[${ | ||
size === 'default' ? 45 : 90 | ||
}px]`, | ||
className | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
</AvatarPrimitive.Root> | ||
); | ||
}); | ||
|
||
Avatar.displayName = AvatarPrimitive.Root.displayName; | ||
|
||
const AvatarImage = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Image>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image> | ||
>(({ className, onLoadingStatusChange, ...props }, ref) => ( | ||
<AvatarPrimitive.Image | ||
ref={ref} | ||
onLoadingStatusChange={onLoadingStatusChange} | ||
className={cn('h-full w-full rounded-[inherit] object-cover', className)} | ||
{...props} | ||
/> | ||
)); | ||
|
||
AvatarImage.displayName = AvatarPrimitive.Image.displayName; | ||
|
||
const AvatarFallback = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Fallback>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback> | ||
>(({ className, delayMs, children, ...props }, ref) => ( | ||
<AvatarPrimitive.Fallback | ||
ref={ref} | ||
className={cn( | ||
'flex items-center justify-center w-full h-full font-medium text-lg text-foreground/80 border-2 rounded-[inherit] bg-white', | ||
className | ||
)} | ||
delayMs={delayMs} | ||
{...props} | ||
> | ||
{children} | ||
</AvatarPrimitive.Fallback> | ||
)); | ||
|
||
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName; | ||
|
||
export { Avatar, AvatarImage, AvatarFallback }; |
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
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
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
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
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,14 @@ | ||
--- | ||
title: Avatar | ||
description: 이미지 fallback 을 포함하는 유저를 나타낼 수 있는 아바타 컴포넌트 | ||
component: true | ||
radix: | ||
link: https://www.radix-ui.com/docs/primitives/components/avatar | ||
api: https://www.radix-ui.com/docs/primitives/components/avatar#api-reference | ||
--- | ||
|
||
<ComponentExample src='/components/examples/avatar/demo.tsx'> | ||
<div className='max-w-[70%] w-full'> | ||
<AvatarDemo /> | ||
</div> | ||
</ComponentExample> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.