floating_ui_dom/
lib.rs

1//! Rust port of [Floating UI](https://floating-ui.com/).
2//!
3//! This is the library to use Floating UI on the web, wrapping [`floating_ui_core`] with DOM interface logic.
4//!
5//! See [the Rust Floating UI book](https://floating-ui.rustforweb.org/) for more documenation.
6//!
7//! See [@floating-ui/dom](https://www.npmjs.com/package/@floating-ui/dom) for the original package.
8
9mod auto_update;
10mod middleware;
11mod platform;
12mod types;
13mod utils;
14
15pub use self::platform::Platform;
16pub use crate::auto_update::*;
17pub use crate::middleware::*;
18pub use crate::types::*;
19pub use floating_ui_core::{
20    Boundary, ComputePositionReturn, Derivable, DerivableFn, DetectOverflowOptions, ElementContext,
21    Middleware, MiddlewareData, MiddlewareReturn, MiddlewareState, MiddlewareWithOptions,
22    RootBoundary,
23};
24#[doc(no_inline)]
25pub use floating_ui_utils::{
26    AlignedPlacement, Alignment, Axis, ClientRectObject, Coords, Dimensions, ElementRects, Length,
27    Padding, PartialSideObject, Placement, Rect, Side, SideObject, Strategy, VirtualElement, dom,
28};
29
30use floating_ui_core::{
31    ComputePositionConfig as CoreComputePositionConfig, compute_position as compute_position_core,
32};
33use web_sys::Element;
34
35const PLATFORM: Platform = Platform {};
36
37/// Options for [`compute_position`].
38#[derive(Clone, Default)]
39pub struct ComputePositionConfig {
40    /// Where to place the floating element relative to the reference element.
41    ///
42    /// Defaults to [`Placement::Bottom`].
43    pub placement: Option<Placement>,
44
45    /// The strategy to use when positioning the floating element.
46    ///
47    /// Defaults to [`Strategy::Absolute`].
48    pub strategy: Option<Strategy>,
49
50    /// Vector of middleware objects to modify the positioning or provide data for rendering.
51    ///
52    /// Defaults to an empty vector.
53    pub middleware: Option<MiddlewareVec>,
54}
55
56impl ComputePositionConfig {
57    /// Set `placement` option.
58    pub fn placement(mut self, value: Placement) -> Self {
59        self.placement = Some(value);
60        self
61    }
62
63    /// Set `strategy` option.
64    pub fn strategy(mut self, value: Strategy) -> Self {
65        self.strategy = Some(value);
66        self
67    }
68
69    /// Set `middleware` option.
70    pub fn middleware(mut self, value: MiddlewareVec) -> Self {
71        self.middleware = Some(value);
72        self
73    }
74}
75
76/// Computes the `x` and `y` coordinates that will place the floating element next to a given reference element.
77pub fn compute_position(
78    reference: ElementOrVirtual,
79    floating: &Element,
80    config: ComputePositionConfig,
81) -> ComputePositionReturn {
82    // TODO: cache
83
84    compute_position_core(
85        reference,
86        floating,
87        CoreComputePositionConfig {
88            platform: &PLATFORM,
89            placement: config.placement,
90            strategy: config.strategy,
91            middleware: config.middleware,
92        },
93    )
94}