Skip to content

Commit

Permalink
Add an example of DOM access
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Mar 21, 2018
1 parent 8e894fc commit dd054fa
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"examples/hello_world",
"examples/smorgasboard",
"examples/console_log",
"examples/dom",
]

[profile.release]
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The examples here are:
dialog greeting you
* `console_log` - a showcase of `#[wasm_bindgen]` importing classes and how to
bind `console.log`
* `dom` - an example of accessing the global `document` object and appending
HTML to it
* `smorgasboard` - a bunch of features all thrown into one, showing off the
various capabilities of the `#[wasm_bindgen]` macro and what you can do with
it from JS
4 changes: 4 additions & 0 deletions examples/dom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package-lock.json
dom.js
dom_bg.js
dom_bg.wasm
10 changes: 10 additions & 0 deletions examples/dom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "dom"
version = "0.1.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = { path = "../.." }
22 changes: 22 additions & 0 deletions examples/dom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# DOM access

This directory is an example of using the `#[wasm_bindgen]` macro to interact
with the DOM, specifically the `document` object. You'll see here a few examples
of defining access to the global `document` variable as well as appending a new
HTML node to it.

You can build the example with:

```
$ ./build.sh
```

(or running the commands on Windows manually)

and then opening up `index.html` in a web browser should show a hello message on
the web page generated by the wasm.

For more information about this example be sure to check out
[`hello_world`][hello] which also has more comments about caveats and such.

[hello]: https://github.com/alexcrichton/wasm-bindgen/tree/master/examples/hello_world
12 changes: 12 additions & 0 deletions examples/dom/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# For more coments about what's going on here, see the `hello_world` example

set -ex

cargo +nightly build --target wasm32-unknown-unknown
cargo +nightly run --manifest-path ../../crates/wasm-bindgen-cli/Cargo.toml \
--bin wasm-bindgen -- \
../../target/wasm32-unknown-unknown/debug/dom.wasm --out-dir .
npm install
npm run serve
9 changes: 9 additions & 0 deletions examples/dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<script src='./index.js'></script>
<p>A greeting from rust looks like...</p>
</body>
</html>
4 changes: 4 additions & 0 deletions examples/dom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// For more comments about what's going on here, check out the `hello_world`
// example
const rust = import("./dom");
rust.then(m => m.run());
10 changes: 10 additions & 0 deletions examples/dom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"serve": "webpack-dev-server"
},
"devDependencies": {
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10",
"webpack-dev-server": "^3.1.0"
}
}
39 changes: 39 additions & 0 deletions examples/dom/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![feature(proc_macro)]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

// Definitions of the functionality available in JS, which wasm-bindgen will
// generate shims for today (and eventually these should be near-0 cost!)
//
// These definitions need to be hand-written today but the current vision is
// that we'll use WebIDL to generate this `extern` block into a crate which you
// can link and import. There's a tracking issue for this at
// https://github.com/alexcrichton/wasm-bindgen/issues/42
//
// In the meantime these are written out by hand and correspond to the names and
// signatures documented on MDN, for example
#[wasm_bindgen]
extern {
type HTMLDocument;
static document: HTMLDocument;
#[wasm_bindgen(method)]
fn createElement(this: &HTMLDocument, tagName: &str) -> Element;
#[wasm_bindgen(method, getter)]
fn body(this: &HTMLDocument) -> Element;

type Element;
#[wasm_bindgen(method, setter)]
fn set_innerHTML(this: &Element, html: &str);
#[wasm_bindgen(method)]
fn appendChild(this: &Element, other: Element);
}

// Called by our JS entry point to run the example
#[wasm_bindgen]
pub fn run() {
let val = document.createElement("p");
val.set_innerHTML("Hello from Rust!");
document.body().appendChild(val);
}
10 changes: 10 additions & 0 deletions examples/dom/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
mode: "development"
};

0 comments on commit dd054fa

Please sign in to comment.