@telus-uds/components-base 3.30.0 → 3.32.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 (45) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/lib/cjs/Autocomplete/Autocomplete.js +13 -1
  3. package/lib/cjs/Autocomplete/constants.js +1 -1
  4. package/lib/cjs/Button/ButtonGroup.js +91 -23
  5. package/lib/cjs/Card/CardBase.js +75 -47
  6. package/lib/cjs/ColourToggle/ColourBubble.js +65 -12
  7. package/lib/cjs/ColourToggle/ColourToggle.js +40 -11
  8. package/lib/cjs/ColourToggle/constants.js +15 -0
  9. package/lib/cjs/ColourToggle/dictionary.js +15 -0
  10. package/lib/cjs/Listbox/ListboxOverlay.js +7 -1
  11. package/lib/cjs/MultiSelectFilter/ModalOverlay.js +7 -1
  12. package/lib/cjs/MultiSelectFilter/MultiSelectFilter.js +2 -28
  13. package/lib/cjs/TextInput/TextInputBase.js +2 -1
  14. package/lib/cjs/Validator/Validator.js +5 -1
  15. package/lib/cjs/utils/useOverlaidPosition.js +83 -42
  16. package/lib/esm/Autocomplete/Autocomplete.js +13 -1
  17. package/lib/esm/Autocomplete/constants.js +1 -1
  18. package/lib/esm/Button/ButtonGroup.js +91 -23
  19. package/lib/esm/Card/CardBase.js +75 -47
  20. package/lib/esm/ColourToggle/ColourBubble.js +66 -13
  21. package/lib/esm/ColourToggle/ColourToggle.js +41 -12
  22. package/lib/esm/ColourToggle/constants.js +8 -0
  23. package/lib/esm/ColourToggle/dictionary.js +9 -0
  24. package/lib/esm/Listbox/ListboxOverlay.js +7 -1
  25. package/lib/esm/MultiSelectFilter/ModalOverlay.js +8 -2
  26. package/lib/esm/MultiSelectFilter/MultiSelectFilter.js +2 -28
  27. package/lib/esm/TextInput/TextInputBase.js +2 -1
  28. package/lib/esm/Validator/Validator.js +5 -1
  29. package/lib/esm/utils/useOverlaidPosition.js +83 -42
  30. package/lib/package.json +2 -2
  31. package/package.json +2 -2
  32. package/src/Autocomplete/Autocomplete.jsx +11 -2
  33. package/src/Autocomplete/constants.js +1 -1
  34. package/src/Button/ButtonGroup.jsx +93 -24
  35. package/src/Card/CardBase.jsx +94 -75
  36. package/src/ColourToggle/ColourBubble.jsx +62 -7
  37. package/src/ColourToggle/ColourToggle.jsx +50 -10
  38. package/src/ColourToggle/constants.js +10 -0
  39. package/src/ColourToggle/dictionary.js +6 -0
  40. package/src/Listbox/ListboxOverlay.jsx +6 -2
  41. package/src/MultiSelectFilter/ModalOverlay.jsx +9 -3
  42. package/src/MultiSelectFilter/MultiSelectFilter.jsx +1 -31
  43. package/src/TextInput/TextInputBase.jsx +2 -1
  44. package/src/Validator/Validator.jsx +5 -1
  45. package/src/utils/useOverlaidPosition.js +84 -34
