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

feat: Support non-ID query selector #20

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions demo/skip-to-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
list-label="Skip links"
:to="[
{ anchor: '#main', label: 'Main content' },
{ anchor: '#footer', label: 'Footer' },
{ anchor: '.footer', label: 'Footer' },
]"
data-vst="skip-to-list"
></vue-skip-to>
Expand All @@ -41,7 +41,7 @@ <h1>Press tab</h1>
</p>
</main>

<footer id="footer">
<footer class="footer">
<p>
Sed elit nunc, volutpat in urna vel, hendrerit vulputate justo.
<a href="https://google.com">Google</a>
Expand Down
51 changes: 42 additions & 9 deletions src/VueSkipToSingle.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<template>
<a
:href="to"
<component
:is="comp"
v-bind="props"
@click.prevent="handleFocusElement"
class="vue-skip-to__link"
>
<slot>{{ label }}</slot>
</a>
</component>
</template>

<script>
Expand All @@ -17,22 +18,54 @@ export default {
type: String,
default: 'Skip to main content'
},

to: {
type: String,
default: '#main'
}
},

computed: {
targetIsId () {
return this.to.substring(0, 1) === '#'
},

comp () {
if (this.targetIsId) return 'a'
return 'button'
},

props () {
if (this.targetIsId) {
return {
href: this.to
}
}

return {}
}
},

methods: {
handleFocusElement ({ target }) {
this.focusElement(target.getAttribute('href').substring(1))
handleFocusElement () {
if (this.targetIsId) {
const id = this.to.substring(1)
if (!id) return
const element = window.document.getElementById(id)
this.focusElement(element)
} else {
const element = window.document.querySelector(this.to)
this.focusElement(element)
}
},

focusElement (id) {
if (!id) return
const element = window.document.getElementById(id)
focusElement (element) {
if (!element) return
if (!/^(a|select|input|button|textarea)/i.test(element.tagName.toLowerCase())) {
if (
!/^(a|select|input|button|textarea)/i.test(
element.tagName.toLowerCase()
)
) {
element.setAttribute('tabindex', -1)
}
element.focus()
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/integration/skip-to-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ describe('Skip link list', () => {
it('Should focus relevant element on link press', () => {
cy.get('body').tab().click()
cy.focused().should('have.id', 'main')

cy.get('body').tab().tab().click()
cy.focused().should('have.class', 'footer')
})
})