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

Prevent focusing hidden/disabled elements #263

Merged
merged 6 commits into from
May 8, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/eighty-steaks-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/behaviors': minor
---

Adjusts mutation observer to now track `hidden` and `disabled` attributes being applied or removed.
102 changes: 102 additions & 0 deletions src/__tests__/focus-zone.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,105 @@ it('Shoud move to tabbable elements if onlyTabbable', async () => {

controller.abort()
})

it('Should ignore hidden elements after focus zone is enabled', async () => {
const user = userEvent.setup()
const {container, rerender} = render(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

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

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

rerender(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0} hidden>
Banana
</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

await user.keyboard('{arrowdown}')
expect(document.activeElement).toEqual(thirdButton)

controller.abort()
})

it('Should respect unhidden elements after focus zone is enabled', async () => {
const user = userEvent.setup()
const {container, rerender} = render(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0} hidden>
Banana
</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

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

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

await user.keyboard('{arrowdown}')
expect(document.activeElement).toEqual(thirdButton)

rerender(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

await user.keyboard('{arrowup}')
expect(document.activeElement).toEqual(secondButton)

controller.abort()
})

it('Should ignore disabled elements after focus zone is enabled', async () => {
const user = userEvent.setup()
const {container, rerender} = render(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

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

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

rerender(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0} disabled>
Banana
</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

await user.keyboard('{arrowdown}')
expect(document.activeElement).toEqual(thirdButton)

controller.abort()
})
16 changes: 16 additions & 0 deletions src/focus-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,19 +527,35 @@ export function focusZone(container: HTMLElement, settings?: FocusZoneSettings):
endFocusManagement(...iterateFocusableElements(removedNode, iterateFocusableElementsOptions))
}
}
// If an element is hidden or disabled, remove it from the list of focusable elements
if (mutation.type === 'attributes' && mutation.oldValue === null) {
if (mutation.target instanceof HTMLElement) {
endFocusManagement(mutation.target)
}
}
}
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode instanceof HTMLElement) {
beginFocusManagement(...iterateFocusableElements(addedNode, iterateFocusableElementsOptions))
}
}

// Similarly, if an element is unhidden or "enabled", add it to the list of focusable elements
// If `mutation.oldValue` is not null, then we may assume that the element was previously hidden or disabled
if (mutation.type === 'attributes' && mutation.oldValue !== null) {
if (mutation.target instanceof HTMLElement) {
beginFocusManagement(mutation.target)
}
}
}
})

observer.observe(container, {
subtree: true,
childList: true,
attributeFilter: ['hidden', 'disabled'],
attributeOldValue: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this narrow down the observed set of elements in the first place? Seems like the filter was broader and the attributeFilter narrows it down. Just trying to understand how the observer works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

attributeFilter allows us to watch for any changes of the attributes in the array, in addition to the rules that we already have. So it won't narrow our existing ruleset down, as it will still only run the callback if either a node is added/removed (childList), or if an element receives/removes the hidden, or disabled attributes within the container. This essentially just slightly widens what we watch for.

})

const controller = new AbortController()
Expand Down
Loading