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
Implementations
sourceimpl<E: Clone + JsCast> ElRef<E>
impl<E: Clone + JsCast> ElRef<E>
pub fn new() -> Self
sourcepub fn get(&self) -> Option<E>
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
.
sourcepub fn map_type<T>(&self) -> ElRef<T>
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 methodfocus
, but parent interfaceHtmlElement
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
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more