lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! Building blocks for floating panels, menus, and dialogs that work on every Dioxus renderer.
//!
//! Lumen's web runtime uses document listeners and the top layer to dismiss and place panels.
//! Dioxus Native has neither, so lumen-dioxus panels stay in the normal tree (Lumen's CSS places
//! them with its absolute-positioning fallback) and dismiss through:
//!
//! - [`DismissLayer`]: a transparent, fixed layer behind an open panel. A click outside the panel
//!   lands on the layer and closes it.
//! - [`is_escape`]: Escape handling on the panel's wrapper, which holds focus while it is open.

use std::rc::Rc;

use dioxus::prelude::*;

/// Props for [`DismissLayer`].
#[derive(Props, Clone, PartialEq)]
pub struct DismissLayerProps {
    /// Called when the user clicks outside the open panel.
    pub on_dismiss: EventHandler<()>,
    /// Darkens the page behind modal surfaces such as dialogs and sheets.
    #[props(default)]
    pub modal: bool,
}

/// A transparent layer that covers the viewport behind an open panel and closes it on click.
///
/// Render it only while the panel is open, as a sibling placed before the panel. It uses the
/// `ui-dioxus-dismiss-layer` class from the bundled stylesheet.
#[component]
pub fn DismissLayer(props: DismissLayerProps) -> Element {
    let on_dismiss = props.on_dismiss;

    rsx! {
        div {
            aria_hidden: "true",
            class: if props.modal { "ui-dioxus-dismiss-layer ui-dioxus-dismiss-layer--modal" } else { "ui-dioxus-dismiss-layer" },
            "data-slot": "dismiss-layer",
            onclick: move |event| {
                event.stop_propagation();
                on_dismiss.call(());
            },
        }
    }
}

/// Whether a key event is Escape.
pub fn is_escape(event: &KeyboardEvent) -> bool {
    event.key() == Key::Escape
}

/// Moves focus to a mounted element without blocking the event handler.
pub fn focus_element(element: Option<Rc<MountedData>>) {
    if let Some(element) = element {
        spawn(async move {
            let _ = element.set_focus(true).await;
        });
    }
}

/// Wraps an index into `0..len`, moving `step` places from `current` (for roving focus).
pub fn wrap_index(current: Option<usize>, step: isize, len: usize) -> Option<usize> {
    if len == 0 {
        return None;
    }

    let len_signed = isize::try_from(len).ok()?;
    let start = current.map_or(if step >= 0 { -1 } else { len_signed }, |index| {
        isize::try_from(index).unwrap_or(0)
    });

    usize::try_from((start + step).rem_euclid(len_signed)).ok()
}

#[cfg(test)]
mod tests {
    use super::wrap_index;

    #[test]
    fn wrap_index_cycles_both_ways() {
        assert_eq!(wrap_index(None, 1, 3), Some(0));
        assert_eq!(wrap_index(None, -1, 3), Some(2));
        assert_eq!(wrap_index(Some(2), 1, 3), Some(0));
        assert_eq!(wrap_index(Some(0), -1, 3), Some(2));
        assert_eq!(wrap_index(Some(1), 1, 0), None);
    }
}