Drawer
詳細パネルのように、画面の端からスライドして表示されるパネル。
デフォルトでは、Drawerは非モーダルなダイアログです。開いている間もページの残りの部分を引き続き使用できます。modalを指定すると、Dialogと同様にDialog (Modal) WAI-ARIAデザインパターンに従います。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Drawer } from '@leve-ui/drawer';
import { LuX } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
// Non-modal by default: the page stays usable while the panel is open
return (
<Drawer.Root class="drawer-default">
<Drawer.Trigger class="drawer-default-trigger">Order details</Drawer.Trigger>
<button type="button" class="drawer-default-trigger">
Another page action
</button>
<Drawer.Content class="drawer-default-content">
<Drawer.Title class="drawer-default-title">Order #1042</Drawer.Title>
<Drawer.Description class="drawer-default-description">
Shipped on November 12 to Osaka.
</Drawer.Description>
<dl class="drawer-default-list">
<dt>Items</dt>
<dd>3</dd>
<dt>Total</dt>
<dd>¥12,800</dd>
</dl>
<Drawer.Close class="drawer-default-close" aria-label="Close">
<LuX class="drawer-default-icon" aria-hidden="true" />
</Drawer.Close>
</Drawer.Content>
</Drawer.Root>
);
});.drawer-default {
display: flex;
gap: 8px;
font-family: inherit;
}
.drawer-default-trigger {
padding: 8px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.drawer-default-trigger:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* The Content is a native <dialog>; data-side places it on an edge of the screen */
.drawer-default-content {
box-sizing: border-box;
margin: 0;
padding: 24px;
border: none;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
.drawer-default-content[data-side="right"] {
inset: 0 0 0 auto;
width: min(320px, 100vw);
height: 100dvh;
max-height: none;
border-left: 1px solid light-dark(#cbd5e1, #ffffff3d);
}
/* Slide in when it opens */
.drawer-default-content[data-side="right"]:popover-open {
transition: translate 200ms ease;
@starting-style {
translate: 100% 0;
}
}
.drawer-default-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.drawer-default-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.drawer-default-list {
display: grid;
grid-template-columns: auto 1fr;
gap: 8px 16px;
margin: 0;
font-size: 14px;
}
.drawer-default-list dt {
color: light-dark(#475569, #c9ddf0);
}
.drawer-default-list dd {
margin: 0;
}
.drawer-default-close {
position: absolute;
top: 12px;
right: 12px;
display: inline-flex;
padding: 4px;
border: none;
border-radius: 6px;
background: none;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.drawer-default-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-default-icon {
width: 16px;
height: 16px;
}
.drawer-default-trigger:focus-visible,
.drawer-default-close:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}使い分け#
| コンポーネント | 用途 |
|---|---|
| Drawer | テーブル内の行の詳細など、ページが使用可能な状態のままコンテンツの横に開くパネル。 |
modalを指定したDrawer | 小さな画面でのナビゲーションメニューなど、ページをブロックするパネル。 |
| Dialog | ページに戻る前に完了しなければならないタスクや決定。 |
通常の<aside> | 常に表示され、開閉することのないパネル。ダイアログではなく、レイアウトの一部です。 |
インストール#
個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。
pnpm add @leve-ui/drawernpm install @leve-ui/draweryarn add @leve-ui/drawerbun add @leve-ui/drawerpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/core構造#
import { Drawer } from '@leve-ui/drawer'; // or '@leve-ui/core'
<Drawer.Root>
<Drawer.Trigger />
<Drawer.Content>
<Drawer.Title />
<Drawer.Description />
<Drawer.Close />
</Drawer.Content>
</Drawer.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Drawer.Root | <div> | パーツをラップし、開閉状態、modal、sideを保持します。 |
Drawer.Trigger | <button> | Drawerを開閉するボタンです。別の要素やコンポーネントをレンダリングするにはasを使用します。 |
Drawer.Content | <dialog> | パネル本体。 |
Drawer.Title | <h2> | ドロワーの名前を示すタイトル。 |
Drawer.Description | <p> | ドロワーの説明。 |
Drawer.Close | <button> | Drawerを閉じるボタンです。別の要素やコンポーネントをレンダリングするにはasを使用します。 |
Drawer.TriggerまたはDrawer.Closeのasに渡されるコンポーネントは、id、aria-*、data-*、およびon*$イベントハンドラーを含む受け取ったpropsを単一の要素にスプレッドする必要があります。
Drawer.Contentはネイティブの<dialog>であり、配置した場所にレンダリングされ、トップレイヤーでページの上に表示されます。非モーダルドロワーはshowPopover()で表示されるpopover="manual"要素であり、モーダルドロワーはshowModal()で開かれます。sideはdata-sideを設定するだけです。デモのようにCSSでパネルとそのアニメーションを配置してください。モーダルドロワーにはバックドロップがあり、Dialogと同様に::backdropでスタイルを設定します。
例#
Modal#
modalはページをブロックします。フォーカスはドロワー内にとどまり、ページはスクロールせず、バックドロップをクリックするとドロワーが閉じます。side="left"はdata-side="left"を設定します。
import { component$, type PropsOf, Slot, useStyles$ } from '@builder.io/qwik';
import { Drawer } from '@leve-ui/drawer';
import { LuMenu, LuX } from '@qwikest/icons/lucide';
import styles from './modal.css?inline';
// Custom button component passed via `as` must spread received props onto one element
const IconButton = component$<PropsOf<'button'>>(({ type = 'button', ...props }) => (
<button type={type} {...props}>
<Slot />
</button>
));
export default component$(() => {
useStyles$(styles);
// modal blocks the page like a Dialog, for example for navigation on small screens
return (
<Drawer.Root modal side="left" class="drawer-modal">
<Drawer.Trigger as={IconButton} class="drawer-modal-trigger" aria-label="Open navigation">
<LuMenu class="drawer-modal-icon" aria-hidden="true" />
</Drawer.Trigger>
<Drawer.Content class="drawer-modal-content">
<Drawer.Title class="drawer-modal-title">Navigation</Drawer.Title>
<Drawer.Description class="drawer-modal-description">
Go to a section of the site.
</Drawer.Description>
<nav class="drawer-modal-nav">
<a href="#home" class="drawer-modal-link">
Home
</a>
<a href="#projects" class="drawer-modal-link">
Projects
</a>
<a href="#settings" class="drawer-modal-link">
Settings
</a>
</nav>
<Drawer.Close as={IconButton} class="drawer-modal-close" aria-label="Close">
<LuX class="drawer-modal-icon" aria-hidden="true" />
</Drawer.Close>
</Drawer.Content>
</Drawer.Root>
);
});.drawer-modal-trigger {
display: inline-flex;
padding: 8px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
cursor: pointer;
}
.drawer-modal-trigger:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* The Content is a native <dialog>; data-side places it on an edge of the screen */
.drawer-modal-content {
box-sizing: border-box;
margin: 0;
padding: 24px;
border: none;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
.drawer-modal-content[data-side="left"] {
inset: 0 auto 0 0;
width: min(280px, 100vw);
height: 100dvh;
max-height: none;
}
/* A modal drawer has a backdrop, the ::backdrop of the <dialog> */
.drawer-modal-content::backdrop {
background: light-dark(rgb(15 23 42 / 0.5), rgb(0 0 0 / 0.6));
}
/* Slide in when it opens */
.drawer-modal-content[data-side="left"][open] {
transition: translate 200ms ease;
@starting-style {
translate: -100% 0;
}
}
.drawer-modal-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.drawer-modal-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.drawer-modal-nav {
display: flex;
flex-direction: column;
gap: 2px;
}
.drawer-modal-link {
padding: 8px 10px;
border-radius: 6px;
font-size: 14px;
color: inherit;
text-decoration: none;
}
.drawer-modal-link:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-modal-close {
position: absolute;
top: 12px;
right: 12px;
display: inline-flex;
padding: 4px;
border: none;
border-radius: 6px;
background: none;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.drawer-modal-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.drawer-modal-icon {
width: 16px;
height: 16px;
}
.drawer-modal-trigger:focus-visible,
.drawer-modal-link:focus-visible,
.drawer-modal-close:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}APIリファレンス#
Root#
| Prop | デフォルト | 型 |
|---|---|---|
| ids | – | { trigger?: string; content?: string; title?: string; description?: string } パーツの要素IDをオーバーライドします。 |
| defaultOpen | false | boolean ドロワーを初期状態で開いておくかどうか(非制御)。クライアント側から表示されるため、ページがレジュームしたときにドロワーが開きます。 |
| bind:open | – | Signal<boolean> 開閉状態を保持するシグナル(制御)。 |
| onOpenChange$ | – | QRL<(open: boolean) => void> ユーザーのインタラクションによってドロワーが開閉したときに呼び出されます。 |
| modal | false | boolean ドロワーをモーダルにするかどうか。Dialogと同様にフォーカスを閉じ込め、ページをブロックし、外部クリックで閉じます。デフォルトでは、ドロワーが開いている間もページを引き続き使用できます。 |
| side | 'right' | 'top' | 'right' | 'bottom' | 'left' ドロワーが取り付けられる画面の端。 |
すべての<div>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer | 常に存在 |
data-state | "open" | "closed" |
data-side | "top" | "right" | "bottom" | "left" |
開閉状態はdefaultOpenで非制御、またはbind:openで制御できます。onOpenChange$は、ユーザーがドロワーを開閉したときに呼び出されます。SSR HTMLは常にドロワーを閉じた状態でレンダリングします。初期状態で開いているドロワーは、ページがレジュームしたときに開きます。
Trigger#
| Prop | デフォルト | 型 |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any>
|
すべての<button>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer-trigger | 常に存在 |
data-state | "open" | "closed" |
トリガーはドロワーを切り替えます。非モーダルドロワーは、開いている間もトリガーを使用可能なままにします。
Content#
| Prop | デフォルト | 型 |
|---|---|---|
| closeOnEscape | true | boolean Escapeでドロワーを閉じるかどうか。 |
| closeOnOutsideClick | true | boolean バックドロップでのポインターダウンでモーダルドロワーを閉じるかどうか。非モーダルドロワーは、外部のインタラクションによって閉じることはありません。 |
すべての<dialog>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer-content | 常に存在 |
data-state | "open" | "closed" |
data-side | "top" | "right" | "bottom" | "left" |
非モーダルドロワーは表示中に:popover-openに一致し、モーダルドロワーにはopen属性が付与されます。これらを@starting-styleと一緒に使用して、ドロワーが表示される際のアニメーションを設定します。
タイトル#
独自のpropはありません。 すべての<h2>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer-title | 常に存在 |
説明#
独自のpropはありません。 すべての<p>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer-description | 常に存在 |
閉じる#
| Prop | デフォルト | 型 |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any>
|
すべての<button>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-drawer-close | 常に存在 |
data-state | "open" | "closed" |
アクセシビリティ#
非モーダルダイアログ用のWAI-ARIA APGパターンはありません。非モーダルドロワーはaria-modalのないrole="dialog"であり、ドロワーとページの間でフォーカスを自由に移動できます(ADR 0028)。modalを指定すると、DrawerはDialog (Modal) WAI-ARIAデザインパターンに準拠します。
Drawer.Contentは、Drawer.Titleを指すaria-labelledby、Drawer.Descriptionを指すaria-describedbyをレンダリングし、modalの場合にのみaria-modal="true"をレンダリングします。Drawer.Triggerは、aria-haspopup="dialog"、aria-expanded、およびContentを指すaria-controlsをレンダリングします。- ドロワーが開くと、フォーカスはその最初のフォーカス可能要素に移動します。閉じると、開く前にフォーカスされていた要素、またはTriggerにフォーカスが戻ります。フォーカスがページの他の場所にある間に非モーダルドロワーが閉じた場合、フォーカスは移動しません。
- 非モーダルドロワーはフォーカスを閉じ込めたり、ページのスクロールを止めたりせず、ページがクリックされても閉じません。モーダルドロワーはこれら3つすべてを行います。
Drawer.Closeにアイコンのみが含まれる場合は、aria-labelを指定してください。
キーボード操作#
| キー | 説明 |
|---|---|
Tab | 非モーダル:ドロワー内を順にフォーカス移動し、ページへと移動します。モーダル:ドロワー内のフォーカス可能要素間を循環します。 |
Shift + Tab | 同様に、逆方向にフォーカスを移動します。 |
Escape | closeOnEscapeがfalseでない限り、ドロワーを閉じます。 |