Skip to content

crate

weft_web

Weft's browser host.

  • DomHost applies virtual-tree patches to the DOM, generates Tailwind CSS just in time for every class it renders, manages declarative focus and modal focus trapping, and delegates events back to the App.
  • canvas2d surfaces are drawn with Canvas 2D (execute); scene3d and shader surfaces with WebGL2 (GlRenderer), animated by a shared frame loop that honours prefers-reduced-motion.
  • Each labelled graphic gets an absolutely-positioned proxy element so canvas content is visible to screen readers and keyboard users.

Modules

  • actions

    Declarative page actions, handled by one delegated click listener:

  • motion

    GSAP-style animation in the browser, driven by weft_motion.

Structs

Enums

Functions

animation_loop

pub fn animation_loop(f: impl FnMut(f64) + 'static) -> AnimationHandle

Call f(timestamp_ms) every animation frame until stopped.

attribute_changes

pub fn attribute_changes(current: &[(String, String)], desired: &[(String, String)]) -> (Vec<String>, Vec<(String, String)>)

Attribute names to remove and pairs to set to turn current into desired (host-internal attributes are left alone).

color_uniforms

pub fn color_uniforms(colors: &[Color]) -> [f32; 12]

u_color[4] uniform data, cycling through the given colors.

create_node

pub fn create_node(document: &Document, node: &VNode) -> Result<Node, JsValue>

Create the DOM for a virtual node (graphics surfaces get a wrapper, canvas and proxies; drawing happens in the host).

current_path

pub fn current_path() -> String

The current route path ("/post/hello").

device_pixel_size

pub fn device_pixel_size(w: f32, h: f32, dpr: f64) -> (u32, u32)

Backing-store size of a canvas for a CSS size and device pixel ratio.

embed_asset

pub fn embed_asset(url: &str, bytes: &'static [u8])

Build an asset into the app: scenes asking for url get bytes without fetching. For files made at build time, so they show at once:

weft::web::embed_asset("/models/chair.wmodel", include_bytes!("../public/models/chair.wmodel"));

execute

pub fn execute(ctx: &CanvasRenderingContext2d, commands: &[DrawCommand])

Run each command against ctx.

fetch

pub async fn fetch(req: Request) -> Result<Response, Error>

Send a request with fetch.

focus

pub fn focus(element: &Element)

Focus an element (e.g. from an event handler).

focus_paths

pub fn focus_paths(tree: &[VNode]) -> Vec<Vec<usize>>

Paths of elements with a truthy focus attribute (declarative focus).

graphics_paths

pub fn graphics_paths(tree: &[VNode]) -> Vec<(Vec<usize>, &VElement)>

Every graphics surface in the tree, with its path, in document order.

graphics_prefix

pub fn graphics_prefix(tree: &[VNode], path: &[usize]) -> Option<Vec<usize>>

The path of the graphics surface that is, or contains, path.

pub fn handles_link(c: LinkClick<'_>) -> bool

Whether the app shows a link's page itself rather than letting the browser load it: a plain click with the main button on a link without target or download, to a page of this origin the app has a route for, that isn't a jump within the current page.

hash_route

pub fn hash_route() -> Signal<String>

A signal holding the current location.hash (e.g. "#/post/hello"), updated on hashchange. Pair with weft_core::router::Router.

input_value_text

pub fn input_value_text(input_type: &str, checked: bool, value: &str) -> String

The Event::value reported for an input element.

install_base_css

pub fn install_base_css(document: &Document) -> Result<(), JsValue>

Add Weft's base stylesheet to <head> (once).

install_panic_hook

pub fn install_panic_hook()

Route Rust panics to console.error.

is_activation_key

pub fn is_activation_key(key: &str) -> bool

Keys that activate a focused button-like proxy.

load

pub fn load<F, E>(resource: &Resource, f: F) where F: Future<Output = Result<Value, E>> + 'static, E: Display + 'static

Load f's result into resource (marking it loading meanwhile).

location_path

pub fn location_path() -> String

The page's own path (location.pathname, e.g. "/about/"), for sites with one prerendered page per path.

location_query

pub fn location_query(name: &str) -> Option<String>

A value in the page's query string (?s=asia → location_query("s") is Some("asia")), decoded.

mesh_key

pub fn mesh_key(p: &Primitive) -> String

Cache key for a primitive's GPU mesh.

modal_path

pub fn modal_path(tree: &[VNode]) -> Option<Vec<usize>>

The last (topmost) element with aria-modal="true".

mount

pub fn mount(selector: &str, root: Component, registry: Registry) -> Result<App, JsValue>

Mount root into the first element matching selector.

mount_themed

pub fn mount_themed(element: &Element, root: Component, registry: Registry, theme: Theme) -> Result<App, JsValue>

Mount root into element using a custom Tailwind theme.

mount_to

pub fn mount_to(element: &Element, root: Component, registry: Registry) -> Result<App, JsValue>

Mount root into element with the default theme.

navigate

pub fn navigate(path: &str)

Navigate to path (sets location.hash and updates hash_route immediately).

new_focus_requests

pub fn new_focus_requests(before: &[Vec<usize>], now: &[Vec<usize>]) -> Vec<Vec<usize>>

Focus requests that weren't present after the previous render.

now_iso

pub fn now_iso() -> String

The current time as an RFC 3339 / ISO 8601 string (UTC).

orbit_camera

pub fn orbit_camera(base: &Camera, o: &Orbit) -> Camera

The camera after orbiting base around its target.

page_data

pub fn page_data(id: &str) -> Option<Value>

JSON embedded in the page as <script type="application/json" id="…"> (how a host page — Gatsby, a CMS template — hands data to the app).

path_route

pub fn path_route() -> Signal<String>

A signal holding location.pathname ("/blog/hello/"), updated by visit and by the browser's back and forward buttons.

pointer_uv

pub fn pointer_uv(rect: (f32, f32, f32, f32), x: f32, y: f32) -> (f32, f32)

Pointer position as shader UV (0..1, y up) within rect = (left, top, width, height) in client pixels.

prefers_reduced_motion

pub fn prefers_reduced_motion() -> bool

Whether the user asked the OS to minimise motion.

query_value

pub fn query_value(search: &str, name: &str) -> Option<String>

name's value in a query string (?a=1&b=two+words).

random

pub fn random() -> f64

A random number in [0, 1).

recall

pub fn recall(key: &str) -> Option<(f64, Value)>

What remember kept under key, and how long ago (milliseconds).

remember

pub fn remember(key: &str, value: &Value)

Keep value in this browser under key (localStorage), with when. Best effort: a full or blocked store keeps nothing.

spawn

pub fn spawn(f: impl Future<Output = ()> + 'static)

Run a future on the page's event loop.

start

pub fn start(selector: &str, root: Component, registry: Registry) -> Result<(), JsValue>

Mount and keep the app alive for the lifetime of the page.

start_app

pub fn start_app(selector: &str, routes: Routes, registry: Registry, theme: Theme) -> Result<(), JsValue>

Start an app made from app/ (see weft::app!): render the page at the browser's path in its layouts, keep the document's title in step, and, unless the app's mode is mpa, show its pages without loading them when their links are clicked.

start_themed

pub fn start_themed(selector: &str, root: Component, registry: Registry, theme: Theme) -> Result<(), JsValue>

start with a custom Tailwind theme.

stats

pub fn stats() -> HostStats

Current debug counters.

surface_is_animated

pub fn surface_is_animated(el: &VElement) -> bool

Whether a surface needs redrawing every frame.

surface_resolution

pub fn surface_resolution(kind: SurfaceKind, css_w: f32, css_h: f32, dpr: f64) -> (u32, u32)

Backing-store size for a surface of css_w × css_h CSS pixels. Shader backgrounds (smooth, full-bleed) render at ≤1× density and a small pixel budget; 3D scenes at ≤2×; 2D canvases at full density. The browser scales the result to the CSS box, so this mostly trades invisible detail for GPU memory and fill rate.

surface_target

pub fn surface_target(el: &VElement, x: f32, y: f32) -> Option<Vec<usize>>

The child index of the item under (x, y) on a graphics surface.

sync_attributes

pub fn sync_attributes(el: &Element, desired: &[(String, String)]) -> Result<(), JsValue>

Make el's attributes equal to desired (host-internal ones are kept).

tint_svg

pub fn tint_svg(svg: &str, color: &str) -> String

An SVG with every hex colour (#rgb, #rrggbb, #rrggbbaa) replaced by color, so an icon can take a particle's colour.

trap_focus_index

pub fn trap_focus_index(count: usize, current: Option<usize>, backwards: bool) -> usize

Next index when Tab-cycling through count focusable elements inside a modal (current is the focused one, if it is inside).

visit

pub fn visit(url: &str)

Show the page at url (/blog/hello/, /docs/#install) without loading it: push it onto the history and update path_route.

Constants

DELEGATED_EVENTS

pub const DELEGATED_EVENTS: &[&str] = _;

DOM events listened for (via delegation on the mount root).

INTERNAL_ATTR_PREFIX

pub const INTERNAL_ATTR_PREFIX: &str = "data-weft-";

Prefix attribute names the host owns; attribute syncing never removes them.

MAX_SCENE_PIXELS

pub const MAX_SCENE_PIXELS: u32 = 2_500_000;

Pixel budget for 3D scenes.

MAX_SHADER_PIXELS

pub const MAX_SHADER_PIXELS: u32 = 600_000;

Pixel budget for full-surface shader backgrounds.