-
Notifications
You must be signed in to change notification settings - Fork 2
✨Feat: 공통 드롭다운 메뉴 기능 구현 #43
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
Changes from all commits
eeaa935
3250c47
5371baf
dea20fa
01716be
a1c4d5c
bb6dbf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 'use client' | ||
|
|
||
| import { cn } from '@lib/cn' // 클래스 이름 병합 유틸리티 | ||
| import { ReactNode, useEffect, useRef, useState } from 'react' | ||
|
|
||
| type DropdownProps = { | ||
| trigger: ReactNode | ||
| children: ReactNode | ||
| align?: 'left' | 'right' | 'center' | ||
| width?: string | ||
| } | ||
|
|
||
| export default function Dropdown({ | ||
| trigger, | ||
| children, | ||
| align = 'center', | ||
| width = 'w-40', | ||
| }: DropdownProps) { | ||
| const [isOpen, setIsOpen] = useState(false) | ||
| const ref = useRef<HTMLDivElement>(null) | ||
|
|
||
| useEffect(() => { | ||
| const handleClickOutside = (e: MouseEvent) => { | ||
| if (ref.current && !ref.current.contains(e.target as Node)) { | ||
| setIsOpen(false) | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('mousedown', handleClickOutside) | ||
| return () => document.removeEventListener('mousedown', handleClickOutside) | ||
| }, []) | ||
|
|
||
| return ( | ||
| <div ref={ref} className="relative inline-block"> | ||
| <div | ||
| onClick={() => setIsOpen((prev) => !prev)} | ||
| className="cursor-pointer" | ||
| > | ||
| {trigger} | ||
| </div> | ||
|
|
||
| {isOpen && ( | ||
| <div | ||
| className={cn( | ||
| 'BG-gray absolute z-50 mt-4 rounded border shadow-md', | ||
| width, | ||
| { | ||
| 'left-0': align === 'left', | ||
| 'right-0': align === 'right', | ||
| 'left-1/2 -translate-x-1/2': align === 'center', | ||
| }, | ||
| )} | ||
| > | ||
| {children} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 'use client' | ||
|
|
||
| import Dropdown from '@components/common/Dropdown/Dropdown' | ||
| import { Profile } from '@components/common/Profile' | ||
| import { useRouter } from 'next/navigation' | ||
|
|
||
| export default function UserDropdown() { | ||
| const router = useRouter() | ||
|
|
||
| const goToMypage = () => { | ||
| router.push('/mypage') | ||
| } | ||
|
|
||
| const handleLogout = () => { | ||
| console.log('로그아웃 처리') | ||
| } | ||
|
|
||
| return ( | ||
| <Dropdown | ||
| trigger={ | ||
| <div className="flex cursor-pointer items-center gap-8"> | ||
| <Profile nickname="닉네임" size={36} /> | ||
| </div> | ||
|
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 닉네임 하드코딩
- <Profile nickname="닉네임" size={36} />
+ <Profile nickname={user.nickname} imageUrl={user.imageUrl} size={36} />
🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오... 프롭으로 아예 jsx 덩어리를 전달할수도 있군요
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞아용~ JSX 덩어리 전체를 prop으로 전달하는 게 가능하다고 합니다. |
||
| } | ||
| width="w-80" | ||
| align="right" | ||
| > | ||
| <button | ||
| onClick={goToMypage} | ||
| className="w-full p-5 text-xs hover:bg-gray-100 hover:text-black" | ||
| > | ||
| 마이페이지 | ||
| </button> | ||
| <button | ||
| onClick={handleLogout} | ||
| className="w-full p-5 text-xs hover:bg-gray-100 hover:text-black" | ||
| > | ||
| 로그아웃 | ||
| </button> | ||
| </Dropdown> | ||
| ) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 작성하면 어떻게 적용이 되는 건가요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
드롭다운 컴포넌트 사용 시에 와 같이 사용해주시면 좌측 정렬이 가능해집니다!!
이후에 사용해보고 너무 치우친다 싶으면
'leftt-0'부분을'left-4'와 같이 조정해주는 식으로 변경하면 될 것 같습니당