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

$focus: Add 'onlyTabbables' modifier #3615

Closed
wants to merge 6 commits into from
Closed
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
29 changes: 20 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/docs/src/en/plugins/focus.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ This plugin offers many smaller utilities for managing focus within a page. Thes
| `focus(el)` | Focus the passed element (handling annoyances internally: using nextTick, etc.) |
| `focusable(el)` | Detect whether or not an element is focusable |
| `focusables()` | Get all "focusable" elements within the current element |
| `tabbables()` | Get all "tabbable" elements within the current element |
| `onlyTabbables()` | Specifies that any focus "action" should only act on `focusable` elements. (next, previous, within, etc.) |
| `focused()` | Get the currently focused element on the page |
| `lastFocused()` | Get the last focused element on the page |
| `within(el)` | Specify an element to scope the `$focus` magic to (the current element by default) |
Expand Down
16 changes: 14 additions & 2 deletions packages/focus/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createFocusTrap } from 'focus-trap'
import { focusable, isFocusable } from 'tabbable'
import { focusable, tabbable, isFocusable, isTabbable } from 'tabbable'

export default function (Alpine) {
let lastFocused
Expand All @@ -16,14 +16,19 @@ export default function (Alpine) {
return {
__noscroll: false,
__wrapAround: false,
__tabbableOnly: false,
within(el) { within = el; return this },
withoutScrolling() { this.__noscroll = true; return this },
noscroll() { this.__noscroll = true; return this },
withWrapAround() { this.__wrapAround = true; return this },
wrap() { return this.withWrapAround() },
onlyTabbables() { this.__tabbableOnly = true; return this },
focusable(el) {
return isFocusable(el)
},
tabbable(el) {
return isTabbable(el)
},
previouslyFocused() {
return lastFocused
},
Expand All @@ -38,7 +43,14 @@ export default function (Alpine) {

return focusable(within, { displayCheck: 'none' })
},
all() { return this.focusables() },
tabbables() {
if (Array.isArray(within)) return within

return tabbable(within, { displayCheck: 'none' })
},
all() {
return this.__tabbableOnly ? this.tabbables() : this.focusables()
},
isFirst(el) {
let els = this.all()

Expand Down
45 changes: 45 additions & 0 deletions tests/cypress/integration/plugins/focus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ test('$focus.focusable',
},
)

test('$focus.tabbables.focusable',
[html`
<div x-data>
<button id="1" tabindex="-1" x-text="$focus.tabbable($el)"></button>
<button id="2" x-text="$focus.tabbable($el)"></button>
</div>
`],
({ get }) => {
get('#1').should(haveText('false'))
get('#2').should(haveText('true'))
},
)

test('$focus.focusables',
[html`
<div x-data>
Expand Down Expand Up @@ -294,6 +307,38 @@ test('$focus.prev',
},
)

test('$focus.onlyTabbables.next',
[html`
<div x-data>
<div x-ref="first">
<button id="1" @click="$focus.onlyTabbables().within($refs.first).next(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
<button tabindex="-1">2</button>
<button>3</button>
</div>
</div>
`],
({ get }) => {
get('#1').click()
get('#1').should(haveText('3'))
},
)

test('$focus.onlyTabbables.prev',
[html`
<div x-data>
<div x-ref="first">
<button>3</button>
<button tabindex="-1">2</button>
<button id="1" @click="$focus.onlyTabbables().within($refs.first).prev(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
</div>
</div>
`],
({ get }) => {
get('#1').click()
get('#1').should(haveText('3'))
},
)

test('$focus.wrap',
[html`
<div x-data>
Expand Down