Is there any support or tutorial to build as a web component ?

Open
#216 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
rust, wasm

Research direction

Start by reading the linked trunk-rs/trunk issue 1034 and the provided cargo build and wasm-bindgen commands to understand the current web-component approach. No repository file or test is named; done would require a clearly scoped support article or tutorial covering the requested build flow.

Written by the indexing model from the issue text.

Description

Linked to https://github.com/trunk-rs/trunk/issues/1034

#!/bin/bash


cargo build --target wasm32-unknown-unknown
# cargo install -f wasm-bindgen-cli --version 0.2.106
OUTPUT=web_component
ELEMENT=eframe-example
rm -rf "$OUTPUT"
wasm-bindgen --target=web "--out-dir=target/wasm-bindgen/debug" "--out-name=eframe_template" "target/wasm32-unknown-unknown/debug/eframe_template.wasm" "--no-typescript"
mkdir -p "$OUTPUT"
cp target/wasm-bindgen/debug/eframe_template_bg.wasm "$OUTPUT"
cp target/wasm-bindgen/debug/eframe_template.js "$OUTPUT"
cat "$OUTPUT/eframe_template.js" > "$OUTPUT/temp.js"
echo "customElements.define('$ELEMENT',class extends HTMLElement {
    constructor() {
      super();
      const shadowRoot = this.attachShadow({ mode: 'open' });
      const canvas = document.createElement('canvas');
      canvas.id = 'the_canvas_id';
      shadowRoot.appendChild(canvas);
    }
    connectedCallback() {
        this.init();
    }
    async init() {
        const wasm = await __wbg_init({ module_or_path: './eframe_template_bg.wasm' });
        // window.wasmBindings = bindings;
        dispatchEvent(new CustomEvent('TrunkApplicationStarted', {detail: {wasm}}));
    }
  },
);" >> "$OUTPUT/temp.js"
mv "$OUTPUT/temp.js" "$OUTPUT/eframe_template.js"


echo "
<script type='module' src='eframe_template.js'></script>
<eframe-example></eframe-example>
" > "$OUTPUT/index.html"

code to get the canvas inside the shadowRoot

let canvas = document
    .body()
    .expect("Failed to find <body>")
    .child_nodes()
    .get(0)
    .expect("Failed to find the CustomElement")
    .dyn_into::<web_sys::Element>()
    .expect("convert failed")
    .shadow_root()
    .expect("Failed to find shadowRoot")
    .dyn_into::<web_sys::ShadowRoot>()
    .expect("failed convert ShadowRoot")
    .get_element_by_id("the_canvas_id")
    .expect("Failed to find the_canvas_id")
    .dyn_into::<web_sys::HtmlCanvasElement>()
    .expect("the_canvas_id was not a HtmlCanvasElement");
Image
Dominant language
Rust
Stars
1.2k
Forks
235
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from emilk/eframe_template

All issues in emilk/eframe_template

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.