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

fix(focusZone): handle complex child reordering #1225

Merged
merged 3 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/yellow-dolphins-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

handle complex child reordering within a focusZone
49 changes: 49 additions & 0 deletions src/__tests__/behaviors/focusZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ async function nextTick() {
return new Promise(resolve => setTimeout(resolve, 0))
}

const moveDown = () => userEvent.type(document.activeElement!, '{arrowdown}')
Copy link
Member

Choose a reason for hiding this comment

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

Verified the magic string '{arrowdown}': https://github.com/testing-library/user-event#special-characters 👍

const moveUp = () => userEvent.type(document.activeElement!, '{arrowup}')

// Since we use strict `isTabbable` checks within focus trap, we need to mock these
// properties that Jest does not populate.
beforeAll(() => {
Expand Down Expand Up @@ -472,3 +475,49 @@ it('Should set aria-activedescendant correctly', () => {

controller.abort()
})

it('Should handle elements being reordered', async () => {
const {container} = render(
<div>
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
<button tabIndex={0}>Durian</button>
</div>
</div>
)

const focusZoneContainer = container.querySelector<HTMLElement>('#focusZone')!
const [firstButton, secondButton, thirdButton, fourthButton] = focusZoneContainer.querySelectorAll('button')
const controller = focusZone(focusZoneContainer)

firstButton.focus()
expect(document.activeElement).toEqual(firstButton)

moveDown()
expect(document.activeElement).toEqual(secondButton)

moveUp()
expect(document.activeElement).toEqual(firstButton)

// move secondButton and thirdButton to the end of the zone, in reverse order
focusZoneContainer.appendChild(thirdButton)
Copy link
Member

@smockle smockle May 13, 2021

Choose a reason for hiding this comment

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

TIL (from Node.appendChild() on MDN):

If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position (there is no requirement to remove the node from its parent node before appending it to some other node).

focusZoneContainer.appendChild(secondButton)

// The mutation observer fires asynchronously
await nextTick()

expect(document.activeElement).toEqual(firstButton)

moveDown()
expect(document.activeElement).toEqual(fourthButton)

moveDown()
expect(document.activeElement).toEqual(thirdButton)

moveDown()
expect(document.activeElement).toEqual(secondButton)

controller.abort()
})
13 changes: 8 additions & 5 deletions src/behaviors/focusZone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,21 @@ export function focusZone(container: HTMLElement, settings?: FocusZoneSettings):
// If the DOM structure of the container changes, make sure we keep our state up-to-date
// with respect to the focusable elements cache and its order
const observer = new MutationObserver(mutations => {
// Perform all removals first, in case element order has simply changed
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode instanceof HTMLElement) {
beginFocusManagement(...iterateFocusableElements(addedNode))
}
}
for (const removedNode of mutation.removedNodes) {
if (removedNode instanceof HTMLElement) {
endFocusManagement(...iterateFocusableElements(removedNode))
}
}
}
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode instanceof HTMLElement) {
beginFocusManagement(...iterateFocusableElements(addedNode))
}
}
}
})

observer.observe(container, {
Expand Down