@greenharness/greenrack-ui 7.1.1 → 7.2.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.
Files changed (29) hide show
  1. package/dist/components/Button/Button.stories.d.ts +7 -0
  2. package/dist/components/Button/Button.stories.d.ts.map +1 -1
  3. package/dist/components/Button/styles.css.d.ts +1 -0
  4. package/dist/components/Button/styles.css.d.ts.map +1 -1
  5. package/dist/components/Combobox/Combobox.d.ts +6 -2
  6. package/dist/components/Combobox/Combobox.d.ts.map +1 -1
  7. package/dist/components/Combobox/Combobox.stories.d.ts +23 -0
  8. package/dist/components/Combobox/Combobox.stories.d.ts.map +1 -1
  9. package/dist/components/Combobox/ComboboxContext.d.ts +4 -0
  10. package/dist/components/Combobox/ComboboxContext.d.ts.map +1 -1
  11. package/dist/components/Combobox/comboboxState.d.ts +4 -2
  12. package/dist/components/Combobox/comboboxState.d.ts.map +1 -1
  13. package/dist/components/Combobox/useComboboxRoot.d.ts.map +1 -1
  14. package/dist/components/LinkButton/styles.css.d.ts +1 -0
  15. package/dist/components/LinkButton/styles.css.d.ts.map +1 -1
  16. package/dist/index.css +1 -1
  17. package/dist/index.js +3025 -3003
  18. package/dist/index.js.map +1 -1
  19. package/package.json +2 -2
  20. package/src/components/Button/Button.stories.tsx +37 -0
  21. package/src/components/Button/styles.css.ts +4 -0
  22. package/src/components/Combobox/Combobox.meta.ts +1 -1
  23. package/src/components/Combobox/Combobox.native.tsx +18 -4
  24. package/src/components/Combobox/Combobox.stories.tsx +355 -2
  25. package/src/components/Combobox/Combobox.tsx +54 -11
  26. package/src/components/Combobox/ComboboxContext.ts +4 -0
  27. package/src/components/Combobox/comboboxState.ts +19 -3
  28. package/src/components/Combobox/useComboboxRoot.ts +128 -47
  29. package/src/components/LinkButton/styles.css.ts +2 -0
