@kolkrabbi/kol-component 0.265.0 → 0.267.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,357 +1,11 @@
1
- import { useEffect, useId, useRef, useState } from 'react'
2
- import { Icon } from '@kolkrabbi/kol-icons'
3
- import SearchInput from '../molecules/SearchInput.jsx'
4
- import Tag from '../atoms/Tag.jsx'
5
- import Kbd from '../atoms/Kbd.jsx'
6
- import OptionRow from '../molecules/OptionRow.jsx'
7
- import Button from '../atoms/Button.jsx'
8
- import { Tooltip } from '../utilities/Popover.jsx'
9
-
10
- /* ONE COLUMN (2026-09-30): rows, headings and footer sit on the field's own
11
- * geometry — the p-2 inset, then the md control's 1px ring + 16px pad — so
12
- * every glyph and word lines up under the field's icon and caret. */
13
-
14
- /* Rows bucketed by `group` in first-seen order, rank kept inside a group —
15
- * one heading per group; arrows rove this order, not the engine's. */
16
- function groupRows(rows) {
17
- const order = []
18
- const byGroup = new Map()
19
- rows.forEach((r) => {
20
- const g = r.group ?? ''
21
- if (!byGroup.has(g)) { byGroup.set(g, []); order.push(g) }
22
- byGroup.get(g).push(r)
23
- })
24
- return order.flatMap((g) => byGroup.get(g))
25
- }
26
-
27
- /**
28
- * HighlightMatch — default row renderer: underlines the first
29
- * case-insensitive `query` slice inside `label` at full ink. Exported for
30
- * consumers building their own rows; not in the package barrel.
31
- *
32
- * @param {string} label full row label
33
- * @param {string} query current query (empty / no match → plain label)
34
- */
35
- export function HighlightMatch({ label, query, ranges }) {
36
- /* An engine that already knows where it matched (kol-search's `highlights`)
37
- * passes `ranges` — every hit, not just the first slice of the raw query. */
38
- if (Array.isArray(ranges)) {
39
- if (!ranges.length) return <span>{label}</span>
40
- const out = []
41
- let at = 0
42
- ranges.forEach(([s, e], i) => {
43
- if (s > at) out.push(<span key={`t${i}`}>{label.slice(at, s)}</span>)
44
- out.push(<span key={`m${i}`} className="text-fg underline decoration-2 underline-offset-[3px]">{label.slice(s, e)}</span>)
45
- at = e
46
- })
47
- if (at < label.length) out.push(<span key="rest">{label.slice(at)}</span>)
48
- return <>{out}</>
49
- }
50
- const idx = query ? label.toLowerCase().indexOf(query.toLowerCase()) : -1
51
- if (idx === -1) return <span>{label}</span>
52
- return (
53
- <>
54
- <span>{label.slice(0, idx)}</span>
55
- <span className="text-fg underline decoration-2 underline-offset-[3px]">
56
- {label.slice(idx, idx + query.length)}
57
- </span>
58
- <span>{label.slice(idx + query.length)}</span>
59
- </>
60
- )
61
- }
1
+ import CommandOverlay from './CommandOverlay.jsx'
62
2
 
