pub struct Modal {
    pub children: Children<1>,
    pub on_key: KeyHandler,
}
Available on crate feature experimental only.
Expand description

A component supporting modal-style overlays.

The Modal component allows a user to present a Component on top of the children of the modal, from anywhere inside it. This is useful for popups such as input boxes, or error messages.

For example, if we wanted to show an error message whenever the Enter key is pressed, we can do something like this:

#[component(MyComponent)]
fn render() {
  let modal = use_modal();

  let on_key = on_key! {
    KeyEvent { code: Enter, .. } => modal.show(render! {
      Centered() {
        Section(title: "Error", border: Color::Red) {
          Text(text: "Enter was pressed!")
        }
      }
    }),

    KeyEvent { code: Esc, .. } if modal.is_shown() => modal.hide(),
    KeyEvent { code: Esc, .. } => event::quit(),
  };

  render! {
    Section(title: "Some Example UI", on_key)
  }
}

#[component(Root)]
fn render() {
  render! {
    Modal() {
      MyComponent()
    }
  }
}

In order to overlay an error message on top of MyComponent, we render it as a child of a Modal. Then, in any descendant of this Modal, we can call use_modal to mutate the state of that Modal.

Internals

The Modal is somewhat special in that it does not (yet) use the built-in use_state hooks, but instead has its own internal static hook manager.

Fields§

§children: Children<1>§on_key: KeyHandler

Trait Implementations§

source§

impl Component for Modal

source§

impl Default for Modal

source§

fn default() -> Modal

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Modal

§

impl Send for Modal

§

impl Sync for Modal

§

impl Unpin for Modal

§

impl !UnwindSafe for Modal

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.