import { useEffect, useId, useRef } from 'react' import AddressLookup from './AddressLookup' import type { LocationData } from '../contexts/LocationContext' const TEAL_DARK = '#0f766e' interface Props { open: boolean onClose: () => void /** The place set now ("Tuscaloosa, AL"), or null when none is. */ current: string | null onPicked: (loc: LocationData) => void } /** * The "Find your town" picker (#1179, shared since #1191): a centred dialog over the real geocoder * (AddressLookup → api/routes/geocode.py → Nominatim). Opened from the header's place control and the home page. * No place is suggested or hard-coded: any town the geocoder finds can be picked. */ export default function LocationPickerDialog({ open, onClose, current, onPicked }: Props) { const titleId = useId() const panelRef = useRef(null) // Escape closes; Tab is trapped; focus moves into the dialog on open and back to whatever opened it on close. useEffect(() => { if (!open) return const opener = document.activeElement as HTMLElement | null // Modal: the page behind doesn't scroll, and Tab stays inside the dialog. const prevOverflow = document.body.style.overflow document.body.style.overflow = 'hidden' const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.stopPropagation() onClose() return } if (e.key !== 'Tab' || !panelRef.current) return const nodes = Array.from( panelRef.current.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])'), ) if (nodes.length === 0) return const first = nodes[0]! const last = nodes[nodes.length - 1]! const inside = panelRef.current.contains(document.activeElement) if (e.shiftKey && (document.activeElement === first || !inside)) { e.preventDefault() last.focus() } else if (!e.shiftKey && (document.activeElement === last || !inside)) { e.preventDefault() first.focus() } } document.addEventListener('keydown', onKey, true) const raf = requestAnimationFrame(() => { const first = panelRef.current?.querySelector('input, button, [href], [tabindex]:not([tabindex="-1"])') first?.focus() }) return () => { document.removeEventListener('keydown', onKey, true) document.body.style.overflow = prevOverflow cancelAnimationFrame(raf) if (opener && document.contains(opener)) opener.focus() } }, [open, onClose]) if (!open) return null return (
e.stopPropagation()} >

{current ? 'Change your location' : 'Find your community'}

{current ? `Currently set to ${current}. Enter a new address to change your location.` : 'Enter your address to see the meetings, votes, spending, and debates near you.'}

{ onPicked(loc) onClose() }} />
) }