63
3
  /**
64
- * ShellSearchOverlay — The ⌘K search modal. the ⌘K search modal: fullscreen dim + centered
65
- * panel, a bare SearchInput on top, result rows beneath (HighlightMatch
66
- * label, dim hint line, right-aligned group label). Distinct from Modal
67
- * (prompt/confirm only) — this is the search/command primitive.
68
- *
69
- * Content-agnostic: the consumer filters and passes `results`; selection
70
- * emits `onSelect(item)` (no navigation here — ported off react-router).
71
- * The ⌘K binding itself lives in the shell's key handler, not here.
72
- *
73
- * Keyboard: ArrowUp/ArrowDown rove the active row (mouse hover roves too),
74
- * Enter opens it (the top row from the first keystroke), ⌘/Ctrl+Enter opens every hit,
75
- * Escape closes. Focus trap: focus moves into the input on open, returns to
76
- * the opener on close, and Tab is pinned — rows are combobox options driven
77
- * via aria-activedescendant, never tab stops.
78
- *
79
- * @param {boolean} open mount/unmount the overlay
80
- * @param {Function} onClose () => void — backdrop click, Escape, post-select
81
- * @param {Array} results pre-filtered rows: { id, label, group?, hint?, icon?, disabled?, disabledHint? }
82
- * @param {Array} [suggestions] rows shown while the query is empty (same shape) —
83
- * the search modal opens on somewhere to go, not a blank box
84
- * @param {string} query controlled query (drives the highlight slice)
85
- * @param {Function} onQueryChange (string) => void — input change
86
- * @param {Function} onSelect (item) => void — row click / Enter; consumer navigates. A row
87
- * with `keepOpen` or `action` selects and the modal stays open
88
- * @param {string} placeholder input placeholder
89
- * @param {string} [enterLabel] what Enter does when no row is picked — shown as
90
- * the panel's last line, so Enter is never a surprise
91
- * (e.g. `All results for “q”`). Rows may carry
92
- * `highlights` ([start, end] ranges) from the engine.
93
- * @param {Function} [onOpenResults] (query) => void — ⌘/Ctrl+Enter: every hit, on the consumer's
94
- * own results surface. Plain Enter keeps its meaning.
95
- * @param {string} [resultsLabel] the footer's second line for ⌘Enter (e.g. `All results`)
96
- * @param {Function} [onBack] a drill-in's way back (CommandOverlayModes, kol-monitor 2026-10-17): set, the
97
- * field row leads with a back arrow, and Backspace on an empty field calls it —
98
- * instead of a "← All" row that scrolls away with the list
99
- * @param {string} [backLabel] the arrow's tooltip (default `Back`) — name where it goes ("All modules")
100
- * @param {string} [selectLabel] the footer's Enter line while a row is highlighted (default
101
- * `Go to page`) — a modal whose rows are not pages says what
102
- * Enter does instead (the rack's module search: `Add module`)
4
+ * @deprecated 2026-10-17 — alias of CommandOverlay (CommandOverlayModes). Drops when nobody imports it (04-retirements.md).
5
+ * ShellSearchOverlay — Old name for CommandOverlay. The ⌘K search modal is now one overlay with modes; this
6
+ * name keeps every call-site working. Migrate imports to `CommandOverlay`.
103
7
  */
