Skip to main content

Dialog

Struct Dialog 

Source
pub struct Dialog<S> { /* private fields */ }
Expand description

A modal dialog on a page whose signals are S.

Declare one as a const and use the same value for the dialog and for the buttons that open and close it, so they agree on its id. The buttons carry commandfor and command, and the browser opens the <dialog> with showModal(): it makes the rest of the page inert, so that Tab moves between the dialog’s controls and the browser’s own and never reaches the page behind, closes on Escape, and returns focus to the button that opened it. The browser needs invoker commands for this, which Chrome 135, Firefox 144, and Safari 26.2 ship.

The dialog is labeled by its title and described by its description through ids derived from the name. Two dialogs with the same name on one page share an id, and rendering the page panics.

use hypersig::Signals;
use hypersig::html::p;
use hypersig_ui::{ButtonSize, ButtonVariant, Dialog, dialog_footer};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Profile {}

const EDIT: Dialog<Profile> = Dialog::new("edit-profile");

let open = EDIT.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Edit profile");
let dialog = EDIT.dialog(
    "Edit profile",
    "Changes are saved when you press Save.",
    dialog_footer().child(EDIT.close(ButtonVariant::Default, ButtonSize::Default).text("Save")),
);
assert!(open.render().contains(r#"command="show-modal""#));
assert!(dialog.render().contains(r#"aria-labelledby="edit-profile-title""#));

Implementations§

Source§

impl<S> Dialog<S>

Source

pub const fn new(name: &'static str) -> Self

A dialog whose id is name.

§Panics

Panics if name is empty or holds a character other than an ASCII letter, digit, -, or _. In a const, that is a compile error.

Source

pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>

A button that opens the dialog.

Source

pub fn open(self) -> Stmt<S>

A statement that opens the dialog, as its trigger button does.

Use it where no button opens the dialog, such as a keyboard shortcut from Page::on_key. Focus returns to the element that had it when the dialog closes.

Source

pub fn dismiss(self) -> Stmt<S>

A statement that closes the dialog, as its close button does.

Source

pub fn close(self, variant: ButtonVariant, size: ButtonSize) -> El<S>

A button that closes the dialog. Put it inside the dialog.

Source

pub fn dialog( self, title: impl Into<String>, description: impl Into<String>, content: impl Into<Node<S>>, ) -> El<S>

The dialog, with title, description, content, and a close button in its corner.

A click on the backdrop closes it as Escape does (closedby="any"), in a browser that supports closedby. Focus moves to the first control in content when it opens, and the corner button comes after content so that it is not that control.

Source

pub fn sheet( self, side: Side, title: impl Into<String>, description: impl Into<String>, content: impl Into<Node<S>>, ) -> El<S>

A sheet: a dialog along the side edge of the viewport, with title, description, content, and a close button in its corner.

It is the same modal <dialog> as Dialog::dialog, opened by Dialog::trigger, with the same focus, Escape, and backdrop behavior. Only its place and size differ: it spans the full height of the viewport at the left or the right, and the full width at the top or the bottom.

use hypersig::Signals;
use hypersig::html::p;
use hypersig_ui::{ButtonSize, ButtonVariant, Dialog, Side};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Shop {}

const CART: Dialog<Shop> = Dialog::new("cart");

let open = CART.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Cart");
let sheet = CART.sheet(Side::Right, "Cart", "Two items.", p().text("Tea, cups"));
assert!(sheet.render().contains(r#"aria-labelledby="cart-title""#));
Source

pub fn alert_dialog( self, title: impl Into<String>, description: impl Into<String>, content: impl Into<Node<S>>, ) -> El<S>

An alert dialog, which asks the visitor to confirm or cancel before going on, with title, description, and content.

It has role="alertdialog", no corner button, and stays open on a click on the backdrop, so that the visitor answers with one of the buttons in content. Escape still cancels it.

Trait Implementations§

Source§

impl<S> Clone for Dialog<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Copy for Dialog<S>

Source§

impl<S> Debug for Dialog<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Dialog<S>

§

impl<S> RefUnwindSafe for Dialog<S>

§

impl<S> Send for Dialog<S>

§

impl<S> Sync for Dialog<S>

§

impl<S> Unpin for Dialog<S>

§

impl<S> UnsafeUnpin for Dialog<S>

§

impl<S> UnwindSafe for Dialog<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.