Elements

DOM nodes

There are many reasons why you might want to create or manage DOM nodes manually in Yew, such as when integrating with JS libraries that can cause conflicts with managed components.

Using web-sys, you can create DOM elements and convert them into a Node - which can then be used as a Html value using VRef:

  1. use yew::{utils::document, web_sys::{Element, Node}, Html};
  2. // ...
  3. fn view(&self) -> Html {
  4. // Create a div element from the document
  5. let div: Element = document().create_element("div").unwrap();
  6. // Add content, classes etc.
  7. div.set_inner_html("Hello, World!");
  8. // Convert Element into a Node
  9. let node: Node = div.into();
  10. // Return that Node as a Html value
  11. Html::VRef(node)
  12. }

Dynamic tag names

When building a higher-order component you might find yourself in a situation where the element’s tag name isn’t static. For example, you might have a Title component which can render anything from h1 to h6 depending on a level prop. Instead of having to use a big match expression, Yew allows you to set the tag name dynamically using @{name} where name can be any expression that returns a string.

  1. use yew::html;
  2. let level = 5;
  3. let text = "Hello World!".to_owned()
  4. html! {
  5. <@{format!("h{}", level)} class="title">{ text }</@>
  6. }

Boolean Attributes

Some content attributes (e.g checked, hidden, required) are called boolean attributes. In Yew, boolean attributes need to be set to a bool value:

  1. use yew::html;
  2. html! {
  3. <div hidden=true>
  4. { "This div is hidden." }
  5. </div>
  6. }

This will result in HTML that’s functionally equivalent to this:

  1. <div hidden>This div is hidden.</div>

Setting a boolean attribute to false is equivalent to not using the attribute at all; values from boolean expressions can be used:

  1. use yew::html;
  2. let no = 1 + 1 != 2;
  3. html! {
  4. <div hidden=no>
  5. { "This div is NOT hidden." }
  6. </div>
  7. }

This will result in the following HTML:

  1. <div>This div is NOT hidden.</div>

Optional attributes for HTML elements

Most HTML attributes can use optional values (Some(x) or None). This allows us to omit the attribute if the attribute is marked as optional.

  1. use yew::html;
  2. let maybe_id = Some("foobar");
  3. html! {
  4. <div id=maybe_id></div>
  5. }

If the attribute is set to None, the attribute won’t be set in the DOM.

Please note that it is also valid to give only the value as properties behave like Into<Option<T>>:

  1. use yew::html;
  2. let id = "foobar";
  3. html! {
  4. <div id=id></div>
  5. }

Listeners

Listener attributes need to be passed a Callback which is a wrapper around a closure. How you create your callback depends on how you wish your app to react to a listener event:

  • Component handler
  • Agent Handler
  • Other Cases
  1. use yew::{Component, Context, html, Html};
  2. struct MyComponent;
  3. enum Msg {
  4. Click,
  5. }
  6. impl Component for MyComponent {
  7. type Message = Msg;
  8. type Properties = ();
  9. fn create(_ctx: &Context<Self>) -> Self {
  10. Self
  11. }
  12. fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
  13. match msg {
  14. Msg::Click => {
  15. // Handle Click
  16. }
  17. };
  18. true
  19. }
  20. fn view(&self, ctx: &Context<Self>) -> Html {
  21. // Create a callback from a component link to handle it in a component
  22. let click_callback = ctx.link().callback(|_| Msg::Click);
  23. html! {
  24. <button onclick={click_callback}>
  25. { "Click me!" }
  26. </button>
  27. }
  28. }
  29. }
  1. use yew::{html, Component, Context, Html};
  2. use yew_agent::{Dispatcher, Dispatched};
  3. use website_test::agents::{MyWorker, WorkerMsg};
  4. struct MyComponent {
  5. worker: Dispatcher<MyWorker>,
  6. }
  7. impl Component for MyComponent {
  8. type Message = WorkerMsg;
  9. type Properties = ();
  10. fn create(_ctx: &Context<Self>) -> Self {
  11. MyComponent {
  12. worker: MyWorker::dispatcher(),
  13. }
  14. }
  15. fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
  16. self.worker.send(msg);
  17. false
  18. }
  19. fn view(&self, ctx: &Context<Self>) -> Html {
  20. // Create a callback from a worker to handle it in another context
  21. let click_callback = ctx.link().callback(|_| WorkerMsg::Process);
  22. html! {
  23. <button onclick={click_callback}>
  24. { "Click me!" }
  25. </button>
  26. }
  27. }
  28. }
  1. use yew::{Callback, Context, Component, html, Html};
  2. use weblog::console_log;
  3. struct MyComponent;
  4. impl Component for MyComponent {
  5. type Message = ();
  6. type Properties = ();
  7. fn create(_ctx: &Context<Self>) -> Self {
  8. MyComponent
  9. }
  10. fn view(&self, _ctx: &Context<Self>) -> Html {
  11. // Create an ephemeral callback
  12. let click_callback = Callback::from(|_| {
  13. console_log!("clicked!");
  14. });
  15. html! {
  16. <button onclick={click_callback}>
  17. { "Click me!" }
  18. </button>
  19. }
  20. }
  21. }

Relevant examples