Support scrollIntoView alignment options (#3952)

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Otger RoglaandJonathan Kelley authored and GitHub committed 2025-05-07 13:09:17 -07:00
1 parent 85b373deff
commit 28d38307a2
7 files changed
+79 -19

No files matched your search

+54 -2
View File
@@ -34,7 +34,7 @@ pub trait RenderedElementBacking: std::any::Any {
/// Scroll to make the element visible
fn scroll_to(
&self,
_behavior: ScrollBehavior,
_options: ScrollToOptions,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
Box::pin(async { Err(MountedError::NotSupported) })
}
@@ -72,6 +72,46 @@ pub enum ScrollBehavior {
Smooth,
}
/// The desired final position within the scrollable ancestor container for a given axis.
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[doc(alias = "ScrollIntoViewOptions")]
pub enum ScrollLogicalPosition {
/// Aligns the element's start edge (top or left) with the start of the scrollable container,
/// making the element appear at the start of the visible area.
#[cfg_attr(feature = "serialize", serde(rename = "start"))]
Start,
/// Aligns the element at the center of the scrollable container,
/// positioning it in the middle of the visible area.
#[cfg_attr(feature = "serialize", serde(rename = "center"))]
Center,
/// Aligns the element's end edge (bottom or right) with the end of the scrollable container,
/// making the element appear at the end of the visible area
#[cfg_attr(feature = "serialize", serde(rename = "end"))]
End,
/// Scrolls the element to the nearest edge in the given axis.
/// This minimizes the scrolling distance.
#[cfg_attr(feature = "serialize", serde(rename = "nearest"))]
Nearest,
}
/// The way that scrolling should be performed
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[doc(alias = "ScrollIntoViewOptions")]
pub struct ScrollToOptions {
pub behavior: ScrollBehavior,
pub vertical: ScrollLogicalPosition,
pub horizontal: ScrollLogicalPosition,
}
impl Default for ScrollToOptions {
fn default() -> Self {
Self {
behavior: ScrollBehavior::Smooth,
vertical: ScrollLogicalPosition::Start,
horizontal: ScrollLogicalPosition::Center,
}
}
}
/// An Element that has been rendered and allows reading and modifying information about it.
///
/// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries.
@@ -125,7 +165,19 @@ impl MountedData {
&self,
behavior: ScrollBehavior,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
self.inner.scroll_to(behavior)
self.inner.scroll_to(ScrollToOptions {
behavior,
..ScrollToOptions::default()
})
}
/// Scroll to make the element visible
#[doc(alias = "scrollIntoView")]
pub fn scroll_to_with_options(
&self,
options: ScrollToOptions,
) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
self.inner.scroll_to(options)
}
/// Scroll to the given element offsets