1use std::rc::Rc;
12
13use dioxus::prelude::*;
14
15use super::dialog_form::DialogFormContext;
16use super::shared::{button_class, focus_stored, surface};
17use crate::class::{Glass, compose};
18use crate::hooks::{Controllable, use_controllable};
19use crate::overlay::{DismissLayer, is_escape};
20use crate::{ButtonSize, ButtonVariant};
21
22#[derive(Clone, Copy, PartialEq)]
26pub struct DialogController {
27 open: Signal<bool>,
28 trigger: Signal<Option<Rc<MountedData>>>,
29}
30
31impl DialogController {
32 pub fn is_open(&self) -> bool {
34 *self.open.read()
35 }
36
37 pub fn set_open(&self, open: bool) {
39 let mut state = self.open;
40
41 if *state.peek() != open {
42 state.set(open);
43 }
44 }
45
46 pub fn open(&self) {
48 self.set_open(true);
49 }
50
51 pub fn close(&self) {
53 self.set_open(false);
54 }
55
56 pub fn toggle(&self) {
58 let open = *self.open.peek();
59 self.set_open(!open);
60 }
61
62 pub(super) fn is_open_untracked(&self) -> bool {
64 *self.open.peek()
65 }
66
67 pub(super) fn set_trigger(&self, node: Option<Rc<MountedData>>) {
69 let mut trigger = self.trigger;
70 trigger.set(node);
71 }
72}
73
74pub fn use_dialog() -> DialogController {
77 use_hook(|| DialogController {
78 open: Signal::new(false),
79 trigger: Signal::new(None),
80 })
81}
82
83#[derive(Props, Clone, PartialEq)]
85pub struct DialogTriggerProps {
86 pub dialog: DialogController,
88 #[props(default)]
90 pub variant: ButtonVariant,
91 #[props(default)]
93 pub size: ButtonSize,
94 #[props(default)]
96 pub disabled: bool,
97 pub onclick: Option<EventHandler<MouseEvent>>,
99 #[props(into)]
101 pub class: Option<String>,
102 #[props(extends = GlobalAttributes, extends = button)]
104 pub attributes: Vec<Attribute>,
105 pub children: Element,
107}
108
109#[component]
111pub fn DialogTrigger(props: DialogTriggerProps) -> Element {
112 let dialog = props.dialog;
113 let onclick = props.onclick;
114 let mut trigger = dialog.trigger;
115 let class = button_class(props.variant, props.size, props.class.as_deref());
116
117 rsx! {
118 button {
119 aria_haspopup: "dialog",
120 class,
121 "data-slot": "button",
122 disabled: props.disabled,
123 r#type: "button",
124 onclick: move |event: MouseEvent| {
125 if let Some(handler) = onclick {
126 handler.call(event.clone());
127 }
128
129 if event.default_action_enabled() {
130 dialog.open();
131 }
132 },
133 onmounted: move |event| trigger.set(Some(event.data())),
134 ..props.attributes,
135 span { class: "ui-button__content", {props.children} }
136 }
137 }
138}
139
140#[derive(Clone, Copy)]
143pub(super) struct DialogHandle {
144 state: Controllable<bool>,
145 controller: CopyValue<Option<DialogController>>,
146 locked: Signal<bool>,
148}
149
150impl DialogHandle {
151 pub(super) fn set_open(self, open: bool) {
152 if let Some(controller) = *self.controller.peek() {
153 controller.set_open(open);
154 }
155
156 self.state.set(open);
157 }
158
159 pub(super) fn is_open(self) -> bool {
161 self.state.get()
162 }
163
164 fn dismiss(self) {
167 if !*self.locked.peek() {
168 self.set_open(false);
169 }
170 }
171
172 pub(super) fn set_locked(self, locked: bool) {
174 let mut state = self.locked;
175
176 if *state.peek() != locked {
177 state.set(locked);
178 }
179 }
180
181 pub(super) fn release_lock(self) {
183 let mut state = self.locked;
184
185 if let Ok(mut locked) = state.try_write() {
186 *locked = false;
187 }
188 }
189}
190
191pub(super) fn use_dialog_handle(
192 controller: Option<DialogController>,
193 open: Option<bool>,
194 default_open: bool,
195 on_open_change: Option<EventHandler<bool>>,
196) -> DialogHandle {
197 let open_value = controller.map(|owner| owner.is_open()).or(open);
198 let state = use_controllable(open_value, || default_open, on_open_change);
199 let mut stored = use_hook(|| CopyValue::new(controller));
200 stored.set(controller);
201 let locked = use_signal(|| false);
202 let handle = DialogHandle {
203 state,
204 controller: stored,
205 locked,
206 };
207
208 use_context_provider(|| handle);
209 handle
210}
211
212#[derive(Clone, Copy, PartialEq, Eq)]
213pub(super) enum DialogKind {
214 Dialog,
215 Sheet,
216 Drawer,
217}
218
219pub(super) struct DialogShell {
220 pub(super) handle: DialogHandle,
221 pub(super) kind: DialogKind,
222 pub(super) alert: bool,
223 pub(super) class: String,
224 pub(super) layout: Option<&'static str>,
225 pub(super) surface: &'static str,
226 pub(super) dismiss_on_escape: bool,
227 pub(super) dismiss_on_outside_press: bool,
228 pub(super) initial_focus: Option<Signal<Option<Rc<MountedData>>>>,
230 pub(super) attributes: Vec<Attribute>,
231 pub(super) children: Element,
232}
233
234fn use_dialog_focus(
237 open: bool,
238 handle: DialogHandle,
239 node: Signal<Option<Rc<MountedData>>>,
240 initial_focus: Option<Signal<Option<Rc<MountedData>>>>,
241) {
242 let mut was_open = use_hook(|| CopyValue::new(false));
243
244 use_effect(use_reactive((&open,), move |(open,)| {
245 let previous = *was_open.peek();
246 was_open.set(open);
247
248 if open && !previous {
249 let initial = initial_focus.and_then(|target| target.peek().clone());
250 focus_stored(initial.or_else(|| node.peek().clone()));
251 } else if !open
252 && previous
253 && let Some(controller) = *handle.controller.peek()
254 {
255 focus_stored(controller.trigger.peek().clone());
256 }
257 }));
258}
259
260pub(super) fn render_dialog(shell: DialogShell) -> Element {
261 let DialogShell {
262 handle,
263 kind,
264 alert,
265 class,
266 layout,
267 surface,
268 dismiss_on_escape,
269 dismiss_on_outside_press,
270 initial_focus,
271 attributes,
272 children,
273 } = shell;
274 let open = handle.state.get();
275 let locked = *handle.locked.read();
276 let mut node = use_signal(|| None::<Rc<MountedData>>);
277 use_dialog_focus(open, handle, node, initial_focus);
278 let lifted = open && kind != DialogKind::Dialog;
280
281 rsx! {
282 if open {
283 DismissLayer {
284 modal: true,
285 on_dismiss: move |()| {
286 if dismiss_on_outside_press {
287 handle.dismiss();
288 }
289 },
290 }
291 }
292 dialog {
293 aria_modal: "true",
294 class,
295 "data-dismiss-locked": locked.then_some(""),
296 "data-layout": layout,
297 "data-surface": surface,
298 "data-ui-alert-dialog": alert.then_some("true"),
299 "data-ui-bound": "true",
300 "data-ui-dialog": (!alert).then_some("true"),
301 "data-ui-drawer": (kind == DialogKind::Drawer).then_some("true"),
302 "data-ui-sheet": (kind == DialogKind::Sheet).then_some("true"),
303 open,
304 role: if alert { "alertdialog" } else { "dialog" },
305 style: lifted.then_some("z-index: 50"),
306 tabindex: open.then_some("-1"),
307 onkeydown: move |event: KeyboardEvent| {
308 if !is_escape(&event) || !event.default_action_enabled() {
309 return;
310 }
311
312 event.prevent_default();
313 event.stop_propagation();
314
315 if dismiss_on_escape {
316 handle.dismiss();
317 }
318 },
319 onmounted: move |event| node.set(Some(event.data())),
320 ..attributes,
321 {children}
322 }
323 }
324}
325
326#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
328#[non_exhaustive]
329pub enum DialogLayout {
330 #[default]
332 Centered,
333 Fullscreen,
335 Responsive,
337}
338
339impl DialogLayout {
340 pub fn as_str(self) -> &'static str {
342 match self {
343 Self::Centered => "centered",
344 Self::Fullscreen => "fullscreen",
345 Self::Responsive => "responsive",
346 }
347 }
348}
349
350#[derive(Props, Clone, PartialEq)]
352pub struct DialogProps {
353 pub dialog: Option<DialogController>,
356 pub open: Option<bool>,
358 #[props(default)]
360 pub default_open: bool,
361 pub on_open_change: Option<EventHandler<bool>>,
363 #[props(default)]
365 pub alert: bool,
366 #[props(default = true)]
368 pub dismiss_on_escape: bool,
369 pub dismiss_on_outside_press: Option<bool>,
371 #[props(default)]
373 pub layout: DialogLayout,
374 #[props(default, into)]
376 pub glass: Glass,
377 #[props(into)]
379 pub class: Option<String>,
380 #[props(extends = GlobalAttributes, extends = dialog)]
382 pub attributes: Vec<Attribute>,
383 pub children: Element,
385}
386
387#[component]
389pub fn Dialog(props: DialogProps) -> Element {
390 let handle = use_dialog_handle(
391 props.dialog,
392 props.open,
393 props.default_open,
394 props.on_open_change,
395 );
396 let glass = props.glass.class("ui-dialog");
397 let class = compose([
398 Some("ui-dialog"),
399 props.alert.then_some("ui-alert-dialog"),
400 (props.layout == DialogLayout::Fullscreen).then_some("ui-dialog--fullscreen"),
401 (props.layout == DialogLayout::Responsive).then_some("ui-dialog--responsive"),
402 glass.as_deref(),
403 props.class.as_deref(),
404 ]);
405
406 render_dialog(DialogShell {
407 handle,
408 kind: DialogKind::Dialog,
409 alert: props.alert,
410 class,
411 layout: Some(props.layout.as_str()),
412 surface: surface(props.glass),
413 dismiss_on_escape: props.dismiss_on_escape,
414 dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(!props.alert),
415 initial_focus: None,
416 attributes: props.attributes,
417 children: props.children,
418 })
419}
420
421#[derive(Props, Clone, PartialEq)]
423pub struct DialogSurfaceProps {
424 pub dialog: Option<DialogController>,
427 pub open: Option<bool>,
429 #[props(default)]
431 pub default_open: bool,
432 pub on_open_change: Option<EventHandler<bool>>,
434 #[props(default = true)]
436 pub dismiss_on_escape: bool,
437 pub dismiss_on_outside_press: Option<bool>,
439 #[props(default, into)]
441 pub glass: Glass,
442 #[props(into)]
444 pub class: Option<String>,
445 #[props(extends = GlobalAttributes, extends = dialog)]
447 pub attributes: Vec<Attribute>,
448 pub children: Element,
450}
451
452pub type AlertDialogProps = DialogSurfaceProps;
454pub type DrawerProps = DialogSurfaceProps;
456
457#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
459#[non_exhaustive]
460pub enum SheetSide {
461 Start,
463 #[default]
465 End,
466}
467
468impl SheetSide {
469 pub fn as_str(self) -> &'static str {
471 match self {
472 Self::Start => "start",
473 Self::End => "end",
474 }
475 }
476}
477
478#[derive(Props, Clone, PartialEq)]
480pub struct SheetProps {
481 pub dialog: Option<DialogController>,
484 pub open: Option<bool>,
486 #[props(default)]
488 pub default_open: bool,
489 pub on_open_change: Option<EventHandler<bool>>,
491 #[props(default = true)]
493 pub dismiss_on_escape: bool,
494 pub dismiss_on_outside_press: Option<bool>,
496 #[props(default)]
498 pub side: SheetSide,
499 #[props(default, into)]
501 pub glass: Glass,
502 #[props(into)]
504 pub class: Option<String>,
505 #[props(extends = GlobalAttributes, extends = dialog)]
507 pub attributes: Vec<Attribute>,
508 pub children: Element,
510}
511
512#[component]
514pub fn AlertDialog(props: DialogSurfaceProps) -> Element {
515 let handle = use_dialog_handle(
516 props.dialog,
517 props.open,
518 props.default_open,
519 props.on_open_change,
520 );
521 let glass = props.glass.class("ui-dialog");
522 let class = compose([
523 Some("ui-dialog ui-alert-dialog"),
524 glass.as_deref(),
525 props.class.as_deref(),
526 ]);
527
528 render_dialog(DialogShell {
529 handle,
530 kind: DialogKind::Dialog,
531 alert: true,
532 class,
533 layout: None,
534 surface: surface(props.glass),
535 dismiss_on_escape: props.dismiss_on_escape,
536 dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(false),
537 initial_focus: None,
538 attributes: props.attributes,
539 children: props.children,
540 })
541}
542
543fn side_panel(
544 props: DialogSurfaceProps,
545 kind: DialogKind,
546 base: &str,
547 modifier: Option<&str>,
548) -> Element {
549 let handle = use_dialog_handle(
550 props.dialog,
551 props.open,
552 props.default_open,
553 props.on_open_change,
554 );
555 let glass = props.glass.class(base);
556 let class = compose([
557 Some(base),
558 modifier,
559 glass.as_deref(),
560 props.class.as_deref(),
561 ]);
562
563 render_dialog(DialogShell {
564 handle,
565 kind,
566 alert: false,
567 class,
568 layout: None,
569 surface: surface(props.glass),
570 dismiss_on_escape: props.dismiss_on_escape,
571 dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(true),
572 initial_focus: None,
573 attributes: props.attributes,
574 children: props.children,
575 })
576}
577
578#[component]
581pub fn Sheet(props: SheetProps) -> Element {
582 let SheetProps {
583 dialog,
584 open,
585 default_open,
586 on_open_change,
587 dismiss_on_escape,
588 dismiss_on_outside_press,
589 side,
590 glass,
591 class,
592 attributes,
593 children,
594 } = props;
595
596 side_panel(
597 DialogSurfaceProps {
598 dialog,
599 open,
600 default_open,
601 on_open_change,
602 dismiss_on_escape,
603 dismiss_on_outside_press,
604 glass,
605 class,
606 attributes,
607 children,
608 },
609 DialogKind::Sheet,
610 "ui-sheet",
611 (side == SheetSide::Start).then_some("ui-sheet--start"),
612 )
613}
614
615#[component]
617pub fn Drawer(props: DialogSurfaceProps) -> Element {
618 side_panel(props, DialogKind::Drawer, "ui-drawer", None)
619}
620
621#[derive(Props, Clone, PartialEq)]
623pub struct DialogPartProps {
624 #[props(into)]
626 pub class: Option<String>,
627 #[props(extends = GlobalAttributes)]
629 pub attributes: Vec<Attribute>,
630 pub children: Element,
632}
633
634#[component]
636pub fn DialogHeader(props: DialogPartProps) -> Element {
637 let class = compose([Some("ui-dialog-header"), props.class.as_deref()]);
638
639 rsx! {
640 header { class, "data-slot": "dialog-header", ..props.attributes, {props.children} }
641 }
642}
643
644#[component]
646pub fn DialogBody(props: DialogPartProps) -> Element {
647 let class = compose([Some("ui-dialog-body"), props.class.as_deref()]);
648
649 rsx! {
650 div { class, "data-slot": "dialog-body", ..props.attributes, {props.children} }
651 }
652}
653
654#[component]
656pub fn DialogFooter(props: DialogPartProps) -> Element {
657 let class = compose([Some("ui-dialog-footer"), props.class.as_deref()]);
658
659 rsx! {
660 footer { class, "data-slot": "dialog-footer", ..props.attributes, {props.children} }
661 }
662}
663
664#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
666#[non_exhaustive]
667pub enum DialogTitleLevel {
668 #[default]
670 H2,
671 H3,
673 H4,
675}
676
677#[derive(Props, Clone, PartialEq)]
679pub struct DialogTitleProps {
680 #[props(default)]
682 pub level: DialogTitleLevel,
683 #[props(into)]
685 pub class: Option<String>,
686 #[props(extends = GlobalAttributes)]
688 pub attributes: Vec<Attribute>,
689 pub children: Element,
691}
692
693#[component]
695pub fn DialogTitle(props: DialogTitleProps) -> Element {
696 let class = compose([Some("ui-dialog-title"), props.class.as_deref()]);
697
698 match props.level {
699 DialogTitleLevel::H2 => rsx! {
700 h2 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
701 },
702 DialogTitleLevel::H3 => rsx! {
703 h3 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
704 },
705 DialogTitleLevel::H4 => rsx! {
706 h4 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
707 },
708 }
709}
710
711#[derive(Props, Clone, PartialEq)]
713pub struct DialogCloseProps {
714 #[props(default)]
716 pub variant: ButtonVariant,
717 #[props(default)]
719 pub size: ButtonSize,
720 #[props(default)]
722 pub disabled: bool,
723 pub onclick: Option<EventHandler<MouseEvent>>,
725 #[props(into)]
727 pub class: Option<String>,
728 #[props(extends = GlobalAttributes, extends = button)]
730 pub attributes: Vec<Attribute>,
731 pub children: Element,
733}
734
735#[component]
738pub fn DialogClose(props: DialogCloseProps) -> Element {
739 let handle = try_use_context::<DialogHandle>();
740 let pending = try_use_context::<DialogFormContext>().is_some_and(DialogFormContext::is_pending);
741 let onclick = props.onclick;
742 let extra = compose([Some("ui-dialog-close"), props.class.as_deref()]);
743 let class = button_class(props.variant, props.size, Some(&extra));
744
745 rsx! {
746 button {
747 class,
748 "data-slot": "dialog-close",
749 disabled: props.disabled || pending,
750 r#type: "button",
751 onclick: move |event: MouseEvent| {
752 if let Some(handler) = onclick {
753 handler.call(event.clone());
754 }
755
756 if event.default_action_enabled()
757 && let Some(handle) = handle
758 {
759 handle.dismiss();
760 }
761 },
762 ..props.attributes,
763 span { class: "ui-button__content", {props.children} }
764 }
765 }
766}