Skip to content

Commit 79a2f02

Browse files
ASR-ASUealmloffjkelleyrtp
authored
Add the onvisible event handler to Element (#2911)
* Add the `onvisible` event using the IntersectionObserver * Add a visible example This example is based on https://codepen.io/ryanfinni/pen/VwZeGxN * Remove web-sys CustomEvent feature for html package * update js bindings and hash * Use dioxus doc instead of lorem ipsum * Fix clippy warning * Add get_target method to the event data * Make `get_time` return an Instant instead of a f64 * fix merge conflicts, compilinga * remove target * small nits * serialzie as time_ms * fix timing, works on desktop * remove latent nits, fix hash * box size clippy * fix browserlist cargo.lock --------- Co-authored-by: Evan Almloff <[email protected]> Co-authored-by: Jonathan Kelley <[email protected]>
1 parent b114ed3 commit 79a2f02

File tree

20 files changed

+961
-289
lines changed

20 files changed

+961
-289
lines changed

Cargo.lock

+325-271
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ doc-scrape-examples = true
436436
name = "streams"
437437
doc-scrape-examples = true
438438

439+
[[example]]
440+
name = "visible"
441+
doc-scrape-examples = true
442+
439443
[[example]]
440444
name = "window_event"
441445
required-features = ["desktop"]

examples/assets/visible.css

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
.container {
6+
height: 500px;
7+
width: 500px;
8+
margin: 100px auto;
9+
}
10+
11+
p {
12+
line-height: 30px;
13+
14+
}
15+
16+
.animated-text {
17+
font-size: 30px;
18+
text-align: center;
19+
opacity: 0;
20+
transform: translateY(100px);
21+
transition: opacity 1s ease, transform 1s ease;
22+
}
23+
24+
.animated-text.visible {
25+
opacity: 1;
26+
transform: translateX(0);
27+
}

examples/clock.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! A simple little clock that updates the time every few milliseconds.
2-
//!
32
43
use async_std::task::sleep;
54
use dioxus::prelude::*;

examples/visible.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! Port of the https://codepen.io/ryanfinni/pen/VwZeGxN example
2+
3+
use dioxus::prelude::*;
4+
5+
fn main() {
6+
dioxus::launch(app);
7+
}
8+
9+
fn app() -> Element {
10+
let mut animated_classes = use_signal(|| ["animated-text", ""]);
11+
12+
rsx! {
13+
document::Link { rel: "stylesheet", href: asset!("./examples/assets/visible.css") }
14+
15+
div {
16+
class: "container",
17+
18+
p {
19+
"Scroll to the bottom of the page. The text will transition in when it becomes visible in the viewport."
20+
}
21+
22+
p {
23+
"First, let's create a new project for our hacker news app. We can use the CLI to create a new
24+
project. You can select a platform of your choice or view the getting started guide for more information
25+
on each option. If you aren't sure what platform to try out, we recommend getting started with web or
26+
desktop:"
27+
}
28+
29+
p {
30+
"The template contains some boilerplate to help you get started. For this guide, we will be rebuilding some of the code
31+
from scratch for learning purposes. You can clear the src/main.rs file. We will be adding new code in the next
32+
sections."
33+
}
34+
35+
p {
36+
"Next, let's setup our dependencies. We need to set up a few dependencies to work with the hacker news API: "
37+
}
38+
39+
p {
40+
"First, let's create a new project for our hacker news app. We can use the CLI to create a new
41+
project. You can select a platform of your choice or view the getting started guide for more information
42+
on each option. If you aren't sure what platform to try out, we recommend getting started with web or
43+
desktop:"
44+
}
45+
46+
p {
47+
"The template contains some boilerplate to help you get started. For this guide, we will be rebuilding some of the code
48+
from scratch for learning purposes. You can clear the src/main.rs file. We will be adding new code in the next
49+
sections."
50+
}
51+
52+
p {
53+
"Next, let's setup our dependencies. We need to set up a few dependencies to work with the hacker news API: "
54+
}
55+
56+
p {
57+
"First, let's create a new project for our hacker news app. We can use the CLI to create a new
58+
project. You can select a platform of your choice or view the getting started guide for more information
59+
on each option. If you aren't sure what platform to try out, we recommend getting started with web or
60+
desktop:"
61+
}
62+
63+
p {
64+
"The template contains some boilerplate to help you get started. For this guide, we will be rebuilding some of the code
65+
from scratch for learning purposes. You can clear the src/main.rs file. We will be adding new code in the next
66+
sections."
67+
}
68+
69+
p {
70+
"Next, let's setup our dependencies. We need to set up a few dependencies to work with the hacker news API: "
71+
}
72+
73+
h2 {
74+
class: animated_classes().join(" "),
75+
onvisible: move |evt| {
76+
let data = evt.data();
77+
if let Ok(is_intersecting) = data.is_intersecting() {
78+
animated_classes.write()[1] = if is_intersecting { "visible" } else { "" };
79+
}
80+
},
81+
82+
"Animated Text"
83+
}
84+
}
85+
}
86+
}

packages/core-types/src/bubbles.rs

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub fn event_bubbles(evt: &str) -> bool {
9595
"transitionend" => true,
9696
"toggle" => true,
9797
"mounted" => false,
98+
"visible" => false,
9899
_ => true,
99100
}
100101
}

packages/desktop/src/events.rs

+8
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ impl HtmlEventConverter for SerializedHtmlEventConverter {
161161
.into()
162162
}
163163

164+
fn convert_visible_data(&self, event: &PlatformEventData) -> VisibleData {
165+
event
166+
.downcast::<SerializedVisibleData>()
167+
.cloned()
168+
.unwrap()
169+
.into()
170+
}
171+
164172
fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData {
165173
event
166174
.downcast::<SerializedWheelData>()

packages/html/src/events/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ pub trait HtmlEventConverter: Send + Sync {
146146
fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData;
147147
/// Convert a general event to a transition data event
148148
fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData;
149+
/// Convert a general event to a visible data event
150+
fn convert_visible_data(&self, event: &PlatformEventData) -> VisibleData;
149151
/// Convert a general event to a wheel data event
150152
fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData;
151153
}
@@ -258,6 +260,12 @@ impl From<&PlatformEventData> for TransitionData {
258260
}
259261
}
260262

263+
impl From<&PlatformEventData> for VisibleData {
264+
fn from(val: &PlatformEventData) -> Self {
265+
with_event_converter(|c| c.convert_visible_data(val))
266+
}
267+
}
268+
261269
impl From<&PlatformEventData> for WheelData {
262270
fn from(val: &PlatformEventData) -> Self {
263271
with_event_converter(|c| c.convert_wheel_data(val))
@@ -282,6 +290,7 @@ mod selection;
282290
mod toggle;
283291
mod touch;
284292
mod transition;
293+
mod visible;
285294
mod wheel;
286295

287296
pub use animation::*;
@@ -302,4 +311,5 @@ pub use selection::*;
302311
pub use toggle::*;
303312
pub use touch::*;
304313
pub use transition::*;
314+
pub use visible::*;
305315
pub use wheel::*;

0 commit comments

Comments
 (0)