@@ -54,11 +54,6 @@ function getOverlaidPosition({
54
54
  offsets = {},
55
55
  align
56
56
  }) {
57
- // Web-only: this will be difficult to mimic on native because there's no global scroll position.
58
- // TODO: wire something in e.g. a scroll ref accessible from a provider included in Allium provider
59
- // that can be passed to the appropriate ScrollView?
60
- const { scrollX = 0, scrollY = 0 } = typeof window === 'object' ? window : {}
61
-
62
57
  // Will have top, bottom, left and/or right offsets depending on `align`
63
58
  const positioning = {}
64
59
 
@@ -68,40 +63,44 @@ function getOverlaidPosition({
68
63
  if (align.top)
69
64
  positioning.top = getPosition({
70
65
  edge: getEdgeType(align, 'top'),
71
- fromEdge: sourceLayout.y + scrollY + verticalOffset,
66
+ fromEdge: sourceLayout.y + verticalOffset,
72
67
  sourceSize: sourceLayout.height
73
68
  })
74
69
  if (align.middle)
75
70
  positioning.top = getPosition({
76
71
  edge: getEdgeType(align, 'middle'),
77
- fromEdge: sourceLayout.y + scrollY + verticalOffset - targetDimensions.height / 2,
78
- sourceSize: sourceLayout.height
79
- })
80
- if (align.bottom)
81
- positioning.bottom = getPosition({
82
- edge: getEdgeType(align, 'bottom'),
83
- fromEdge:
84
- windowDimensions.height - (sourceLayout.y + scrollY + sourceLayout.height - verticalOffset),
72
+ fromEdge: sourceLayout.y + verticalOffset - targetDimensions.height / 2,
85
73
  sourceSize: sourceLayout.height
86
74
  })
75
+ if (align.bottom) {
76
+ if (Platform.OS !== 'web') {
77
+ // On native, position:absolute is parent-relative, so use negative top offset instead of window-based bottom math.
78
+ positioning.top = sourceLayout.y - targetDimensions.height - verticalOffset
79
+ } else {
80
+ positioning.bottom = getPosition({
81
+ edge: getEdgeType(align, 'bottom'),
82
+ fromEdge: windowDimensions.height - (sourceLayout.y + sourceLayout.height - verticalOffset),
83
+ sourceSize: sourceLayout.height
84
+ })
85
+ }
86
+ }
87
87
 
88
88
  if (align.left)
89
89
  positioning.left = getPosition({
90
90
  edge: getEdgeType(align, 'left'),
91
- fromEdge: sourceLayout.x + scrollX + horizontalOffset,
91
+ fromEdge: sourceLayout.x + horizontalOffset,
92
92
  sourceSize: sourceLayout.width
93
93
  })
94
94
  if (align.center)
95
95
  positioning.left = getPosition({
96
96
  edge: getEdgeType(align, 'center'),
97
- fromEdge: sourceLayout.x + scrollX + horizontalOffset - targetDimensions.width / 2,
97
+ fromEdge: sourceLayout.x + horizontalOffset - targetDimensions.width / 2,
98
98
  sourceSize: sourceLayout.width
99
99
  })
100
100
  if (align.right)
101
101
  positioning.right = getPosition({
102
102
  edge: getEdgeType(align, 'right'),
103
- fromEdge:
104
- windowDimensions.width - (sourceLayout.x + scrollX + sourceLayout.width - horizontalOffset),
103
+ fromEdge: windowDimensions.width - (sourceLayout.x + sourceLayout.width - horizontalOffset),
105
104
  sourceSize: sourceLayout.width
106
105
  })
107
106
 
@@ -149,6 +148,34 @@ const useOverlaidPosition = ({
149
148
 
150
149
  const [windowDimensions, setWindowDimensions] = useState(null)
151
150
 
151
+ const hasRemeasuredRef = useRef(false)
152
+
153
+ const applySourceLayout = useCallback((dims, x, y, width, height) => {
154
+ setWindowDimensions(dims)
155
+ setSourceLayout({ x, y, width, height })
156
+ }, [])
157
+
158
+ const measureSourceOnWeb = useCallback(
159
+ (windowDims) => {
160
+ const el = sourceRef.current
161
+ if (!el) return
162
+ const domNode = el._nativeTag ?? el
163
+ const dims = windowDims ?? Dimensions.get('window')
164
+ const rect =
165
+ typeof domNode.getBoundingClientRect === 'function' ? domNode.getBoundingClientRect() : null
166
+ if (rect) {
167
+ const x = rect.left + (typeof window.scrollX === 'number' ? window.scrollX : 0)
168
+ const y = rect.top + (typeof window.scrollY === 'number' ? window.scrollY : 0)
169
+ applySourceLayout(dims, x, y, rect.width, rect.height)
170
+ } else {
171
+ el.measureInWindow((mx, my, width, height) => {
172
+ applySourceLayout(dims, mx, my, width, height)
173
+ })
174
+ }
175
+ },
176
+ [applySourceLayout]
177
+ )
178
+
152
179
  const onTargetLayout = useCallback(
153
180
  ({
154
181
  nativeEvent: {
@@ -173,12 +200,14 @@ const useOverlaidPosition = ({
173
200
  const readyToShow = Boolean(isShown && sourceRef.current)
174
201
  useEffect(() => {
175
202
  const handleDimensionsChange = ({ window }) => {
176
- const measurementFunction = Platform.OS === 'web' ? 'measureInWindow' : 'measure'
177
-
178
- sourceRef.current?.[measurementFunction]((x, y, width, height) => {
179
- setWindowDimensions(window)
180
- setSourceLayout({ x, y, width, height })
181
- })
203
+ if (Platform.OS === 'web') {
204
+ measureSourceOnWeb(window)
205
+ } else {
206
+ sourceRef.current?.measure((x, y, width, height) => {
207
+ setWindowDimensions(window)
208
+ setSourceLayout({ x, y, width, height })
209
+ })
210
+ }
182
211
  }
183
212
 
184
213
  let subscription
@@ -192,6 +221,7 @@ const useOverlaidPosition = ({
192
221
  }
193
222
  setSourceLayout(null)
194
223
  setTargetDimensions(null)
224
+ hasRemeasuredRef.current = false
195
225
  }
196
226
 
197
227
  if (readyToShow) {
@@ -202,19 +232,29 @@ const useOverlaidPosition = ({
202
232
  }
203
233
 
204
234
  return unsubscribe
205
- }, [readyToShow])
235
+ }, [readyToShow, measureSourceOnWeb])
236
+
237
+ // Re-measure source when targetDimensions first becomes available.
238
+ // Without this, there is a race condition: getBoundingClientRect() resolves faster than
239
+ // measureInWindow used to, so sourceLayout may be set before the dropdown has rendered
240
+ // and reported its dimensions via onTargetLayout. When targetDimensions finally arrives,
241
+ // sourceLayout is stale (no scroll has occurred to trigger handleScroll).
242
+ // Setting hasRemeasuredRef.current=true here also prevents the blink: isReady (on web)
243
+ // won't become true until this effect completes, guaranteeing the dropdown is always shown
244
+ // at its final correct position without an intermediate incorrect-position frame.
245
+ useEffect(() => {
246
+ if (!isShown || !sourceRef.current || !targetDimensions || Platform.OS !== 'web') return
247
+
248
+ measureSourceOnWeb()
249
+ hasRemeasuredRef.current = true
250
+ }, [targetDimensions, isShown, measureSourceOnWeb])
206
251
 
207
252
  useEffect(() => {
208
253
  if (Platform.OS !== 'web') {
209
254
  return undefined
210
255
  }
211
256
 
212
- const handleScroll = debounce(() => {
213
- sourceRef.current?.measureInWindow((x, y, width, height) => {
214
- setWindowDimensions(window)
215
- setSourceLayout({ x, y, width, height })
216
- })
217
- }, DEBOUNCE_DELAY)
257
+ const handleScroll = debounce(measureSourceOnWeb, DEBOUNCE_DELAY)
218
258
 
219
259
  window.addEventListener('scroll', handleScroll)
220
260
 
@@ -222,9 +262,19 @@ const useOverlaidPosition = ({
222
262
  window.removeEventListener('scroll', handleScroll)
223
263
  handleScroll.cancel()
224
264
  }
225
- }, [sourceRef])
265
+ }, [measureSourceOnWeb])
226
266
 
227
- const isReady = Boolean(isShown && sourceLayout && windowDimensions && targetDimensions)
267
+ // On web, require hasRemeasuredRef to be true before declaring isReady. This ensures
268
+ // the dropdown is never shown with an intermediate stale position (the blink).
269
+ // On native, hasRemeasuredRef stays false (the web-only effect never runs), so we skip
270
+ // that check to preserve the original native behaviour.
271
+ const isReady = Boolean(
272
+ isShown &&
273
+ sourceLayout &&
274
+ windowDimensions &&
275
+ targetDimensions &&
276
+ (Platform.OS !== 'web' || hasRemeasuredRef.current)
277
+ )
228
278
 
229
279
  const overlaidPosition = isReady
230
280
  ? getOverlaidPosition({
@@ -234,7 +284,7 @@ const useOverlaidPosition = ({
234
284
  offsets,
235
285
  align
236
286
  })
237
- : {}
287
+ : { top: 0, left: 0 }
238
288
 
239
289
  return {
240
290
  overlaidPosition,