@@ -7,7 +7,11 @@ export interface ComboboxContextValue {
7
7
  value: string | null
8
8
  setValue: (value: string) => void
9
9
  inputValue: string
10
+ /** What filters the list: empty while the field shows the chosen option's label. */
11
+ query: string
10
12
  setInputValue: (value: string) => void
13
+ onInputFocus: () => void
14
+ onInputBlur: () => void
11
15
  labels: Record<string, string>
12
16
  registerLabel: (value: string, label: string) => void
13
17
  listboxId: string
@@ -1,4 +1,4 @@
1
- import {useCallback, useEffect, useState} from 'react'
1
+ import {useCallback, useEffect, useRef, useState} from 'react'
2
2
  import {useControllableValue} from '../../hooks/useControllableValue'
3
3
 
4
4
  export interface ComboboxStateOptions {
@@ -7,6 +7,8 @@ export interface ComboboxStateOptions {
7
7
  onValueChange?: (value: string | null) => void
8
8
  inputValue?: string
9
9
  onInputChange?: (value: string) => void
10
+ /** Called once per real open/close transition, never inside a state updater. */
11
+ onOpenChange?: (open: boolean) => void
10
12
  initialLabels?: Record<string, string>
11
13
  }
12
14
 
@@ -28,6 +30,7 @@ export function useComboboxState({
28
30
  onValueChange,
29
31
  inputValue: controlledInputValue,
30
32
  onInputChange,
33
+ onOpenChange,
31
34
  initialLabels,
32
35
  }: ComboboxStateOptions) {
33
36
  const [value, setValue] = useControllableValue<string | null>(
@@ -51,7 +54,20 @@ export function useComboboxState({
51
54
  [controlledInputValue, onInputChange],
52
55
  )
53
56
 
54
- const [open, setOpen] = useState(false)
57
+ const [open, setOpenState] = useState(false)
58
+ // The latest open, written before the state setter so two calls in one
59
+ // event see each other; onOpenChange fires only when it changes.
60
+ const openRef = useRef(false)
61
+ const onOpenChangeRef = useRef(onOpenChange)
62
+ useEffect(() => {
63
+ onOpenChangeRef.current = onOpenChange
64
+ })
65
+ const setOpen = useCallback((next: boolean) => {
66
+ if (openRef.current === next) return
67
+ openRef.current = next
68
+ setOpenState(next)
69
+ onOpenChangeRef.current?.(next)
70
+ }, [])
55
71
  const [labels, setLabels] = useState<Record<string, string>>(initialLabels ?? {})
56
72
 
57
73
  const registerLabel = useCallback((v: string, label: string) => {
@@ -65,7 +81,7 @@ export function useComboboxState({
65
81
  setInputValue(labels[itemValue] ?? itemValue)
66
82
  setOpen(false)
67
83
  },
68
- [setValue, labels, setInputValue],
84
+ [setValue, labels, setInputValue, setOpen],
69
85
  )
70
86
 
71
87
  // Reconcile input text with the committed value once the popup closes.
@@ -9,20 +9,50 @@ import {matchesQuery, useComboboxState, type ComboboxStateOptions} from './combo
9
9
  export function useComboboxRoot(options: ComboboxStateOptions) {
10
10
  const {value, inputValue, setInputValue, open, setOpen, labels, registerLabel, chooseValue} =
11
11
  useComboboxState(options)
12
- const [highlightedId, setHighlightedId] = useState<string | null>(null)
12
+ const [highlightedId, setHighlightedState] = useState<string | null>(null)
13
+ const highlightedRef = useRef<string | null>(null)
14
+ const setHighlightedId = useCallback((id: string | null) => {
15
+ highlightedRef.current = id
16
+ setHighlightedState(id)
17
+ }, [])
18
+ // The list opens with every option while the field shows the chosen
19
+ // option's label; the label only filters once it is edited.
20
+ const query = value && inputValue === labels[value] ? '' : inputValue
21
+ const blurTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
22
+ const refocusAfterChooseRef = useRef(false)
23
+ // The list's items mount after the open, so the highlight they should take
24
+ // is settled as they register.
25
+ const pendingHighlightRef = useRef(false)
26
+ // Synced after each commit: an option that mounts in the same commit as a
27
+ // value change would read the old one. Unreachable today (the options mount
28
+ // in the commit after the open); a controlled value changed while the list is
29
+ // open would need the highlight settled from an effect on `value`.
30
+ const valueRef = useRef(value)
31
+ useEffect(() => {
32
+ valueRef.current = value
33
+ })
13
34
  const listboxId = useId()
14
35
  const rootRef = useRef<HTMLDivElement>(null)
15
36
  const inputRef = useRef<HTMLInputElement | null>(null)
16
37
  const itemsRef = useRef(new Map<string, {value: string; element: HTMLElement}>())
17
38
  const orderRef = useRef<string[]>([])
18
39
 
40
+ const cancelBlurClose = useCallback(() => {
41
+ if (blurTimerRef.current !== null) clearTimeout(blurTimerRef.current)
42
+ blurTimerRef.current = null
43
+ }, [])
44
+
45
+ useEffect(() => cancelBlurClose, [cancelBlurClose])
46
+
19
47
  const close = useCallback(() => {
20
48
  setOpen(false)
49
+ pendingHighlightRef.current = false
21
50
  setHighlightedId(null)
22
- }, [setOpen])
51
+ }, [setOpen, setHighlightedId])
23
52
 
24
53
  const openPopup = useCallback(() => {
25
54
  setOpen(true)
55
+ pendingHighlightRef.current = true
26
56
  // Highlight the selected item or first visible item
27
57
  const items = itemsRef.current
28
58
  const order = orderRef.current
@@ -35,43 +65,80 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
35
65
  }
36
66
  }
37
67
  setHighlightedId(found ?? (order.length > 0 ? order[0] : null))
38
- }, [value, setOpen])
68
+ if (found) pendingHighlightRef.current = false
69
+ }, [value, setOpen, setHighlightedId])
70
+
71
+ // Focus opens the list, except the refocus that follows choosing an option.
72
+ const onInputFocus = useCallback(() => {
73
+ cancelBlurClose()
74
+ if (refocusAfterChooseRef.current) return
75
+ openPopup()
76
+ }, [cancelBlurClose, openPopup])
77
+
78
+ // Delay close to allow an item click to register.
79
+ const onInputBlur = useCallback(() => {
80
+ cancelBlurClose()
81
+ blurTimerRef.current = setTimeout(() => {
82
+ blurTimerRef.current = null
83
+ close()
84
+ }, 150)
85
+ }, [cancelBlurClose, close])
39
86
 
40
87
  // Choosing keeps focus in the input (the web's editable combobox).
41
88
  const selectValue = useCallback(
42
89
  (itemValue: string) => {
90
+ cancelBlurClose()
43
91
  chooseValue(itemValue)
44
92
  setHighlightedId(null)
45
- inputRef.current?.focus()
93
+ refocusAfterChooseRef.current = true
94
+ try {
95
+ inputRef.current?.focus()
96
+ } finally {
97
+ refocusAfterChooseRef.current = false
98
+ }
46
99
  },
47
- [chooseValue],
100
+ [chooseValue, cancelBlurClose, setHighlightedId],
48
101
  )
49
102
 
50
- const registerItem = useCallback((id: string, itemValue: string, element: HTMLElement) => {
51
- itemsRef.current.set(id, {value: itemValue, element})
52
- const map = itemsRef.current
53
- const ids = [...map.keys()]
54
- ids.sort((a, b) => {
55
- const elA = map.get(a)?.element
56
- const elB = map.get(b)?.element
57
- if (!elA || !elB) return 0
58
- return elA.compareDocumentPosition(elB) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1
59
- })
60
- orderRef.current = ids
61
- return () => {
62
- itemsRef.current.delete(id)
63
- orderRef.current = orderRef.current.filter((i) => i !== id)
64
- }
65
- }, [])
103
+ const registerItem = useCallback(
104
+ (id: string, itemValue: string, element: HTMLElement) => {
105
+ itemsRef.current.set(id, {value: itemValue, element})
106
+ // holds-until: a few hundred options. Each (re-)registration sorts every
107
+ // registered id by document position, and a filter change re-registers
108
+ // each option it shows again; past that, sort once per pass instead.
109
+ const map = itemsRef.current
110
+ const ids = [...map.keys()]
111
+ ids.sort((a, b) => {
112
+ const elA = map.get(a)?.element
113
+ const elB = map.get(b)?.element
114
+ if (!elA || !elB) return 0
115
+ return elA.compareDocumentPosition(elB) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1
116
+ })
117
+ orderRef.current = ids
118
+ if (pendingHighlightRef.current) {
119
+ if (itemValue === valueRef.current) {
120
+ pendingHighlightRef.current = false
121
+ setHighlightedId(id)
122
+ } else if (highlightedRef.current === null) {
123
+ setHighlightedId(ids[0] ?? null)
124
+ }
125
+ }
126
+ return () => {
127
+ itemsRef.current.delete(id)
128
+ orderRef.current = orderRef.current.filter((i) => i !== id)
129
+ }
130
+ },
131
+ [setHighlightedId],
132
+ )
66
133
 
67
134
  // Get visible items (those matching the filter)
68
135
  const getVisibleOrder = useCallback(() => {
69
- if (!inputValue) return orderRef.current
136
+ if (!query) return orderRef.current
70
137
  return orderRef.current.filter((id) => {
71
138
  const item = itemsRef.current.get(id)
72
- return item ? matchesQuery(labels[item.value] ?? item.value, inputValue) : false
139
+ return item ? matchesQuery(labels[item.value] ?? item.value, query) : false
73
140
  })
74
- }, [inputValue, labels])
141
+ }, [query, labels])
75
142
 
