-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep focus inside of the
<ComboboxInput />
component (#3073)
* bail the refocus if focus is already on the correct element * use `mousedown` instead of `click` event The `mousedown` event happens before the `focus` event. When we `e.preventDefault()` in this listener, the `focus` event will not fire. This also means that the focus is not lost on the actual `input` component which in turn means that we can maintain the selection / cursor position inside the `input`. We still use the `refocusInput()` as a fallback in case something else goes wrong. * add comments to describe _why_ we use `mousedown` * ensure we handle mouse buttons correctly * ensure we handle `Enter` and `Space` explicitly Now that we use the `mousedown` event instead of the `click` event, we have to make sure that we handle the `enter` and `space` keys explicitly. This used to be covered by the `click` event, but not for the `mousedown` event. * ensure we focus the first element when using `ArrowDown` on the `ComboboxButton` We go to the last one on `ArrownUp`, but we forgot to do this on `ArrowDown`. * fix tiny typo Not related to this PR, but noticed it and fixed it anyway. * update changelog * ensure we reset the `isTyping` flag While we are typing, the flag can remain true. But once we stop typing, the `nextFrame` handler will kick in and set it to `false` again. It currently behaves as a debounce-like function such that the `nextFrame` callbacks are cancelled once a new event is fired. * ensure unique callbacks in the `_disposables` array This allows us to keep re-adding dispose functions and only register the callbacks once. Ideally we can use a `Set`, but we also want to remove a single callback if the callback is disposed on its own instead of disposing the whole group. For this we do require an `idx` which is not available in a `Set` unless you are looping over all disposable functions. * Update packages/@headlessui-react/src/components/combobox/combobox.tsx Co-authored-by: Jonathan Reinink <[email protected]> * Update packages/@headlessui-react/src/components/combobox/combobox.tsx Co-authored-by: Jonathan Reinink <[email protected]> * update comments * abstract confusing logic to a `useFrameDebounce()` hook * use correct path import * add some breathing room --------- Co-authored-by: Jonathan Reinink <[email protected]>
- Loading branch information
1 parent
4f89588
commit 4ed69f6
Showing
7 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum MouseButton { | ||
Left = 0, | ||
Right = 2, | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/@headlessui-react/src/hooks/use-frame-debounce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useDisposables } from './use-disposables' | ||
import { useEvent } from './use-event' | ||
|
||
/** | ||
* Schedule some task in the next frame. | ||
* | ||
* - If you call the returned function multiple times, only the last task will | ||
* be executed. | ||
* - If the component is unmounted, the task will be cancelled. | ||
*/ | ||
export function useFrameDebounce() { | ||
let d = useDisposables() | ||
|
||
return useEvent((cb: () => void) => { | ||
d.dispose() | ||
d.nextFrame(() => cb()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters