lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Dialogs: `Dialog` and its parts, `AlertDialog`, `Sheet`, `Drawer`, `DialogTrigger`, and
//! [`use_dialog`].
//!
//! lumen-react opens these with `showModal()`. Dioxus Native has no top layer, so lumen-dioxus
//! renders `<dialog open>` in the tree with a modal [`DismissLayer`] behind it. Escape closes the
//! dialog through a key handler on the dialog, focus moves to the dialog when it opens, and a
//! [`DialogTrigger`] gets focus back when it closes. While a
//! [`DialogForm`](super::DialogForm) submission is pending, the dialog is marked
//! `data-dismiss-locked` and Escape, outside clicks, and [`DialogClose`] leave it open.

use std::rc::Rc;

use dioxus::prelude::*;

use super::dialog_form::DialogFormContext;
use super::shared::{button_class, focus_stored, surface};
use crate::class::{Glass, compose};
use crate::hooks::{Controllable, use_controllable};
use crate::overlay::{DismissLayer, is_escape};
use crate::{ButtonSize, ButtonVariant};

/// Open state and trigger of a dialog, shared by a [`DialogTrigger`] and the dialog it opens.
///
/// Create it with [`use_dialog`]. It is `Copy`, so event handlers can capture it.
#[derive(Clone, Copy, PartialEq)]
pub struct DialogController {
    open: Signal<bool>,
    trigger: Signal<Option<Rc<MountedData>>>,
}

impl DialogController {
    /// Whether the dialog is open. Reading it subscribes the component.
    pub fn is_open(&self) -> bool {
        *self.open.read()
    }

    /// Opens or closes the dialog.
    pub fn set_open(&self, open: bool) {
        let mut state = self.open;

        if *state.peek() != open {
            state.set(open);
        }
    }

    /// Opens the dialog.
    pub fn open(&self) {
        self.set_open(true);
    }

    /// Closes the dialog. Focus returns to the [`DialogTrigger`].
    pub fn close(&self) {
        self.set_open(false);
    }

    /// Opens a closed dialog or closes an open one.
    pub fn toggle(&self) {
        let open = *self.open.peek();
        self.set_open(!open);
    }

    /// Whether the dialog is open, without subscribing the component.
    pub(super) fn is_open_untracked(&self) -> bool {
        *self.open.peek()
    }

    /// Sets the element that gets focus back when the dialog closes; `None` restores nothing.
    pub(super) fn set_trigger(&self, node: Option<Rc<MountedData>>) {
        let mut trigger = self.trigger;
        trigger.set(node);
    }
}

/// A closed dialog controller, like `useDialog` in lumen-react. Pass it to a [`DialogTrigger`]
/// and to the `dialog` prop of [`Dialog`], [`AlertDialog`], [`Sheet`], or [`Drawer`].
pub fn use_dialog() -> DialogController {
    use_hook(|| DialogController {
        open: Signal::new(false),
        trigger: Signal::new(None),
    })
}

