Skip to content

Commit 3f14839

Browse files
Warn instead of error when there are no focusable elements (#775)
* warn instead of error when there are no focusable elements * update changelog Co-authored-by: Krystof Rehacek <[email protected]>
1 parent b59c091 commit 3f14839

File tree

5 files changed

+30
-35
lines changed

5 files changed

+30
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Only add `type=button` to real buttons ([#709](https://github.com/tailwindlabs/headlessui/pull/709))
1313
- Fix `escape` bug not closing Dialog after clicking in Dialog ([#754](https://github.com/tailwindlabs/headlessui/pull/754))
14+
- Use `console.warn` instead of throwing an error when there are no focusable elements ([#775](https://github.com/tailwindlabs/headlessui/pull/775))
1415

1516
## [Unreleased - Vue]
1617

@@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1920
- Only add `type=button` to real buttons ([#709](https://github.com/tailwindlabs/headlessui/pull/709))
2021
- Add Vue emit types ([#679](https://github.com/tailwindlabs/headlessui/pull/679), [#712](https://github.com/tailwindlabs/headlessui/pull/712))
2122
- Fix `escape` bug not closing Dialog after clicking in Dialog ([#754](https://github.com/tailwindlabs/headlessui/pull/754))
23+
- Use `console.warn` instead of throwing an error when there are no focusable elements ([#775](https://github.com/tailwindlabs/headlessui/pull/775))
2224

2325
## [@headlessui/react@v1.4.0] - 2021-07-29
2426

packages/@headlessui-react/src/components/focus-trap/focus-trap.test.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,19 @@ it('should focus the initialFocus element inside the FocusTrap even if another e
6262
assertActiveElement(document.getElementById('c'))
6363
})
6464

65-
it(
66-
'should error when there is no focusable element inside the FocusTrap',
67-
suppressConsoleLogs(() => {
68-
expect(() => {
69-
render(
70-
<FocusTrap>
71-
<span>Nothing to see here...</span>
72-
</FocusTrap>
73-
)
74-
}).toThrowErrorMatchingInlineSnapshot(
75-
`"There are no focusable elements inside the <FocusTrap />"`
65+
it('should warn when there is no focusable element inside the FocusTrap', () => {
66+
let spy = jest.spyOn(console, 'warn').mockImplementation(jest.fn())
67+
68+
function Example() {
69+
return (
70+
<FocusTrap>
71+
<span>Nothing to see here...</span>
72+
</FocusTrap>
7673
)
77-
})
78-
)
74+
}
75+
render(<Example />)
76+
expect(spy.mock.calls[0][0]).toBe('There are no focusable elements inside the <FocusTrap />')
77+
})
7978

8079
it(
8180
'should not be possible to programmatically escape the focus trap',

packages/@headlessui-react/src/hooks/use-focus-trap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function useFocusTrap(
8989
focusElement(initialFocus.current)
9090
} else {
9191
if (focusIn(container.current, Focus.First) === FocusResult.Error) {
92-
throw new Error('There are no focusable elements inside the <FocusTrap />')
92+
console.warn('There are no focusable elements inside the <FocusTrap />')
9393
}
9494
}
9595

packages/@headlessui-vue/src/components/focus-trap/focus-trap.test.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,22 @@ it('should focus the initialFocus element inside the FocusTrap even if another e
109109
assertActiveElement(document.getElementById('c'))
110110
})
111111

112-
it(
113-
'should error when there is no focusable element inside the FocusTrap',
114-
suppressConsoleLogs(async () => {
115-
expect.assertions(1)
112+
it('should warn when there is no focusable element inside the FocusTrap', async () => {
113+
expect.assertions(1)
114+
let spy = jest.spyOn(console, 'warn').mockImplementation(jest.fn())
116115

117-
renderTemplate({
118-
template: html`
119-
<FocusTrap>
120-
<span>Nothing to see here...</span>
121-
</FocusTrap>
122-
`,
123-
errorCaptured(err: unknown) {
124-
expect((err as Error).message).toMatchInlineSnapshot(
125-
`"There are no focusable elements inside the <FocusTrap />"`
126-
)
127-
return false
128-
},
129-
})
116+
renderTemplate(
117+
html`
118+
<FocusTrap>
119+
<span>Nothing to see here...</span>
120+
</FocusTrap>
121+
`
122+
)
130123

131-
await new Promise(nextTick)
132-
})
133-
)
124+
await new Promise(nextTick)
125+
126+
expect(spy.mock.calls[0][0]).toBe('There are no focusable elements inside the <FocusTrap />')
127+
})
134128

135129
it(
136130
'should not be possible to programmatically escape the focus trap',

packages/@headlessui-vue/src/hooks/use-focus-trap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function useFocusTrap(
5353
}
5454
}
5555

56-
if (!couldFocus) throw new Error('There are no focusable elements inside the <FocusTrap />')
56+
if (!couldFocus) console.warn('There are no focusable elements inside the <FocusTrap />')
5757
}
5858

5959
previousActiveElement.value = document.activeElement as HTMLElement

0 commit comments

Comments
 (0)