Struct seed::virtual_dom::el_ref::ElRef

source ·
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§

source§

impl<E: Clone + JsCast> ElRef<E>

source

pub fn new() -> Self

source

pub fn get(&self) -> Option<E>

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.
source

pub fn map_type<T>(&self) -> ElRef<T>

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§

source§

impl<E: Clone> Clone for ElRef<E>

source§

fn clone(&self) -> ElRef<E>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<E: Debug> Debug for ElRef<E>

source§

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

Formats the value using the given formatter. Read more
source§

impl<E> Default for ElRef<E>

source§

fn default() -> Self

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

impl<Ms, E: Clone> UpdateEl<Ms> for ElRef<E>

source§

fn update_el(self, el: &mut El<Ms>)

Auto Trait Implementations§

§

impl<E> !RefUnwindSafe for ElRef<E>

§

impl<E> !Send for ElRef<E>

§

impl<E> !Sync for ElRef<E>

§

impl<E> Unpin for ElRef<E>where E: Unpin,

§

impl<E> !UnwindSafe for ElRef<E>

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V