/// Props for [`DialogTrigger`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogTriggerProps {
    /// The dialog this button opens.
    pub dialog: DialogController,
    /// Visual treatment.
    #[props(default)]
    pub variant: ButtonVariant,
    /// Control size.
    #[props(default)]
    pub size: ButtonSize,
    /// Disables the trigger.
    #[props(default)]
    pub disabled: bool,
    /// Click handler. Call `prevent_default()` to keep the dialog closed.
    pub onclick: Option<EventHandler<MouseEvent>>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<button>` or global attribute.
    #[props(extends = GlobalAttributes, extends = button)]
    pub attributes: Vec<Attribute>,
    /// Button label.
    pub children: Element,
}

/// A Lumen button that opens a dialog and gets focus back when the dialog closes.
#[component]
pub fn DialogTrigger(props: DialogTriggerProps) -> Element {
    let dialog = props.dialog;
    let onclick = props.onclick;
    let mut trigger = dialog.trigger;
    let class = button_class(props.variant, props.size, props.class.as_deref());

    rsx! {
        button {
            aria_haspopup: "dialog",
            class,
            "data-slot": "button",
            disabled: props.disabled,
            r#type: "button",
            onclick: move |event: MouseEvent| {
                if let Some(handler) = onclick {
                    handler.call(event.clone());
                }

                if event.default_action_enabled() {
                    dialog.open();
                }
            },
            onmounted: move |event| trigger.set(Some(event.data())),
            ..props.attributes,
            span { class: "ui-button__content", {props.children} }
        }
    }
}

/// Open state shared by a dialog shell, its [`DialogClose`] buttons, and a
/// [`DialogForm`](super::DialogForm) inside it.
#[derive(Clone, Copy)]
pub(super) struct DialogHandle {
    state: Controllable<bool>,
    controller: CopyValue<Option<DialogController>>,
    /// Set while a `DialogForm` submission is pending, like lumen-core's `data-dismiss-locked`.
    locked: Signal<bool>,
}

impl DialogHandle {
    pub(super) fn set_open(self, open: bool) {
        if let Some(controller) = *self.controller.peek() {
            controller.set_open(open);
        }

        self.state.set(open);
    }

    /// Whether the dialog is open. Reading it subscribes the component.
    pub(super) fn is_open(self) -> bool {
        self.state.get()
    }

    /// Closes the dialog for Escape, an outside click, or a [`DialogClose`], unless a pending
    /// `DialogForm` locked dismissal.
    fn dismiss(self) {
        if !*self.locked.peek() {
            self.set_open(false);
        }
    }

    /// Locks or releases dismissal.
    pub(super) fn set_locked(self, locked: bool) {
        let mut state = self.locked;

        if *state.peek() != locked {
            state.set(locked);
        }
    }

    /// Releases the lock while a `DialogForm` unmounts, when the dialog may be tearing down too.
    pub(super) fn release_lock(self) {
        let mut state = self.locked;

        if let Ok(mut locked) = state.try_write() {
            *locked = false;
        }
    }
}

pub(super) fn use_dialog_handle(
    controller: Option<DialogController>,
    open: Option<bool>,
    default_open: bool,
    on_open_change: Option<EventHandler<bool>>,
) -> DialogHandle {
    let open_value = controller.map(|owner| owner.is_open()).or(open);
    let state = use_controllable(open_value, || default_open, on_open_change);
    let mut stored = use_hook(|| CopyValue::new(controller));
    stored.set(controller);
    let locked = use_signal(|| false);
    let handle = DialogHandle {
        state,
        controller: stored,
        locked,
    };

    use_context_provider(|| handle);
    handle
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum DialogKind {
    Dialog,
    Sheet,
    Drawer,
}

pub(super) struct DialogShell {
    pub(super) handle: DialogHandle,
    pub(super) kind: DialogKind,
    pub(super) alert: bool,
    pub(super) class: String,
    pub(super) layout: Option<&'static str>,
    pub(super) surface: &'static str,
    pub(super) dismiss_on_escape: bool,
    pub(super) dismiss_on_outside_press: bool,
    /// Element focused when the dialog opens, such as a search field. Defaults to the dialog.
    pub(super) initial_focus: Option<Signal<Option<Rc<MountedData>>>>,
    pub(super) attributes: Vec<Attribute>,
    pub(super) children: Element,
}

/// Focus moves into the dialog (or `initial_focus`, once mounted) when it opens and back to the
/// controller's trigger when it closes.
fn use_dialog_focus(
    open: bool,
    handle: DialogHandle,
    node: Signal<Option<Rc<MountedData>>>,
    initial_focus: Option<Signal<Option<Rc<MountedData>>>>,
) {
    let mut was_open = use_hook(|| CopyValue::new(false));

    use_effect(use_reactive((&open,), move |(open,)| {
        let previous = *was_open.peek();
        was_open.set(open);

        if open && !previous {
            let initial = initial_focus.and_then(|target| target.peek().clone());
            focus_stored(initial.or_else(|| node.peek().clone()));
        } else if !open
            && previous
            && let Some(controller) = *handle.controller.peek()
        {
            focus_stored(controller.trigger.peek().clone());
        }
    }));
}

pub(super) fn render_dialog(shell: DialogShell) -> Element {
    let DialogShell {
        handle,
        kind,
        alert,
        class,
        layout,
        surface,
        dismiss_on_escape,
        dismiss_on_outside_press,
        initial_focus,
        attributes,
        children,
    } = shell;
    let open = handle.state.get();
    let locked = *handle.locked.read();
    let mut node = use_signal(|| None::<Rc<MountedData>>);
    use_dialog_focus(open, handle, node, initial_focus);
    // Sheets and drawers have no z-index of their own; lift them above the dismiss layer.
    let lifted = open && kind != DialogKind::Dialog;

    rsx! {
        if open {
            DismissLayer {
                modal: true,
                on_dismiss: move |()| {
                    if dismiss_on_outside_press {
                        handle.dismiss();
                    }
                },
            }
        }
        dialog {
            aria_modal: "true",
            class,
            "data-dismiss-locked": locked.then_some(""),
            "data-layout": layout,
            "data-surface": surface,
            "data-ui-alert-dialog": alert.then_some("true"),
            "data-ui-bound": "true",
            "data-ui-dialog": (!alert).then_some("true"),
            "data-ui-drawer": (kind == DialogKind::Drawer).then_some("true"),
            "data-ui-sheet": (kind == DialogKind::Sheet).then_some("true"),
            open,
            role: if alert { "alertdialog" } else { "dialog" },
            style: lifted.then_some("z-index: 50"),
            tabindex: open.then_some("-1"),
            onkeydown: move |event: KeyboardEvent| {
                if !is_escape(&event) || !event.default_action_enabled() {
                    return;
                }

                event.prevent_default();
                event.stop_propagation();

                if dismiss_on_escape {
                    handle.dismiss();
                }
            },
            onmounted: move |event| node.set(Some(event.data())),
            ..attributes,
            {children}
        }
    }
}

/// Layout of a [`Dialog`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DialogLayout {
    /// A centered panel.
    #[default]
    Centered,
    /// Covers the whole viewport.
    Fullscreen,
    /// Centered on wide screens, a bottom sheet on narrow ones.
    Responsive,
}

impl DialogLayout {
    /// The `data-layout` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Centered => "centered",
            Self::Fullscreen => "fullscreen",
            Self::Responsive => "responsive",
        }
    }
}

/// Props for [`Dialog`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogProps {
    /// A controller from [`use_dialog`]. It owns the open state and lets a [`DialogTrigger`] get
    /// focus back when the dialog closes.
    pub dialog: Option<DialogController>,
    /// Controlled open state.
    pub open: Option<bool>,
    /// Initial open state when uncontrolled.
    #[props(default)]
    pub default_open: bool,
    /// Called with the requested open state.
    pub on_open_change: Option<EventHandler<bool>>,
    /// Makes it an `alertdialog` that ignores outside clicks by default.
    #[props(default)]
    pub alert: bool,
    /// Closes on Escape. Defaults to true.
    #[props(default = true)]
    pub dismiss_on_escape: bool,
    /// Closes on a click outside the dialog. Defaults to true, or false for alerts.
    pub dismiss_on_outside_press: Option<bool>,
    /// Layout.
    #[props(default)]
    pub layout: DialogLayout,
    /// Glass intensity.
    #[props(default, into)]
    pub glass: Glass,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dialog>` or global attribute. Name the dialog with `aria_labelledby`.
    #[props(extends = GlobalAttributes, extends = dialog)]
    pub attributes: Vec<Attribute>,
    /// [`DialogHeader`], [`DialogBody`], [`DialogFooter`], or any content.
    pub children: Element,
}

