Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build examples with trunk #1559

Merged
merged 59 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
f679ec0
remove old files
siku2 Sep 14, 2020
6f06181
update counter
siku2 Sep 14, 2020
c0716ac
update crm example
siku2 Sep 14, 2020
dabf0da
Add readme for counter
siku2 Sep 14, 2020
085f054
update custom_components
siku2 Sep 14, 2020
7057e05
update webgl example
siku2 Sep 14, 2020
fe0365d
update two_apps
siku2 Sep 14, 2020
a19c26a
update todomvc
siku2 Sep 14, 2020
bdde3ae
mark special case multithread
siku2 Sep 14, 2020
da58dc7
update dashboard
siku2 Sep 14, 2020
16315fc
update examples readme
siku2 Sep 14, 2020
4d6f0a7
file_upload example
siku2 Sep 14, 2020
956a640
fragments example
siku2 Sep 14, 2020
6212b7e
futures example
siku2 Sep 14, 2020
8dc6c16
game_of_life example
siku2 Sep 15, 2020
85b464a
inner_html example
siku2 Sep 15, 2020
d1e0714
update trunk-examples
siku2 Sep 15, 2020
81a6afb
js_callback example
siku2 Sep 15, 2020
25256e2
keyed_list example
siku2 Sep 15, 2020
86750e6
remove large_table
siku2 Sep 15, 2020
6f781ee
remove minimal
siku2 Sep 15, 2020
0506037
mount_point example
siku2 Sep 15, 2020
9a68a90
I can't count apparently
siku2 Sep 15, 2020
53c7d47
nested_list example
siku2 Sep 15, 2020
303f04f
node_refs example
siku2 Sep 15, 2020
5412793
timer example
siku2 Sep 15, 2020
a15a1d8
store example
siku2 Sep 15, 2020
8aa61eb
pub_sub example
siku2 Sep 15, 2020
66418c7
rip npm_and_my_sanity
siku2 Sep 15, 2020
b90ced0
use a title:tm:
siku2 Sep 16, 2020
3972863
clean up multi_thread
siku2 Sep 16, 2020
c4672c3
fix format
siku2 Sep 16, 2020
31afc28
update trunk-examples for boid example
siku2 Sep 16, 2020
4bb4337
boids part 1
siku2 Sep 16, 2020
88f0432
boids part 2
siku2 Sep 18, 2020
dc497ec
add workflow
siku2 Sep 18, 2020
a075765
remove my little scratchpad again
siku2 Sep 18, 2020
53052d8
add boids to examples table
siku2 Sep 18, 2020
2521114
runtime-generated list in nested_list example
siku2 Sep 19, 2020
c98b398
update workflow
siku2 Sep 19, 2020
6df08e3
first batch of yewtil examples
siku2 Sep 19, 2020
052770a
clippy "futures"
siku2 Sep 19, 2020
9f4d90e
remove old yew-router examples
siku2 Sep 20, 2020
ca0b178
add a new router example to the main examples
siku2 Sep 20, 2020
5406380
remove remaining yewtil examples
siku2 Sep 20, 2020
7ffef88
more progress
siku2 Sep 20, 2020
b554a53
update for testing purposes
siku2 Sep 20, 2020
f881a63
update trunk-examples to latest commit
siku2 Sep 20, 2020
bdf2e61
author list
siku2 Sep 20, 2020
0395ada
improve content generation
siku2 Sep 21, 2020
0491c63
revert this mistake
siku2 Sep 21, 2020
3537e21
missed a few
siku2 Sep 21, 2020
a7e458f
turn router switch example into test
siku2 Sep 21, 2020
61424d0
seems to be working
siku2 Sep 21, 2020
ded9f56
make it possible to host the router example on a subpath
siku2 Sep 21, 2020
d4b1a59
create a 404 file for SPA
siku2 Sep 21, 2020
ad8c536
remove the three examples and update table
siku2 Sep 21, 2020
f5d0aea
remove the 404 file because it isn't working anyway
siku2 Sep 21, 2020
c562772
fix small router issue relating to the sub-path hack
siku2 Sep 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ members = [
"examples/dashboard",
"examples/file_upload",
"examples/fragments",
"examples/futures_wp",
"examples/futures",
"examples/game_of_life",
"examples/inner_html",
"examples/js_callback",
Expand Down
58 changes: 58 additions & 0 deletions _PR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Changes

This is just the file I'm using to keep track of everything. It won't be part of the final PR.

## TODO

- update all examples
- update examples for yew-router (fix #1377)
- update examples for yewtil

## Somewhat related issues

- #1073 (should probably be closed now)
- #1016 (promises some good stylin' for the examples but it's been a while)

## Examples

## 1. counter

## 2. crm

Modified to make the example more modular.
What used to be a single component handling a lot of message spaghetti is now split into dedicated components.

## 3. custom_components

**Should be updated or removed**.

## 4. dashboard

## 5. file_upload

## 5. fragments

**Should be updated or removed**.

## 6. futures

Renamed from "futures_wp".

## 7. game_of_life

Split code across two files.

## 8. inner_html

## 16. multithread

**TODO** this example is special!
siku2 marked this conversation as resolved.
Show resolved Hide resolved

## 25. todomvc

Split `State` into a separate file to make it more manageable.
Updates TodoMVC CSS to latest version.

## 26. two_apps

## 26. webgl
6 changes: 1 addition & 5 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
*/static/package.json
*/static/wasm_bg.wasm
*/static/wasm_bg.d.ts
*/static/wasm.d.ts
*/static/wasm.js
*/dist/
116 changes: 42 additions & 74 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
# Yew Examples

In order to run the examples, we provide the `run_example.sh` script.
This script takes care of compiling the example and runs a web server for you.
All that's left is for you to play around with the examples :).

> **A note for Windows users:**<br>
> 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>
> See <https://gitforwindows.org/#bash> for more information.
>
> We're always trying to improve the developer experience for developers across all platforms.
> There's an ongoing effort to replace the bash scripts with a Rust command line tool ([#1418](https://github.com/yewstack/yew/issues/1418)).
> If at any point you encounter an issue, don't hesitate to ask a question or open an issue.

## Dependencies

Before we can run the examples we need to get a few things ready.

Some examples currently use `wasm-bindgen` and others use `wasm-pack`.
You can install them both using the following command:
The examples are built with [trunk](https://github.com/thedodd/trunk).
You can install it with the following command:

```bash
cargo install wasm-pack wasm-bindgen-cli
# at some point in the future, trunk should automatically download wasm-bindgen for you
cargo install trunk wasm-bindgen-cli
siku2 marked this conversation as resolved.
Show resolved Hide resolved
```

### Optional dependencies

We've written a small web server which you can use to serve the built examples. In order to
use it, you'll need to have installed Python (3.6 or greater).
You can also use a different web server, provided that it can serve static files from a directory.

> **Note:**<br>
> Some examples don't have an `index.html` file in their static directory.
> The python web server handles this by serving a default index file.
> If you aren't using it, you will need to create the index file manually.

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).
This extension is used to serve static files.
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.

## Run an example

```bash
Expand All @@ -48,54 +20,50 @@ git clone https://github.com/yewstack/yew.git
cd yew/examples

# run the "todomvc" example
./run_example.sh todomvc
cd todomvc
trunk serve
```

## Script options

The general structure of the command looks like this:<br>
`./run_example.sh <example> [OPTIONS]`

`<example>` is the name of the example (i.e. the name of the directory).

The following table describes all possible options:

| Option | Description |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------- |
| `--debug`<br>`--release` | Specifies which profile to use for `cargo build`. Defaults to `--debug`. |
| `--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. |
Some examples will may require additional steps.
In those cases, instructions are placed in the example's README.md file.

## List of examples

| Example | Description | Has README |
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | :--------: |
| [counter](counter) | A single component which displays a stateful number. The number can be incremented and decremented using buttons | ✖ |
| [crm](crm) | See the `README` file for details | ✔ |
| [custom_components](custom_components) | Demonstrates the use of components | ✔ |
| [dashboard](dashboard) | Uses the `fetch` and `websocket` services to load external data | ✔ |
| [file_upload](file_upload) | Uses the `reader` service to read the content of user uploaded files | ✖ |
| [fragments](fragments) | Similar to the counter example but demonstrating the use of [fragments](https://yew.rs/docs/concepts/html/lists#fragments) | ✖ |
| [futures_wp](futures_wp) | Demonstrates how you can use futures and async code with Yew. Features a Markdown renderer. | ✔ |
| [game_of_life](game_of_life) | Implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) | ✖ |
| [inner_html](inner_html) | Embeds an external document as raw HTML by manually managing the element | ✖ |
| [js_callback](js_callback) | Interacts with JavaScript code | ✔ |
| [keyed_list](keyed_list) | Demonstrates how to use keys to improve the performance of lists | ✖ |
| [large_table](large_table) | Renders a big table which highlights the selected cell | ✖ |
| [minimal](minimal) | A simple button that listens to click events | ✖ |
| [minimal_wp](minimal) | Same as the minimal example but using `wasm-pack` | ✖ |
| [mount_point](mount_point) | Shows how to mount the root component to a custom element | ✖ |
| [multi_thread](multi_thread) | Demonstrates the use of Web Workers to offload computation to the background | ✔ |
| [nested_list](nested_list) | Renders a styled list which tracks hover events | ✖ |
| [node_refs](node_refs) | Uses a [`NodeRef`](https://yew.rs/docs/concepts/components/refs) to focus the input element under the cursor | ✖ |
| [npm_and_rest](npm_and_rest) | A more elaborate demonstration of the `fetch` service | ✖ |
| [pub_sub](pub_sub) | Cross-component communication using [Agents](https://yew.rs/docs/concepts/agents) | ✔ |
| [store](store) | Showcases the `yewtil::store` API | ✔ |
| [textarea](textarea) | Shows how to use the value of a textarea or input tag | ✖ |
| [timer](timer) | Demonstrates the use of the interval and timeout services | ✖ |
| [todomvc](todomvc) | Implementation of the [TodoMVC](http://todomvc.com/) app | ✔ |
| [two_apps](two_apps) | Runs two separate Yew apps at the same time | ✖ |
| [webgl](webgl) | Controls a [WebGL canvas](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL) from Yew | ✔ |
| Example | Description |
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [counter](counter) | A single component which displays a stateful number. The number can be incremented and decremented using buttons |
| [crm](crm) | See the `README` file for details |
| [custom_components](custom_components) | Demonstrates the use of components |
| [dashboard](dashboard) | Uses the `fetch` and `websocket` services to load external data |
| [file_upload](file_upload) | Uses the `reader` service to read the content of user uploaded files |
| [fragments](fragments) | Similar to the counter example but demonstrating the use of [fragments](https://yew.rs/docs/concepts/html/lists#fragments) |
| [futures_wp](futures_wp) | Demonstrates how you can use futures and async code with Yew. Features a Markdown renderer. |
| [game_of_life](game_of_life) | Implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) |
| [inner_html](inner_html) | Embeds an external document as raw HTML by manually managing the element |
| [js_callback](js_callback) | Interacts with JavaScript code |
| [keyed_list](keyed_list) | Demonstrates how to use keys to improve the performance of lists |
| [large_table](large_table) | Renders a big table which highlights the selected cell |
| [minimal](minimal) | A simple button that listens to click events |
| [minimal_wp](minimal) | Same as the minimal example but using `wasm-pack` |
| [mount_point](mount_point) | Shows how to mount the root component to a custom element |
| [multi_thread](multi_thread) | Demonstrates the use of Web Workers to offload computation to the background |
| [nested_list](nested_list) | Renders a styled list which tracks hover events |
| [node_refs](node_refs) | Uses a [`NodeRef`](https://yew.rs/docs/concepts/components/refs) to focus the input element under the cursor |
| [npm_and_rest](npm_and_rest) | A more elaborate demonstration of the `fetch` service |
| [pub_sub](pub_sub) | Cross-component communication using [Agents](https://yew.rs/docs/concepts/agents) |
| [store](store) | Showcases the `yewtil::store` API |
| [textarea](textarea) | Shows how to use the value of a textarea or input tag |
| [timer](timer) | Demonstrates the use of the interval and timeout services |
| [todomvc](todomvc) | Implementation of the [TodoMVC](http://todomvc.com/) app |
| [two_apps](two_apps) | Runs two separate Yew apps at the same time |
| [webgl](webgl) | Controls a [WebGL canvas](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL) from Yew |

## Next steps

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.

## Help out

Most examples have an "improvements" section in their README.md which lists ways to improve the example.

The biggest point of improvement is the presentation of the examples (ex. styling).
11 changes: 11 additions & 0 deletions examples/counter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Counter Example

A counter which can be increased or decreased with the press of a button.

## Concepts

Demonstrates the use of messages to update state.

## Improvements

- Improve the presentation of the example with CSS.
9 changes: 9 additions & 0 deletions examples/counter/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yew • Counter</title>
</head>

<body></body>
</html>
62 changes: 0 additions & 62 deletions examples/counter/src/lib.rs

This file was deleted.

70 changes: 69 additions & 1 deletion examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
use js_sys::Date;
use yew::services::ConsoleService;
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub enum Msg {
Increment,
Decrement,
}

pub struct Model {
link: ComponentLink<Self>,
value: i64,
}

impl Component for Model {
type Message = Msg;
type Properties = ();

fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
Model { link, value: 0 }
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Increment => {
self.value += 1;
ConsoleService::log("plus one");
true
}
Msg::Decrement => {
self.value -= 1;
ConsoleService::log("minus one");
true
}
}
}

fn change(&mut self, _props: Self::Properties) -> ShouldRender {
unimplemented!()
}

fn view(&self) -> Html {
html! {
<div>
<nav class="menu">
<button onclick=self.link.callback(|_| Msg::Increment)>
{ "Increment" }
</button>
<button onclick=self.link.callback(|_| Msg::Decrement)>
{ "Decrement" }
</button>
<button onclick=self.link.batch_callback(|_| vec![Msg::Increment, Msg::Increment])>
{ "Increment Twice" }
</button>
</nav>
<p>
<b>{ "Current value: " }</b>
{ self.value }
</p>
<p>
<b>{ "Rendered at: " }</b>
{ String::from(Date::new_0().to_string()) }
</p>
</div>
}
}
}

fn main() {
yew::start_app::<counter::Model>();
yew::start_app::<Model>();
}
Loading