104
- export default function ShellSearchOverlay({
105
- open,
106
- onClose,
107
- results: rawResults = [],
108
- suggestions = [],
109
- /* EXPANDED — the search modal's second state (user ruling 2026-08-01). Enter
110
- * commits the query and opens `children` as the results body; the search modal
111
- * and the old tag overlay are one surface with two states, not two
112
- * components. `chips` are the committed tag facets of the same query. */
113
- expanded = false,
114
- onExpand,
115
- chips = [],
116
- onRemoveChip,
117
- children,
118
- query = '',
119
- onQueryChange,
120
- onSelect,
121
- placeholder = 'Search…',
122
- enterLabel,
123
- onOpenResults,
124
- resultsLabel = 'All results',
125
- selectLabel = 'Go to page',
126
- onBack,
127
- backLabel = 'Back',
128
- }) {
129
- const panelRef = useRef(null)
130
- const listRef = useRef(null)
131
- const listId = useId()
132
- const [activeIndex, setActiveIndex] = useState(0)
133
- /* Has the user actually chosen a row? See the Enter branch — without this,
134
- * index 0 counts as a selection and Enter navigates somewhere unasked. */
135
- const [navigated, setNavigated] = useState(false)
136
- const results = groupRows(query ? rawResults : suggestions)
137
- /* a `disabled` row is drawn but never active: ↑↓ step over it, Enter and click never reach
138
- * `onSelect` (ShellSearchDisabledRow, kol-monitor 2026-10-15 — a module with no room on the rack) */
139
- const clamped = Math.min(activeIndex, results.length - 1)
140
- const active = clamped >= 0 && !results[clamped].disabled ? clamped : -1
141
- const step = (from, dir) => {
142
- for (let i = from + dir; i >= 0 && i < results.length; i += dir) if (!results[i].disabled) return i
143
- return from
144
- }
145
-
146
- /* Focus in on open, restore the opener on close. querySelector instead of
147
- * a ref through SearchInput — ref-as-prop needs React 19 and the package
148
- * peer range still allows 18. */
149
- useEffect(() => {
150
- if (!open) return undefined
151
- const prev = document.activeElement
152
- panelRef.current?.querySelector('input')?.focus()
153
- return () => { if (prev instanceof HTMLElement) prev.focus() }
154
- }, [open])
155
-
156
- /* Roving row resets to the top on every query change / reopen — and when the ROWS change under an
157
- * unchanged query (ShellSearchOverlayActionKeepsOpen, kol-fxr 2026-10-13: a drill-in swaps the
158
- * suggestions for a category's presets). Keyed on the row ids, not the array's identity, so a
159
- * consumer passing a fresh array every render does not snap the highlight back to the top. */
160
- const rowsKey = results.map((r) => r.id).join('\u0000')
161
- useEffect(() => { setActiveIndex(Math.max(0, results.findIndex((r) => !r.disabled))); setNavigated(false) }, [query, open, rowsKey]) // eslint-disable-line react-hooks/exhaustive-deps
162
-
163
- /* Keep the active row visible inside the scrolling list. */
164
- useEffect(() => {
165
- if (active < 0) return
166
- listRef.current?.querySelectorAll('[role="option"]')[active]?.scrollIntoView({ block: 'nearest' })
167
- }, [active])
168
-
169
- if (!open) return null
170
-
171
- /* A row that KEEPS THE MODAL OPEN — `keepOpen`, or an `action` row (the contract below always said
172
- * so; until 0.252.0 every pick closed — ShellSearchOverlayActionKeepsOpen, kol-fxr 2026-10-13). It
173
- * still reaches `onSelect`, and the closure stays the consumer's to run there: the overlay running
174
- * it too would fire it twice in every shell that already does (workshop's tag rows). */
175
- const select = (item) => {
176
- if (item?.disabled) return
177
- onSelect?.(item)
178
- if (!(item?.keepOpen || typeof item?.action === 'function')) onClose?.()
179
- }
180
-
181
- const optionId = (item) => `${listId}-${item.id}`
182
-
183
- const handleKeyDown = (e) => {
184
- if (e.key === 'Backspace' && !query && onBack) {
185
- e.preventDefault()
186
- onBack()
187
- } else if (e.key === 'Escape') {
188
- e.preventDefault()
189
- onClose?.()
190
- } else if (e.key === 'ArrowDown') {
191
- e.preventDefault()
192
- setNavigated(true)
193
- setActiveIndex((i) => step(Math.min(i, results.length - 1), 1))
194
- } else if (e.key === 'ArrowUp') {
195
- e.preventDefault()
196
- setNavigated(true)
197
- setActiveIndex((i) => step(Math.min(i, results.length - 1), -1))
198
- } else if (e.key === 'Enter') {
199
- e.preventDefault()
200
- /* ⌘ENTER OPENS EVERY HIT (user 2026-09-30: *"enter would focus searched item, and maybe
201
- * command enter would open the results page"*) — only where the consumer has a place for them. */
202
- if ((e.metaKey || e.ctrlKey) && onOpenResults && query) { onOpenResults(query); onClose?.(); return }
203
- /* ENTER OPENS THE HIGHLIGHTED ROW (user 2026-10-01: *"I think enter should also take you to
204
- * the atom but something like command enter take you to the index"* — reverses the
205
- * 2026-08-01 "Enter commits, it only selects once you have arrowed"). The top row is drawn
206
- * highlighted from the first keystroke, so Enter goes where the highlight says. With no
207
- * rows there is nothing to open and Enter commits the query. */
208
- if (active >= 0) select(results[active])
209
- else onExpand?.()
210
- } else if (e.key === 'Tab') {
211
- /* Focus trap — the input is the search modal's only tab stop. */
212
- e.preventDefault()
213
- }
214
- }
215
-
216
- return (
217
- <div className="fixed inset-0 flex items-start justify-center pt-[20vh]" style={{ zIndex: 'var(--kol-z-modal, 100)' }}>
218
- {/* A BUTTON, not a div (OverlayScrimTapDismiss, 2026-09-01): iOS Safari
219
- * does not bubble tap-clicks from non-interactive elements, so the div's
220
- * onClick never fired on a phone and the only way out was the close
221
- * control. A button always fires — and it ends the other half of the
222
- * defect, an interactive target carrying `aria-hidden`. */}
223
- <button
224
- type="button"
225
- aria-label="Close search"
226
- className="absolute inset-0 kol-overlay-scrim"
227
- onClick={onClose}
228
- />
229
- <div
230
- ref={panelRef}
231
- role="dialog"
232
- aria-modal="true"
233
- aria-label="Search"
234
- className={`kol-overlay-panel mx-4 ${expanded ? 'max-w-[var(--kol-content-panel)]' : 'max-w-lg'}`}
235
- >
236
- {/* THE MODE, said out loud (user 2026-08-01: "how do you set search
237
- * mode? theres no helper, message or mode clearly readble"). Two
238
- * modes exist — FILTER (tags narrow a set) and FIND (a keyword jumps
239
- * to a destination) — and the only signal was whether chips happened
240
- * to be present. The chips ARE the mode, so they get a label. */}
241
- {chips.length > 0 && (
242
- <div className="flex flex-wrap items-center gap-1.5 px-4 pt-3">
243
- <span className="kol-helper-10 text-fg-48 shrink-0">FILTERING BY</span>
244
- {chips.map((t) => (
245
- <Tag key={t} onRemove={() => onRemoveChip?.(t)}>{t}</Tag>
246
- ))}
247
- </div>
248
- )}
249
- {/* A REAL FIELD, inset in the panel (2026-09-30, shadcn's search modal as
250
- * the aim) — the flush `bare` strip read as a hole, not a control. */}
251
- <div className="kol-tone-grey p-2 flex items-center gap-2">
252
- {onBack && (
253
- <Tooltip label={backLabel}>
254
- {/* mousedown keeps focus in the field, as the rows do */}
255
- <Button iconOnly="arrow-left" tone="ghost" size="md" aria-label={backLabel} onMouseDown={(e) => e.preventDefault()} onClick={onBack} />
256
- </Tooltip>
257
- )}
258
- <SearchInput
259
- className="w-full min-w-0 flex-1"
260
- value={query}
261
- onChange={(e) => onQueryChange?.(e.target.value)}
262
- placeholder={chips.length > 0 ? 'Narrow these results…' : placeholder}
263
- onKeyDown={handleKeyDown}
264
- role="combobox"
265
- aria-expanded={results.length > 0}
266
- aria-controls={listId}
267
- aria-activedescendant={active >= 0 ? optionId(results[active]) : undefined}
268
- />
269
- </div>
270
-
271
- {/* WHY THIS IS NOT `molecules/Dropdown` (asked 2026-08-01). Dropdown is
272
- * a SELECT: a trigger, a `value`, `onChange(value)`, and rows that are
273
- * options. This is a COMBOBOX — a text query filtering a live list
274
- * whose rows carry a `group`, a `hint`, and may fire an `action`
275
- * instead of selecting a value. Same ARIA family, different control.
276
- * Folding one into the other would mean giving Dropdown a query, a
277
- * hint slot and an action escape hatch, i.e. building this inside it.
278
- *
279
- * THE ROW CONTRACT (was documented nowhere):
280
- * label the row's text, match-highlighted against the query
281
- * group section heading the row files under — 'Atoms', 'Documentation', 'Tags'
282
- * icon optional leading glyph (kol-icons name)
283
- * hint subtext shown when the LABEL was not what matched
284
- * href a destination; dismisses the search modal
285
- * action a closure, run by the consumer's `onSelect`; the search modal STAYS OPEN (tag rows)
286
- * disabled drawn muted, skipped by ↑↓, never reaches `onSelect`; `disabledHint` replaces
287
- * `hint` with the reason (a full rack: "no room")
288
- * keepOpen the row selects and the search modal STAYS OPEN — a drill-in (category →
289
- * its items, a "← All" row back); the consumer swaps `suggestions`/`results`
290
- * Built by `buildShellSearchItems` (showcase/src/nav/shell-nav.js). */}
291
- {expanded ? (
292
- <div className="border-t border-fg-08 max-h-[70vh] overflow-y-auto">{children}</div>
293
- ) : results.length > 0 && (
294
- /* FIXED BODY HEIGHT — the panel holds still while the list narrows. */
295
- <ul
296
- ref={listRef}
297
- id={listId}
298
- role="listbox"
299
- className="kol-tone-grey h-80 overflow-y-auto px-2 pb-2"
300
- >
301
- {results.map((item, i) => {
302
- const heading = (item.group ?? '') !== (results[i - 1]?.group ?? '') || i === 0
303
- return (
304
- <li key={item.id} role="presentation">
305
- {heading && item.group && (
306
- <p className="kol-helper-12 text-fg-48 px-4 border-x border-transparent pt-3 pb-2">{item.group}</p>
307
- )}
308
- <OptionRow
309
- id={optionId(item)}
310
- role="option"
311
- aria-selected={i === active}
312
- aria-disabled={item.disabled || undefined}
313
- active={i === active}
314
- disabled={item.disabled}
315
- icon={item.icon}
316
- label={<HighlightMatch label={item.label} query={query} ranges={item.highlights} />}
317
- hint={(item.disabled && item.disabledHint) || item.hint}
318
- /* preventDefault keeps focus in the input through the click */
319
- onMouseDown={(e) => e.preventDefault()}
320
- onClick={() => select(item)}
321
- onMouseEnter={() => { if (!item.disabled) { setActiveIndex(i); setNavigated(true) } }}
322
- />
323
- </li>
324
- )
325
- })}
326
- </ul>
327
- )}
328
- {/* THE FOOTER SAYS WHAT ENTER DOES — always, not only once typing. */}
329
- {!expanded && (results.length > 0 || (query && (enterLabel || onOpenResults))) && (
330
- <div className="border-t border-fg-08 py-2">
331
- {/* Enter's line names what Enter does NOW: a row is highlighted → it opens that row;
332
- * no rows → it commits the query. */}
333
- {(results.length > 0 || enterLabel) && (
334
- <p className="flex items-center gap-2 kol-helper-12 text-fg-48 mx-2 px-4 border-x border-transparent">
335
- <Kbd icon="corner-down-left" />
336
- {results.length > 0 ? selectLabel : enterLabel}
337
- </p>
338
- )}
339
- {/* THE RESULTS LINE IS A LINK (user 2026-10-01: *"make 'all results...' at the bottom
340
- * also be a link to the results page"*) — the same door ⌘Enter opens. */}
341
- {query && onOpenResults && (
342
- <button
343
- type="button"
344
- className="flex items-center gap-2 kol-helper-12 text-fg-48 hover:text-fg-default transition-colors mx-2 px-4 border-x border-transparent pt-1"
345
- onMouseDown={(e) => e.preventDefault()}
346
- onClick={() => { onOpenResults(query); onClose?.() }}
347
- >
348
- <Kbd icon="command"><Icon name="corner-down-left" size={12} /></Kbd>
349
- {resultsLabel}
350
- </button>
351
- )}
352
- </div>
353
- )}
354
- </div>
355
- </div>
356
- )
8
+ export default function ShellSearchOverlay(props) {
9
+ return <CommandOverlay {...props} />
357
10
  }
11
+ export { HighlightMatch } from './CommandOverlay.jsx'
@@ -22,8 +22,8 @@ export const MASTHEADS = {
22
22
 
23
23
  /* the title role per voice and size — PageHeader's ladder, here so a tool's own title reads it too */
24
24
  export const TITLE_ROLES = {
25
- sans: { sm: 'kol-sans-heading-03', md: 'kol-sans-display-03', lg: 'kol-sans-display-02' },
26
- mono: { sm: 'kol-mono-heading-03', md: 'kol-mono-display-03', lg: 'kol-mono-display-02' },
25
+ sans: { sm: 'kol-sans-heading-03', md: 'kol-sans-display-03', lg: 'kol-sans-display-02', xl: 'kol-sans-display-01' },
26
+ mono: { sm: 'kol-mono-heading-03', md: 'kol-mono-display-03', lg: 'kol-mono-display-02', xl: 'kol-mono-display-02' },
27
27
  }
28
28
 
29
29
  export const MastheadContext = createContext(null)