From c3f870c8b1803b4d8cfc7befa4c91760d71dd3b4 Mon Sep 17 00:00:00 2001 From: Bram van der Holst Date: Fri, 4 Dec 2026 15:23:19 +0100 Subject: [PATCH] GCOM-1639 Let the search field take focus only when the customer opens it --- .changeset/search-field-focus.md | 5 +++++ .../components/ProductFiltersPro/SearchField.tsx | 7 +++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 .changeset/search-field-focus.md diff --git a/.changeset/search-field-focus.md b/.changeset/search-field-focus.md new file mode 100644 index 0000000000..1bbab497d5 --- /dev/null +++ b/.changeset/search-field-focus.md @@ -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. diff --git a/packages/magento-search/components/ProductFiltersPro/SearchField.tsx b/packages/magento-search/components/ProductFiltersPro/SearchField.tsx index 3f728d7945..119e0aa2ad 100644 --- a/packages/magento-search/components/ProductFiltersPro/SearchField.tsx +++ b/packages/magento-search/components/ProductFiltersPro/SearchField.tsx @@ -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' @@ -27,6 +27,7 @@ export function SearchField(props: SearchFieldProps) { }, [searchPage]) const visible = incomingVisible || expanded || searchPage + const focus = useCallback((element: HTMLInputElement | null) => element?.focus(), []) return ( <> @@ -34,8 +35,7 @@ export function SearchField(props: SearchFieldProps) { element?.focus()} - // autoFocus + inputRef={expanded ? focus : undefined} buttonProps={{ onClick: () => { setExpanded(false) @@ -49,7 +49,6 @@ export function SearchField(props: SearchFieldProps) { { setExpanded(true) - // inputRef.current?.focus() }} color='inherit' size='large'