Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-anchor.noflip modifier #4382

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions packages/anchor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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)

Expand Down Expand Up @@ -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 }
}
30 changes: 29 additions & 1 deletion packages/docs/src/en/plugins/anchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ You can add an offset to your anchored element using the `.offset.[px value]` mo
</div>
<!-- END_VERBATIM -->

<a name="prevent-flipping"></a>
## 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
<div x-data="{ open: false }">
<button x-ref="button" @click="open = ! open">Toggle</button>

<div x-show="open" x-anchor.noflip="$refs.button">
Dropdown content
</div>
</div>
```

<!-- START_VERBATIM -->
<div x-data="{ open: false }" class="demo overflow-hidden">
<div class="flex justify-center">
<button x-ref="button" @click="open = ! open">Toggle</button>
</div>

<div x-show="open" x-anchor.noflip="$refs.button" class="bg-white rounded p-4 border shadow z-10">
Dropdown content
</div>
</div>
<!-- END_VERBATIM -->

<a name="manual-styling"></a>
## Manual styling

Expand Down Expand Up @@ -210,4 +239,3 @@ Because `x-anchor` accepts a reference to any DOM element, you can use utilities
</div>
</div>
<!-- END_VERBATIM -->

Loading