Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/search-field-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-search': patch
---

The search field takes focus once: when the customer opens it, or when the search page loads. A field with `visible` keeps the focus where the customer puts it.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { t } from '@lingui/core/macro'
import type { FabProps } from '@mui/material'
import { Fab } from '@mui/material'
import dynamic from 'next/dynamic'
import { useMemo, useState } from 'react'
import { useCallback, useMemo, useState } from 'react'
import type { ProductFiltersProSearchInputProps } from './ProductFiltersProSearchInput'
import { useSearchPageAndParam } from './useSearchPageAndParam'

Expand All @@ -27,15 +27,15 @@ export function SearchField(props: SearchFieldProps) {
}, [searchPage])

const visible = incomingVisible || expanded || searchPage
const focus = useCallback((element: HTMLInputElement | null) => element?.focus(), [])

return (
<>
{visible && (
<ProductFiltersProSearchInputLazy
{...props}
formControl={formControl}
inputRef={(element: HTMLInputElement) => element?.focus()}
// autoFocus
inputRef={expanded ? focus : undefined}
buttonProps={{
onClick: () => {
setExpanded(false)
Expand All @@ -49,7 +49,6 @@ export function SearchField(props: SearchFieldProps) {
<Fab
onClick={() => {
setExpanded(true)
// inputRef.current?.focus()
}}
color='inherit'
size='large'
Expand Down
Loading