Skip to content

Commit a747b0a

Browse files
authored
fix(tab): skip elements with visibility:hidden (#799)
1 parent ae3f07c commit a747b0a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/utils/misc/isVisible.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ export function isVisible(element: Element): boolean {
88
el?.ownerDocument;
99
el = el.parentElement
1010
) {
11-
const display = window.getComputedStyle(el).display
11+
const {display, visibility} = window.getComputedStyle(el)
1212
if (display === 'none') {
1313
return false
1414
}
15+
if (visibility === 'hidden') {
16+
return false
17+
}
1518
}
1619

1720
return true

tests/tab.ts

+16
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,19 @@ test('calls FocusEvents with relatedTarget', async () => {
564564
?.relatedTarget,
565565
).toBe(element0)
566566
})
567+
568+
test('should not focus on children of element with style `visiblity: hidden`', () => {
569+
const {
570+
elements: [inputA, , inputB],
571+
} = setup(`
572+
<input/>
573+
<div style="visibility: hidden;">
574+
<input/>
575+
</div>
576+
<input/>
577+
`)
578+
579+
inputA.focus()
580+
userEvent.tab()
581+
expect(inputB).toHaveFocus()
582+
})

tests/utils/misc/isVisible.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ test('check if element is visible', async () => {
99
<input data-testid="styledHiddenInput" style="display: none">
1010
<input data-testid="styledDisplayedInput" hidden style="display: block"/>
1111
<div style="display: none"><input data-testid="childInput" /></div>
12+
<input data-testid="styledVisibiliyHiddenInput" style="visibility: hidden">
1213
`)
1314

1415
expect(isVisible(screen.getByTestId('visibleInput'))).toBe(true)
1516
expect(isVisible(screen.getByTestId('styledDisplayedInput'))).toBe(true)
1617
expect(isVisible(screen.getByTestId('styledHiddenInput'))).toBe(false)
1718
expect(isVisible(screen.getByTestId('childInput'))).toBe(false)
1819
expect(isVisible(screen.getByTestId('hiddenInput'))).toBe(false)
20+
expect(isVisible(screen.getByTestId('styledVisibiliyHiddenInput'))).toBe(
21+
false,
22+
)
1923
})

0 commit comments

Comments
 (0)