teqo-backup/src/components/campaign/dashboard/CampaignHomeSearch.tsx
Cursor Agent 266fc67516
feat(B112): collapse drawer on blur∅ / nav / handle+swipe, pad ⅓
- Empty blur → collapsed; blur with text stays FULL (drawer whenEmpty policy).
- Handle tap and snap→collapsed clear search so uiFocused cannot reopen FULL.
- Keep all snap points while focused so swipe ↓ can dismiss.
- Pathname change (in-chrome) clears + collapses; initial load stays dock.
- Compact dock/search stack gap (~⅓) without cutting safe-area.

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-01 23:54:59 +00:00

79 lines
2.6 KiB
TypeScript

'use client'
import type { ReactNode } from 'react'
import { useHomeSearch } from '@/components/campaign/dashboard/HomeSearchContext'
import { CampaignSearchInput } from '@/components/campaign/shared/CampaignSearchInput'
import { homeSearchShouldUnfocusOnBlur } from '@/lib/campaignHomeSearchContract'
import { HOME_SEARCH_QUERY_MAX_LENGTH } from '@/lib/schemas/homeSearch'
import { cn } from '@/lib/utils'
const HOME_SEARCH_INPUT_ID = 'homeSearchQuery'
const HOME_SEARCH_LABEL = 'Buscar na campanha'
const HOME_SEARCH_PLACEHOLDER = 'Município, liderança, atividade…'
export const CampaignHomeSearch = ({
children,
resultsBusy,
placeholder = HOME_SEARCH_PLACEHOLDER,
showResults = true,
blurUnfocusPolicy = 'whenInactive',
compactStack = false,
}: {
children?: ReactNode
/** When set, must already fold debounce (see `useHomeSearchResultsState.isFetching`). */
resultsBusy?: boolean
/** Empty string = discreet peek (collapsed drawer); sr-only label stays. */
placeholder?: string
/** When false, keeps the live region mounted but visually hidden (collapsed peek). */
showResults?: boolean
/**
* Início: unfocus when query inactive. Drawer (B112): unfocus only when raw
* is empty so blur-with-text keeps FULL.
*/
blurUnfocusPolicy?: 'whenInactive' | 'whenEmpty'
/** B112 — ~⅓ of the default input→results gap in the bottom drawer. */
compactStack?: boolean
}) => {
const { query, setRaw, clear, isDebouncing, setInputFocused } = useHomeSearch()
const ariaBusy = (resultsBusy ?? isDebouncing) || undefined
return (
<div className={cn('flex w-full min-w-0 flex-col', compactStack ? 'gap-1.5' : 'gap-4')}>
<CampaignSearchInput
id={HOME_SEARCH_INPUT_ID}
label={HOME_SEARCH_LABEL}
placeholder={placeholder}
value={query.raw}
onChange={(event) => setRaw(event.target.value)}
onFocus={() => setInputFocused(true)}
onBlur={() => {
if (homeSearchShouldUnfocusOnBlur(query.raw, blurUnfocusPolicy, query.isActive)) {
setInputFocused(false)
}
}}
onKeyDown={(event) => {
if (event.key === 'Escape') {
event.preventDefault()
clear()
}
}}
autoComplete="off"
enterKeyHint="search"
aria-busy={ariaBusy}
maxLength={HOME_SEARCH_QUERY_MAX_LENGTH}
/>
<div
role="region"
aria-live="polite"
aria-label="Resultados da busca"
aria-busy={ariaBusy}
className="min-w-0"
data-slot="home-search-results"
hidden={!showResults}
>
{children}
</div>
</div>
)
}