76
143
  // Keyboard navigation
77
144
  useEffect(() => {
@@ -79,24 +146,23 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
79
146
  if (!root || !open) return
80
147
 
81
148
  function handleKeyDown(e: KeyboardEvent) {
149
+ pendingHighlightRef.current = false
82
150
  const visibleOrder = getVisibleOrder()
83
151
  if (visibleOrder.length === 0 && e.key !== 'Escape' && e.key !== 'Tab') return
84
152
 
85
153
  switch (e.key) {
86
154
  case 'ArrowDown': {
87
155
  e.preventDefault()
88
- setHighlightedId((prev) => {
89
- const idx = prev ? visibleOrder.indexOf(prev) : -1
90
- return visibleOrder[(idx + 1) % visibleOrder.length] ?? null
91
- })
156
+ const idx = highlightedRef.current ? visibleOrder.indexOf(highlightedRef.current) : -1
157
+ setHighlightedId(visibleOrder[(idx + 1) % visibleOrder.length] ?? null)
92
158
  break
93
159
  }
94
160
  case 'ArrowUp': {
95
161
  e.preventDefault()
96
- setHighlightedId((prev) => {
97
- const idx = prev ? visibleOrder.indexOf(prev) : 0
98
- return visibleOrder[(idx - 1 + visibleOrder.length) % visibleOrder.length] ?? null
99
- })
162
+ const idx = highlightedRef.current ? visibleOrder.indexOf(highlightedRef.current) : 0
163
+ setHighlightedId(
164
+ visibleOrder[(idx - 1 + visibleOrder.length) % visibleOrder.length] ?? null,
165
+ )
100
166
  break
101
167
  }
102
168
  case 'Home': {
@@ -111,16 +177,17 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
111
177
  }
112
178
  case 'Enter': {
113
179
  e.preventDefault()
114
- setHighlightedId((prev) => {
115
- if (prev) {
116
- const item = itemsRef.current.get(prev)
117
- if (item) selectValue(item.value)
118
- } else if (!inputValue.trim()) {
119
- // Empty input + Enter = clear selection (reset to "All")
120
- selectValue('')
121
- }
122
- return prev
123
- })
180
+ const highlighted = highlightedRef.current
181
+ if (highlighted) {
182
+ // A highlighted option the filter has hidden is never committed.
183
+ const highlightedOption = visibleOrder.includes(highlighted)
184
+ ? itemsRef.current.get(highlighted)
185
+ : undefined
186
+ if (highlightedOption) selectValue(highlightedOption.value)
187
+ } else if (!inputValue.trim()) {
188
+ // Empty input + Enter = clear selection (reset to "All")
189
+ selectValue('')
190
+ }
124
191
  break
125
192
  }
126
193
  case 'Escape': {
@@ -138,14 +205,22 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
138
205
 
139
206
  root.addEventListener('keydown', handleKeyDown)
140
207
  return () => root.removeEventListener('keydown', handleKeyDown)
141
- }, [open, close, selectValue, getVisibleOrder])
208
+ }, [open, close, selectValue, getVisibleOrder, inputValue, setHighlightedId])
142
209
 
143
- // Scroll highlighted item into view
210
+ // Keep the highlighted item in view, also when the filter widens the list
211
+ // above it (typing the chosen label back shows every option again). An
212
+ // option the filter hides has unregistered (its effects ran before this
213
+ // one): drop the highlight, so aria-activedescendant never names a missing
214
+ // element and Enter commits nothing hidden.
144
215
  useEffect(() => {
145
216
  if (!highlightedId) return
146
- const item = itemsRef.current.get(highlightedId)
147
- item?.element.scrollIntoView({block: 'nearest'})
148
- }, [highlightedId])
217
+ const highlightedOption = itemsRef.current.get(highlightedId)
218
+ if (!highlightedOption) {
219
+ setHighlightedId(null)
220
+ return
221
+ }
222
+ highlightedOption.element.scrollIntoView({block: 'nearest'})
223
+ }, [highlightedId, query, setHighlightedId])
149
224
 
150
225
  const ctx: ComboboxContextValue = useMemo(
151
226
  () => ({
@@ -155,7 +230,10 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
155
230
  value,
156
231
  setValue: selectValue,
157
232
  inputValue,
233
+ query,
158
234
  setInputValue,
235
+ onInputFocus,
236
+ onInputBlur,
159
237
  labels,
160
238
  registerLabel,
161
239
  listboxId,
@@ -172,7 +250,10 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
172
250
  value,
173
251
  selectValue,
174
252
  inputValue,
253
+ query,
175
254
  setInputValue,
255
+ onInputFocus,
256
+ onInputBlur,
176
257
  labels,
177
258
  registerLabel,
178
259
  listboxId,
@@ -24,6 +24,8 @@ export const styles = css.create({
24
24
  transitionTimingFunction: easing.standard,
25
25
  // Matches Button: the 44px target under a coarse pointer (5.6).
26
26
  minWidth: {default: null, '@media (pointer: coarse)': sizes.touchTarget},
27
+ // As Button: the min-width above would let a crowded row squeeze the label.
28
+ flexShrink: 0,
27
29
  },
28
30
  sizeDefault: {
29
31
  paddingTop: spacing.sm,