Dialog
現在の作業を中断して、ユーザーに 1 つのことを求める。フォーカスは中に閉じ込められ、Esc で閉じ、閉じると開いたボタンに戻る。取り消せない操作には確認用の ConfirmDialog を使い、外側をクリックしても閉じないようにする。
tsx
import { Dialog, DialogClose, ConfirmDialog } from "knowidea-ui";カテゴリ: Feedback。同じカテゴリ: Alert、Toast、Sheet、Progress、Skeleton、Spinner、EmptyState
Demos
Basic
入力を求める例。外側をクリックすると閉じる。
Show code
tsx
import { Dialog, DialogClose, Button, Field, Input } from "knowidea-ui";function Basic() { return ( <Dialog trigger={<Button variant="secondary">ダイアログを開く</Button>} title="名前を変更する" description="一覧と履歴の表示が変わる。" actions={<><DialogClose>キャンセル</DialogClose><DialogClose variant="primary">保存</DialogClose></>} > <Field label="新しい名前"><Input placeholder="例: sample-name" /></Field> </Dialog> );}With Select
中に Select や Menu などの開く部品を置ける。開いた一覧はダイアログの前に出て、マウスでもキーボードでも選べる。
Show code
tsx
import { Dialog, DialogClose, Button, Field, Select } from "knowidea-ui";/** 中に Select を置く例。開いた一覧はダイアログの前に出る */function WithSelect() { return ( <Dialog trigger={<Button variant="secondary">担当を選ぶ</Button>} title="担当を変える" actions={<><DialogClose>キャンセル</DialogClose><DialogClose variant="primary">保存</DialogClose></>} > <Field label="担当"> <Select aria-label="担当" placeholder="選ぶ" options={[ { value: "design", label: "デザイン" }, { value: "dev", label: "開発" }, { value: "review", label: "レビュー" }, ]} /> </Field> </Dialog> );}Confirm (ConfirmDialog)
取り消せない操作に使う。外側をクリックしても閉じず、最初のフォーカスはキャンセル側に置かれる。
Show code
tsx
import { ConfirmDialog, Button } from "knowidea-ui";function Confirm() { return ( <ConfirmDialog trigger={<Button variant="danger">削除する</Button>} title="「サンプル」を削除する" description="履歴も削除される。元に戻せない。" confirmLabel="削除する" /> );}Verify the result
確認ダイアログでリクエストを送り、リクエスト中の表示を経て、結果をマークで示す。反映まで時間がかかる操作に使う。
Show code
tsx
import { useState } from "react";import { ConfirmDialog, Button, Icon, Spinner, Status } from "knowidea-ui";/** 結果を確かめる操作: 確認ダイアログ → リクエスト中 → 完了 */function Verify() { const [state, setState] = useState<"idle" | "requested" | "done">("idle"); const request = () => { setState("requested"); setTimeout(() => setState("done"), 1200); }; if (state === "requested") return <span style={{ display: "inline-flex", gap: "var(--space-2)", alignItems: "center" }} role="status"><Spinner />リクエスト中(反映されるまで待つ)</span>; if (state === "done") { return ( <> <Status kind="stopped" label="停止した" /> <Button variant="quiet" icon={<Icon name="resume" />} onClick={() => setState("idle")}>再開</Button> </> ); } return ( <ConfirmDialog trigger={<Button variant="secondary" icon={<Icon name="stop" />}>停止する</Button>} title="「サンプルの処理」を停止する" description="停止をリクエストする。反映されるまでは、処理が動き続けることがある。" confirmLabel="停止をリクエスト" cancelLabel="キャンセル" onConfirm={request} /> );}Plain HTML
React を使わないページでは、knowidea-css の CSS を読み込み、クラスを指定する。enhance.js を読み込むと、data 属性で開閉できる。
Show code
html
<button type="button" class="ki-button" data-variant="secondary" data-ki-dialog-open="d1">ダイアログを開く</button><dialog id="d1" class="ki-dialog" aria-labelledby="d1-t"> <h2 id="d1-t" class="ki-dialog__title">名前を変更する</h2> <p class="ki-dialog__description">一覧と履歴の表示が変わる。</p> <div class="ki-dialog__actions"> <button type="button" class="ki-button" data-variant="secondary" data-ki-dialog-close>キャンセル</button> <button type="button" class="ki-button" data-ki-dialog-close>保存</button> </div></dialog>States
すべてのコンポーネントが、Empty、Loading、Error、Disabled、Focus の 5 つの状態を備える。Hover と Active は、確認用に固定して表示している。そのコンポーネントにあてはまらない状態は「対象外」とし、理由を添える。
| State | Appearance |
|---|---|
| Empty | 対象外。中身のないダイアログは作らない |
| Loading | |
| Error | |
| Disabled | |
| Focus | 開くと最初の要素にフォーカスが移る。確認ダイアログではキャンセル側が先 |
| Hover | 対象外。ダイアログ自体は変わらない。中のボタンが変わる |
| Active | 対象外。押したときの見た目は、開くボタン(Button)のものを使う。確認ダイアログは「キャンセル」と「削除する」で閉じる |
API
| Name | Type | Default | Description |
|---|---|---|---|
trigger | ReactElement | - | ダイアログを開くボタン。要素 1 つ |
title | ReactNode | - | タイトル。必須。スクリーンリーダー向けの名前になる |
description | ReactNode | - | 説明。1〜2 文 |
actions | ReactNode | - | 下部の操作。DialogClose を使うと閉じる |
loading | boolean | false | 本文の代わりにスピナーと文言を表示する |
open / defaultOpen / onOpenChange | boolean | - | 制御と非制御 |
ConfirmDialog: confirmLabel | string | - | 実行ボタンの文言。「削除する」のように、何をするのかを書く。「OK」は使わない |
ConfirmDialog: tone | "danger" | "primary" | "danger" | 取り消せない操作は danger にする |
ConfirmDialog: onConfirm | () => void | - | 実行時に呼ばれる |
CSS
Classes and data attributes
| Name | Description |
|---|---|
.ki-dialog | 素の HTML では <dialog> に付けて showModal() で開く(フォーカスの閉じ込めと Esc はブラウザが担う) |
.ki-dialog__title、__description、__actions | タイトル、説明、操作 |
.ki-dialog__popup、__backdrop | Base UI の Popup と Backdrop に当てる |
data-ki-dialog-open、data-ki-dialog-close | enhance.js が開閉を実行する |
CSS variables
コンポーネントが参照するのは、役割を表す名前の変数だけで、素の値は参照しない。オーバーライドの方法は Overriding を参照。
| Variable | Usage |
|---|---|
| --color-surface、--color-line | 背景、枠線 |
| --shadow-pop | 影(浮かび上がる要素用) |
| --color-bg | 背面を覆うオーバーレイ(70%) |
| --radius-3、--space-4、--space-5 | 角丸、画面端との余白(16px)、内側の余白 |
| --motion-duration-state | 表示と非表示のアニメーション。prefers-reduced-motion で止まる |
Accessibility
- Base UI の Dialog を使う。フォーカスを閉じ込め、Esc で閉じ、閉じると開いたボタンにフォーカスが戻る
- title がスクリーンリーダー向けの名前になる。必須
- 確認ダイアログは alertdialog になる。最初のフォーカスはキャンセル側に置かれ、Enter で誤って実行することがない