pub struct ElRef<E> {
    pub shared_node_ws: SharedNodeWs,
    /* private fields */
}
Expand description

DOM element reference. You want to use it instead of DOM selectors to get raw DOM elements.

Note: Cloning is cheap, it uses only phantom data and Rc under the hood.

Example

use seed::{prelude::*, *};

#[derive(Default)]
struct Model {
    canvas: ElRef<web_sys::HtmlCanvasElement>,
}

enum Msg {
    Rendered
}

fn view(mdl: &Model) -> impl IntoNodes<Msg> {
    canvas![
        el_ref(&mdl.canvas),
        attrs![ /* ... */ ],
    ]
}

fn update(msg: Msg, mdl: &mut Model, orders: &mut impl Orders<Msg>) {
    match msg {
        Msg::Rendered => {
            let canvas = mdl.canvas.get().expect("get canvas element");
            // ...
            orders.after_next_render(|_| Msg::Rendered).skip();
        }
    }
}

Fields

shared_node_ws: SharedNodeWs

Implementations

Get referenced DOM element.

It returns Some(element) when:

  • An associated DOM element has been already attached during render.
  • The DOM element is still a part of the current DOM.
  • The DOM element has the same type like ElRef.

Map ElRef type.

  • It just changes type saved in the phantom - it’s cheap.

  • It’s useful when you have, for instance, ElRef<HtmlInputElement> and want to focus the referenced input. HtmlInputElement doesn’t have method focus, but parent interface HtmlElement has.

Example
use seed::{prelude::*, *};
use web_sys::{HtmlInputElement, HtmlElement};

struct Model {
  my_input: ElRef<HtmlInputElement>
}

enum Msg {}

fn update(_: Msg, mdl: &mut Model, orders: &mut impl Orders<Msg>) {
    let input: ElRef<HtmlInputElement> = mdl.my_input.clone();
    orders.after_next_render(move |_| {
        input
            .map_type::<HtmlElement>()
            .get()
            .expect("get `my_input`")
            .focus()
            .expect("focus 'my_input'");
    });
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.