Skip to content

Commit c946b99

Browse files
authored
Overhaul examples (yewstack#1559)
* remove old files * update counter * update crm example * Add readme for counter * update custom_components * update webgl example * update two_apps * update todomvc * mark special case multithread * update dashboard * update examples readme * file_upload example * fragments example * futures example * game_of_life example * inner_html example * js_callback example * keyed_list example * remove large_table * remove minimal * mount_point example * I can't count apparently * nested_list example * node_refs example * timer example * store example * pub_sub example * rip npm_and_my_sanity * use a title:tm: * clean up multi_thread * fix format * boids part 1 * boids part 2 * add workflow See: <siku2#13> * remove my little scratchpad again * add boids to examples table * runtime-generated list in nested_list example * update workflow * first batch of yewtil examples * clippy "futures" * remove old yew-router examples * add a new router example to the main examples * remove remaining yewtil examples * more progress * update for testing purposes * author list * improve content generation * revert this mistake I thought it would be great to use `unimplemented!()` in case the component doesn't have any properties. This helps avoid the mistake of forgetting to update the change method when adding props later on. What I didn't consider is that just because the props are () that doesn't mean that Yew isn't going to call it... So yeah, it's still a good idea for update, but certainly not for change. * missed a few * turn router switch example into test * seems to be working * make it possible to host the router example on a sub-path * create a 404 file for SPA * remove the three examples and update table * remove the 404 file because it isn't working anyway * fix small router issue relating to the sub-path hack
1 parent 1ed78fa commit c946b99

File tree

247 files changed

+6315
-6110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

247 files changed

+6315
-6110
lines changed
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Publish Examples
2+
on:
3+
push:
4+
branches: [master]
5+
6+
jobs:
7+
publish:
8+
runs-on: ubuntu-latest
9+
env:
10+
# leave empty for /
11+
PUBLIC_URL_PREFIX: ""
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions-rs/toolchain@v1
16+
with:
17+
toolchain: stable
18+
target: wasm32-unknown-unknown
19+
override: true
20+
profile: minimal
21+
22+
- uses: actions/cache@v2
23+
with:
24+
path: |
25+
~/.cargo/registry
26+
~/.cargo/git
27+
target
28+
key: cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.toml') }}
29+
restore-keys: |
30+
cargo-${{ runner.os }}-
31+
32+
- name: Install trunk
33+
run: cargo install trunk wasm-bindgen-cli
34+
35+
- name: Build examples
36+
run: |
37+
output="$(pwd)/dist"
38+
39+
for path in examples/*; do
40+
if [[ ! -d $path ]]; then
41+
continue
42+
fi
43+
44+
example=$(basename "$path")
45+
46+
# multi_thread doesn't work yet. See <https://github.com/thedodd/trunk/issues/40>.
47+
if [[ "$example" == "multi_thread" ]]; then
48+
continue
49+
fi
50+
51+
echo "building: $example"
52+
(
53+
cd "$path"
54+
dist_dir="$output/$example"
55+
56+
export PUBLIC_URL="$PUBLIC_URL_PREFIX/$example"
57+
trunk build --release --dist "$dist_dir" --public-url "$PUBLIC_URL"
58+
)
59+
done
60+
61+
- name: Deploy
62+
uses: peaceiris/actions-gh-pages@v3.7.0-8
63+
with:
64+
github_token: ${{ secrets.GITHUB_TOKEN }}
65+
publish_dir: ./dist

Cargo.toml

+2-20
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,31 @@ members = [
1010
"yew-router",
1111
"yew-router-macro",
1212
"yew-router-route-parser",
13-
"yew-router/examples/minimal",
14-
"yew-router/examples/router_component",
15-
"yew-router/examples/switch",
1613

1714
# Utilities
1815
"yewtil",
1916
"yewtil-macro",
20-
"yewtil/examples/pure_component",
21-
# "yewtil/examples/dsl",
22-
"yewtil/examples/lrc",
23-
"yewtil/examples/history",
24-
"yewtil/examples/mrc_irc",
25-
"yewtil/examples/effect",
26-
"yewtil/examples/fetch",
27-
"yewtil/examples/futures",
28-
"yewtil/examples/function_component",
2917

3018
# dsl
3119
"yew-dsl",
3220

3321
# Examples
3422
"examples/counter",
3523
"examples/crm",
36-
"examples/custom_components",
3724
"examples/dashboard",
3825
"examples/file_upload",
39-
"examples/fragments",
40-
"examples/futures_wp",
26+
"examples/futures",
4127
"examples/game_of_life",
4228
"examples/inner_html",
4329
"examples/js_callback",
4430
"examples/keyed_list",
45-
"examples/large_table",
46-
"examples/minimal",
47-
"examples/minimal_wp",
4831
"examples/mount_point",
4932
"examples/multi_thread",
5033
"examples/nested_list",
5134
"examples/node_refs",
52-
"examples/npm_and_rest",
5335
"examples/pub_sub",
36+
"examples/router",
5437
"examples/store",
55-
"examples/textarea",
5638
"examples/timer",
5739
"examples/todomvc",
5840
"examples/two_apps",

examples/.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
*/static/package.json
2-
*/static/wasm_bg.wasm
3-
*/static/wasm_bg.d.ts
4-
*/static/wasm.d.ts
5-
*/static/wasm.js
1+
*/dist/

examples/README.md

+37-74
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
# Yew Examples
22

3-
In order to run the examples, we provide the `run_example.sh` script.
4-
This script takes care of compiling the example and runs a web server for you.
5-
All that's left is for you to play around with the examples :).
6-
7-
> **A note for Windows users:**<br>
8-
> Depending on how you installed `git` you will already have a bash emulator at your disposal. This allows you to run the `run_example.sh` script (and any other bash script) normally.<br>
9-
> See <https://gitforwindows.org/#bash> for more information.
10-
>
11-
> We're always trying to improve the developer experience for developers across all platforms.
12-
> There's an ongoing effort to replace the bash scripts with a Rust command line tool ([#1418](https://github.com/yewstack/yew/issues/1418)).
13-
> If at any point you encounter an issue, don't hesitate to ask a question or open an issue.
14-
153
## Dependencies
164

17-
Before we can run the examples we need to get a few things ready.
18-
19-
Some examples currently use `wasm-bindgen` and others use `wasm-pack`.
20-
You can install them both using the following command:
5+
The examples are built with [trunk](https://github.com/thedodd/trunk).
6+
You can install it with the following command:
217

228
```bash
23-
cargo install wasm-pack wasm-bindgen-cli
9+
# at some point in the future, trunk should automatically download wasm-bindgen for you
10+
cargo install trunk wasm-bindgen-cli
2411
```
2512

26-
### Optional dependencies
27-
28-
We've written a small web server which you can use to serve the built examples. In order to
29-
use it, you'll need to have installed Python (3.6 or greater).
30-
You can also use a different web server, provided that it can serve static files from a directory.
31-
32-
> **Note:**<br>
33-
> Some examples don't have an `index.html` file in their static directory.
34-
> The python web server handles this by serving a default index file.
35-
> If you aren't using it, you will need to create the index file manually.
36-
37-
One alternative to the built-in web server there is an extension for [Visual Studio Code](https://code.visualstudio.com/) called [Live Server](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer).
38-
This extension is used to serve static files.
39-
To do so, open the `index.html` file in the static directory of the example you wish to run and press "Open with Live Server" in the context menu.
40-
4113
## Run an example
4214

4315
```bash
@@ -48,54 +20,45 @@ git clone https://github.com/yewstack/yew.git
4820
cd yew/examples
4921

5022
# run the "todomvc" example
51-
./run_example.sh todomvc
23+
cd todomvc
24+
trunk serve --release
5225
```
5326

54-
## Script options
55-
56-
The general structure of the command looks like this:<br>
57-
`./run_example.sh <example> [OPTIONS]`
58-
59-
`<example>` is the name of the example (i.e. the name of the directory).
60-
61-
The following table describes all possible options:
62-
63-
| Option | Description |
64-
| ------------------------ | ------------------------------------------------------------------------------------------------------------------- |
65-
| `--debug`<br>`--release` | Specifies which profile to use for `cargo build`. Defaults to `--debug`. |
66-
| `--build-only` | Disables the built-in server.<br>Use this if you don't have Python installed or if you want to use your own server. |
27+
Some examples might require additional steps.
28+
In this case, instructions can be found in the example's `README` file.
6729

6830
## List of examples
6931

70-
| Example | Description | Has README |
71-
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | :--------: |
72-
| [counter](counter) | A single component which displays a stateful number. The number can be incremented and decremented using buttons ||
73-
| [crm](crm) | See the `README` file for details ||
74-
| [custom_components](custom_components) | Demonstrates the use of components ||
75-
| [dashboard](dashboard) | Uses the `fetch` and `websocket` services to load external data ||
76-
| [file_upload](file_upload) | Uses the `reader` service to read the content of user uploaded files ||
77-
| [fragments](fragments) | Similar to the counter example but demonstrating the use of [fragments](https://yew.rs/docs/concepts/html/lists#fragments) ||
78-
| [futures_wp](futures_wp) | Demonstrates how you can use futures and async code with Yew. Features a Markdown renderer. ||
79-
| [game_of_life](game_of_life) | Implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) ||
80-
| [inner_html](inner_html) | Embeds an external document as raw HTML by manually managing the element ||
81-
| [js_callback](js_callback) | Interacts with JavaScript code ||
82-
| [keyed_list](keyed_list) | Demonstrates how to use keys to improve the performance of lists ||
83-
| [large_table](large_table) | Renders a big table which highlights the selected cell ||
84-
| [minimal](minimal) | A simple button that listens to click events ||
85-
| [minimal_wp](minimal) | Same as the minimal example but using `wasm-pack` ||
86-
| [mount_point](mount_point) | Shows how to mount the root component to a custom element ||
87-
| [multi_thread](multi_thread) | Demonstrates the use of Web Workers to offload computation to the background ||
88-
| [nested_list](nested_list) | Renders a styled list which tracks hover events ||
89-
| [node_refs](node_refs) | Uses a [`NodeRef`](https://yew.rs/docs/concepts/components/refs) to focus the input element under the cursor ||
90-
| [npm_and_rest](npm_and_rest) | A more elaborate demonstration of the `fetch` service ||
91-
| [pub_sub](pub_sub) | Cross-component communication using [Agents](https://yew.rs/docs/concepts/agents) ||
92-
| [store](store) | Showcases the `yewtil::store` API ||
93-
| [textarea](textarea) | Shows how to use the value of a textarea or input tag ||
94-
| [timer](timer) | Demonstrates the use of the interval and timeout services ||
95-
| [todomvc](todomvc) | Implementation of the [TodoMVC](http://todomvc.com/) app ||
96-
| [two_apps](two_apps) | Runs two separate Yew apps at the same time ||
97-
| [webgl](webgl) | Controls a [WebGL canvas](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL) from Yew ||
32+
| Example | Description |
33+
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
34+
| [boids](boids) | Yew port of [Boids](https://en.wikipedia.org/wiki/Boids) |
35+
| [counter](counter) | Simple counter which can be incremented and decremented |
36+
| [crm](crm) | Shallow customer relationship management tool |
37+
| [dashboard](dashboard) | Uses the `fetch` and `websocket` services to load external data |
38+
| [file_upload](file_upload) | Uses the `reader` service to read the content of user uploaded files |
39+
| [futures](futures) | Demonstrates how you can use futures and async code with Yew. Features a Markdown renderer. |
40+
| [game_of_life](game_of_life) | Implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) |
41+
| [inner_html](inner_html) | Embeds an external document as raw HTML by manually managing the element |
42+
| [js_callback](js_callback) | Interacts with JavaScript code |
43+
| [keyed_list](keyed_list) | Demonstrates how to use keys to improve the performance of lists |
44+
| [mount_point](mount_point) | Shows how to mount the root component to a custom element |
45+
| [multi_thread](multi_thread) | Demonstrates the use of Web Workers to offload computation to the background |
46+
| [nested_list](nested_list) | Renders a styled list which tracks hover events |
47+
| [node_refs](node_refs) | Uses a [`NodeRef`](https://yew.rs/docs/concepts/components/refs) to focus the input element under the cursor |
48+
| [pub_sub](pub_sub) | Cross-component communication using [Agents](https://yew.rs/docs/concepts/agents) |
49+
| [router](router) | The best yew blog built with `yew-router` |
50+
| [store](store) | Showcases the `yewtil::store` API |
51+
| [timer](timer) | Demonstrates the use of the interval and timeout services |
52+
| [todomvc](todomvc) | Implementation of [TodoMVC](http://todomvc.com/) |
53+
| [two_apps](two_apps) | Runs two separate Yew apps which can communicate with each other |
54+
| [webgl](webgl) | Controls a [WebGL canvas](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL) from Yew |
9855

9956
## Next steps
10057

10158
Have a look at Yew's [starter templates](https://yew.rs/docs/getting-started/starter-templates) when starting a project using Yew – they can significantly simplify things.
59+
60+
## Help out
61+
62+
Most examples have an "improvements" section in their README.md which lists ways to improve the example.
63+
64+
The biggest point of improvement is the presentation of the examples (ex. styling).

examples/boids/.gitignore

-2
This file was deleted.

examples/boids/Cargo.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ authors = ["motoki saito <stmtk13044032@gmail.com>"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
9-
[lib]
10-
crate-type = ["cdylib", "rlib"]
11-
128
[dependencies]
13-
rand = { version = "0.7.3", features = ["wasm-bindgen"] }
9+
anyhow = "1.0"
10+
rand = { version = "0.7", features = ["wasm-bindgen"] }
11+
serde = { version = "1.0", features = ["derive"] }
1412
yew = { path = "../../yew" }

examples/boids/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Boids Example
2+
3+
A version of [Boids](https://en.wikipedia.org/wiki/Boids) implemented in Yew.
4+
5+
This example doesn't make use of a [Canvas](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API),
6+
instead, each boid has its own element demonstrating the performance of Yew's virtual DOM.
7+
8+
## Running
9+
10+
You should run this example with the `--release` flag:
11+
12+
```bash
13+
trunk serve --release
14+
```
15+
16+
## Concepts
17+
18+
The example uses [`IntervalService`] to drive the game loop.
19+
20+
## Improvements
21+
22+
- Add the possibility to switch the behaviour from flocking to scattering by inverting the cohesion rule so that boids avoid each other.
23+
This should also invert the color adaption to restore some variety.
24+
- Add keyboard shortcuts (using the `KeyboardService`) for the actions.
25+
- Make it possible to hide the settings panel entirely
26+
- Bigger boids should accelerate slower than smaller ones
27+
- Share settings by encoding them into the URL
28+
- Resize the boids when "Spacing" is changed.
29+
The setting should then also be renamed to something like "Size".
30+
31+
[`intervalservice`]: https://docs.rs/yew/latest/yew/services/struct.IntervalService.html

examples/boids/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Yew • Boids</title>
6+
7+
<link rel="stylesheet" href="index.scss" />
8+
</head>
9+
10+
<body></body>
11+
</html>

0 commit comments

Comments
 (0)