-
Notifications
You must be signed in to change notification settings - Fork 16
Scroll #75
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
Merged
Merged
Scroll #75
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "use_scroll" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
dioxus-sdk = { workspace = true, features = ["scroll"] } | ||
dioxus = { workspace = true } | ||
|
||
[features] | ||
web = ["dioxus/web"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# use_window_size | ||
|
||
Learn how to use `use_root_scroll`. | ||
|
||
|
||
### Run | ||
|
||
**Web** | ||
```dioxus serve --platform web``` |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use dioxus::{ | ||
logger::tracing::{info, Level}, | ||
prelude::*, | ||
}; | ||
use dioxus_sdk::utils::scroll::use_root_scroll; | ||
|
||
fn main() { | ||
dioxus::logger::init(Level::TRACE).unwrap(); | ||
launch(App); | ||
} | ||
|
||
#[component] | ||
fn App() -> Element { | ||
let random_text = "This is some random repeating text. ".repeat(1000); | ||
|
||
let scroll_metrics = use_root_scroll(); | ||
use_effect(move || { | ||
let scroll_metrics = scroll_metrics(); | ||
let distance_from_bottom = scroll_metrics.scroll_height | ||
- (scroll_metrics.scroll_top + scroll_metrics.client_height); | ||
info!("Distance from bottom: {}", distance_from_bottom); | ||
let scroll_percentage = (scroll_metrics.scroll_top + scroll_metrics.client_height) | ||
/ scroll_metrics.scroll_height; | ||
info!("Scroll percentage: {}", scroll_percentage); | ||
}); | ||
|
||
rsx! { | ||
div { style: "text-align: center; padding: 20px; font-family: sans-serif;", | ||
h1 { "Random Text" } | ||
div { style: "margin: 20px; padding: 15px; border: 1px solid #ccc; border-radius: 5px;", | ||
p { "{random_text}" } | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use dioxus::prelude::*; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
/// Scroll metrics. | ||
#[derive(serde::Deserialize, Clone, Debug)] | ||
pub struct ScrollMetrics { | ||
/// Current scroll position from top: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop | ||
pub scroll_top: f64, | ||
/// Current scroll position from left: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft | ||
pub scroll_left: f64, | ||
|
||
/// Viewport height: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight | ||
pub client_height: f64, | ||
/// Viewport width: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth | ||
pub client_width: f64, | ||
|
||
/// Content height: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight | ||
pub scroll_height: f64, | ||
/// Content width: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth | ||
pub scroll_width: f64, | ||
} | ||
|
||
// Static counter to generate unique IDs for each scroll tracker instance | ||
static SCROLL_TRACKER_COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
||
/// Creates a signal that tracks root scrolling. | ||
/// ```rust | ||
/// use dioxus::{logger::tracing::{info, Level}, prelude::*}; | ||
/// use dioxus_sdk::utils::scroll::use_root_scroll; | ||
/// | ||
/// fn main() { | ||
/// dioxus::logger::init(Level::TRACE).unwrap(); | ||
/// launch(App); | ||
/// } | ||
/// | ||
/// #[component] | ||
/// fn App() -> Element { | ||
/// let random_text = "This is some random repeating text. ".repeat(1000); | ||
/// | ||
/// let scroll_metrics = use_root_scroll(); | ||
/// use_effect(move || { | ||
/// let scroll_metrics = scroll_metrics(); | ||
/// let distance_from_bottom = scroll_metrics.scroll_height - (scroll_metrics.scroll_top + scroll_metrics.client_height); | ||
/// info!("Distance from bottom: {}", distance_from_bottom); | ||
/// let scroll_percentage = (scroll_metrics.scroll_top + scroll_metrics.client_height) / scroll_metrics.scroll_height; | ||
/// info!("Scroll percentage: {}", scroll_percentage); | ||
/// }); | ||
/// | ||
/// rsx! { | ||
/// p { "{random_text}" } | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn use_root_scroll() -> Signal<ScrollMetrics> { | ||
let callback_name = use_hook(|| { | ||
let instance_id = SCROLL_TRACKER_COUNTER.fetch_add(1, Ordering::SeqCst); | ||
format!("scrollCallback_{}", instance_id) | ||
}); | ||
|
||
let mut scroll_metrics = use_signal(|| ScrollMetrics { | ||
scroll_top: 0.0, | ||
scroll_left: 0.0, | ||
client_height: 0.0, | ||
client_width: 0.0, | ||
scroll_height: 0.0, | ||
scroll_width: 0.0, | ||
}); | ||
|
||
let future_callback_name = callback_name.clone(); | ||
use_future(move || { | ||
let future_callback_name = future_callback_name.clone(); | ||
async move { | ||
let callback_name = future_callback_name; | ||
let js_code = format!( | ||
r#" | ||
function {callback_name}() {{ | ||
const doc = document.documentElement; | ||
const scrollTop = window.scrollY || doc.scrollTop; | ||
const scrollLeft = window.scrollX || doc.scrollLeft; | ||
const viewportHeight = window.innerHeight; | ||
const viewportWidth = window.innerWidth; | ||
const contentHeight = doc.scrollHeight; | ||
const contentWidth = doc.scrollWidth; | ||
|
||
dioxus.send({{ | ||
scroll_top: scrollTop, | ||
scroll_left: scrollLeft, | ||
client_height: viewportHeight, | ||
client_width: viewportWidth, | ||
scroll_height: contentHeight, | ||
scroll_width: contentWidth, | ||
}}); | ||
}} | ||
|
||
{callback_name}(); | ||
|
||
window['{callback_name}'] = {callback_name}; | ||
window.addEventListener('scroll', window['{callback_name}']); | ||
window.addEventListener('resize', window['{callback_name}']); | ||
"#, | ||
); | ||
|
||
let mut eval = document::eval(&js_code); | ||
|
||
loop { | ||
match eval.recv::<ScrollMetrics>().await { | ||
Ok(metrics) => { | ||
dioxus::logger::tracing::trace!("Got scroll metrics {:?}", metrics); | ||
scroll_metrics.set(metrics); | ||
} | ||
Err(error) => dioxus::logger::tracing::error!( | ||
"Error receiving scroll metrics: {:?}", | ||
error | ||
), | ||
} | ||
} | ||
} | ||
}); | ||
|
||
use_drop(move || { | ||
let cleanup_code = format!( | ||
r#" | ||
window.removeEventListener('scroll', window['{callback_name}']); | ||
window.removeEventListener('resize', window['{callback_name}']); | ||
delete window['{callback_name}']; | ||
"#, | ||
); | ||
let _ = document::eval(&cleanup_code); | ||
}); | ||
|
||
scroll_metrics | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion to simplify this part:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. PR seems to be stuck in "processing updates" for the past 10 min though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10 hours later and still processing.. weird, had to amend and force push the same commit, but it is showing now.