JS with RS

Yew centrally operates on the idea of keeping everything that a reusable piece of UI may need in one place - rust files, while also keeping the underlying technology accessible where necessary.

As of today, WebAssembly is not feature-complete for DOM interactions. This means even in Yew we sometimes rely on calling Javascript. What follows is an overview of the involved libraries.

wasm-bindgen

wasm-bindgen is a library and tool that enables calls to javascript from rust and back to rust from javascript.

We highly recommend you take a look at their documentation and our quick guide.

web-sys

The web-sys crate provides bindings for Web APIs and allows us to write Javascript code in a rustyfied and safe way.

Example:

  • JS
  • RS
1. let document = window.document
1. use wasm_bindgen::UnwrapThrowExt;
2. use web_sys::window;

4. let document = window()
5. .expect_throw("window is undefined")
6. .document()
7. .expect_throw("document is undefined");

Once again we highly recommend you take a look at their documentation and our quick guide.