floating_ui/floating_ui/dom/
middleware.rs1use wasm_bindgen::prelude::*;
2
3use crate::{core::Middleware, Result};
4
5#[wasm_bindgen(js_namespace= ["window", "FloatingUIDOM"])]
6extern "C" {
7 #[wasm_bindgen(js_name = offset)]
8 fn js_offset(options: JsValue) -> JsValue;
9
10}
11
12pub fn offset(options: OffsetOptions) -> Result<Middleware> {
13 let js = js_offset(options.into());
14 Ok(js.try_into()?)
15}
16
17pub enum OffsetOptions {
18 Number(f64),
19 Object {
20 main_axis: Option<f64>,
21 cross_axis: Option<f64>,
22 alignment_axis: Option<f64>,
23 },
24}
25
26impl Into<JsValue> for OffsetOptions {
27 fn into(self) -> JsValue {
28 match self {
29 OffsetOptions::Number(value) => JsValue::from_f64(value),
30 OffsetOptions::Object {
31 main_axis,
32 cross_axis,
33 alignment_axis,
34 } => {
35 let obj = js_sys::Object::new();
36 if let Some(value) = main_axis {
37 js_sys::Reflect::set(
38 &obj,
39 &JsValue::from_str("mainAxis"),
40 &JsValue::from_f64(value),
41 )
42 .unwrap();
43 }
44 if let Some(value) = cross_axis {
45 js_sys::Reflect::set(
46 &obj,
47 &JsValue::from_str("crossAxis"),
48 &JsValue::from_f64(value),
49 )
50 .unwrap();
51 }
52 if let Some(value) = alignment_axis {
53 js_sys::Reflect::set(
54 &obj,
55 &JsValue::from_str("alignmentAxis"),
56 &JsValue::from_f64(value),
57 )
58 .unwrap();
59 }
60 obj.into()
61 }
62 }
63 }
64}