File size: 4,152 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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<HTMLDivElement>(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<HTMLElement>('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<HTMLElement>('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 (
    <div
      className="fixed inset-0 bg-black/50 z-[70] flex items-center justify-center p-4"
      onClick={onClose}
      style={{ animation: 'fadeIn 150ms ease' }}
    >
      <div
        ref={panelRef}
        role="dialog"
        aria-modal="true"
        aria-labelledby={titleId}
        className="bg-white rounded-2xl shadow-2xl p-6 sm:p-8 max-w-2xl w-full text-left"
        onClick={(e) => e.stopPropagation()}
      >
        <div className="flex items-center justify-between mb-4">
          <h2 id={titleId} className="text-2xl font-bold" style={{ color: TEAL_DARK, fontFamily: 'Playfair Display, Georgia, serif' }}>
            {current ? 'Change your location' : 'Find your community'}
          </h2>
          <button
            type="button"
            onClick={onClose}
            aria-label="Close"
            className="hover:bg-stone-100"
            style={{ padding: 8, borderRadius: 10, border: 'none', background: 'none', color: '#78716c', fontSize: 20, cursor: 'pointer', lineHeight: 1 }}
          >
            ✕
          </button>
        </div>
        <p className="text-gray-600 mb-6">
          {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.'}
        </p>
        <AddressLookup
          onLocationFound={(loc) => {
            onPicked(loc)
            onClose()
          }}
        />
      </div>
    </div>
  )
}