Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/src/compute_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::types::{
MiddlewareReturn, MiddlewareState, Reset, ResetRects,
};

/// Maximum number of resets that can occur before bailing to avoid infinite reset loops.
const MAX_RESET_COUNT: i32 = 50;

/// Computes the `x` and `y` coordinates that will place the floating element next to a given reference element.
///
/// This export does not have any `platform` interface logic. You will need to write one for the platform you are using Floating UI with.
Expand Down Expand Up @@ -87,7 +90,7 @@ pub fn compute_position<Element: Clone, Window: Clone>(
}

if let Some(reset) = reset
&& reset_count <= 50
&& reset_count < MAX_RESET_COUNT
{
reset_count += 1;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/middleware/auto_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use floating_ui_utils::{
use serde::{Deserialize, Serialize};

use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
detect_overflow::DetectOverflowOptions,
types::{
Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
MiddlewareWithOptions, Reset, ResetValue,
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element,
allowed_placements
};

let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: elements.clone(),
..state
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/middleware/flip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use floating_ui_utils::{
use serde::{Deserialize, Serialize};

use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
detect_overflow::DetectOverflowOptions,
middleware::arrow::{ARROW_NAME, ArrowData},
types::{
Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
Expand Down Expand Up @@ -264,7 +264,7 @@ impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element,

placements.insert(0, initial_placement);

let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: elements.clone(),
..state
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/middleware/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use floating_ui_utils::{ALL_SIDES, Rect, SideObject};
use serde::{Deserialize, Serialize};

use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
detect_overflow::DetectOverflowOptions,
types::{
Derivable, DerivableFn, ElementContext, Middleware, MiddlewareReturn, MiddlewareState,
MiddlewareWithOptions,
Expand Down Expand Up @@ -132,14 +132,17 @@ impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element,
let options = self.options.evaluate(state.clone());

let MiddlewareState {
elements, rects, ..
elements,
rects,
platform,
..
} = state;

let strategy = options.strategy.unwrap_or_default();

match strategy {
HideStrategy::ReferenceHidden => {
let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: elements.clone(),
..state
Expand Down Expand Up @@ -168,7 +171,7 @@ impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element,
}
}
HideStrategy::Escaped => {
let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: elements.clone(),
..state
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/middleware/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use floating_ui_utils::{Axis, Coords, Side, clamp, get_opposite_axis, get_side_a
use serde::{Deserialize, Serialize};

use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
detect_overflow::DetectOverflowOptions,
middleware::{OFFSET_NAME, OffsetData},
types::{
Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
Expand Down Expand Up @@ -166,7 +166,11 @@ impl<Element: Clone + PartialEq + 'static, Window: Clone + PartialEq + 'static>
let options = self.options.evaluate(state.clone());

let MiddlewareState {
x, y, placement, ..
x,
y,
placement,
platform,
..
} = state;

let check_main_axis = options.main_axis.unwrap_or(true);
Expand All @@ -175,7 +179,7 @@ impl<Element: Clone + PartialEq + 'static, Window: Clone + PartialEq + 'static>
let limiter = options.limiter.unwrap_or(Box::<DefaultLimiter>::default());

let coords = Coords { x, y };
let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: state.elements.clone(),
..state
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/middleware/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ptr;
use floating_ui_utils::{Alignment, Axis, Rect, Side, get_side_axis};

use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
detect_overflow::DetectOverflowOptions,
middleware::shift::{SHIFT_NAME, ShiftData},
types::{
Derivable, DerivableFn, Middleware, MiddlewareReturn, MiddlewareState,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<Element: Clone + PartialEq, Window: Clone + PartialEq> Middleware<Element,
..
} = state;

let overflow = detect_overflow(
let overflow = platform.detect_overflow(
MiddlewareState {
elements: elements.clone(),
..state
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use floating_ui_utils::{Dimensions, ElementRects, Rect};
use floating_ui_utils::{Dimensions, ElementRects, Rect, SideObject};

use crate::types::{GetClippingRectArgs, GetElementRectsArgs, Platform};
use crate::{
detect_overflow::{DetectOverflowOptions, detect_overflow},
types::{GetClippingRectArgs, GetElementRectsArgs, MiddlewareState, Platform},
};

#[derive(Clone, Debug)]
pub struct Element {}
Expand Down Expand Up @@ -49,6 +52,14 @@ impl Platform<Element, Window> for TestPlatform {
height: 10.0,
}
}

fn detect_overflow(
&self,
state: MiddlewareState<Element, Window>,
options: DetectOverflowOptions<Element>,
) -> SideObject {
detect_overflow(state, options)
}
}

pub const PLATFORM: TestPlatform = TestPlatform {};
10 changes: 9 additions & 1 deletion packages/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use serde::{Serialize, de::DeserializeOwned};

use floating_ui_utils::{
ClientRectObject, Coords, Dimensions, ElementOrVirtual, ElementOrWindow, ElementRects, Length,
OwnedElementOrWindow, Placement, Rect, Strategy,
OwnedElementOrWindow, Placement, Rect, SideObject, Strategy,
};

use crate::detect_overflow::DetectOverflowOptions;

pub type DerivableFn<'a, Element, Window, T> = &'a dyn Fn(MiddlewareState<Element, Window>) -> T;

pub enum Derivable<'a, Element: Clone + 'static, Window: Clone, T: Clone> {
Expand Down Expand Up @@ -133,6 +135,12 @@ pub trait Platform<Element: Clone, Window: Clone>: Debug {
fn get_client_length(&self, _element: &Element, _length: Length) -> Option<f64> {
None
}

fn detect_overflow(
&self,
state: MiddlewareState<Element, Window>,
options: DetectOverflowOptions<Element>,
) -> SideObject;
}

/// Data stored by middleware.
Expand Down
14 changes: 12 additions & 2 deletions packages/dom/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ pub mod get_scale;
pub mod is_rtl;

use floating_ui_core::{
ConvertOffsetParentRelativeRectToViewportRelativeRectArgs, GetClippingRectArgs,
GetElementRectsArgs, Platform as CorePlatform,
ConvertOffsetParentRelativeRectToViewportRelativeRectArgs, DetectOverflowOptions,
GetClippingRectArgs, GetElementRectsArgs, MiddlewareState, Platform as CorePlatform,
detect_overflow,
};
use floating_ui_utils::dom::get_document_element;
use floating_ui_utils::{
ClientRectObject, Coords, Dimensions, ElementRects, Length, OwnedElementOrWindow, Rect,
SideObject,
};
use web_sys::{Element, Window};

Expand Down Expand Up @@ -79,4 +81,12 @@ impl CorePlatform<Element, Window> for Platform {
fn get_client_length(&self, element: &Element, length: Length) -> Option<f64> {
Some(get_client_length(element, length))
}

fn detect_overflow(
&self,
state: MiddlewareState<Element, Window>,
options: DetectOverflowOptions<Element>,
) -> SideObject {
detect_overflow(state, options)
}
}
2 changes: 1 addition & 1 deletion upstream.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[releases]
core = "1.7.3"
core = "1.7.5"
dom = "1.7.4"
utils = "0.2.11"
vue = "1.1.9"
Loading