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#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
65#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
66#[doc(alias = "ScrollIntoViewOptions")]
67pub enum ScrollBehavior {
68    /// Scroll to the element immediately
69    #[cfg_attr(feature = "serialize", serde(rename = "instant"))]
70    Instant,
71
72    /// Scroll to the element smoothly
73    #[default]
74    #[cfg_attr(feature = "serialize", serde(rename = "smooth"))]
75    Smooth,
76}
77
78/// The desired final position within the scrollable ancestor container for a given axis.
79#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
80#[doc(alias = "ScrollIntoViewOptions")]
81pub enum ScrollLogicalPosition {
82    /// Aligns the element's start edge (top or left) with the start of the scrollable container,
83    /// making the element appear at the start of the visible area.
84    #[cfg_attr(feature = "serialize", serde(rename = "start"))]
85    Start,
86    /// Aligns the element at the center of the scrollable container,
87    /// positioning it in the middle of the visible area.
88    #[cfg_attr(feature = "serialize", serde(rename = "center"))]
89    Center,
90    /// Aligns the element's end edge (bottom or right) with the end of the scrollable container,
91    /// making the element appear at the end of the visible area
92    #[cfg_attr(feature = "serialize", serde(rename = "end"))]
93    End,
94    /// Scrolls the element to the nearest edge in the given axis.
95    /// This minimizes the scrolling distance.
96    #[cfg_attr(feature = "serialize", serde(rename = "nearest"))]
97    Nearest,
98}
99
100/// The way that scrolling should be performed
101#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
102#[doc(alias = "ScrollIntoViewOptions")]
103pub struct ScrollToOptions {
104    pub behavior: ScrollBehavior,
105    pub vertical: ScrollLogicalPosition,
106    pub horizontal: ScrollLogicalPosition,
107}
108impl Default for ScrollToOptions {
109    fn default() -> Self {
110        Self {
111            behavior: ScrollBehavior::Smooth,
112            vertical: ScrollLogicalPosition::Start,
113            horizontal: ScrollLogicalPosition::Center,
114        }
115    }
116}
117
118/// An Element that has been rendered and allows reading and modifying information about it.
119///
120/// 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.
121pub struct MountedData {
122    inner: Box<dyn RenderedElementBacking>,
123}
124
125impl Debug for MountedData {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        f.debug_struct("MountedData").finish()
128    }
129}
130
131impl<E: RenderedElementBacking> From<E> for MountedData {
132    fn from(e: E) -> Self {
133        Self { inner: Box::new(e) }
134    }
135}
136
137impl MountedData {
138    /// Create a new MountedData
139    pub fn new(registry: impl RenderedElementBacking + 'static) -> Self {
140        Self {
141            inner: Box::new(registry),
142        }
143    }
144
145    /// Get the number of pixels that an element's content is scrolled
146    #[doc(alias = "scrollTop")]
147    #[doc(alias = "scrollLeft")]
148    pub async fn get_scroll_offset(&self) -> MountedResult<PixelsVector2D> {
149        self.inner.get_scroll_offset().await
150    }
151
152    /// Get the size of an element's content, including content not visible on the screen due to overflow
153    #[doc(alias = "scrollWidth")]
154    #[doc(alias = "scrollHeight")]
155    pub async fn get_scroll_size(&self) -> MountedResult<PixelsSize> {
156        self.inner.get_scroll_size().await
157    }
158
159    /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position)
160    #[doc(alias = "getBoundingClientRect")]
161    pub async fn get_client_rect(&self) -> MountedResult<PixelsRect> {
162        self.inner.get_client_rect().await
163    }
164
165    /// Scroll to make the element visible
166    #[doc(alias = "scrollIntoView")]
167    pub fn scroll_to(
168        &self,
169        behavior: ScrollBehavior,
170    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
171        self.inner.scroll_to(ScrollToOptions {
172            behavior,
173            ..ScrollToOptions::default()
174        })
175    }
176
177    /// Scroll to make the element visible
178    #[doc(alias = "scrollIntoView")]
179    pub fn scroll_to_with_options(
180        &self,
181        options: ScrollToOptions,
182    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
183        self.inner.scroll_to(options)
184    }
185
186    /// Scroll to the given element offsets
187    #[doc(alias = "scrollTo")]
188    pub fn scroll(
189        &self,
190        coordinates: PixelsVector2D,
191        behavior: ScrollBehavior,
192    ) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
193        self.inner.scroll(coordinates, behavior)
194    }
195
196    /// Set the focus on the element
197    #[doc(alias = "focus")]
198    #[doc(alias = "blur")]
199    pub fn set_focus(&self, focus: bool) -> Pin<Box<dyn Future<Output = MountedResult<()>>>> {
200        self.inner.set_focus(focus)
201    }
202
203    /// Downcast this event to a concrete event type
204    #[inline(always)]
205    pub fn downcast<T: 'static>(&self) -> Option<&T> {
206        self.inner.as_any().downcast_ref::<T>()
207    }
208}
209
210use dioxus_core::Event;
211
212use crate::geometry::{PixelsRect, PixelsSize, PixelsVector2D};
213
214pub type MountedEvent = Event<MountedData>;
215
216impl_event! [
217    MountedData;
218
219    #[doc(alias = "ref")]
220    #[doc(alias = "createRef")]
221    #[doc(alias = "useRef")]
222    /// 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.
223    ///
224    /// 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.
225    ///
226    /// # Examples
227    ///
228    /// ```rust, no_run
229    /// # use dioxus::prelude::*;
230    /// fn App() -> Element {
231    ///     let mut header_element = use_signal(|| None);
232    ///
233    ///     rsx! {
234    ///         div {
235    ///             h1 {
236    ///                 // The onmounted event will run the first time the h1 element is mounted
237    ///                 onmounted: move |element| header_element.set(Some(element.data())),
238    ///                 "Scroll to top example"
239    ///             }
240    ///
241    ///             for i in 0..100 {
242    ///                 div { "Item {i}" }
243    ///             }
244    ///
245    ///             button {
246    ///                 // When you click the button, if the header element has been mounted, we scroll to that element
247    ///                 onclick: move |_| async move {
248    ///                     if let Some(header) = header_element.cloned() {
249    ///                         let _ = header.scroll_to(ScrollBehavior::Smooth).await;
250    ///                     }
251    ///                 },
252    ///                 "Scroll to top"
253    ///             }
254    ///         }
255    ///     }
256    /// }
257    /// ```
258    ///
259    /// 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:
260    ///
261    /// ```rust, ignore
262    /// use dioxus::prelude::*;
263    /// use dioxus_web::WebEventExt; // provides [`as_web_event()`] method
264    ///
265    /// fn App() -> Element {
266    ///     rsx! {
267    ///         div {
268    ///             id: "some-id",
269    ///             onmounted: move |element| {
270    ///                 // 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
271    ///                 let web_sys_element = element.as_web_event();
272    ///                 assert_eq!(web_sys_element.id(), "some-id");
273    ///             }
274    ///         }
275    ///     }
276    /// }
277    /// ```
278    onmounted
279];
280
281pub use onmounted as onmount;
282
283/// The MountedResult type for the MountedData
284pub type MountedResult<T> = Result<T, MountedError>;
285
286#[derive(Debug)]
287/// The error type for the MountedData
288#[non_exhaustive]
289pub enum MountedError {
290    /// The renderer does not support the requested operation
291    NotSupported,
292    /// The element was not found
293    OperationFailed(Box<dyn std::error::Error>),
294}
295
296impl Display for MountedError {
297    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
298        match self {
299            MountedError::NotSupported => {
300                write!(f, "The renderer does not support the requested operation")
301            }
302            MountedError::OperationFailed(e) => {
303                write!(f, "The operation failed: {}", e)
304            }
305        }
306    }
307}
308
309impl std::error::Error for MountedError {}