File size: 3,743 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
import type { ReactNode } from 'react'

/**
 * A heading written as a sentence with blanks that are the controls: "What did [Alabama] say about [data centers]?".
 * The props mirror the shared component another branch is building (`components/story/SentenceHeading`), so the two can be
 * merged into one afterwards: a template of text parts and blanks, each blank `{ id, label, value, display, options, onChange,
 * searchable? }`, and an optional "Try" row. Local addition: a blank may bring its own `control` (the place blank uses the
 * place popover, which also takes a community), shown instead of the select built from `options`.
 */
export interface BlankOption {
  value: string
  label: string
  group?: string
  hint?: string
}
export interface Blank {
  id: string
  /** The accessible name ("Place", "Topic"). */
  label: string
  value: string
  /** The text the blank shows for the current value. */
  display: string
  options: BlankOption[]
  onChange: (value: string) => void
  searchable?: boolean
  /** Replaces the select built from `options`. */
  control?: ReactNode
}
export type TemplatePart = string | { blank: string }
export interface Suggestion {
  label: string
  onPick: () => void
}
interface Props {
  template: TemplatePart[]
  blanks: Blank[]
  as?: 'h1' | 'h2'
  className?: string
  suggestions?: Suggestion[]
}

/** A template part that is only closing punctuation ("?", "."), which must stay on the line of the word before it. */
export function isClosingMark(part: string): boolean {
  return /^\s*[?!.,;:]+\s*$/.test(part)
}

function BlankSelect({ b }: { b: Blank }) {
  const groups = [...new Set(b.options.map((o) => o.group ?? ''))]
  return (
    <span className="stance-blank-wrap">
      <select className="stance-blank" aria-label={b.label} value={b.value} onChange={(e) => b.onChange(e.target.value)}>
        {groups.map((g) => {
          const opts = b.options
            .filter((o) => (o.group ?? '') === g)
            .map((o) => (
              <option key={o.value} value={o.value} title={o.hint}>
                {o.label}
              </option>
            ))
          return g ? (
            <optgroup key={g} label={g}>
              {opts}
            </optgroup>
          ) : (
            opts
          )
        })}
      </select>
      <span aria-hidden="true" className="stance-blank-caret">
        ▾
      </span>
    </span>
  )
}

export default function SentenceHeading({ template, blanks, as: Tag = 'h1', className, suggestions }: Props) {
  return (
    <>
      <Tag className={`stance-sentence${className ? ` ${className}` : ''}`}>
        {template.map((part, i) => {
          if (typeof part === 'string') {
            // Closing punctuation is drawn with the blank before it (below), so "?" never wraps onto a line alone.
            if (isClosingMark(part) && i > 0 && typeof template[i - 1] !== 'string') return null
            return <span key={i}>{part}</span>
          }
          const b = blanks.find((x) => x.id === part.blank)
          if (!b) return null
          const next = template[i + 1]
          const mark = typeof next === 'string' && isClosingMark(next) ? next.trim() : ''
          return (
            <span key={i} className={mark ? 'stance-nowrap' : undefined}>
              {b.control ?? <BlankSelect b={b} />}
              {mark}
            </span>
          )
        })}
      </Tag>
      {suggestions && suggestions.length > 0 && (
        <p className="stance-try">
          Try:{' '}
          {suggestions.map((s) => (
            <button key={s.label} type="button" className="folio-link-btn" onClick={s.onPick}>
              {s.label}
            </button>
          ))}
        </p>
      )}
    </>
  )
}