Skip to content

Commit

Permalink
Merge branch 'master' into dom-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldSEnder committed Mar 5, 2022
2 parents 02c7dd6 + 3a11f15 commit c1a7ba9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Expand Up @@ -50,7 +50,7 @@ As an example, check out the TodoMVC example here: <https://examples.yew.rs/todo
| [router](router) | S | The best yew blog built with `yew-router` |
| [simple_ssr](simple_ssr) | F | Demonstrates server-side rendering |
| [store](store) | S | Showcases the `yewtil::store` API |
| [suspense](suspense) | F | This is an example that demonstrates <Suspense /> support |
| [suspense](suspense) | F | This is an example that demonstrates `<Suspense />` support |
| [function_memory_game](function_memory_game) | F | Implementation of [Memory Game](https://github.com/bradlygreen/Memory-Game) |
| [timer](timer) | S | Demonstrates the use of the interval and timeout services |
| [todomvc](todomvc) | S | Implementation of [TodoMVC](http://todomvc.com/) |
Expand Down
37 changes: 24 additions & 13 deletions packages/yew/src/html/component/scope.rs
Expand Up @@ -103,24 +103,35 @@ impl AnyScope {
}

/// Attempts to downcast into a typed scope
///
/// # Panics
///
/// If the self value can't be cast into the target type.
pub fn downcast<COMP: BaseComponent>(self) -> Scope<COMP> {
self.try_downcast::<COMP>().unwrap()
}

/// Attempts to downcast into a typed scope
///
/// Returns [`None`] if the self value can't be cast into the target type.
pub fn try_downcast<COMP: BaseComponent>(self) -> Option<Scope<COMP>> {
let state = self.state.borrow();

state
.as_ref()
.map(|m| {
m.inner
.as_any()
.downcast_ref::<CompStateInner<COMP>>()
.unwrap()
.context
.link()
.clone()
})
.unwrap()
state.as_ref().map(|m| {
m.inner
.as_any()
.downcast_ref::<CompStateInner<COMP>>()
.unwrap()
.context
.link()
.clone()
})
}

pub(super) fn find_parent_scope<C: BaseComponent>(&self) -> Option<Scope<C>> {
/// Attempts to find a parent scope of a certain type
///
/// Returns [`None`] if no parent scope with the specified type was found.
pub fn find_parent_scope<C: BaseComponent>(&self) -> Option<Scope<C>> {
let expected_type_id = TypeId::of::<C>();
iter::successors(Some(self), |scope| scope.get_parent())
.filter(|scope| scope.get_type_id() == &expected_type_id)
Expand Down
4 changes: 4 additions & 0 deletions website/docs/concepts/html/events.mdx
Expand Up @@ -694,3 +694,7 @@ which will remove the event listener from the element.

For more information on `EventListener`, see the
[gloo_events docs.rs](https://docs.rs/gloo-events/0.1.1/gloo_events/struct.EventListener.html).

## Using EventListener in a Function Component

See [Applying event listeners to the DOM](../function-components/hooks/use-effect#applying-event-listeners-to-the-dom).
17 changes: 6 additions & 11 deletions website/docs/tutorial/index.mdx
Expand Up @@ -24,13 +24,7 @@ get an overview of the talks and watch them all from one page.

### Prerequisites

To get started, let's make sure we have an up-to-date development environment.
We will need the following tools:
- [Rust](https://www.rust-lang.org/)
- [`trunk`](https://trunkrs.dev/)
- `wasm32-unknown-unknown` target, the WASM compiler and build target for Rust.

This tutorial also assumes you're already familiar with Rust. If you're new to Rust,
This tutorial assumes you're already familiar with Rust. If you're new to Rust,
the free [Rust Book](https://doc.rust-lang.org/book/ch00-00-introduction.html) offers a great starting point for
beginners and continues to be an excellent resource even for experienced Rust developers.

Expand Down Expand Up @@ -78,7 +72,7 @@ version = "0.1.0"
edition = "2018"

[dependencies]
+ yew = { git = "https://github.com/yewstack/yew/" }
yew = { git = "https://github.com/yewstack/yew/" }
```

```rust ,no_run title="src/main.rs"
Expand Down Expand Up @@ -409,7 +403,7 @@ fn video_details(VideosDetailsProps { video }: &VideosDetailsProps) -> Html {

Now, modify the `App` component to display `VideoDetails` component whenever a video is selected.

```rust ,ignore {4,6-11,13-15,24-29}
```rust ,ignore {4,6-11,13-15,22-23,25-29}
#[function_component(App)]
fn app() -> Html {
// ...
Expand Down Expand Up @@ -439,14 +433,15 @@ fn app() -> Html {
- <h3>{ "John Doe: Building and breaking things" }</h3>
- <img src="https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder" alt="video thumbnail" />
- </div>
- </>
</>
}
}
```

Don't worry about the `use_state` right now, we will come back to that later.
Note the trick we pulled with `{ for details }`. `Option<_>` implements `Iterator` so we can use it to display the only
element returned by the `Iterator` with the `{ for ... }` syntax.
element returned by the `Iterator` with a special `{ for ... }` syntax
[supported by the `html!` macro](concepts/html/lists).

### Handling state

Expand Down
@@ -0,0 +1,25 @@
---
title: Refs
description: Out-of-band DOM access
---

`ref`は、任意のHTML要素やコンポーネントの内部で、割り当てられているDOM`Element`を取得するために使用することができます。
これは、`view` ライフサイクルメソッドの外でDOMに変更を加えるために使用できます。

これは、キャンバスの要素を取得したり、ページの異なるセクションにスクロールしたりするのに便利です。

構文は以下の通りです:

```rust
// In create
self.node_ref = NodeRef::default();

// In view
html! {
<div ref={self.node_ref.clone()}></div>
}

// In update
let has_attributes = self.node_ref.try_into::<Element>().has_attributes();
```

1 comment on commit c1a7ba9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: c1a7ba9 Previous: 3a11f15 Ratio
yew-struct-keyed 01_run1k 161.952 210.1425 0.77
yew-struct-keyed 02_replace1k 180.2415 226.9655 0.79
yew-struct-keyed 03_update10th1k_x16 418.801 393.74 1.06
yew-struct-keyed 04_select1k 77.785 89.3675 0.87
yew-struct-keyed 05_swap1k 90.681 102.049 0.89
yew-struct-keyed 06_remove-one-1k 27.7025 34.9915 0.79
yew-struct-keyed 07_create10k 1893.6195 2382.73 0.79
yew-struct-keyed 08_create1k-after1k_x2 384.038 467.164 0.82
yew-struct-keyed 09_clear1k_x8 200.3 186.206 1.08
yew-struct-keyed 21_ready-memory 0.9634475708007812 0.9634475708007812 1
yew-struct-keyed 22_run-memory 1.5048332214355469 1.5028305053710938 1.00
yew-struct-keyed 23_update5-memory 1.5063819885253906 1.4616203308105469 1.03
yew-struct-keyed 24_run5-memory 1.5059242248535156 1.5110435485839844 1.00
yew-struct-keyed 25_run-clear-memory 1.128643035888672 1.1257438659667969 1.00
yew-struct-keyed 31_startup-ci 1810.3185000000003 1789.48175 1.01
yew-struct-keyed 32_startup-bt 34.26399999999999 37.82199999999999 0.91
yew-struct-keyed 34_startup-totalbytes 359.8974609375 359.8974609375 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.