Skip to main content

leptos_use_intersection_observer/
options.rs

1use crate::IntoElementMaybeSignal;
2use default_struct_builder::DefaultBuilder;
3use std::marker::PhantomData;
4
5/// Options for [`use_intersection_observer_with_options`](crate::use_intersection_observer_with_options).
6#[derive(DefaultBuilder)]
7pub struct UseIntersectionObserverOptions<El, M>
8where
9    El: IntoElementMaybeSignal<web_sys::Element, M>,
10{
11    /// If `true`, the `IntersectionObserver` will be attached immediately. Otherwise it
12    /// will only be attached after the returned `resume` closure is called. That is
13    /// `use_intersections_observer` will be started "paused".
14    pub(crate) immediate: bool,
15
16    /// A `web_sys::Element` or `web_sys::Document` object which is an ancestor of the intended `target`,
17    /// whose bounding rectangle will be considered the viewport.
18    /// Any part of the target not visible in the visible area of the `root` is not considered visible.
19    /// Defaults to `None` (which means the root `document` will be used).
20    /// Please note that setting this to a `Some(document)` may not be supported by all browsers.
21    /// See [Browser Compatibility](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#browser_compatibility)
22    pub(crate) root: Option<El>,
23
24    /// A string which specifies a set of offsets to add to the root's [bounding box](https://developer.mozilla.org/en-US/docs/Glossary/Bounding_box)
25    /// when calculating intersections, effectively shrinking or growing the root for calculation purposes. The syntax is approximately the same as that for the CSS
26    /// [`margin`](https://developer.mozilla.org/en-US/docs/Web/CSS/margin) property; see
27    /// [The intersection root and root margin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#the_intersection_root_and_root_margin)
28    /// for more information on how the margin works and the syntax. The default is `"0px"`.
29    #[builder(into)]
30    pub(crate) root_margin: String,
31
32    // TODO : validate that each number is between 0 and 1 ?
33    /// A `Vec` of numbers between 0.0 and 1.0, specifying a ratio of intersection area to total
34    /// bounding box area for the observed target. A value of 0.0 means that even a single
35    /// visible pixel counts as the target being visible. 1.0 means that the entire target
36    /// element is visible. See [Thresholds](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#thresholds)
37    /// for a more in-depth description of how thresholds are used.
38    /// The default is a single threshold of `[0.0]`.
39    pub(crate) thresholds: Vec<f64>,
40
41    #[builder(skip)]
42    _marker: PhantomData<M>,
43}
44
45impl<M> Default for UseIntersectionObserverOptions<web_sys::Element, M>
46where
47    web_sys::Element: IntoElementMaybeSignal<web_sys::Element, M>,
48{
49    fn default() -> Self {
50        Self {
51            immediate: true,
52            root: None,
53            root_margin: "0px".into(),
54            thresholds: vec![0.0],
55            _marker: PhantomData,
56        }
57    }
58}