Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Mar 27, 2024
1 parent 565b174 commit b34e0ca
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 109 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/compute_coords_from_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub fn compute_coords_from_placement(

let common_x = reference.x + reference.width / 2.0 - floating.width / 2.0;
let common_y = reference.y + reference.height / 2.0 - floating.height / 2.0;
let common_align =
reference.get_length(align_length) / 2.0 - floating.get_length(align_length) / 2.0;
let common_align = reference.length(align_length) / 2.0 - floating.length(align_length) / 2.0;

let mut coords = match side {
Side::Top => Coords {
Expand Down
32 changes: 14 additions & 18 deletions packages/core/src/middleware/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Arrow<'a
Axis::Y => Side::Bottom,
};

let start_diff = coords.get_axis(axis) - rects.reference.get_axis(axis);
let end_diff = rects.reference.get_length(length) + rects.reference.get_axis(axis)
- coords.get_axis(axis)
- rects.floating.get_length(length);
let start_diff = coords.axis(axis) - rects.reference.axis(axis);
let end_diff = rects.reference.length(length) + rects.reference.axis(axis)
- coords.axis(axis)
- rects.floating.length(length);

let arrow_offset_parent = platform.get_offset_parent(&options.element);
let client_size = arrow_offset_parent
Expand All @@ -114,38 +114,34 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Arrow<'a
platform.get_client_length(elements.floating, length)
}
})
.unwrap_or(rects.floating.get_length(length));
.unwrap_or(rects.floating.length(length));

let center_to_reference = end_diff / 2.0 - start_diff / 2.0;

// If the padding is large enough that it causes the arrow to no longer be centered, modify the padding so that it is centered.
let largest_possible_padding =
client_size / 2.0 - arrow_dimensions.get_length(length) / 2.0 - 1.0;
let min_padding = padding_object
.get_side(min_prop)
.min(largest_possible_padding);
let max_padding = padding_object
.get_side(max_prop)
.min(largest_possible_padding);
client_size / 2.0 - arrow_dimensions.length(length) / 2.0 - 1.0;
let min_padding = padding_object.side(min_prop).min(largest_possible_padding);
let max_padding = padding_object.side(max_prop).min(largest_possible_padding);

// Make sure the arrow doesn't overflow the floating element if the center point is outside the floating element's bounds.
let min = min_padding;
let max = client_size - arrow_dimensions.get_length(length) - max_padding;
let max = client_size - arrow_dimensions.length(length) - max_padding;
let center =
client_size / 2.0 - arrow_dimensions.get_length(length) / 2.0 + center_to_reference;
client_size / 2.0 - arrow_dimensions.length(length) / 2.0 + center_to_reference;
let offset = clamp(min, center, max);

