Dropdown (드롭다운)
Dropdown은 사용자가 선택할 수 있는 동작이나 옵션 목록을 표시하는 컴포넌트예요. 트리거 버튼을 누르면 팝오버에 메뉴가 펼쳐져요. 해요체로 쉽게 설명드릴게요.
출처: HeroUI Dropdown
본문
사용법 (Usage)
import { Dropdown } from '@heroui/react';
{"name": "dropdown-default"}
구조 (Anatomy)
import { Dropdown, Button, Label, Description, Header, Kbd, Separator } from '@heroui/react';
export default () => (
<Dropdown>
<Dropdown.Trigger>
<Button />
</Dropdown.Trigger>
<Dropdown.Popover>
<Dropdown.Menu>
<Dropdown.Item>
<Label />
<Description />
<Kbd slot="keyboard" />
<Dropdown.ItemIndicator />
</Dropdown.Item>
<Separator />
<Dropdown.Section>
<Header />
<Dropdown.Item />
</Dropdown.Section>
<Dropdown.SubmenuTrigger>
<Dropdown.Item>
<Label />
<Dropdown.SubmenuIndicator />
</Dropdown.Item>
<Dropdown.Popover>
<Dropdown.Menu>
<Dropdown.Item />
</Dropdown.Menu>
</Dropdown.Popover>
</Dropdown.SubmenuTrigger>
</Dropdown.Menu>
</Dropdown.Popover>
</Dropdown>
)
예시 (Examples)
아이콘과 함께 (With Icons)
{"name": "dropdown-with-icons"}
설명과 함께 (With Descriptions)
{"name": "dropdown-with-descriptions"}
비활성화된 항목 (With Disabled Items)
{"name": "dropdown-with-disabled-items"}
섹션과 함께 (With Sections)
{"name": "dropdown-with-sections"}
다중 선택 (With Multiple Selection)
{"name": "dropdown-with-multiple-selection"}
제어된 상태 (Controlled)
{"name": "dropdown-controlled"}
제어된 열림 상태 (Controlled Open State)
{"name": "dropdown-controlled-open-state"}
단일 선택 (With Single Selection)
{"name": "dropdown-with-single-selection"}
사용자 지정 인디케이터 (Single With Custom Indicator)
{"name": "dropdown-single-with-custom-indicator"}
섹션 수준 선택 (With Section Level Selection)
{"name": "dropdown-with-section-level-selection"}
키보드 단축키 (With Keyboard Shortcuts)
{"name": "dropdown-with-keyboard-shortcuts"}
하위 메뉴 (With Submenus)
{"name": "dropdown-with-submenus"}
사용자 지정 하위 메뉴 인디케이터 (With Custom Submenu Indicator)
{"name": "dropdown-with-custom-submenu-indicator"}
커스텀 트리거 (Custom Trigger)
{"name": "dropdown-custom-trigger"}
길게 누르기 트리거 (Long Press Trigger)
{"name": "dropdown-long-press-trigger"}
커스터마이징 (Customization)
Tailwind CSS
{"name": "dropdown-custom-styles"}
Global CSS
Dropdown 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.dropdown {
@apply flex flex-col gap-1;
}
.dropdown__trigger {
@apply outline-none;
}
.dropdown__popover {
@apply rounded-lg border border-border bg-overlay p-2;
}
.dropdown__menu {
@apply flex flex-col gap-1;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.
CSS 클래스 (CSS Classes)
Dropdown 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 [!toc]
.dropdown- 기본 드롭다운 컨테이너.dropdown__trigger- 드롭다운을 여는 버튼 또는 요소.dropdown__popover- 팝오버 컨테이너.dropdown__menu- 팝오버 안의 메뉴 컨테이너
상태 클래스 [!toc]
.dropdown__trigger[data-focus-visible="true"]- 포커스된 트리거 상태.dropdown__trigger[data-disabled="true"]- 비활성화된 트리거 상태.dropdown__trigger[data-pressed="true"]- 눌린 트리거 상태.dropdown__popover[data-entering]- 진입 애니메이션 상태.dropdown__popover[data-exiting]- 종료 애니메이션 상태.dropdown__menu[data-selection-mode="single"]- 단일 선택 모드.dropdown__menu[data-selection-mode="multiple"]- 다중 선택 모드
메뉴 컴포넌트 클래스 (Menu Component Classes)
Dropdown 컴포넌트는 Menu, MenuItem, MenuSection을 기본 컴포넌트로 사용해요. 이 클래스들도 커스터마이징할 수 있어요:
메뉴 클래스 [!toc]
.menu- 기본 메뉴 컨테이너 (menu.css)[data-slot="separator"]- 메뉴 안의 구분선 요소
MenuItem 클래스 [!toc]
.menu-item- 기본 메뉴 항목 컨테이너 (menu-item.css).menu-item__indicator- 선택 인디케이터 (체크 표시 또는 점)[data-slot="menu-item-indicator--checkmark"]- 체크 표시 인디케이터 SVG[data-slot="menu-item-indicator--dot"]- 점 인디케이터 SVG
.menu-item__indicator--submenu- 하위 메뉴 인디케이터 (화살표).menu-item--default- 기본 변형 스타일.menu-item--danger- 위험 변형 스타일
MenuItem 상태 클래스 [!toc]
.menu-item[data-focus-visible="true"]- 포커스된 항목 상태 (키보드 포커스).menu-item[data-focus="true"]- 포커스된 항목 상태.menu-item[data-pressed]- 눌린 항목 상태.menu-item[data-hovered]- 호버된 항목 상태.menu-item[data-selected="true"]- 선택된 항목 상태.menu-item[data-disabled]- 비활성화된 항목 상태.menu-item[data-has-submenu="true"]- 하위 메뉴가 있는 항목.menu-item[data-selection-mode="single"]- 단일 선택 모드.menu-item[data-selection-mode="multiple"]- 다중 선택 모드.menu-item[aria-checked="true"]- 체크된 항목 (ARIA).menu-item[aria-selected="true"]- 선택된 항목 (ARIA)
MenuSection 클래스 [!toc]
.menu-section- 기본 메뉴 섹션 컨테이너 (menu-section.css)
상호작용 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover):
:hover또는[data-hovered="true"](트리거와 항목) - 포커스 (Focus):
:focus-visible또는[data-focus-visible="true"](트리거와 항목) - 비활성화 (Disabled):
:disabled또는[data-disabled="true"](트리거와 항목) - 눌림 (Pressed):
:active또는[data-pressed="true"](트리거와 항목) - 선택됨 (Selected):
[data-selected="true"]또는[aria-selected="true"](항목)
API 참고 (API Reference)
Dropdown
| Prop | Type | Default | Description |
|---|---|---|---|
isOpen |
boolean |
- | Sets the open state of the menu (controlled) |
defaultOpen |
boolean |
- | Sets the default open state of the menu (uncontrolled) |
onOpenChange |
(isOpen: boolean) => void |
- | Handler called when the open state changes |
trigger |
"press" | "longPress" |
"press" |
The type of interaction that triggers the menu |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Dropdown content |
Dropdown.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Trigger content or render function |
Button을 트리거로 사용할 때 모든 Button props도 지원돼요.
Dropdown.Popover
| Prop | Type | Default | Description |
|---|---|---|---|
placement |
"bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right" | "right top" | "right bottom" | "end" | "end top" | "end bottom" |
"bottom" |
Placement of the popover relative to the trigger |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Content children |
모든 Popover props도 지원돼요.
Dropdown.Menu
| Prop | Type | Default | Description |
|---|---|---|---|
selectionMode |
"single" | "multiple" | "none" |
"none" |
Whether single or multiple selection is enabled |
selectedKeys |
Iterable<Key> |
- | The currently selected keys (controlled) |
defaultSelectedKeys |
Iterable<Key> |
- | The initial selected keys (uncontrolled) |
onSelectionChange |
(keys: Selection) => void |
- | Handler called when the selection changes |
disabledKeys |
Iterable<Key> |
- | Keys of disabled items |
onAction |
(key: Key) => void |
- | Handler called when an item is activated |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Menu content |
모든 Menu props도 지원돼요.
Dropdown.Section
| Prop | Type | Default | Description |
|---|---|---|---|
selectionMode |
"single" | "multiple" |
- | Selection mode for items within this section |
selectedKeys |
Iterable<Key> |
- | The currently selected keys (controlled) |
defaultSelectedKeys |
Iterable<Key> |
- | The initial selected keys (uncontrolled) |
onSelectionChange |
(keys: Selection) => void |
- | Handler called when the selection changes |
disabledKeys |
Iterable<Key> |
- | Keys of disabled items |
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Section content |
모든 MenuSection props도 지원돼요.
Dropdown.Item
| Prop | Type | Default | Description |
|---|---|---|---|
id |
Key |
- | Unique identifier for the item |
textValue |
string |
- | Text content of the item for typeahead |
variant |
"default" | "danger" |
"default" |
Visual variant of the item |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Item content or render function |
모든 MenuItem props도 지원돼요.
Dropdown.ItemIndicator
| Prop | Type | Default | Description |
|---|---|---|---|
type |
"checkmark" | "dot" |
"checkmark" |
Type of indicator to display |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Custom indicator content or render function |
렌더 함수를 사용할 때 이 값들이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
Whether the item is selected |
isIndeterminate |
boolean |
Whether the item is in an indeterminate state |
Dropdown.SubmenuIndicator
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Custom indicator content |
Dropdown.SubmenuTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode |
- | Submenu trigger content |
모든 SubmenuTrigger props도 지원돼요.
렌더 프롭 (Render Props)
Dropdown.Item과 함께 렌더 함수를 사용하면 이 값들이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
Whether the item is selected |
isFocused |
boolean |
Whether the item is focused |
isDisabled |
boolean |
Whether the item is disabled |
isPressed |
boolean |
Whether the item is being pressed |
접근성 (Accessibility)
Dropdown 컴포넌트는 ARIA 메뉴 패턴을 구현하며 다음을 제공해요:
- 전체 키보드 탐색 지원 (방향키, home/end, 타자 입력 검색)
- 동작과 선택 변경에 대한 화면 낭독기 알림
- 적절한 포커스 관리
- 비활성화 상태 지원
- 길게 누르기 상호작용 지원
- 하위 메뉴 탐색
자세한 내용은 React Aria Menu 문서를 참고해요.
더 알아보기 (Learn more)
- React Aria Menu 문서 - ARIA 메뉴 패턴