### Install npm packages Source: https://kagura.gitbook.io/kagura-nusa-en Command to install all the necessary Node.js packages and development dependencies listed in package.json. ```bash npm install ``` -------------------------------- ### Install wasm-pack Source: https://kagura.gitbook.io/kagura-nusa-en Use this command to install the wasm-pack tool, which is necessary for compiling Rust to WebAssembly. ```bash cargo install wasm-pack ``` -------------------------------- ### Query Documentation Dynamically Source: https://kagura.gitbook.io/kagura-nusa-en/hello-world Perform an HTTP GET request with the 'ask' query parameter to get dynamic answers from the documentation. Use this for clarifications or additional context not explicitly on the page. ```http GET https://kagura.gitbook.io/kagura-nusa-en/hello-world.md?ask= ``` -------------------------------- ### package.json scripts Source: https://kagura.gitbook.io/kagura-nusa-en Node.js package configuration, defining scripts for starting a development server and building the project with webpack. ```json { "name": "hello-world", "version": "0.1.0", "description": "", "main": "index.js", "scripts": { "start": "./node_modules/.bin/webpack-dev-server", "make": "./node_modules/.bin/webpack" }, "devDependencies": { "@wasm-tool/wasm-pack-plugin": "^1.6.0", "html-webpack-plugin": "^5.3.2", "webpack": "^5.70", "webpack-cli": "^4.9.0", "webpack-dev-server": "^4.3.1" } } ``` -------------------------------- ### Render HTML to DOM with BasicDomNode Source: https://kagura.gitbook.io/kagura-nusa-en/hello-world/creates-a-page Use `BasicDomNode` to render HTML to the real DOM. It requires a mount point and a function that returns the HTML elements to be rendered. This is typically done within a `wasm_bindgen(start)` function. ```rust extern crate js_sys; extern crate kagura; extern crate nusa; extern crate wasm_bindgen; extern crate wasm_bindgen_futures; extern crate web_sys; use nusa::html::html_element::Attributes; use nusa::html::html_element::Events; use nusa::Html; use wasm_bindgen::prelude::*; #[wasm_bindgen(start)] pub fn main() { wasm_bindgen_futures::spawn_local(async { kagura::Runtime::run(nusa::dom_node::BasicDomNode::new(entry_point(), |_| { vec![Html::h1( Attributes::new(), Events::new(), vec![Html::text("Hello World")], )] })) .await; }); } fn entry_point() -> web_sys::Node { web_sys::window() .unwrap() .document() .unwrap() .get_element_by_id("app") .unwrap() .into() } ``` -------------------------------- ### Mount a Component: src/lib.rs Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/creates-a-component This snippet demonstrates how to mount the HelloComponent into the DOM. It sets up the necessary external crates and defines the main function to run the Kagura runtime. ```rust extern crate js_sys; extern crate kagura; extern crate nusa; extern crate wasm_bindgen; extern crate wasm_bindgen_futures; extern crate web_sys; use nusa::html::html_element::Attributes; use nusa::html::html_element::Events; use nusa::Html; use wasm_bindgen::prelude::*; mod hello_component; use hello_component::HelloComponent; #[wasm_bindgen(start)] pub fn main() { wasm_bindgen_futures::spawn_local(kagura::Runtime::run(nusa::dom_node::BasicDomNode::new( entry_point(), |this| { vec![HelloComponent::new( this, None, hello_component::Props {}, Sub::none(), () )] }, ))); } fn entry_point() -> web_sys::Node { web_sys::window() .unwrap() .document() .unwrap() .get_element_by_id("app") .unwrap() .into() } ``` -------------------------------- ### JavaScript entry point Source: https://kagura.gitbook.io/kagura-nusa-en Simple JavaScript file that imports the compiled WebAssembly module. ```javascript import("../pkg"); ``` -------------------------------- ### Create new Cargo project Source: https://kagura.gitbook.io/kagura-nusa-en Command to create a new Rust library project that will be compiled to WebAssembly. ```bash cargo new --lib crate-name ``` -------------------------------- ### Define a Component: HelloComponent Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/creates-a-component Defines a basic component named HelloComponent with its properties, messages, events, and rendering logic. This serves as a template for creating new components. ```rust use kagura::pelude::*; use nusa::prelude::*; pub struct Props {} pub enum Msg {} pub enum On {} pub struct HelloComponent {} impl Component for HelloComponent { type Props = Props; type Msg = Msg; type Event = On; } impl HtmlComponent for HelloComponent {} impl Constructor for HelloComponent { fn constructor(_props: Self::Props) -> Self { Self {} } } impl Update for HelloComponent {} impl Render for HelloComponent { type Children = (); fn render(&self, _children: Self::Children) -> Html { Html::h1( Attributes::new(), Events::new(), vec![Html::text("Hello Component")] ) } } ``` -------------------------------- ### Webpack configuration Source: https://kagura.gitbook.io/kagura-nusa-en Configuration for webpack, setting up the build process for a WebAssembly project, including HTML and WasmPack plugins. ```javascript const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); module.exports = { mode: "production", experiments: { syncWebAssembly: true }, entry: "./template", output: { path: path.join(__dirname, "./dist") }, resolve: { extensions: [".js"] }, module: { rules: [], }, plugins: [ new HtmlWebpackPlugin({ template: path.join(__dirname, "./template/index.html"), inlineSource: ".(js|css)$" }), new WasmPackPlugin({ crateDirectory: path.join(__dirname, "./"), forceMode: "production", target: "web", args: "--log-level error", }), ], devServer: { historyApiFallback: true, } }; ``` -------------------------------- ### gitignore configuration Source: https://kagura.gitbook.io/kagura-nusa-en Standard .gitignore file for a Rust/Wasm project to exclude build artifacts and dependencies. ```gitignore /target /node_modules /pkg ``` -------------------------------- ### Constructor Trait for Component Initialization Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/nssesary-traits Provides a constructor for initializing a component when it is mounted. Props are typically passed from a parent component. ```rust pub trait Constructor: Component { fn constructor(_props: Self::Props) -> Self; } ``` -------------------------------- ### Cargo.toml dependencies Source: https://kagura.gitbook.io/kagura-nusa-en Configuration file for a Rust project, specifying dependencies like js-sys, kagura, nusa, and wasm-bindgen. ```toml [package] name = "hello-world" version = "0.1.0" edition="2021" [lib] crate-type = ["cdylib", "rlib"] [dependencies] js-sys="^0.3" kagura="^0.14" nusa="^0.1" wasm-bindgen="^0.2" wasm-bindgen-futures = "^0.4" [dependencies.web-sys] version="^0.3" features=[ # If you want to use HtmlCanvasElement, Blob, IdbDatabase and so on, # add features hear. ] ``` -------------------------------- ### HTML template Source: https://kagura.gitbook.io/kagura-nusa-en Basic HTML structure for the web page, including meta tags and a placeholder div for the application. ```html Kagura
``` -------------------------------- ### Update Trait for Component Logic Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/nssesary-traits Handles component updates and lifecycle events. Use `update` for general updates, `on_assemble` for the first mount, and `on_load` for subsequent mounts. `Cmd` is used to interact with Kagura. ```rust pub trait Update: Component { fn on_assemble(self: Pin<&mut Self>) -> Cmd { Cmd::None } fn on_load(self: Pin<&mut Self>, _props: Self::Props) -> Cmd { Cmd::None } fn update(self: Pin<&mut Self>, _msg: Self::Msg) -> Cmd { Cmd::None } } ``` -------------------------------- ### Render Trait for HTML Components Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/nssesary-traits Required for components that render HTML. Defines the type of output and associated children. ```rust pub trait Render: Component { type Children: Default; fn render(&self, children: Self::Children) -> T; } ``` -------------------------------- ### Component Trait Definition Source: https://kagura.gitbook.io/kagura-nusa-en/single-component/nssesary-traits Defines the fundamental types for a component: Props, Event, and Msg. This is the base trait for all components. ```rust pub trait Component: Sized { type Props; type Event; type Msg; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.