// If the reference is small enough that the arrow's padding causes it to to point to nothing for an aligned placement, adjust the offset of the floating element itself.
// To ensure `shift()` continues to take action, a single reset is performed when this is true.
let should_add_offset = data.is_none()
&& get_alignment(placement).is_some()
&& center != offset
&& rects.reference.get_length(length) / 2.0
&& rects.reference.length(length) / 2.0
- (match center < min {
true => min_padding,
false => max_padding,
})
- arrow_dimensions.get_length(length) / 2.0
- arrow_dimensions.length(length) / 2.0
< 0.0;
let alignment_offset = match should_add_offset {
true => match center < min {
Expand All @@ -157,12 +153,12 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Arrow<'a

MiddlewareReturn {
x: match axis {
Axis::X => Some(coords.get_axis(axis) + alignment_offset),
Axis::X => Some(coords.axis(axis) + alignment_offset),
Axis::Y => None,
},
y: match axis {
Axis::X => None,
Axis::Y => Some(coords.get_axis(axis) + alignment_offset),
Axis::Y => Some(coords.axis(axis) + alignment_offset),
},
data: Some(
serde_json::to_value(ArrowData {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/middleware/auto_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window>
}

let current_overflows = vec![
overflow.get_side(get_side(current_placement)),
overflow.get_side(alignment_sides.0),
overflow.get_side(alignment_sides.1),
overflow.side(get_side(current_placement)),
overflow.side(alignment_sides.0),
overflow.side(alignment_sides.1),
];

let mut all_overflows = data.overflows.clone();
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/middleware/flip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Flip<'a,
let mut overflows_data = data.overflows;

if check_main_axis {
overflows.push(overflow.get_side(side));
overflows.push(overflow.side(side));
}
if check_cross_axis {
let sides = get_alignment_sides(placement, rects, rtl);
overflows.push(overflow.get_side(sides.0));
overflows.push(overflow.get_side(sides.1));
overflows.push(overflow.side(sides.0));
overflows.push(overflow.side(sides.1));
}

overflows_data.push(FlipDataOverflow {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/middleware/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

/// Name of the [`Offset`] middleware.
const OFFSET_NAME: &str = "offset";
pub const OFFSET_NAME: &str = "offset";

fn convert_value_to_coords<Element, Window>(
state: MiddlewareState<Element, Window>,
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/middleware/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Shift<'a
let cross_axis = get_side_axis(placement);
let main_axis = get_opposite_axis(cross_axis);

let mut main_axis_coord = coords.get_axis(main_axis);
let mut cross_axis_coord = coords.get_axis(cross_axis);
let mut main_axis_coord = coords.axis(main_axis);
let mut cross_axis_coord = coords.axis(cross_axis);

if check_main_axis {
let min_side = match main_axis {
Expand All @@ -150,8 +150,8 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Shift<'a
Axis::X => Side::Right,
Axis::Y => Side::Bottom,
};
let min = main_axis_coord + overflow.get_side(min_side);
let max = main_axis_coord + overflow.get_side(max_side);
let min = main_axis_coord + overflow.side(min_side);
let max = main_axis_coord + overflow.side(max_side);

main_axis_coord = clamp(min, main_axis_coord, max);
}
Expand All @@ -165,8 +165,8 @@ impl<'a, Element: Clone, Window: Clone> Middleware<Element, Window> for Shift<'a
Axis::X => Side::Right,
Axis::Y => Side::Bottom,
};
let min = cross_axis_coord + overflow.get_side(min_side);
let max = cross_axis_coord + overflow.get_side(max_side);
let min = cross_axis_coord + overflow.side(min_side);
let max = cross_axis_coord + overflow.side(max_side);

cross_axis_coord = clamp(min, cross_axis_coord, max);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/dom/example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::rc::Rc;

use floating_ui_dom::{
compute_position, get_opposite_side, get_side, Arrow, ArrowData, ArrowOptions,
ComputePositionConfig, ComputePositionReturn, DetectOverflowOptions, Flip, FlipOptions, Offset,
OffsetOptions, Padding, Placement, Shift, ShiftOptions, Side, ARROW_NAME,
compute_position, Arrow, ArrowData, ArrowOptions, ComputePositionConfig, ComputePositionReturn,
DetectOverflowOptions, Flip, FlipOptions, Offset, OffsetOptions, Padding, Placement, Shift,
ShiftOptions, Side, ARROW_NAME,
};
use log::Level;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn run() -> Result<(), JsValue> {

let arrow_data: Option<ArrowData> = middleware_data.get_as(ARROW_NAME);
if let Some(arrow_data) = arrow_data {
let static_side = get_opposite_side(get_side(placement));
let static_side = placement.side().opposite();

let arrow_x = arrow_data.x.map_or(String::new(), |x| format!("{x}px"));
let arrow_y = arrow_data.y.map_or(String::new(), |y| format!("{y}px"));
Expand Down
13 changes: 9 additions & 4 deletions packages/dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ mod types;
mod utils;

pub use crate::middleware::*;
pub use crate::types::*;
pub use floating_ui_core::{
ComputePositionReturn, Derivable, DetectOverflowOptions, Middleware, MiddlewareData,
MiddlewareReturn, MiddlewareState, MiddlewareWithOptions,
};
#[doc(no_inline)]
pub use floating_ui_utils::*;
pub use floating_ui_utils::{
dom, AlignedPlacement, Alignment, Axis, ClientRectObject, Coords, Dimensions, ElementRects,
Length, Padding, PartialSideObject, Placement, Rect, Side, SideObject, Strategy,
VirtualElement,
};

use floating_ui_core::{
compute_position as compute_position_core, ComputePositionConfig as CoreComputePositionConfig,
};
use web_sys::{Element, Window};
use web_sys::Element;

use self::platform::Platform;

Expand All @@ -39,10 +44,10 @@ pub struct ComputePositionConfig {
/// Defaults to [`Strategy::Absolute`].
pub strategy: Option<Strategy>,

/// Array of middleware objects to modify the positioning or provide data for rendering.
/// Vector of middleware objects to modify the positioning or provide data for rendering.
///
/// Defaults to an empty vector.
pub middleware: Option<Vec<Box<dyn Middleware<Element, Window>>>>,
pub middleware: Option<MiddlewareVec>,
}

/// Computes the `x` and `y` coordinates that will place the floating element next to a given reference element.
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use web_sys::{Element, Window};
pub use floating_ui_core::middleware::{
ArrowData, ArrowOptions, AutoPlacementData, AutoPlacementDataOverflow, AutoPlacementOptions,
FlipData, FlipDataOverflow, FlipOptions, OffsetData, OffsetOptions, OffsetOptionsValues,
ShiftData, ShiftOptions, ARROW_NAME,
ShiftData, ShiftOptions, ARROW_NAME, AUTO_PLACEMENT_NAME, FLIP_NAME, OFFSET_NAME, SHIFT_NAME,
};

/// Provides data to position an inner element of the floating element so that it appears centered to the reference element.
Expand Down
19 changes: 4 additions & 15 deletions packages/dom/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
use floating_ui_core::Boundary as CoreBoundary;
// use floating_ui_core::{
// AutoPlacementOptions as CoreAutoPlacementOptions, Boundary as CoreBoundary,
// ComputePositionConfig as CoreComputePositionConfig,
// DetectOverflowOptions as CoreDetectOverflowOptions, Elements as CoreElements,
// MiddlewareState as CoreMiddlewareState,
// };
use floating_ui_core::{Boundary as CoreBoundary, Middleware};
use floating_ui_utils::ElementOrVirtual as CoreElementOrVirtual;
use web_sys::Element;
use web_sys::{Element, Window};

pub type Boundary = CoreBoundary<Element>;

// pub type ComputePositionConfig<'a> = CoreComputePositionConfig<'a, Element, Window>;

pub type ElementOrVirtual<'a> = CoreElementOrVirtual<'a, Element>;

// pub type Elements<'a> = CoreElements<'a, Element>;

// pub type MiddlewareState<'a> = CoreMiddlewareState<'a, Element, Window>;

// pub type AutoPlacementOptions<'a> = CoreAutoPlacementOptions<'a, Element>;
/// Vector of middleware used in [`ComputePositionConfig`][`crate::ComputePositionConfig`].
pub type MiddlewareVec = Vec<Box<dyn Middleware<Element, Window>>>;
22 changes: 14 additions & 8 deletions packages/leptos/example/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use floating_ui_leptos::{use_floating, Placement, UseFloatingOptions, UseFloatingReturn};
use floating_ui_leptos::{
use_floating, Arrow, ArrowOptions, MiddlewareVec, Offset, OffsetOptions, Placement,
UseFloatingOptions, UseFloatingReturn,
};
use leptos::{
html::{Div, Span},
*,
Expand All @@ -13,20 +16,23 @@ pub fn App() -> impl IntoView {
let floating = create_node_ref::<Div>();
let floating_arrow = create_node_ref::<Div>();

let middleware: MiddlewareVec = vec![
Box::new(Arrow::new(ArrowOptions {
element: floating_arrow,
padding: None,
})),
Box::new(Offset::new(OffsetOptions::Value(4.0))),
];

let UseFloatingReturn {
floating_styles, ..
} = use_floating(
reference,
floating,
UseFloatingOptions::default()
.open(open.into())
.placement(placement.into()),
// .middleware(Some(MaybeSignal::Static(Some(vec![&Arrow::new(
// ArrowOptions {
// element: floating_arrow,
// padding: None,
// },
// )])))),
.placement(placement.into())
.middleware(middleware.into()),
);

view! {
Expand Down
6 changes: 3 additions & 3 deletions packages/leptos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use floating_ui_dom::{
AutoPlacementDataOverflow, AutoPlacementOptions, Axis, ClientRectObject, ComputePositionConfig,
ComputePositionReturn, Coords, DetectOverflowOptions, Dimensions, ElementOrVirtual,
ElementRects, Flip, FlipData, FlipDataOverflow, FlipOptions, Length, Middleware,
MiddlewareData, MiddlewareReturn, MiddlewareState, MiddlewareWithOptions, Offset, OffsetData,
OffsetOptions, OffsetOptionsValues, Padding, Placement, Rect, Shift, ShiftData, ShiftOptions,
Side, Strategy, VirtualElement,
MiddlewareData, MiddlewareReturn, MiddlewareState, MiddlewareVec, MiddlewareWithOptions,
Offset, OffsetData, OffsetOptions, OffsetOptionsValues, Padding, Placement, Rect, Shift,
ShiftData, ShiftOptions, Side, Strategy, VirtualElement,
};
2 changes: 1 addition & 1 deletion packages/leptos/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use floating_ui_dom::{Middleware, MiddlewareData, Placement, Strategy};
use leptos::{Attribute, IntoAttribute, MaybeProp, Signal};
use web_sys::{Element, Window};

/// Options for [`use_floating`].
/// Options for [`use_floating`][`crate::use_floating::use_floating`].
#[derive(Clone, Default)]
pub struct UseFloatingOptions {
/// Represents the open/close state of the floating element.
Expand Down
Loading

0 comments on commit b34e0ca

Please sign in to comment.