dioxus_html/events/
mounted.rs

1//! Handles querying data from the renderer
2
3use std::{
4    fmt::{Debug, Display, Formatter},
5    future::Future,
6    pin::Pin,
7};
8
9/// An Element that has been rendered and allows reading and modifying information about it.
10///
11/// 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.
12// we can not use async_trait here because it does not create a trait that is object safe
13pub trait RenderedElementBacking: std::any::Any {
14    /// return self as Any
15    fn as_any(&self) -> &dyn std::any::Any;
16
17    /// Get the number of pixels that an element's content is scrolled
18    fn get_scroll_offset(&self) -> Pin<Box<dyn Future<Output = MountedResult<PixelsVector2D>>>> {
19        Box::pin(async { Err(MountedError::NotSupported) })
20    }
21
22    /// Get the size of an element's content, including content not visible on the screen due to overflow
23    #[allow(clippy::type_complexity)]
24    fn get_scroll_size(&self) -> Pin<Box<dyn Future<Output = MountedResult<PixelsSize>>>> {
25        Box::pin(async { Err(MountedError::NotSupported) })
26    }
27
28    /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
29    #[allow(clippy::type_complexity)]
30    fn get_client_rect(&self) -> Pin<Box<dyn Future<Output = MountedResult<PixelsRect>>>> {
31        Box::pin(async { Err(MountedError::NotSupported) })
32    }
33
34    /// Scroll to make the element visible
35    fn scroll_to(
36        &self,
37        _options: ScrollToOptions,
38    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
39        Box::pin(async { Err(MountedError::NotSupported) })
40    }
41
42    /// Scroll to the given element offsets
43    fn scroll(
44        &self,
45        _coordinates: PixelsVector2D,
46        _behavior: ScrollBehavior,
47    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
48        Box::pin(async { Err(MountedError::NotSupported) })
49    }
50
51    /// Set the focus on the element
52    fn set_focus(&self, _focus: bool) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
53        Box::pin(async { Err(MountedError::NotSupported) })
54    }
55}
56
57impl RenderedElementBacking for () {
58    fn as_any(&self) -> &dyn std::any::Any {
59        self
60    }
61}
62
63/// The way that scrolling should be performed
64#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
65#[doc(alias = "ScrollIntoViewOptions")]
66pub enum ScrollBehavior {
67    /// Scroll to the element immediately
68    #[cfg_attr(feature = "serialize", serde(rename = "instant"))]
69    Instant,
70    /// Scroll to the element smoothly
71    #[cfg_attr(feature = "serialize", serde(rename = "smooth"))]
72    Smooth,
73}
74
75/// The desired final position within the scrollable ancestor container for a given axis.
76#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
77#[doc(alias = "ScrollIntoViewOptions")]
78pub enum ScrollLogicalPosition {
79    /// Aligns the element's start edge (top or left) with the start of the scrollable container,
80    /// making the element appear at the start of the visible area.
81    #[cfg_attr(feature = "serialize", serde(rename = "start"))]
82    Start,
83    /// Aligns the element at the center of the scrollable container,
84    /// positioning it in the middle of the visible area.
85    #[cfg_attr(feature = "serialize", serde(rename = "center"))]
86    Center,
87    /// Aligns the element's end edge (bottom or right) with the end of the scrollable container,
88    /// making the element appear at the end of the visible area
89    #[cfg_attr(feature = "serialize", serde(rename = "end"))]
90    End,
91    /// Scrolls the element to the nearest edge in the given axis.
92    /// This minimizes the scrolling distance.
93    #[cfg_attr(feature = "serialize", serde(rename = "nearest"))]
94    Nearest,
95}
96
97/// The way that scrolling should be performed
98#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
99#[doc(alias = "ScrollIntoViewOptions")]
100pub struct ScrollToOptions {
101    pub behavior: ScrollBehavior,
102    pub vertical: ScrollLogicalPosition,
103    pub horizontal: ScrollLogicalPosition,
104}
105impl Default for ScrollToOptions {
106    fn default() -> Self {
107        Self {
108            behavior: ScrollBehavior::Smooth,
109            vertical: ScrollLogicalPosition::Start,
110            horizontal: ScrollLogicalPosition::Center,
111        }
112    }
113}
114
115/// An Element that has been rendered and allows reading and modifying information about it.
116///
117/// 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.
118pub struct MountedData {
119    inner: Box<dyn RenderedElementBacking>,
120}
121
122impl Debug for MountedData {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        f.debug_struct("MountedData").finish()
125    }
126}
127
128impl<E: RenderedElementBacking> From<E> for MountedData {
129    fn from(e: E) -> Self {
130        Self { inner: Box::new(e) }
131    }
132}
133
134impl MountedData {
135    /// Create a new MountedData
136    pub fn new(registry: impl RenderedElementBacking + 'static) -> Self {
137        Self {
138            inner: Box::new(registry),
139        }
140    }
141
142    /// Get the number of pixels that an element's content is scrolled
143    #[doc(alias = "scrollTop")]
144    #[doc(alias = "scrollLeft")]
145    pub async fn get_scroll_offset(&self) -> MountedResult<PixelsVector2D> {
146        self.inner.get_scroll_offset().await
147    }
148
149    /// Get the size of an element's content, including content not visible on the screen due to overflow
150    #[doc(alias = "scrollWidth")]
151    #[doc(alias = "scrollHeight")]
152    pub async fn get_scroll_size(&self) -> MountedResult<PixelsSize> {
153        self.inner.get_scroll_size().await
154    }
155
156    /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
157    #[doc(alias = "getBoundingClientRect")]
158    pub async fn get_client_rect(&self) -> MountedResult<PixelsRect> {
159        self.inner.get_client_rect().await
160    }
161
162    /// Scroll to make the element visible
163    #[doc(alias = "scrollIntoView")]
164    pub fn scroll_to(
165        &self,
166        behavior: ScrollBehavior,
167    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
168        self.inner.scroll_to(ScrollToOptions {
169            behavior,
170            ..ScrollToOptions::default()
171        })
172    }
173
174    /// Scroll to make the element visible
175    #[doc(alias = "scrollIntoView")]
176    pub fn scroll_to_with_options(
177        &self,
178        options: ScrollToOptions,
179    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
180        self.inner.scroll_to(options)
181    }
182
183    /// Scroll to the given element offsets
184    #[doc(alias = "scrollTo")]
185    pub fn scroll(
186        &self,
187        coordinates: PixelsVector2D,
188        behavior: ScrollBehavior,
189    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
190        self.inner.scroll(coordinates, behavior)
191    }
192
193    /// Set the focus on the element
194    #[doc(alias = "focus")]
195    #[doc(alias = "blur")]
196    pub fn set_focus(&self, focus: bool) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
197        self.inner.set_focus(focus)
198    }
199
200    /// Downcast this event to a concrete event type
201    #[inline(always)]
202    pub fn downcast<T: 'static>(&self) -> Option<&T> {
203        self.inner.as_any().downcast_ref::<T>()
204    }
205}
206
207use dioxus_core::Event;
208
209use crate::geometry::{PixelsRect, PixelsSize, PixelsVector2D};
210
211pub type MountedEvent = Event<MountedData>;
212
213impl_event! [
214    MountedData;
215
216    #[doc(alias = "ref")]
217    #[doc(alias = "createRef")]
218    #[doc(alias = "useRef")]
219    /// The onmounted event is fired when the element is first added to the DOM. This event gives you a [`MountedData`] object and lets you interact with the raw DOM element.
220    ///
221    /// This event is fired once per element. If you need to access the element multiple times, you can store the [`MountedData`] object in a [`use_signal`](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_signal.html) hook and use it as needed.
222    ///
223    /// # Examples
224    ///
225    /// ```rust, no_run
226    /// # use dioxus::prelude::*;
227    /// fn App() -> Element {
228    ///     let mut header_element = use_signal(|| None);
229    ///
230    ///     rsx! {
231    ///         div {
232    ///             h1 {
233    ///                 // The onmounted event will run the first time the h1 element is mounted
234    ///                 onmounted: move |element| header_element.set(Some(element.data())),
235    ///                 "Scroll to top example"
236    ///             }
237    ///
238    ///             for i in 0..100 {
239    ///                 div { "Item {i}" }
240    ///             }
241    ///
242    ///             button {
243    ///                 // When you click the button, if the header element has been mounted, we scroll to that element
244    ///                 onclick: move |_| async move {
245    ///                     if let Some(header) = header_element.cloned() {
246    ///                         let _ = header.scroll_to(ScrollBehavior::Smooth).await;
247    ///                     }
248    ///                 },
249    ///                 "Scroll to top"
250    ///             }
251    ///         }
252    ///     }
253    /// }
254    /// ```
255    ///
256    /// The `MountedData` struct contains cross platform APIs that work on the desktop, mobile, liveview and web platforms. For the web platform, you can also downcast the `MountedData` event to the `web-sys::Element` type for more web specific APIs:
257    ///
258    /// ```rust, ignore
259    /// use dioxus::prelude::*;
260    /// use dioxus_web::WebEventExt; // provides [`as_web_event()`] method
261    ///
262    /// fn App() -> Element {
263    ///     rsx! {
264    ///         div {
265    ///             id: "some-id",
266    ///             onmounted: move |element| {
267    ///                 // You can use the web_event trait to downcast the element to a web specific event. For the mounted event, this will be a web_sys::Element
268    ///                 let web_sys_element = element.as_web_event();
269    ///                 assert_eq!(web_sys_element.id(), "some-id");
270    ///             }
271    ///         }
272    ///     }
273    /// }
274    /// ```
275    onmounted
276];
277
278/// The MountedResult type for the MountedData
279pub type MountedResult<T> = Result<T, MountedError>;
280
281#[derive(Debug)]
282/// The error type for the MountedData
283#[non_exhaustive]
284pub enum MountedError {
285    /// The renderer does not support the requested operation
286    NotSupported,
287    /// The element was not found
288    OperationFailed(Box<dyn std::error::Error>),
289}
290
291impl Display for MountedError {
292    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
293        match self {
294            MountedError::NotSupported => {
295                write!(f, "The renderer does not support the requested operation")
296            }
297            MountedError::OperationFailed(e) => {
298                write!(f, "The operation failed: {}", e)
299            }
300        }
301    }
302}
303
304impl std::error::Error for MountedError {}