コンポーネント工房:Reflexライブラリ100
·
講座の概要
レイアウト
·
Box
·
Flex
·
Stack
·
Grid
·
Container
·
Section
·
Center
·
Spacer
·
Separator
·
Card
·
Inset
·
Aspect Ratio
·
Fragment
タイポグラフィ
·
Text
·
Heading
·
Link
·
Code
·
Markdown
·
Blockquote
·
Quote
·
Strong
·
Em
·
Kbd
フォーム
·
Button
·
Input
·
Text Area
·
Checkbox
·
Switch
·
Radio Group
·
Select
·
Slider
·
Form
·
Upload
データ表示
·
Badge
·
Avatar
·
Icon
·
Callout
·
List
·
Data List
·
Code Block
·
Progress
·
Spinner
·
Scroll Area
·
Moment
開閉と切り替え
·
Tabs
·
Accordion
·
Segmented Control
オーバーレイ
·
Tooltip
·
Popover
·
Hover Card
·
Dialog
·
Alert Dialog
·
Drawer
·
Dropdown Menu
·
Context Menu
·
Toast
動的レンダリング
·
Cond
·
Match
·
Foreach
·
Auto Scroll
メディア
·
Image
·
Audio
·
Video
表とデータグリッド
·
Table
·
Data Table
·
Data Editor
チャート
·
Area Chart
·
Bar Chart
·
Line Chart
·
Composed Chart
·
Pie Chart
·
Radar Chart
·
Radial Bar Chart
·
Scatter Chart
·
Funnel Chart
·
Treemap
·
Sankey Chart
·
Error Bar
チャートの部品
·
Axis
·
Cartesian Grid
·
Chart Tooltip
·
Legend
·
Label
·
Reference
·
Brush
ほかのチャートライブラリ
·
Plotly
·
Pyplot
HTML要素
·
HTML
·
HTML Text
·
HTML Layout
·
HTML Forms
·
HTML Media
·
HTML Tables
·
SVG
そのほかの道具
·
Theme
·
Skeleton
·
Clipboard
·
Html Embed
·
Script
·
Memo
rx.alert_dialog.rootrx.alert_dialog.triggerrx.alert_dialog.contentrx.alert_dialog.titlerx.alert_dialog.descriptionrx.alert_dialog.actionrx.alert_dialog.cancel公式ドキュメント
工房の注文書
プロジェクト一覧のゴミ箱ボタンを押したらすぐに消さず、本当に削除するかもう一度たずねるウィンドウを出してください。
rx.alert_dialogは、ユーザーの操作の流れを止めて答えを受け取る確認ウィンドウです。通常のDialogと違ってウィンドウの外を押しても閉じないので、ユーザーは「キャンセル」か「実行」のどちらかを選ぶことになります(Escキーはキャンセル扱い)。削除や権限の取り消しのように、元に戻しにくい操作の前に置きます。
alert_dialog.root:すべてのパーツを包みます。alert_dialog.trigger:押すとウィンドウを開く要素を包みます。alert_dialog.content:ウィンドウの中身です。alert_dialog.title:ウィンドウのタイトルです。開いたときにスクリーンリーダーが読み上げます。alert_dialog.description:補足の説明です(省略可)。開いたときに一緒に読み上げられます。alert_dialog.action:実行ボタンを包みます。押すとウィンドウが閉じます。alert_dialog.cancel:キャンセルボタンを包みます。押すとウィンドウが閉じます。実行ボタンと見た目で区別できるようにします。rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.button("Revoke access"),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Revoke access"),
rx.alert_dialog.description(
"Are you sure? This application will no longer be accessible and any existing sessions will be expired.",
),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel"),
),
rx.alert_dialog.action(
rx.button("Revoke access"),
),
spacing="3",
),
),
)ウィンドウの余白はcontentのsizeで、ボタンの配置は包んでいるrx.flexのjustifyで決めます。実行ボタンには、操作の重さに合った色を付けます。
プロパティを変えてみる
title
action
color
size
justify
rx.alert_dialog.root(
rx.alert_dialog.trigger(rx.button("Delete", color_scheme="red")),
rx.alert_dialog.content(
rx.alert_dialog.title("Delete this file?"),
rx.alert_dialog.description("This action cannot be undone.", size="2"),
rx.flex(
rx.alert_dialog.cancel(rx.button("Cancel", variant="soft", color_scheme="gray")),
rx.alert_dialog.action(rx.button("Delete", color_scheme="red")),
spacing="3",
margin_top="16px",
justify="end",
),
size="3",
max_width="450px",
),
)| パーツ | プロパティ | 値 | 用途 |
|---|---|---|---|
root | open | True / False | 開閉をStateで決める(制御) |
root | default_open | True / False | 最初の描画で開いておく(非制御) |
root | on_open_change | イベント | 開閉したとき。新しい開閉値(bool)を渡す |
content | size | "1" ~ "4" | 余白と角の丸みの大きさ |
content | force_mount | True / False | 閉じていてもDOMに残す(アニメーション用) |
content | on_open_auto_focus · on_close_auto_focus | イベント | 開くとウィンドウ内へ、閉じるとトリガーへフォーカスが移るとき |
content | on_escape_key_down | イベント | Escキーを押したとき |
trigger、title、description、action、cancelには共通のイベントしかありません。Dialogのcontentにあった「外側を押す」イベントは、ここにはありません。外側を押しても閉じないからです。
実行ボタンとトリガーを赤くし、キャンセルはグレーのsoftで控えめにして、2つのボタンをjustify="end"で右側に寄せた形です。ウィンドウの幅はstyle={"max_width": 450}で狭めています。
rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.button("Revoke access", color_scheme="red"),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Revoke access"),
rx.alert_dialog.description(
"Are you sure? This application will no longer be accessible and any existing sessions will be expired.",
size="2",
),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel", variant="soft", color_scheme="gray"),
),
rx.alert_dialog.action(
rx.button("Revoke access", color_scheme="red", variant="solid"),
),
spacing="3",
margin_top="16px",
justify="end",
),
style={"max_width": 450},
),
)rx.insetで表をウィンドウの左右の端まで広げれば、削除する対象をひと目で見せられます。
rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.button("Delete Users", color_scheme="red"),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Delete Users"),
rx.alert_dialog.description(
"Are you sure you want to delete these users? This action is permanent and cannot be undone.",
size="2",
),
rx.inset(
rx.table.root(
rx.table.header(
rx.table.row(
rx.table.column_header_cell("Full Name"),
rx.table.column_header_cell("Email"),
rx.table.column_header_cell("Group"),
),
),
rx.table.body(
rx.table.row(
rx.table.row_header_cell("Danilo Rosa"),
rx.table.cell("danilo@example.com"),
rx.table.cell("Developer"),
),
rx.table.row(
rx.table.row_header_cell("Zahra Ambessa"),
rx.table.cell("zahra@example.com"),
rx.table.cell("Admin"),
),
),
),
side="x",
margin_top="24px",
margin_bottom="24px",
),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel", variant="soft", color_scheme="gray"),
),
rx.alert_dialog.action(
rx.button("Delete users", color_scheme="red"),
),
spacing="3",
justify="end",
),
style={"max_width": 500},
),
)alert_dialog.rootのon_open_changeは、開くとTrue、閉じるとFalseを渡します。
Opened or closed: 0 times
Alert dialog open: no
class AlertDialogEventState(rx.State):
num_opens: int = 0
opened: bool = False
@rx.event
def count_opens(self, value: bool):
self.opened = value
self.num_opens += 1
rx.flex(
rx.text("Opened or closed: ", AlertDialogEventState.num_opens, " times"),
rx.text("Alert dialog open: ", rx.cond(AlertDialogEventState.opened, "yes", "no")),
rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.button("Revoke access", color_scheme="red"),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Revoke access"),
rx.alert_dialog.description(
"Are you sure? This application will no longer be accessible and any existing sessions will be expired.",
size="2",
),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel", variant="soft", color_scheme="gray"),
),
rx.alert_dialog.action(
rx.button("Revoke access", color_scheme="red", variant="solid"),
),
spacing="3",
margin_top="16px",
justify="end",
),
style={"max_width": 450},
),
on_open_change=AlertDialogEventState.count_opens,
),
direction="column",
spacing="3",
align="start",
)openにStateの値をつなぐと、alert_dialog.triggerなしで、ウィンドウの外にあるボタンからウィンドウを開けます。ウィンドウ内のキャンセル・実行ボタンも、同じハンドラーで値を反転させてウィンドウを閉じます。Escキーで閉じたときもStateが追いつくよう、on_open_changeも一緒につなぎます。
公式ドキュメントの例はself.opened = ~self.openedで値を反転させていますが、Pythonの~はboolを整数(-1、-2)に変えてしまいます。ここではnot self.openedを使います。
class AlertDialogControlState(rx.State):
opened: bool = False
@rx.event
def dialog_open(self):
self.opened = not self.opened
@rx.event
def set_opened(self, value: bool):
self.opened = value
rx.box(
rx.alert_dialog.root(
rx.alert_dialog.content(
rx.alert_dialog.title("Revoke access"),
rx.alert_dialog.description(
"Are you sure? This application will no longer be accessible and any existing sessions will be expired.",
),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel", on_click=AlertDialogControlState.dialog_open),
),
rx.alert_dialog.action(
rx.button("Revoke access", on_click=AlertDialogControlState.dialog_open),
),
spacing="3",
),
),
open=AlertDialogControlState.opened,
on_open_change=AlertDialogControlState.set_opened,
),
rx.button("Button to Open the Dialog", on_click=AlertDialogControlState.dialog_open),
)Dialogと同じやり方でrx.formを入れられます。送信ボタンをalert_dialog.actionで包めば、送信と同時にウィンドウが閉じます。実際のデータベース保存はDialogのページのコードと同じです。
class AlertDialogFormState(rx.State):
current_user: dict[str, str] = {}
@rx.event
def add_user(self, form_data: dict):
self.current_user = form_data
# Here you would save the user to a database (see the Database course).
return rx.toast.info(
f"User {form_data.get('name', '')} has been added.", position="bottom-right"
)
rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.button(rx.icon("plus", size=26), rx.text("Add User", size="4")),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Add New User"),
rx.alert_dialog.description("Fill the form with the user's info"),
rx.form(
rx.flex(
rx.input(placeholder="User Name", name="name"),
rx.input(placeholder="user@reflex.dev", name="email"),
rx.flex(
rx.alert_dialog.cancel(
rx.button("Cancel", variant="soft", color_scheme="gray"),
),
rx.alert_dialog.action(
rx.button("Submit", type="submit"),
),
spacing="3",
justify="end",
),
direction="column",
spacing="4",
),
on_submit=AlertDialogFormState.add_user,
reset_on_submit=False,
),
max_width="450px",
),
)一覧の項目ごとにゴミ箱ボタンと確認ウィンドウを付けます。実行ボタンのon_clickに削除する名前を渡し、ハンドラーが一覧からその項目を取り除いてから通知を表示します。キャンセルやEscを押した場合は何も起こりません。
Website
Mobile app
Newsletter
class AlertDialogProjectState(rx.State):
projects: list[str] = ["Website", "Mobile app", "Newsletter"]
@rx.event
def delete_project(self, name: str):
self.projects = [p for p in self.projects if p != name]
return rx.toast.success(f"Deleted {name}")
@rx.event
def restore(self):
self.projects = ["Website", "Mobile app", "Newsletter"]
rx.vstack(
rx.foreach(
AlertDialogProjectState.projects,
lambda name: rx.flex(
rx.text(name),
rx.spacer(),
rx.alert_dialog.root(
rx.alert_dialog.trigger(
rx.icon_button(rx.icon("trash-2"), color_scheme="red", variant="ghost", aria_label="Delete"),
),
rx.alert_dialog.content(
rx.alert_dialog.title("Delete project?"),
rx.alert_dialog.description(
"The project ", rx.text.strong(name), " and all its files will be deleted.",
size="2",
),
rx.flex(
rx.alert_dialog.cancel(rx.button("Cancel", variant="soft", color_scheme="gray")),
rx.alert_dialog.action(
rx.button("Delete", color_scheme="red", on_click=AlertDialogProjectState.delete_project(name)),
),
spacing="3",
margin_top="16px",
justify="end",
),
max_width="420px",
),
),
width="100%",
align="center",
padding="6px 10px",
border=f"1px solid {rx.color('gray', 5)}",
border_radius="6px",
),
),
rx.button("Restore all", size="1", variant="soft", on_click=AlertDialogProjectState.restore),
width="100%",
max_width="360px",
)工房ノート
root · trigger · content · title · description · action · cancel。外側を押しても閉じない。
actionとcancelはどちらもウィンドウを閉じる。実際の処理はaction内のボタンのon_clickにつなぐ。
実行ボタンは操作の重さに合った色(削除ならred)、キャンセルはグレーのsoftで区別する。
openで制御するときはon_open_changeもつなぎ、Escで閉じた場合をStateに戻す。
元に戻せる操作なら、確認ウィンドウの代わりに実行後の「元に戻す」通知(Toast)も検討する。
新しい授業・講座のお知らせを受け取る
この講座に授業が追加されたり、続きの講座が公開されたりしたらメールでお知らせします。
R
ReflexKR
触って学ぶPythonとReflex。すべての例はReflex 0.10.0で実際に動かして確認しています。
ReflexKRはバナー広告なし・無料で運営しています。役に立ったら、コーヒー一杯で応援してください。 ☕ コーヒーで応援する
アフィリエイトと支援について© 2026 ReflexKR · Since 2026 · CC BY-NC 4.0
質問と回答
この回について気になることを書き込んでください。すべての言語のページの質問がここに集まります。