Dialog
ユーザーに入力や決定を求めるモーダルウィンドウです。
DialogはDialog (Modal) WAI-ARIAデザインパターンに準拠しています。
import { component$, useStyles$ } from '@builder.io/qwik';
import { Dialog } from '@leve-ui/dialog';
import { LuX } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Dialog.Root class="dialog-default">
<Dialog.Trigger class="dialog-default-trigger">Edit profile</Dialog.Trigger>
<Dialog.Content class="dialog-default-content">
<Dialog.Title class="dialog-default-title">Edit profile</Dialog.Title>
<Dialog.Description class="dialog-default-description">
Change how your name appears to others.
</Dialog.Description>
<label class="dialog-default-label">
Name
<input class="dialog-default-input" name="name" value="Ada Lovelace" />
</label>
<div class="dialog-default-actions">
<Dialog.Close class="dialog-default-button">Cancel</Dialog.Close>
<Dialog.Close class="dialog-default-button dialog-default-button-primary">
Save
</Dialog.Close>
</div>
<Dialog.Close class="dialog-default-close" aria-label="Close">
<LuX class="dialog-default-icon" aria-hidden="true" />
</Dialog.Close>
</Dialog.Content>
</Dialog.Root>
);
});.dialog-default-trigger,
.dialog-default-button {
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;
}
.dialog-default-trigger:hover,
.dialog-default-button:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-default-button-primary {
border-color: #009dfd;
background: #009dfd;
color: #ffffff;
}
.dialog-default-button-primary:hover {
background: #006ce9;
}
/* The Content is a native <dialog>; margin: auto centers it, also after a CSS reset */
.dialog-default-content {
position: relative;
margin: auto;
width: min(400px, calc(100vw - 32px));
padding: 24px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
/* The backdrop is the ::backdrop of the <dialog> */
.dialog-default-content::backdrop {
background: light-dark(rgb(15 23 42 / 0.5), rgb(0 0 0 / 0.6));
}
/* Fade in when the dialog opens */
.dialog-default-content[open],
.dialog-default-content[open]::backdrop {
transition: opacity 150ms ease;
@starting-style {
opacity: 0;
}
}
.dialog-default-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.dialog-default-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.dialog-default-label {
display: flex;
flex-direction: column;
gap: 6px;
font-size: 14px;
font-weight: 500;
}
.dialog-default-input {
padding: 8px 10px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
color: inherit;
}
.dialog-default-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 24px;
}
.dialog-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;
}
.dialog-default-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-default-icon {
width: 16px;
height: 16px;
}
.dialog-default-trigger:focus-visible,
.dialog-default-button:focus-visible,
.dialog-default-close:focus-visible,
.dialog-default-input:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}インストール#
個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。
pnpm add @leve-ui/dialognpm install @leve-ui/dialogyarn add @leve-ui/dialogbun add @leve-ui/dialogpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/core構造#
import { Dialog } from '@leve-ui/dialog'; // or '@leve-ui/core'
<Dialog.Root>
<Dialog.Trigger />
<Dialog.Content>
<Dialog.Title />
<Dialog.Description />
<Dialog.Close />
</Dialog.Content>
</Dialog.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Dialog.Root | <div> | 各パーツをラップし、開閉状態を保持します。 |
Dialog.Trigger | <button> | ダイアログを開くボタンです。別の要素やコンポーネントをレンダリングするにはasを使用します。 |
Dialog.Content | <dialog> | showModal()で開かれるダイアログです。 |
Dialog.Title | <h2> | ダイアログの名前を示すタイトルです。 |
Dialog.Description | <p> | ダイアログの説明です。 |
Dialog.Close | <button> | ダイアログを閉じるボタンです。別の要素やコンポーネントをレンダリングするにはasを使用します。 |
Dialog.TriggerまたはDialog.Closeのasに渡されるコンポーネントは、id、aria-*、data-*、およびon*$イベントハンドラーを含む受け取ったpropsを単一の要素にスプレッドする必要があります。
Dialog.Contentはネイティブの<dialog>です。配置した場所にレンダリングされ、ブラウザによってトップレイヤーのページ上部に表示されるため、祖先要素のoverflow: hiddenやz-indexによって切り取られることはありません。Overlayパーツはありません。Contentの::backdrop擬似要素でバックドロップのスタイルを設定してください。
[data-leve-dialog-content]::backdrop {
background: rgb(15 23 42 / 0.5);
}
例#
Controlled#
bind:openはシグナルで開閉状態を制御します。ここでは、保存後にSaveがコードからダイアログを閉じます。
import { $, component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Dialog } from '@leve-ui/dialog';
import styles from './controlled.css?inline';
export default component$(() => {
useStyles$(styles);
const open = useSignal(false);
const saved = useSignal(false);
const save$ = $(() => {
saved.value = true;
open.value = false;
});
return (
<Dialog.Root bind:open={open} class="dialog-controlled">
<Dialog.Trigger class="dialog-controlled-trigger">Rename</Dialog.Trigger>
<span class="dialog-controlled-status">
{open.value ? 'Open' : saved.value ? 'Saved' : 'Closed'}
</span>
<Dialog.Content class="dialog-controlled-content">
<Dialog.Title class="dialog-controlled-title">Rename file</Dialog.Title>
<Dialog.Description class="dialog-controlled-description">
The dialog closes from code after saving.
</Dialog.Description>
<label class="dialog-controlled-label">
File name
<input class="dialog-controlled-input" name="file" value="report.pdf" />
</label>
<div class="dialog-controlled-actions">
<Dialog.Close class="dialog-controlled-button">Cancel</Dialog.Close>
<button
type="button"
class="dialog-controlled-button dialog-controlled-button-primary"
onClick$={save$}
>
Save
</button>
</div>
</Dialog.Content>
</Dialog.Root>
);
});.dialog-controlled-trigger,
.dialog-controlled-button {
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;
}
.dialog-controlled-trigger:hover,
.dialog-controlled-button:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-controlled-button-primary {
border-color: #009dfd;
background: #009dfd;
color: #ffffff;
}
.dialog-controlled-button-primary:hover {
background: #006ce9;
}
/* The Content is a native <dialog>; margin: auto centers it, also after a CSS reset */
.dialog-controlled-content {
position: relative;
margin: auto;
width: min(400px, calc(100vw - 32px));
padding: 24px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
/* The backdrop is the ::backdrop of the <dialog> */
.dialog-controlled-content::backdrop {
background: light-dark(rgb(15 23 42 / 0.5), rgb(0 0 0 / 0.6));
}
/* Fade in when the dialog opens */
.dialog-controlled-content[open],
.dialog-controlled-content[open]::backdrop {
transition: opacity 150ms ease;
@starting-style {
opacity: 0;
}
}
.dialog-controlled-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.dialog-controlled-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.dialog-controlled-label {
display: flex;
flex-direction: column;
gap: 6px;
font-size: 14px;
font-weight: 500;
}
.dialog-controlled-input {
padding: 8px 10px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
color: inherit;
}
.dialog-controlled-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 24px;
}
.dialog-controlled-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;
}
.dialog-controlled-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-controlled-icon {
width: 16px;
height: 16px;
}
.dialog-controlled-trigger:focus-visible,
.dialog-controlled-button:focus-visible,
.dialog-controlled-close:focus-visible,
.dialog-controlled-input:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.dialog-controlled {
display: flex;
align-items: center;
gap: 12px;
font-family: inherit;
}
.dialog-controlled-status {
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}永続#
closeOnOutsideClick={false}を指定すると、バックドロップがクリックされてもダイアログを開いたままにします。Escapeやボタンでは引き続き閉じることができます。
import { component$, type PropsOf, Slot, useStyles$ } from '@builder.io/qwik';
import { Dialog } from '@leve-ui/dialog';
import styles from './persistent.css?inline';
// Custom button component passed via `as` must spread received props onto one element
const Button = component$<PropsOf<'button'>>(({ type = 'button', ...props }) => (
<button type={type} {...props}>
<Slot />
</button>
));
export default component$(() => {
useStyles$(styles);
return (
<Dialog.Root class="dialog-persistent">
<Dialog.Trigger as={Button} class="dialog-persistent-trigger">
Delete project
</Dialog.Trigger>
{/* Clicking the backdrop does not close it; Escape and the buttons still do */}
<Dialog.Content closeOnOutsideClick={false} class="dialog-persistent-content">
<Dialog.Title class="dialog-persistent-title">Delete project?</Dialog.Title>
<Dialog.Description class="dialog-persistent-description">
This deletes the project and its files. You cannot undo it.
</Dialog.Description>
<div class="dialog-persistent-actions">
<Dialog.Close as={Button} class="dialog-persistent-button">
Cancel
</Dialog.Close>
<Dialog.Close
as={Button}
class="dialog-persistent-button dialog-persistent-button-primary"
>
Delete
</Dialog.Close>
</div>
</Dialog.Content>
</Dialog.Root>
);
});.dialog-persistent-trigger,
.dialog-persistent-button {
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;
}
.dialog-persistent-trigger:hover,
.dialog-persistent-button:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-persistent-button-primary {
border-color: #009dfd;
background: #009dfd;
color: #ffffff;
}
.dialog-persistent-button-primary:hover {
background: #006ce9;
}
/* The Content is a native <dialog>; margin: auto centers it, also after a CSS reset */
.dialog-persistent-content {
position: relative;
margin: auto;
width: min(400px, calc(100vw - 32px));
padding: 24px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 16px 40px rgb(15 23 42 / 0.25);
}
/* The backdrop is the ::backdrop of the <dialog> */
.dialog-persistent-content::backdrop {
background: light-dark(rgb(15 23 42 / 0.5), rgb(0 0 0 / 0.6));
}
/* Fade in when the dialog opens */
.dialog-persistent-content[open],
.dialog-persistent-content[open]::backdrop {
transition: opacity 150ms ease;
@starting-style {
opacity: 0;
}
}
.dialog-persistent-title {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.dialog-persistent-description {
margin: 6px 0 20px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}
.dialog-persistent-label {
display: flex;
flex-direction: column;
gap: 6px;
font-size: 14px;
font-weight: 500;
}
.dialog-persistent-input {
padding: 8px 10px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
color: inherit;
}
.dialog-persistent-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 24px;
}
.dialog-persistent-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;
}
.dialog-persistent-close:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.dialog-persistent-icon {
width: 16px;
height: 16px;
}
.dialog-persistent-trigger:focus-visible,
.dialog-persistent-button:focus-visible,
.dialog-persistent-close:focus-visible,
.dialog-persistent-input: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> ユーザー操作によってダイアログが開閉されたときに呼び出されます。 |
すべての<div>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog | 常に存在 |
data-state | "open" | "closed" |
開閉状態はdefaultOpenで非制御に、またはbind:openで制御できます。onOpenChange$は、ユーザーがダイアログを開閉したときに呼び出されます。SSR HTMLでは常にダイアログが閉じた状態でレンダリングされます。showModal()はクライアントで実行されるため、初期状態で開いているダイアログはページのレジューム時に開きます。
Trigger#
| Prop | デフォルト | 型 |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any>
|
すべての<button>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog-trigger | 常に存在 |
data-state | "open" | "closed" |
Content#
| Prop | デフォルト | 型 |
|---|---|---|
| closeOnEscape | true | boolean Escapeでダイアログを閉じるかどうか。他の操作を行わずにEscapeを2回押すとブラウザによって閉じられる場合がありますが、状態はそれに追従します。 |
| closeOnOutsideClick | true | boolean ダイアログの外側(バックドロップ上)でのポインターダウンによってダイアログを閉じるかどうか。 |
すべての<dialog>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog-content | 常に存在 |
data-state | "open" | "closed" |
ブラウザはダイアログの表示中にopen属性を追加します。デモのように、[open]と@starting-styleを使用して、ダイアログとその::backdropの表示時にアニメーションを適用します。
タイトル#
独自のpropはありません。 すべての<h2>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog-title | 常に存在 |
説明#
独自のpropはありません。 すべての<p>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog-description | 常に存在 |
閉じる#
| Prop | デフォルト | 型 |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any>
|
すべての<button>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-dialog-close | 常に存在 |
data-state | "open" | "closed" |
アクセシビリティ#
Dialog (Modal) WAI-ARIAデザインパターンに準拠しています。
Dialog.ContentはshowModal()で開かれるネイティブの<dialog>であるため、開いている間はページの残りの部分がinertになります。aria-modal="true"、Dialog.Titleを指すaria-labelledby、およびDialog.Descriptionを指すaria-describedbyをレンダリングします。Dialog.Triggerは、aria-haspopup="dialog"、aria-expanded、およびContentを指すaria-controlsをレンダリングします。- ダイアログが開くと、フォーカスはその最初のフォーカス可能な要素に移動し(フォーカス可能な要素がない場合はContentに移動)、Tabによる移動はダイアログ内にとどまります。ダイアログが閉じると、開く前にフォーカスされていた要素、またはTriggerにフォーカスが戻ります。
- ダイアログが開いている間、その背面にあるページはスクロールしません。
Dialog.Closeにアイコンのみが含まれる場合は、aria-labelを指定してください。
キーボード操作#
| キー | 説明 |
|---|---|
Tab | ダイアログ内の次のフォーカス可能な要素にフォーカスを移動します。最後の要素からは、最初の要素に移動します。 |
Shift + Tab | ダイアログ内の前のフォーカス可能な要素にフォーカスを移動します。最初の要素からは、最後の要素に移動します。 |
Escape | closeOnEscapeがfalseでない限り、ダイアログを閉じます。 |