Skip to content

Commit c1a7ba9

Browse files
authored
Merge branch 'master' into dom-bundle
2 parents 02c7dd6 + 3a11f15 commit c1a7ba9

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ As an example, check out the TodoMVC example here: <https://examples.yew.rs/todo
5050
| [router](router) | S | The best yew blog built with `yew-router` |
5151
| [simple_ssr](simple_ssr) | F | Demonstrates server-side rendering |
5252
| [store](store) | S | Showcases the `yewtil::store` API |
53-
| [suspense](suspense) | F | This is an example that demonstrates <Suspense /> support |
53+
| [suspense](suspense) | F | This is an example that demonstrates `<Suspense />` support |
5454
| [function_memory_game](function_memory_game) | F | Implementation of [Memory Game](https://github.com/bradlygreen/Memory-Game) |
5555
| [timer](timer) | S | Demonstrates the use of the interval and timeout services |
5656
| [todomvc](todomvc) | S | Implementation of [TodoMVC](http://todomvc.com/) |

packages/yew/src/html/component/scope.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,35 @@ impl AnyScope {
103103
}
104104

105105
/// Attempts to downcast into a typed scope
106+
///
107+
/// # Panics
108+
///
109+
/// If the self value can't be cast into the target type.
106110
pub fn downcast<COMP: BaseComponent>(self) -> Scope<COMP> {
111+
self.try_downcast::<COMP>().unwrap()
112+
}
113+
114+
/// Attempts to downcast into a typed scope
115+
///
116+
/// Returns [`None`] if the self value can't be cast into the target type.
117+
pub fn try_downcast<COMP: BaseComponent>(self) -> Option<Scope<COMP>> {
107118
let state = self.state.borrow();
108119

109-
state
110-
.as_ref()
111-
.map(|m| {
112-
m.inner
113-
.as_any()
114-
.downcast_ref::<CompStateInner<COMP>>()
115-
.unwrap()
116-
.context
117-
.link()
118-
.clone()
119-
})
120-
.unwrap()
120+
state.as_ref().map(|m| {
121+
m.inner
122+
.as_any()
123+
.downcast_ref::<CompStateInner<COMP>>()
124+
.unwrap()
125+
.context
126+
.link()
127+
.clone()
128+
})
121129
}
122130

123-
pub(super) fn find_parent_scope<C: BaseComponent>(&self) -> Option<Scope<C>> {
131+
/// Attempts to find a parent scope of a certain type
132+
///
133+
/// Returns [`None`] if no parent scope with the specified type was found.
134+
pub fn find_parent_scope<C: BaseComponent>(&self) -> Option<Scope<C>> {
124135
let expected_type_id = TypeId::of::<C>();
125136
iter::successors(Some(self), |scope| scope.get_parent())
126137
.filter(|scope| scope.get_type_id() == &expected_type_id)

website/docs/concepts/html/events.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,7 @@ which will remove the event listener from the element.
694694

695695
For more information on `EventListener`, see the
696696
[gloo_events docs.rs](https://docs.rs/gloo-events/0.1.1/gloo_events/struct.EventListener.html).
697+
698+
## Using EventListener in a Function Component
699+
700+
See [Applying event listeners to the DOM](../function-components/hooks/use-effect#applying-event-listeners-to-the-dom).

website/docs/tutorial/index.mdx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ get an overview of the talks and watch them all from one page.
2424

2525
### Prerequisites
2626

27-
To get started, let's make sure we have an up-to-date development environment.
28-
We will need the following tools:
29-
- [Rust](https://www.rust-lang.org/)
30-
- [`trunk`](https://trunkrs.dev/)
31-
- `wasm32-unknown-unknown` target, the WASM compiler and build target for Rust.
32-
33-
This tutorial also assumes you're already familiar with Rust. If you're new to Rust,
27+
This tutorial assumes you're already familiar with Rust. If you're new to Rust,
3428
the free [Rust Book](https://doc.rust-lang.org/book/ch00-00-introduction.html) offers a great starting point for
3529
beginners and continues to be an excellent resource even for experienced Rust developers.
3630

@@ -78,7 +72,7 @@ version = "0.1.0"
7872
edition = "2018"
7973

8074
[dependencies]
81-
+ yew = { git = "https://github.com/yewstack/yew/" }
75+
yew = { git = "https://github.com/yewstack/yew/" }
8276
```
8377

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

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

412-
```rust ,ignore {4,6-11,13-15,24-29}
406+
```rust ,ignore {4,6-11,13-15,22-23,25-29}
413407
#[function_component(App)]
414408
fn app() -> Html {
415409
// ...
@@ -439,14 +433,15 @@ fn app() -> Html {
439433
- <h3>{ "John Doe: Building and breaking things" }</h3>
440434
- <img src="https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder" alt="video thumbnail" />
441435
- </div>
442-
- </>
436+
</>
443437
}
444438
}
445439
```
446440

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

451446
### Handling state
452447

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Refs
3+
description: Out-of-band DOM access
4+
---
5+
6+
`ref`は、任意のHTML要素やコンポーネントの内部で、割り当てられているDOM`Element`を取得するために使用することができます。
7+
これは、`view` ライフサイクルメソッドの外でDOMに変更を加えるために使用できます。
8+
9+
これは、キャンバスの要素を取得したり、ページの異なるセクションにスクロールしたりするのに便利です。
10+
11+
構文は以下の通りです:
12+
13+
```rust
14+
// In create
15+
self.node_ref = NodeRef::default();
16+
17+
// In view
18+
html! {
19+
<div ref={self.node_ref.clone()}></div>
20+
}
21+
22+
// In update
23+
let has_attributes = self.node_ref.try_into::<Element>().has_attributes();
24+
```
25+

0 commit comments

Comments
 (0)