/// A modal dialog with Lumen's dialog surface.
#[component]
pub fn Dialog(props: DialogProps) -> Element {
    let handle = use_dialog_handle(
        props.dialog,
        props.open,
        props.default_open,
        props.on_open_change,
    );
    let glass = props.glass.class("ui-dialog");
    let class = compose([
        Some("ui-dialog"),
        props.alert.then_some("ui-alert-dialog"),
        (props.layout == DialogLayout::Fullscreen).then_some("ui-dialog--fullscreen"),
        (props.layout == DialogLayout::Responsive).then_some("ui-dialog--responsive"),
        glass.as_deref(),
        props.class.as_deref(),
    ]);

    render_dialog(DialogShell {
        handle,
        kind: DialogKind::Dialog,
        alert: props.alert,
        class,
        layout: Some(props.layout.as_str()),
        surface: surface(props.glass),
        dismiss_on_escape: props.dismiss_on_escape,
        dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(!props.alert),
        initial_focus: None,
        attributes: props.attributes,
        children: props.children,
    })
}

/// Props shared by [`AlertDialog`] and [`Drawer`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogSurfaceProps {
    /// A controller from [`use_dialog`]. It owns the open state and lets a [`DialogTrigger`] get
    /// focus back when the dialog closes.
    pub dialog: Option<DialogController>,
    /// Controlled open state.
    pub open: Option<bool>,
    /// Initial open state when uncontrolled.
    #[props(default)]
    pub default_open: bool,
    /// Called with the requested open state.
    pub on_open_change: Option<EventHandler<bool>>,
    /// Closes on Escape. Defaults to true.
    #[props(default = true)]
    pub dismiss_on_escape: bool,
    /// Closes on a click outside. Defaults to true, or false for [`AlertDialog`].
    pub dismiss_on_outside_press: Option<bool>,
    /// Glass intensity.
    #[props(default, into)]
    pub glass: Glass,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dialog>` or global attribute. Name the dialog with `aria_labelledby`.
    #[props(extends = GlobalAttributes, extends = dialog)]
    pub attributes: Vec<Attribute>,
    /// Dialog content.
    pub children: Element,
}

/// Props for [`AlertDialog`].
pub type AlertDialogProps = DialogSurfaceProps;
/// Props for [`Drawer`].
pub type DrawerProps = DialogSurfaceProps;

/// The inline edge a [`Sheet`] docks to and slides in from.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum SheetSide {
    /// The inline start: the left, or the right on right-to-left pages. Suits filters.
    Start,
    /// The inline end: the right, or the left on right-to-left pages.
    #[default]
    End,
}

impl SheetSide {
    /// The side name, like lumen-react's `side` prop.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::End => "end",
        }
    }
}

/// Props for [`Sheet`].
#[derive(Props, Clone, PartialEq)]
pub struct SheetProps {
    /// A controller from [`use_dialog`]. It owns the open state and lets a [`DialogTrigger`] get
    /// focus back when the sheet closes.
    pub dialog: Option<DialogController>,
    /// Controlled open state.
    pub open: Option<bool>,
    /// Initial open state when uncontrolled.
    #[props(default)]
    pub default_open: bool,
    /// Called with the requested open state.
    pub on_open_change: Option<EventHandler<bool>>,
    /// Closes on Escape. Defaults to true.
    #[props(default = true)]
    pub dismiss_on_escape: bool,
    /// Closes on a click outside. Defaults to true.
    pub dismiss_on_outside_press: Option<bool>,
    /// The inline edge the sheet docks to and slides in from. Defaults to the end.
    #[props(default)]
    pub side: SheetSide,
    /// Glass intensity.
    #[props(default, into)]
    pub glass: Glass,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<dialog>` or global attribute. Name the sheet with `aria_labelledby`.
    #[props(extends = GlobalAttributes, extends = dialog)]
    pub attributes: Vec<Attribute>,
    /// Sheet content.
    pub children: Element,
}

