Skip to content
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
18 changes: 17 additions & 1 deletion cypress/e2e/spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,24 @@ describe("focus-shift spec", () => {
])
)

it("preserves scroll as default action", function () {
cy.visit("./cypress/fixtures/default-action-scroll.html")
const getScrollTop = () => cy.document().then((doc) => doc.scrollingElement.scrollTop)

getScrollTop().then(initialScrollTop => {
expect(initialScrollTop).to.equal(0)
cy
.get("body").trigger("keydown", keyevent({ key: "ArrowDown" }))
.then(() => getScrollTop().then((scrollTop) => expect(scrollTop).to.equal(1)))
.then(() => cy.get("#first-button").trigger("keydown", keyevent({ key: "ArrowDown" })))
.then(() => getScrollTop().then((scrollTop) => expect(scrollTop).to.equal(2)))
.then(() => cy.get("#second-button").trigger("keydown", keyevent({ key: "ArrowDown" })))
.then(() => getScrollTop().then((scrollTop) => expect(scrollTop).to.not.equal(3)))
})
})

it("allows preventing scroll", function () {
cy.visit("./cypress/fixtures/scroll.html")
cy.visit("./cypress/fixtures/prevent-scroll.html")
const getScrollLeft = () => cy.document().then((doc) => doc.scrollingElement.scrollLeft)
function testScroll(id, assert) {
getScrollLeft().then((scrollLeftBefore) => {
Expand Down
18 changes: 18 additions & 0 deletions cypress/fixtures/default-action-scroll.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<body class="rows">
<div class="nav-group" style="height: 100vh">
<button id="first-button" data-focus-prevent-scroll>First Button</button>
<button id="second-button" data-focus-prevent-scroll>Second Button</button>
</div>

<footer>
Important information in footer.
</footer>
<script src="../../index.js"></script>
</body>
</html>
File renamed without changes.
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ function handleKeyDown(event) {
shiftFocusEvent
)
} else {
event.preventDefault()
handleUserDirection(KEY_TO_DIRECTION[event.key])
const wasHandled = handleUserDirection(KEY_TO_DIRECTION[event.key])

if (wasHandled) {
// Prevent default action only if we found a candidate to shift focus to,
// otherwise maintain browser's default behaviour (e.g. scroll).
event.preventDefault()
}
}
logging.groupEnd()
}
Expand All @@ -54,21 +59,23 @@ function handleKeyDown(event) {
* Handle a user's request for focus shift.
*
* @param {Direction} direction
* @returns {void}
* @returns {boolean} - Whether a spatial navigation action was performed
*/
function handleUserDirection(direction) {
const container = getBlockingElement()
const activeElement = getActiveElement(container)

if (activeElement == null) {
focusInitial(direction, container)
return
return true
}

const candidates = getFocusCandidates(direction, activeElement, container)
if (candidates.length > 0) {
performMove(direction, activeElement.getBoundingClientRect(), candidates)
return performMove(direction, activeElement.getBoundingClientRect(), candidates)
}

return false
}

/**
Expand Down Expand Up @@ -234,7 +241,7 @@ function getFocusCandidates(direction, activeElement, container) {
* @param {Direction} direction
* @param {DOMRect} originRect - The bounding box of the element that has focus at the time the move is initiated
* @param {Array<AnnotatedElement>} candidates - The candidates from which to pick
* @returns {void}
* @returns {boolean} - Whether a move has been performed
*/
function performMove(direction, originRect, candidates) {
logging.debug("performMove", direction, originRect, candidates)
Expand All @@ -254,6 +261,9 @@ function performMove(direction, originRect, candidates) {
)
if (bestCandidate != null) {
applyFocus(direction, originRect, bestCandidate.element)
return true
} else {
return false
}
}

Expand Down
Loading
Loading