leptos-use/examples/use_intersection_observer/src/main.rs

102 lines
2.6 KiB
Rust
Raw Normal View History

2023-07-03 16:10:58 +01:00
use leptos::html::Div;
2023-06-10 23:59:32 +01:00
use leptos::*;
use leptos_use::docs::{demo_or_body, BooleanDisplay};
use leptos_use::{
use_intersection_observer_with_options, UseIntersectionObserverOptions,
UseIntersectionObserverReturn,
};
#[component]
2023-07-27 18:06:36 +01:00
fn Demo() -> impl IntoView {
let root = create_node_ref::<Div>();
let target = create_node_ref::<Div>();
let (is_visible, set_visible) = create_signal(false);
2023-06-10 23:59:32 +01:00
let UseIntersectionObserverReturn {
is_active,
pause,
resume,
..
} = use_intersection_observer_with_options(
target,
move |entries, _| {
set_visible.set(entries[0].is_intersecting());
2023-06-10 23:59:32 +01:00
},
UseIntersectionObserverOptions::default().root(Some(root)),
);
2023-07-27 19:48:21 +01:00
view! {
<div class="text-center">
2023-06-10 23:59:32 +01:00
<label class="checkbox">
<input
type="checkbox"
prop:checked=move || is_active.get()
2023-06-10 23:59:32 +01:00
name="enabled"
on:input=move |e| {
if event_target_checked(&e) {
resume();
} else {
pause();
}
}
/>
2023-07-27 19:48:21 +01:00
2023-06-10 23:59:32 +01:00
<span>"Enabled"</span>
</label>
</div>
<div node_ref=root class="root">
<p class="notice">"Scroll me down!"</p>
<div node_ref=target class="target">
<p>"Hello world!"</p>
</div>
</div>
<div class="text-center">
"Element "
2023-07-27 19:48:21 +01:00
<BooleanDisplay
value=is_visible
true_str="inside"
false_str="outside"
class="font-bold"
/> " the viewport"
2023-06-10 23:59:32 +01:00
</div>
2023-07-27 19:48:21 +01:00
<style>
"
.root {
border: 2px dashed #ccc;
height: 200px;
margin: 2rem 1rem;
overflow-y: scroll;
}
.notice {
text-align: center;
padding: 3em 0;
margin-bottom: 300px;
font-style: italic;
font-size: 1.8rem;
opacity: 0.8;
}
.target {
border: 2px dashed var(--brand-color);
padding: 10px;
max-height: 150px;
margin: 0 2rem 400px;
}
"
</style>
2023-06-10 23:59:32 +01:00
}
}
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
2023-07-27 18:06:36 +01:00
mount_to(demo_or_body(), || {
2023-07-27 19:48:21 +01:00
view! { <Demo/> }
2023-06-10 23:59:32 +01:00
})
}