/// A confirmation dialog with the `alertdialog` role. Outside clicks do not close it.
#[component]
pub fn AlertDialog(props: DialogSurfaceProps) -> Element {
    let handle = use_dialog_handle(
        props.dialog,
        props.open,
        props.default_open,
        props.on_open_change,
    );
    let glass = props.glass.class("ui-dialog");
    let class = compose([
        Some("ui-dialog ui-alert-dialog"),
        glass.as_deref(),
        props.class.as_deref(),
    ]);

    render_dialog(DialogShell {
        handle,
        kind: DialogKind::Dialog,
        alert: true,
        class,
        layout: None,
        surface: surface(props.glass),
        dismiss_on_escape: props.dismiss_on_escape,
        dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(false),
        initial_focus: None,
        attributes: props.attributes,
        children: props.children,
    })
}

fn side_panel(
    props: DialogSurfaceProps,
    kind: DialogKind,
    base: &str,
    modifier: Option<&str>,
) -> Element {
    let handle = use_dialog_handle(
        props.dialog,
        props.open,
        props.default_open,
        props.on_open_change,
    );
    let glass = props.glass.class(base);
    let class = compose([
        Some(base),
        modifier,
        glass.as_deref(),
        props.class.as_deref(),
    ]);

    render_dialog(DialogShell {
        handle,
        kind,
        alert: false,
        class,
        layout: None,
        surface: surface(props.glass),
        dismiss_on_escape: props.dismiss_on_escape,
        dismiss_on_outside_press: props.dismiss_on_outside_press.unwrap_or(true),
        initial_focus: None,
        attributes: props.attributes,
        children: props.children,
    })
}

/// A modal panel that slides in from an inline edge: the end by default, for details and
/// settings, or the start with [`SheetSide::Start`], for start-edge panels such as filters.
#[component]
pub fn Sheet(props: SheetProps) -> Element {
    let SheetProps {
        dialog,
        open,
        default_open,
        on_open_change,
        dismiss_on_escape,
        dismiss_on_outside_press,
        side,
        glass,
        class,
        attributes,
        children,
    } = props;

    side_panel(
        DialogSurfaceProps {
            dialog,
            open,
            default_open,
            on_open_change,
            dismiss_on_escape,
            dismiss_on_outside_press,
            glass,
            class,
            attributes,
            children,
        },
        DialogKind::Sheet,
        "ui-sheet",
        (side == SheetSide::Start).then_some("ui-sheet--start"),
    )
}

