Drawer는 보조 콘텐츠와 동작을 위한 슬라이드 아웃 패널이에요. 해요체로 쉽게 설명드릴게요.
출처: HeroUI Drawer
본문
사용법 (Usage)
import { Drawer, Button } from "@heroui/react";
{"name": "drawer-basic"}
구조 (Anatomy)
import { Drawer, Button } from "@heroui/react";
export default () => (
<Drawer>
<Button>Open Drawer</Button>
<Drawer.Backdrop>
<Drawer.Content>
<Drawer.Dialog>
<Drawer.Handle /> {/* Optional: Drag handle */}
<Drawer.CloseTrigger /> {/* Optional: Close button */}
<Drawer.Header>
<Drawer.Heading />
</Drawer.Header>
<Drawer.Body />
<Drawer.Footer />
</Drawer.Dialog>
</Drawer.Content>
</Drawer.Backdrop>
</Drawer>
);
예시 (Examples)
배치 (Placement)
{"name": "drawer-placements"}
닫기 불가능 (Non-Dismissable)
Drawer.Backdrop에 isDismissable={false}를 설정하면 바깥 클릭이나 드래그로 닫히는 것을 막을 수 있어요. 사용자는 드로어의 동작 버튼과 상호작용해야 해요.
{"name": "drawer-non-dismissable"}
스크롤 가능한 콘텐츠 (Scrollable Content)
Drawer.Body는 네이티브 스크롤로 오버플로를 자동 처리해요. 드래그로 닫기는 스크롤 충돌을 피하기 위해 본문 영역에서 제외돼요.
{"name": "drawer-scrollable-content"}
제어된 상태 (Controlled State)
{"name": "drawer-controlled"}
{"name": "drawer-with-form"}
내비게이션 드로어 (Navigation Drawer)
{"name": "drawer-navigation"}
백드롭 변형 (Backdrop Variants)
{"name": "drawer-backdrop-variants"}
커스터마이징 (Customization)
Tailwind CSS
{"name": "drawer-custom-styles"}
Global CSS
Drawer 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.drawer__backdrop {
@apply bg-gradient-to-br from-black/50 to-black/70;
}
.drawer__dialog {
@apply rounded-2xl border border-white/10 shadow-2xl;
}
.drawer__header {
@apply text-center;
}
.drawer__close-trigger {
@apply rounded-full bg-white/10 hover:bg-white/20;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.
CSS 클래스 (CSS Classes)
Drawer 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 [!toc]
.drawer__trigger - 드로어를 여는 트리거 요소
.drawer__backdrop - 드로어 뒤의 오버레이 백드롭
.drawer__content - 드로어 패널의 위치 지정 래퍼
.drawer__dialog - 드로어 패널 자체
.drawer__header - 제목용 헤더 섹션
.drawer__heading - 주요 제목 텍스트
.drawer__body - 주요 스크롤 가능 콘텐츠 영역
.drawer__footer - 동작용 푸터 섹션
.drawer__handle - 시각적 드래그 핸들 인디케이터
.drawer__close-trigger - 닫기 버튼 요소
백드롭 변형 [!toc]
.drawer__backdrop--opaque - 불투명 색상 백드롭 (기본값)
.drawer__backdrop--blur - 유리 효과가 있는 블러 백드롭
.drawer__backdrop--transparent - 투명 백드롭 (오버레이 없음)
배치 변형 [!toc]
.drawer__content--bottom - 하단 가장자리에서 위로 슬라이드 (기본값)
.drawer__content--top - 상단 가장자리에서 아래로 슬라이드
.drawer__content--left - 왼쪽 가장자리에서 안으로 슬라이드
.drawer__content--right - 오른쪽 가장자리에서 안으로 슬라이드
다이얼로그 변형 [!toc]
.drawer__dialog--top - 상단 가장자리에서 아래로 슬라이드
.drawer__dialog--bottom - 하단 가장자리에서 위로 슬라이드
.drawer__dialog--left - 왼쪽 가장자리에서 안으로 슬라이드
.drawer__dialog--right - 오른쪽 가장자리에서 안으로 슬라이드
상호작용 상태 (Interactive States)
컴포넌트는 다음 상호작용 상태를 지원해요:
- 포커스 (Focus):
:focus-visible 또는 [data-focus-visible="true"] - 트리거와 닫기 버튼에 적용
- 호버 (Hover):
:hover 또는 [data-hovered="true"] - 호버 시 닫기 버튼에 적용
- 활성 (Active):
:active 또는 [data-pressed="true"] - 누를 때 트리거와 닫기 버튼에 적용
- 진입 (Entering):
[data-entering] - 드로어 열림 애니메이션 중 적용
- 종료 (Exiting):
[data-exiting] - 드로어 닫힘 애니메이션 중 적용
- 배치 (Placement):
[data-placement="*"] - 드로어 위치(top, bottom, left, right)에 따라 적용
API 참고 (API Reference)
Drawer
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Trigger and backdrop elements |
state |
UseOverlayStateReturn |
- |
Controlled overlay state |
Drawer.Trigger
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Custom trigger content |
className |
string |
- |
CSS classes |
Drawer.Backdrop
| Prop |
Type |
Default |
Description |
variant |
"opaque" | "blur" | "transparent" |
"opaque" |
Backdrop overlay style |
isDismissable |
boolean |
true |
Close on backdrop click |
isKeyboardDismissDisabled |
boolean |
false |
Disable ESC key to close |
isOpen |
boolean |
- |
Controlled open state |
onOpenChange |
(isOpen: boolean) => void |
- |
Open state change handler |
className |
string | (values) => string |
- |
Backdrop CSS classes |
Drawer.Content
| Prop |
Type |
Default |
Description |
placement |
"top" | "bottom" | "left" | "right" |
"bottom" |
Edge the drawer slides from |
className |
string | (values) => string |
- |
Content CSS classes |
Drawer.Dialog
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Dialog content |
className |
string |
- |
CSS classes |
role |
string |
"dialog" |
ARIA role |
aria-label |
string |
- |
Accessibility label |
aria-labelledby |
string |
- |
ID of label element |
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Header content |
className |
string |
- |
CSS classes |
Drawer.Heading
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Title text |
className |
string |
- |
CSS classes |
Drawer.Body
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Body content |
className |
string |
- |
CSS classes |
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Footer content |
className |
string |
- |
CSS classes |
Drawer.Handle
| Prop |
Type |
Default |
Description |
className |
string |
- |
CSS classes |
Drawer.CloseTrigger
| Prop |
Type |
Default |
Description |
children |
ReactNode |
- |
Custom close button |
className |
string | (values) => string |
- |
CSS classes |
useOverlayState 훅 (Hook)
import { useOverlayState } from "@heroui/react";
const state = useOverlayState({
defaultOpen: false,
onOpenChange: (isOpen) => console.log(isOpen),
});
state.isOpen; // Current state
state.open(); // Open drawer
state.close(); // Close drawer
state.toggle(); // Toggle state
state.setOpen(); // Set state directly
접근성 (Accessibility)
WAI-ARIA Dialog 패턴을 구현해요:
- 포커스 트랩: 열려 있을 때 드로어 내부로 포커스 고정
- 키보드:
ESC는 닫기(dismissable일 때), Tab은 요소 순회
- 화면 낭독기: React Aria를 통한 적절한 ARIA 속성
- 스크롤 잠금: 열려 있을 때 본문 스크롤 비활성화
- 드래그로 닫기: 핸들, 헤더, 푸터 영역에서 포인터 기반 드래그 제스처 지원
더 알아보기 (Learn more)