Fixing mobile search header (#34326)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useRef } from 'react'
|
import { useState } from 'react'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { IconButton, TextInput } from '@primer/react'
|
import { IconButton, TextInput } from '@primer/react'
|
||||||
import { SearchIcon } from '@primer/octicons-react'
|
import { SearchIcon } from '@primer/octicons-react'
|
||||||
@@ -6,14 +6,15 @@ import { SearchIcon } from '@primer/octicons-react'
|
|||||||
import { useTranslation } from 'components/hooks/useTranslation'
|
import { useTranslation } from 'components/hooks/useTranslation'
|
||||||
import { DEFAULT_VERSION, useVersion } from 'components/hooks/useVersion'
|
import { DEFAULT_VERSION, useVersion } from 'components/hooks/useVersion'
|
||||||
import { useQuery } from 'components/hooks/useQuery'
|
import { useQuery } from 'components/hooks/useQuery'
|
||||||
|
import { useBreakpoint } from './hooks/useBreakpoint'
|
||||||
|
|
||||||
export function Search() {
|
export function Search() {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { query, debug } = useQuery()
|
const { query, debug } = useQuery()
|
||||||
const [localQuery, setLocalQuery] = useState(query)
|
const [localQuery, setLocalQuery] = useState(query)
|
||||||
const inputRef = useRef<HTMLInputElement>(null)
|
|
||||||
const { t } = useTranslation('search')
|
const { t } = useTranslation('search')
|
||||||
const { currentVersion } = useVersion()
|
const { currentVersion } = useVersion()
|
||||||
|
const upToMediumViewport = useBreakpoint('medium')
|
||||||
|
|
||||||
function redirectSearch() {
|
function redirectSearch() {
|
||||||
let asPath = `/${router.locale}`
|
let asPath = `/${router.locale}`
|
||||||
@@ -41,6 +42,8 @@ export function Search() {
|
|||||||
redirectSearch()
|
redirectSearch()
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
{/* This prevents zooming in on iOS when you touch the search input text area */}
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
||||||
<label className="text-normal width-full">
|
<label className="text-normal width-full">
|
||||||
<span
|
<span
|
||||||
className="visually-hidden"
|
className="visually-hidden"
|
||||||
@@ -54,7 +57,8 @@ export function Search() {
|
|||||||
}
|
}
|
||||||
onInput={(e) => (e.target as HTMLInputElement).setCustomValidity('')}
|
onInput={(e) => (e.target as HTMLInputElement).setCustomValidity('')}
|
||||||
data-testid="site-search-input"
|
data-testid="site-search-input"
|
||||||
ref={inputRef}
|
// This adds focus in particular for iOS to focus and bring up the keyboard when you touch the search input text area
|
||||||
|
ref={(inputRef) => upToMediumViewport && inputRef && inputRef.focus()}
|
||||||
type="search"
|
type="search"
|
||||||
placeholder={t`placeholder`}
|
placeholder={t`placeholder`}
|
||||||
autoComplete={localQuery ? 'on' : 'off'}
|
autoComplete={localQuery ? 'on' : 'off'}
|
||||||
|
|||||||
@@ -63,13 +63,10 @@ export const Header = () => {
|
|||||||
return () => window.removeEventListener('keydown', close)
|
return () => window.removeEventListener('keydown', close)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
// For the UI in smaller browswer widths, focus the search input when the
|
// For the UI in smaller browswer widths, and focus the picker menu button when the search
|
||||||
// search input is opened and focus the picker menu button when the search
|
|
||||||
// input is closed.
|
// input is closed.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isSearchOpen) {
|
if (!isSearchOpen && isMounted.current && menuButtonRef.current) {
|
||||||
document.querySelector<HTMLInputElement>('input[type="search"]')?.focus()
|
|
||||||
} else if (!isSearchOpen && isMounted.current && menuButtonRef.current) {
|
|
||||||
menuButtonRef.current.focus()
|
menuButtonRef.current.focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +172,6 @@ export const Header = () => {
|
|||||||
{/* The ... navigation menu at medium and smaller widths */}
|
{/* The ... navigation menu at medium and smaller widths */}
|
||||||
<nav>
|
<nav>
|
||||||
<AnchoredOverlay
|
<AnchoredOverlay
|
||||||
anchorRef={menuButtonRef}
|
|
||||||
renderAnchor={(anchorProps) => (
|
renderAnchor={(anchorProps) => (
|
||||||
<IconButton
|
<IconButton
|
||||||
data-testid="mobile-menu"
|
data-testid="mobile-menu"
|
||||||
|
|||||||
Reference in New Issue
Block a user