Skip to content
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
21 changes: 21 additions & 0 deletions packages/docs/src/en/plugins/focus.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,27 @@ By default, when `x-trap` traps focus within an element, it focuses the first fo

By adding `.noautofocus`, Alpine will not automatically focus any elements when trapping focus.

<a name="preventscroll"></a>
#### .preventscroll

By default `x-trap` or rather `focus-trap` will scroll to the currently focussed element inside its content. Adding `.preventscroll`, disables this effect.

For example:

```alpine
<div x-data="{ open: false }">
<button @click="open = true">Open Dialog</button>

<div x-show="open" x-trap.preventscroll="open">
Dialog Contents

<input type="text" name="your_name" autofocus>

<button @click="open = false">Close Dialog</button>
</div>
</div>
```

<a name="focus-magic"></a>
## $focus

Expand Down
4 changes: 4 additions & 0 deletions packages/focus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export default function (Alpine) {
if (autofocusEl) options.initialFocus = autofocusEl
}

if (modifiers.includes('preventscroll')) {
options.preventScroll = true
}

let trap = createFocusTrap(el, options)

let undoInert = () => {}
Expand Down