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

Save and restore state of focus #35

Open
wants to merge 19 commits 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
59 changes: 42 additions & 17 deletions jump.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
export function jumpKeyUX() {
return window => {
let jumps = []
let lastFocusedElement = null

function focus(next) {
let current = window.document.activeElement
if (current && current !== window.document.body) {
jumps.push(new WeakRef(current))
}
next.focus({ focusVisible: true })
lastFocusedElement = null
}

function back() {
let ref = jumps.pop()
if (!ref) {
window.document.activeElement.blur()
return
function jumpBack() {
let ref = jumps.pop();
if (ref) {
let el = ref.deref();

if (el && el.isConnected) {
el.focus();
return true;
}
return jumpBack();
}
let el = ref.deref()
if (el && el.isConnected) {
el.focus()
} else {
back()
return false;
}

function blur() {
let activeElement = document.activeElement;
if (activeElement && activeElement !== document.body) {
lastFocusedElement = activeElement;
}
activeElement.blur();
}


function jumpBackOrBlur() {
let successfullyJumped = jumpBack();
if (!successfullyJumped) {
blur();
}
}

let tries = 0
let finding

function jump(from) {
clearInterval(finding)
tries = 0
let ariaControls = from.getAttribute('aria-controls')
finding = setInterval(() => {
if (tries++ > 50) {
Expand All @@ -53,14 +71,21 @@ export function jumpKeyUX() {
}, 50)
}

function restoreFocus(event) {
event.preventDefault()
lastFocusedElement.focus({ focusVisible: true })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it was deleted from DOM?

lastFocusedElement = null
}

function keyDown(event) {
if (event.target.getAttribute('aria-controls')) {
if (event.key === 'Enter') {
jump(event.target)
}
if (event.target.getAttribute('aria-controls') && event.key === 'Enter') {
jump(event.target)
}
if (event.key === 'Escape') {
back()

if (event.key === 'Tab' && lastFocusedElement) {
restoreFocus(event)
} else if (event.key === 'Escape') {
jumpBackOrBlur()
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"import": {
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint, hotkeyOverrides, hotkeyMacCompat }"
},
"limit": "2162 B"
"limit": "2208 B"
}
],
"clean-publish": {
Expand Down
31 changes: 30 additions & 1 deletion test/jump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,35 @@ import { setTimeout } from 'node:timers/promises'
import { jumpKeyUX, startKeyUX } from '../index.js'
import { keyboardClick, mouseClick, press } from './utils.js'

test('jumps to next next elemnt, blur and restore focus on the last element', async () => {
let { window } = new JSDOM()
global.document = window.document

startKeyUX(window, [jumpKeyUX()])
window.document.body.innerHTML =
'<a id="step1" href="#"></a>' +
'<a id="step2" href="#"></a>'
let step1 = window.document.querySelector<HTMLElement>('#step1')!
let step2 = window.document.querySelector<HTMLElement>('#step1')!

step1.focus()

press(window, 'Tab')
equal(window.document.activeElement, step1)
await setTimeout(50)
equal(window.document.activeElement, step2)

press(window, 'Escape')
equal(window.document.activeElement, window.document.body)

press(window, 'Tab')
equal(window.document.activeElement, step2)
})

test('jumps to next area by click and back by escape', async () => {
let window = new JSDOM().window
let { window } = new JSDOM()
global.document = window.document

startKeyUX(window, [jumpKeyUX()])
window.document.body.innerHTML =
'<a id="step1" href="#" aria-controls="step2"></a>' +
Expand All @@ -26,6 +53,7 @@ test('jumps to next area by click and back by escape', async () => {

step1.focus()
keyboardClick(window, step1)

equal(window.document.activeElement, step1)
await setTimeout(50)
equal(window.document.activeElement, step2button)
Expand Down Expand Up @@ -71,6 +99,7 @@ test('stops event tracking', async () => {
keyboardClick(window, step1)
await setTimeout(50)
equal(window.document.activeElement, window.document.body)

})

test('ignores mouse click', async () => {
Expand Down