/// A modal panel that slides up from the bottom, for filters and quick actions.
#[component]
pub fn Drawer(props: DialogSurfaceProps) -> Element {
    side_panel(props, DialogKind::Drawer, "ui-drawer", None)
}

/// Props shared by [`DialogHeader`], [`DialogBody`], and [`DialogFooter`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogPartProps {
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute.
    #[props(extends = GlobalAttributes)]
    pub attributes: Vec<Attribute>,
    /// Content.
    pub children: Element,
}

/// The `<header>` of a dialog, usually a [`DialogTitle`] and a description.
#[component]
pub fn DialogHeader(props: DialogPartProps) -> Element {
    let class = compose([Some("ui-dialog-header"), props.class.as_deref()]);

    rsx! {
        header { class, "data-slot": "dialog-header", ..props.attributes, {props.children} }
    }
}

/// The main content of a dialog.
#[component]
pub fn DialogBody(props: DialogPartProps) -> Element {
    let class = compose([Some("ui-dialog-body"), props.class.as_deref()]);

    rsx! {
        div { class, "data-slot": "dialog-body", ..props.attributes, {props.children} }
    }
}

/// The `<footer>` of a dialog, usually actions.
#[component]
pub fn DialogFooter(props: DialogPartProps) -> Element {
    let class = compose([Some("ui-dialog-footer"), props.class.as_deref()]);

    rsx! {
        footer { class, "data-slot": "dialog-footer", ..props.attributes, {props.children} }
    }
}

/// Heading level of a [`DialogTitle`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DialogTitleLevel {
    /// `<h2>`.
    #[default]
    H2,
    /// `<h3>`.
    H3,
    /// `<h4>`.
    H4,
}

/// Props for [`DialogTitle`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogTitleProps {
    /// Heading level, like `as` in lumen-react.
    #[props(default)]
    pub level: DialogTitleLevel,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other global attribute. Give it an `id` and point the dialog's `aria_labelledby` at it.
    #[props(extends = GlobalAttributes)]
    pub attributes: Vec<Attribute>,
    /// Title text.
    pub children: Element,
}

/// The title of a dialog.
#[component]
pub fn DialogTitle(props: DialogTitleProps) -> Element {
    let class = compose([Some("ui-dialog-title"), props.class.as_deref()]);

    match props.level {
        DialogTitleLevel::H2 => rsx! {
            h2 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
        },
        DialogTitleLevel::H3 => rsx! {
            h3 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
        },
        DialogTitleLevel::H4 => rsx! {
            h4 { class, "data-slot": "dialog-title", ..props.attributes, {props.children} }
        },
    }
}

/// Props for [`DialogClose`].
#[derive(Props, Clone, PartialEq)]
pub struct DialogCloseProps {
    /// Visual treatment.
    #[props(default)]
    pub variant: ButtonVariant,
    /// Control size.
    #[props(default)]
    pub size: ButtonSize,
    /// Disables the button.
    #[props(default)]
    pub disabled: bool,
    /// Click handler. Call `prevent_default()` to keep the dialog open.
    pub onclick: Option<EventHandler<MouseEvent>>,
    /// Extra classes appended after the Lumen classes.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<button>` or global attribute.
    #[props(extends = GlobalAttributes, extends = button)]
    pub attributes: Vec<Attribute>,
    /// Button label.
    pub children: Element,
}

/// A Lumen button that closes the dialog it is in. Inside a pending
/// [`DialogForm`](super::DialogForm) it is disabled, and a locked dialog stays open.
#[component]
pub fn DialogClose(props: DialogCloseProps) -> Element {
    let handle = try_use_context::<DialogHandle>();
    let pending = try_use_context::<DialogFormContext>().is_some_and(DialogFormContext::is_pending);
    let onclick = props.onclick;
    let extra = compose([Some("ui-dialog-close"), props.class.as_deref()]);
    let class = button_class(props.variant, props.size, Some(&extra));

    rsx! {
        button {
            class,
            "data-slot": "dialog-close",
            disabled: props.disabled || pending,
            r#type: "button",
            onclick: move |event: MouseEvent| {
                if let Some(handler) = onclick {
                    handler.call(event.clone());
                }

                if event.default_action_enabled()
                    && let Some(handle) = handle
                {
                    handle.dismiss();
                }
            },
            ..props.attributes,
            span { class: "ui-button__content", {props.children} }
        }
    }
}