From 1c262f8de406f595580785b5c245d855ed1e0fee Mon Sep 17 00:00:00 2001 From: antoine Date: Sat, 28 Sep 2024 21:14:50 +0200 Subject: [PATCH] Add anchor `.noflip` modifier --- packages/anchor/src/index.js | 7 +++--- packages/docs/src/en/plugins/anchor.md | 30 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/anchor/src/index.js b/packages/anchor/src/index.js index cbfdcb9c5..4952f018e 100644 --- a/packages/anchor/src/index.js +++ b/packages/anchor/src/index.js @@ -14,7 +14,7 @@ export default function (Alpine) { }) Alpine.directive('anchor', Alpine.skipDuringClone((el, { expression, modifiers, value }, { cleanup, evaluate }) => { - let { placement, offsetValue, unstyled } = getOptions(modifiers) + let { placement, offsetValue, unstyled, allowFlip } = getOptions(modifiers) el._x_anchor = Alpine.reactive({ x: 0, y: 0 }) @@ -27,7 +27,7 @@ export default function (Alpine) { computePosition(reference, el, { placement, - middleware: [flip(), shift({padding: 5}), offset(offsetValue)], + middleware: [allowFlip && flip(), shift({padding: 5}), offset(offsetValue)], }).then(({ x, y }) => { unstyled || setStyles(el, x, y) @@ -72,6 +72,7 @@ function getOptions(modifiers) { offsetValue = modifiers[idx + 1] !== undefined ? Number(modifiers[idx + 1]) : offsetValue } let unstyled = modifiers.includes('no-style') + let allowFlip = ! modifiers.includes('noflip') - return { placement, offsetValue, unstyled } + return { placement, offsetValue, unstyled, allowFlip } } diff --git a/packages/docs/src/en/plugins/anchor.md b/packages/docs/src/en/plugins/anchor.md index b7c3b94c7..53c7d09ba 100644 --- a/packages/docs/src/en/plugins/anchor.md +++ b/packages/docs/src/en/plugins/anchor.md @@ -143,6 +143,35 @@ You can add an offset to your anchored element using the `.offset.[px value]` mo + +## Prevent flipping position + +By default, `x-anchor` will flip the position of the anchored element if it doesn't have enough room to render below the reference element. + +You can prevent this behavior by adding the `.noflip` modifier: + +```alpine +
+ + +
+ Dropdown content +
+
+``` + + +
+
+ +
+ +
+ Dropdown content +
+
+ + ## Manual styling @@ -210,4 +239,3 @@ Because `x-anchor` accepts a reference to any DOM element, you can use utilities -