Skip to content

Commit

Permalink
Warn instead of error when there are no focusable elements (#775)
Browse files Browse the repository at this point in the history
* warn instead of error when there are no focusable elements

* update changelog

Co-authored-by: Krystof Rehacek <[email protected]>
  • Loading branch information
RobinMalfait and Krystofee authored Aug 30, 2021
1 parent b59c091 commit 3f14839
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## [Unreleased - Vue]

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ it('should focus the initialFocus element inside the FocusTrap even if another e
assertActiveElement(document.getElementById('c'))
})

it(
'should error when there is no focusable element inside the FocusTrap',
suppressConsoleLogs(() => {
expect(() => {
render(
<FocusTrap>
<span>Nothing to see here...</span>
</FocusTrap>
)
}).toThrowErrorMatchingInlineSnapshot(
`"There are no focusable elements inside the <FocusTrap />"`
it('should warn when there is no focusable element inside the FocusTrap', () => {
let spy = jest.spyOn(console, 'warn').mockImplementation(jest.fn())

function Example() {
return (
<FocusTrap>
<span>Nothing to see here...</span>
</FocusTrap>
)
})
)
}
render(<Example />)
expect(spy.mock.calls[0][0]).toBe('There are no focusable elements inside the <FocusTrap />')
})

it(
'should not be possible to programmatically escape the focus trap',
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-react/src/hooks/use-focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function useFocusTrap(
focusElement(initialFocus.current)
} else {
if (focusIn(container.current, Focus.First) === FocusResult.Error) {
throw new Error('There are no focusable elements inside the <FocusTrap />')
console.warn('There are no focusable elements inside the <FocusTrap />')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,22 @@ it('should focus the initialFocus element inside the FocusTrap even if another e
assertActiveElement(document.getElementById('c'))
})

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

renderTemplate({
template: html`
<FocusTrap>
<span>Nothing to see here...</span>
</FocusTrap>
`,
errorCaptured(err: unknown) {
expect((err as Error).message).toMatchInlineSnapshot(
`"There are no focusable elements inside the <FocusTrap />"`
)
return false
},
})
renderTemplate(
html`
<FocusTrap>
<span>Nothing to see here...</span>
</FocusTrap>
`
)

await new Promise(nextTick)
})
)
await new Promise(nextTick)

expect(spy.mock.calls[0][0]).toBe('There are no focusable elements inside the <FocusTrap />')
})

it(
'should not be possible to programmatically escape the focus trap',
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-vue/src/hooks/use-focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function useFocusTrap(
}
}

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

previousActiveElement.value = document.activeElement as HTMLElement
Expand Down

0 comments on commit 3f14839

Please sign in to comment.