Skip to content

Commit

Permalink
fix(tab): skip elements with visibility:hidden (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis273 authored Dec 7, 2021
1 parent ae3f07c commit a747b0a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils/misc/isVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export function isVisible(element: Element): boolean {
el?.ownerDocument;
el = el.parentElement
) {
const display = window.getComputedStyle(el).display
const {display, visibility} = window.getComputedStyle(el)
if (display === 'none') {
return false
}
if (visibility === 'hidden') {
return false
}
}

return true
Expand Down
16 changes: 16 additions & 0 deletions tests/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,19 @@ test('calls FocusEvents with relatedTarget', async () => {
?.relatedTarget,
).toBe(element0)
})

test('should not focus on children of element with style `visiblity: hidden`', () => {
const {
elements: [inputA, , inputB],
} = setup(`
<input/>
<div style="visibility: hidden;">
<input/>
</div>
<input/>
`)

inputA.focus()
userEvent.tab()
expect(inputB).toHaveFocus()
})
4 changes: 4 additions & 0 deletions tests/utils/misc/isVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ test('check if element is visible', async () => {
<input data-testid="styledHiddenInput" style="display: none">
<input data-testid="styledDisplayedInput" hidden style="display: block"/>
<div style="display: none"><input data-testid="childInput" /></div>
<input data-testid="styledVisibiliyHiddenInput" style="visibility: hidden">
`)

expect(isVisible(screen.getByTestId('visibleInput'))).toBe(true)
expect(isVisible(screen.getByTestId('styledDisplayedInput'))).toBe(true)
expect(isVisible(screen.getByTestId('styledHiddenInput'))).toBe(false)
expect(isVisible(screen.getByTestId('childInput'))).toBe(false)
expect(isVisible(screen.getByTestId('hiddenInput'))).toBe(false)
expect(isVisible(screen.getByTestId('styledVisibiliyHiddenInput'))).toBe(
false,
)
})

0 comments on commit a747b0a

Please sign in to comment.