Skip to content
Open
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
129 changes: 128 additions & 1 deletion packages/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,10 @@
.map(|side| get_placement(side, alignment))
.collect();

if flip_alignment {
// The flip-alignment duplication only applies to aligned placements. In the
// JS source this block is nested inside `if (alignment)`; without that guard,
// base placements (e.g. Top/Bottom/Left/Right) are returned duplicated.
Comment on lines +715 to +717

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The flip-alignment duplication only applies to aligned placements. In the
// JS source this block is nested inside `if (alignment)`; without that guard,
// base placements (e.g. Top/Bottom/Left/Right) are returned duplicated.

if flip_alignment && alignment.is_some() {
let mut opposite_list: Vec<Placement> = list
.clone()
.into_iter()
Expand Down Expand Up @@ -762,3 +765,127 @@
left: rect.x,
}
}

#[cfg(test)]
mod opposite_axis_placements_tests {
Comment on lines +769 to +770

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(test)]
mod opposite_axis_placements_tests {
#[cfg(test)]
mod tests {

// Ported from @floating-ui/utils
// packages/utils/test/getOppositeAxisPlacements.test.ts
Comment on lines +771 to +772

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Ported from @floating-ui/utils
// packages/utils/test/getOppositeAxisPlacements.test.ts

use super::Placement::{
Bottom, BottomEnd, BottomStart, Left, LeftEnd, LeftStart, Right, RightEnd, RightStart, Top,
TopEnd, TopStart,
};
use super::{Alignment, Placement, get_opposite_axis_placements};

fn goap(placement: Placement, flip: bool, dir: Alignment) -> Vec<Placement> {
get_opposite_axis_placements(placement, flip, Some(dir), None)
}
fn goap_rtl(placement: Placement, flip: bool, dir: Alignment) -> Vec<Placement> {
get_opposite_axis_placements(placement, flip, Some(dir), Some(true))
}

// --- side ---

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- side ---

#[test]
fn side_top() {
assert_eq!(goap(Top, true, Alignment::Start), vec![Left, Right]);
assert_eq!(goap(Top, true, Alignment::End), vec![Right, Left]);
}
#[test]
fn side_bottom() {
assert_eq!(goap(Bottom, true, Alignment::Start), vec![Left, Right]);
assert_eq!(goap(Bottom, true, Alignment::End), vec![Right, Left]);
}
#[test]
fn side_left() {
assert_eq!(goap(Left, true, Alignment::Start), vec![Top, Bottom]);
assert_eq!(goap(Left, true, Alignment::End), vec![Bottom, Top]);
}
#[test]
fn side_right() {
assert_eq!(goap(Right, true, Alignment::Start), vec![Top, Bottom]);
assert_eq!(goap(Right, true, Alignment::End), vec![Bottom, Top]);
}

// --- start alignment ---

Check warning on line 808 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- start alignment ---

#[test]
fn start_top_start() {
assert_eq!(goap(TopStart, false, Alignment::Start), vec![LeftStart, RightStart]);
assert_eq!(goap(TopStart, false, Alignment::End), vec![RightStart, LeftStart]);
assert_eq!(goap(TopStart, true, Alignment::Start), vec![LeftStart, RightStart, LeftEnd, RightEnd]);
assert_eq!(goap(TopStart, true, Alignment::End), vec![RightStart, LeftStart, RightEnd, LeftEnd]);
}
#[test]
fn start_bottom_start() {
assert_eq!(goap(BottomStart, false, Alignment::Start), vec![LeftStart, RightStart]);

Check warning on line 818 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
assert_eq!(goap(BottomStart, false, Alignment::End), vec![RightStart, LeftStart]);
assert_eq!(goap(BottomStart, true, Alignment::Start), vec![LeftStart, RightStart, LeftEnd, RightEnd]);
assert_eq!(goap(BottomStart, true, Alignment::End), vec![RightStart, LeftStart, RightEnd, LeftEnd]);
}
#[test]
fn start_left_start() {
assert_eq!(goap(LeftStart, false, Alignment::Start), vec![TopStart, BottomStart]);

Check warning on line 825 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
assert_eq!(goap(LeftStart, false, Alignment::End), vec![BottomStart, TopStart]);
assert_eq!(goap(LeftStart, true, Alignment::Start), vec![TopStart, BottomStart, TopEnd, BottomEnd]);
assert_eq!(goap(LeftStart, true, Alignment::End), vec![BottomStart, TopStart, BottomEnd, TopEnd]);
}
#[test]
fn start_right_start() {
assert_eq!(goap(RightStart, false, Alignment::Start), vec![TopStart, BottomStart]);

Check warning on line 832 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
assert_eq!(goap(RightStart, false, Alignment::End), vec![BottomStart, TopStart]);
assert_eq!(goap(RightStart, true, Alignment::Start), vec![TopStart, BottomStart, TopEnd, BottomEnd]);
assert_eq!(goap(RightStart, true, Alignment::End), vec![BottomStart, TopStart, BottomEnd, TopEnd]);
}

// --- end alignment ---

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- end alignment ---

#[test]

Check warning on line 839 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
fn end_top_end() {
assert_eq!(goap(TopEnd, false, Alignment::Start), vec![LeftEnd, RightEnd]);
assert_eq!(goap(TopEnd, false, Alignment::End), vec![RightEnd, LeftEnd]);
assert_eq!(goap(TopEnd, true, Alignment::Start), vec![LeftEnd, RightEnd, LeftStart, RightStart]);
assert_eq!(goap(TopEnd, true, Alignment::End), vec![RightEnd, LeftEnd, RightStart, LeftStart]);
}
#[test]
fn end_bottom_end() {
assert_eq!(goap(BottomEnd, false, Alignment::Start), vec![LeftEnd, RightEnd]);

Check warning on line 848 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
assert_eq!(goap(BottomEnd, false, Alignment::End), vec![RightEnd, LeftEnd]);
assert_eq!(goap(BottomEnd, true, Alignment::Start), vec![LeftEnd, RightEnd, LeftStart, RightStart]);
assert_eq!(goap(BottomEnd, true, Alignment::End), vec![RightEnd, LeftEnd, RightStart, LeftStart]);
}
#[test]
fn end_left_end() {
// NOTE: upstream JS test asserts left-end for the non-flip cases and

Check warning on line 855 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
// (verbatim, an upstream copy-paste) left-start for the flip cases.
Comment on lines +855 to +856

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// NOTE: upstream JS test asserts left-end for the non-flip cases and
// (verbatim, an upstream copy-paste) left-start for the flip cases.

assert_eq!(goap(LeftEnd, false, Alignment::Start), vec![TopEnd, BottomEnd]);
assert_eq!(goap(LeftEnd, false, Alignment::End), vec![BottomEnd, TopEnd]);
assert_eq!(goap(LeftStart, true, Alignment::Start), vec![TopStart, BottomStart, TopEnd, BottomEnd]);
assert_eq!(goap(LeftStart, true, Alignment::End), vec![BottomStart, TopStart, BottomEnd, TopEnd]);
}
#[test]
fn end_right_end() {
assert_eq!(goap(RightEnd, false, Alignment::Start), vec![TopEnd, BottomEnd]);

Check warning on line 864 in packages/utils/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust / Format

Diff in /home/runner/work/floating-ui/floating-ui/packages/utils/src/lib.rs
assert_eq!(goap(RightEnd, false, Alignment::End), vec![BottomEnd, TopEnd]);
assert_eq!(goap(RightEnd, true, Alignment::Start), vec![TopEnd, BottomEnd, TopStart, BottomStart]);
assert_eq!(goap(RightEnd, true, Alignment::End), vec![BottomEnd, TopEnd, BottomStart, TopStart]);
}

// --- rtl ---

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- rtl ---

#[test]
fn rtl_top() {
assert_eq!(goap_rtl(Top, true, Alignment::Start), vec![Right, Left]);
assert_eq!(goap_rtl(Top, true, Alignment::End), vec![Left, Right]);
}
#[test]
fn rtl_bottom() {
assert_eq!(goap_rtl(Bottom, true, Alignment::Start), vec![Right, Left]);
assert_eq!(goap_rtl(Bottom, true, Alignment::End), vec![Left, Right]);
}
#[test]
fn rtl_left() {
assert_eq!(goap_rtl(Left, true, Alignment::Start), vec![Top, Bottom]);
assert_eq!(goap_rtl(Left, true, Alignment::End), vec![Bottom, Top]);
}
#[test]
fn rtl_right() {
assert_eq!(goap_rtl(Right, true, Alignment::Start), vec![Top, Bottom]);
assert_eq!(goap_rtl(Right, true, Alignment::End), vec![Bottom, Top]);
}
}
Loading