Skip to content
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

feat(html): add scroll method to MountedData #3722

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions examples/scroll_to_offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Scroll elements using their MountedData
//!
//! Dioxus exposes a few helpful APIs around elements (mimicking the DOM APIs) to allow you to interact with elements
//! across the renderers. This includes scrolling, reading dimensions, and more.
//!
//! In this example we demonstrate how to scroll to a given y offset of the scrollable parent using the `scroll` method on the `MountedData`

use dioxus::html::geometry::PixelsVector2D;
use dioxus::prelude::*;

fn main() {
dioxus::launch(app);
}

fn app() -> Element {
rsx! {
ScrollToCoordinates {}
ScrollToCoordinates {}
}
}

#[component]
fn ScrollToCoordinates() -> Element {
let mut element = use_signal(|| None);

rsx! {
div {
border: "1px solid black",
position: "relative",

div {
height: "300px",
overflow_y: "auto",

onmounted: move |event| element.set(Some(event.data())),

for i in 0..100 {
div { height: "20px", "Item {i}" }
}
}

div { position: "absolute", top: 0, right: 0,
input {
r#type: "number",
min: "0",
max: "99",
oninput: move |event| async move {
if let Some(ul) = element.cloned() {
let data = event.data();
if let Ok(value) = data.parsed::<f64>() {
_ = ul
.scroll(
PixelsVector2D::new(0.0, 20.0 * value),
ScrollBehavior::Smooth,
)
.await;
}
}
},
}
}
}
}
}
30 changes: 30 additions & 0 deletions packages/desktop/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ impl RenderedElementBacking for DesktopElement {
})
}

fn scroll(
&self,
coordinates: PixelsVector2D,
behavior: dioxus_html::ScrollBehavior,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = MountedResult<()>>>> {
let script = format!(
"return window.interpreter.scroll({}, {}, {}, {});",
self.id.0,
coordinates.x,
coordinates.y,
serde_json::to_string(&behavior).expect("Failed to serialize ScrollBehavior")
);
let webview = self
.webview
.upgrade()
.expect("Webview should be alive if the element is being queried");
let fut = self.query.new_query::<bool>(&script, webview).resolve();
Box::pin(async move {
match fut.await {
Ok(true) => Ok(()),
Ok(false) => MountedResult::Err(dioxus_html::MountedError::OperationFailed(
Box::new(DesktopQueryError::FailedToQuery),
)),
Err(err) => {
MountedResult::Err(dioxus_html::MountedError::OperationFailed(Box::new(err)))
}
}
})
}

fn set_focus(
&self,
focus: bool,
Expand Down
19 changes: 19 additions & 0 deletions packages/html/src/events/mounted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ pub trait RenderedElementBacking: std::any::Any {
Box::pin(async { Err(MountedError::NotSupported) })
}

/// Scroll to the given element offsets
fn scroll(
&self,
_coordinates: PixelsVector2D,
_behavior: ScrollBehavior,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
}

/// Set the focus on the element
fn set_focus(&self, _focus: bool) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
Expand Down Expand Up @@ -119,6 +128,16 @@ impl MountedData {
self.inner.scroll_to(behavior)
}

/// Scroll to the given element offsets
#[doc(alias = "scrollTo")]
pub fn scroll(
&self,
coordinates: PixelsVector2D,
behavior: ScrollBehavior,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
self.inner.scroll(coordinates, behavior)
}

/// Set the focus on the element
#[doc(alias = "focus")]
#[doc(alias = "blur")]
Expand Down
2 changes: 1 addition & 1 deletion packages/interpreter/src/js/common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/interpreter/src/js/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/interpreter/src/js/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 10130882040196587188, 5052021921702764563, 12925655762638175824, 5638004933879392817]
[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 9840496485188455822, 5052021921702764563, 12925655762638175824, 5638004933879392817]
Loading
Loading