Trait sycamore::generic_node::GenericNode
source · [−]pub trait GenericNode: 'static + Debug + Clone + PartialEq<Self> + Eq + Hash {
type EventType;
type PropertyType;
const USE_HYDRATION_CONTEXT: bool = false;
const CLIENT_SIDE_HYDRATION: bool = false;
Show 26 methods
fn element<T>() -> Self
where
T: SycamoreElement;
fn element_from_tag(tag: &str) -> Self;
fn text_node(text: &str) -> Self;
fn marker_with_text(text: &str) -> Self;
fn set_attribute(&self, name: &str, value: &str);
fn remove_attribute(&self, name: &str);
fn set_class_name(&self, value: &str);
fn add_class(&self, class: &str);
fn remove_class(&self, class: &str);
fn set_property(&self, name: &str, value: &Self::PropertyType);
fn remove_property(&self, name: &str);
fn append_child(&self, child: &Self);
fn first_child(&self) -> Option<Self>;
fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>);
fn remove_child(&self, child: &Self);
fn replace_child(&self, old: &Self, new: &Self);
fn insert_sibling_before(&self, child: &Self);
fn parent_node(&self) -> Option<Self>;
fn next_sibling(&self) -> Option<Self>;
fn remove_self(&self);
fn event<'a, F>(&self, cx: BoundedScope<'a, 'a>, name: &str, handler: F)
where
F: 'a + FnMut(Self::EventType);
fn update_inner_text(&self, text: &str);
fn dangerously_set_inner_html(&self, html: &str);
fn clone_node(&self) -> Self;
fn text_node_int(int: i32) -> Self { ... }
fn marker() -> Self { ... }
}
Expand description
Abstraction over a rendering backend.
You would probably use this trait as a trait bound when you want to accept any rendering
backend. For example, components are often generic over GenericNode
to be able to render to
different backends.
Note that components are NOT represented by GenericNode
. Instead, components are
disappearing, meaning that they are simply functions that generate GenericNode
s inside a
new reactive context. This means that using components add minimal overhead.
Sycamore ships with a few rendering backends out of the box. Here are some examples:
DomNode
- Rendering in the browser (to real DOM nodes).SsrNode
- Render to a static string (often on the server side for Server Side Rendering, aka. SSR).
To implement your own rendering backend, you will need to create a new struct which implements
GenericNode
.
Cloning
GenericNode
s should be cheaply cloneable (usually backed by a Rc
or other
reference counted container) and preserve reference equality.
Required Associated Types
sourcetype PropertyType
type PropertyType
The type for set_property
.
Provided Associated Constants
sourceconst USE_HYDRATION_CONTEXT: bool = false
const USE_HYDRATION_CONTEXT: bool = false
Whether this rendering backend needs the hydration registry.
sourceconst CLIENT_SIDE_HYDRATION: bool = false
const CLIENT_SIDE_HYDRATION: bool = false
Whether this rendering backend hydrates nodes on the client side.
Required Methods
sourcefn element<T>() -> Selfwhere
T: SycamoreElement,
fn element<T>() -> Selfwhere
T: SycamoreElement,
Create a new element node.
sourcefn element_from_tag(tag: &str) -> Self
fn element_from_tag(tag: &str) -> Self
Create a new element node from a tag string.
sourcefn marker_with_text(text: &str) -> Self
fn marker_with_text(text: &str) -> Self
Create a marker (dummy) node with text content. For empty marker, prefer
GenericNode::marker
instead.
sourcefn set_attribute(&self, name: &str, value: &str)
fn set_attribute(&self, name: &str, value: &str)
Sets an attribute on a node.
sourcefn remove_attribute(&self, name: &str)
fn remove_attribute(&self, name: &str)
Removes an attribute on a node.
sourcefn set_class_name(&self, value: &str)
fn set_class_name(&self, value: &str)
Sets the class
attribute on a node.
This should have the same outcome as calling set_attribute("class", value)
.
For DomNode
, this sets the className
property directly which is about 2x faster (on
Chrome).
Add a class to the element. If multiple classes are specified, delimited by spaces, all the classes should be added. Any classes that are already present should not be added a second time.
sourcefn remove_class(&self, class: &str)
fn remove_class(&self, class: &str)
Remove a class from the element.
sourcefn set_property(&self, name: &str, value: &Self::PropertyType)
fn set_property(&self, name: &str, value: &Self::PropertyType)
Sets a property on a node.
sourcefn remove_property(&self, name: &str)
fn remove_property(&self, name: &str)
Removes a property on a node.
sourcefn append_child(&self, child: &Self)
fn append_child(&self, child: &Self)
Appends a child to the node’s children.
sourcefn first_child(&self) -> Option<Self>
fn first_child(&self) -> Option<Self>
Get the first child of the node.
sourcefn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>)
fn insert_child_before(&self, new_node: &Self, reference_node: Option<&Self>)
Insert a new child node to this node’s children. If reference_node
is Some
, the child
will be inserted before the reference node. Else if None
, the child will be inserted
at the end.
sourcefn remove_child(&self, child: &Self)
fn remove_child(&self, child: &Self)
Remove a child node from this node’s children.
sourcefn replace_child(&self, old: &Self, new: &Self)
fn replace_child(&self, old: &Self, new: &Self)
Replace a child node from this node’s children with a new child node.
sourcefn insert_sibling_before(&self, child: &Self)
fn insert_sibling_before(&self, child: &Self)
Insert a new node before this node.
sourcefn parent_node(&self) -> Option<Self>
fn parent_node(&self) -> Option<Self>
Returns the parent node, or None
if detached.
sourcefn next_sibling(&self) -> Option<Self>
fn next_sibling(&self) -> Option<Self>
Returns the next sibling, or None
if this node is the last sibling.
sourcefn remove_self(&self)
fn remove_self(&self)
Remove this node from the tree.
Add a event handler to the event name
.
The event should be removed once the scope is disposed, as to prevent accessing scope
variables after the scope is disposed.
sourcefn update_inner_text(&self, text: &str)
fn update_inner_text(&self, text: &str)
Update inner text of the node. If the node has elements, all the elements are replaced with a new text node.
sourcefn dangerously_set_inner_html(&self, html: &str)
fn dangerously_set_inner_html(&self, html: &str)
Updates the inner html of the node.
The html will not be parsed in non-browser environments. This means that accessing methods
such as first_child
will return None
.
sourcefn clone_node(&self) -> Self
fn clone_node(&self) -> Self
Create a deep clone of the node.
Provided Methods
sourcefn text_node_int(int: i32) -> Self
fn text_node_int(int: i32) -> Self
Create a new text node from an integer.
Create a marker (dummy) node. For DomNode
, this is implemented by creating an empty
comment node. This is used, for example, in Keyed
and Indexed
for scenarios where you
want to push a new item to the end of the list. If the list is empty, a dummy node is
needed to store the position of the component.