Skip to content

Commit

Permalink
experimental/SelectPanel: Move focus to first item if there is no fil…
Browse files Browse the repository at this point in the history
…ter input (#4320)

* move focus to list if there is no input

* Create neat-toes-build.md
  • Loading branch information
siddharthkp authored Mar 7, 2024
1 parent 5baf736 commit 7cb3464
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-toes-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

experimental/SelectPanel: Move focus to first item if there is no filter input
16 changes: 10 additions & 6 deletions packages/react/src/drafts/SelectPanel2/SelectPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ const Panel: React.FC<SelectPanelProps> = ({
// Autofocus hack: React doesn't support autoFocus for dialog: https://github.com/facebook/react/issues/23301
// tl;dr: react takes over autofocus instead of letting the browser handle it,
// but not for dialogs, so we have to do it
React.useEffect(() => {
if (internalOpen) {
const searchInput = document.querySelector('dialog[open] input') as HTMLInputElement | undefined
searchInput?.focus()
}
}, [internalOpen])
React.useEffect(
function intialFocus() {
if (internalOpen) {
const searchInput = document.querySelector('dialog[open] input') as HTMLInputElement | undefined
if (searchInput) searchInput.focus()
else moveFocusToList()
}
},
[internalOpen],
)

/* Anchored */
const {position} = useAnchoredPosition(
Expand Down

0 comments on commit 7cb3464

Please sign in to comment.