@sanity/ui 2.9.0-canary.5 → 2.9.0-canary.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/ui",
3
- "version": "2.9.0-canary.5",
3
+ "version": "2.9.0-canary.6",
4
4
  "keywords": [
5
5
  "sanity",
6
6
  "ui",
@@ -121,7 +121,7 @@ export const Tooltip = forwardRef(function Tooltip(
121
121
  const fallbackPlacements = useArrayProp(fallbackPlacementsProp)
122
122
  const ref = useRef<HTMLDivElement | null>(null)
123
123
  const [referenceElement, setReferenceElement] = useState<HTMLElement | null>(null)
124
- const arrowRef = useRef<HTMLDivElement | null>(null)
124
+ const [arrowElement, setArrowElement] = useState<HTMLDivElement | null>(null)
125
125
  const rootBoundary: RootBoundary = 'viewport'
126
126
  const [tooltipMaxWidth, setTooltipMaxWidth] = useState(0)
127
127
 
@@ -158,7 +158,7 @@ export const Tooltip = forwardRef(function Tooltip(
158
158
 
159
159
  // Place arrow
160
160
  if (arrowProp) {
161
- ret.push(arrow({element: arrowRef, padding: DEFAULT_TOOLTIP_PADDING}))
161
+ ret.push(arrow({element: arrowElement, padding: DEFAULT_TOOLTIP_PADDING}))
162
162
  }
163
163
 
164
164
  // Determine the origin to scale from.
@@ -168,7 +168,7 @@ export const Tooltip = forwardRef(function Tooltip(
168
168
  }
169
169
 
170
170
  return ret
171
- }, [animate, arrowProp, boundaryElement, fallbackPlacements])
171
+ }, [animate, arrowElement, arrowProp, boundaryElement, fallbackPlacements])
172
172
 
173
173
  const {floatingStyles, placement, middlewareData, refs, update} = useFloating({
174
174
  middleware,
@@ -226,42 +226,42 @@ export const Tooltip = forwardRef(function Tooltip(
226
226
  handleIsOpenChange(false)
227
227
  childProp?.props?.onBlur?.(e)
228
228
  },
229
- [childProp?.props, handleIsOpenChange],
229
+ [childProp, handleIsOpenChange],
230
230
  )
231
231
  const handleClick = useCallback(
232
232
  (e: MouseEvent) => {
233
233
  handleIsOpenChange(false, true)
234
234
  childProp?.props.onClick?.(e)
235
235
  },
236
- [childProp?.props, handleIsOpenChange],
236
+ [childProp, handleIsOpenChange],
237
237
  )
238
238
  const handleContextMenu = useCallback(
239
239
  (e: MouseEvent) => {
240
240
  handleIsOpenChange(false, true)
241
241
  childProp?.props.onContextMenu?.(e)
242
242
  },
243
- [childProp?.props, handleIsOpenChange],
243
+ [childProp, handleIsOpenChange],
244
244
  )
245
245
  const handleFocus = useCallback(
246
246
  (e: FocusEvent) => {
247
247
  handleIsOpenChange(true)
248
248
  childProp?.props?.onFocus?.(e)
249
249
  },
250
- [childProp?.props, handleIsOpenChange],
250
+ [childProp, handleIsOpenChange],
251
251
  )
252
252
  const handleMouseEnter = useCallback(
253
253
  (e: MouseEvent) => {
254
254
  handleIsOpenChange(true)
255
255
  childProp?.props?.onMouseEnter?.(e)
256
256
  },
257
- [childProp?.props, handleIsOpenChange],
257
+ [childProp, handleIsOpenChange],
258
258
  )
259
259
  const handleMouseLeave = useCallback(
260
260
  (e: MouseEvent) => {
261
261
  handleIsOpenChange(false)
262
262
  childProp?.props?.onMouseLeave?.(e)
263
263
  },
264
- [childProp?.props, handleIsOpenChange],
264
+ [childProp, handleIsOpenChange],
265
265
  )
266
266
 
267
267
  // Handle closing the tooltip when the mouse leaves the referenceElement
@@ -312,7 +312,7 @@ export const Tooltip = forwardRef(function Tooltip(
312
312
 
313
313
  const setArrow = useCallback(
314
314
  (arrowEl: HTMLDivElement | null) => {
315
- arrowRef.current = arrowEl
315
+ setArrowElement(arrowEl)
316
316
  update()
317
317
  },
318
318
  [update],
@@ -327,9 +327,9 @@ export const Tooltip = forwardRef(function Tooltip(
327
327
  )
328
328
 
329
329
  const childRef = useRef<HTMLElement | null>(null)
330
+ const [childElement, setChildElement] = useState<HTMLElement | null>(null)
330
331
 
331
- // Merge refs so that any ref we are overriding is called as well
332
- useImperativeHandle((childProp as any)?.ref, () => childRef.current)
332
+ useImperativeHandle(getElementRef(childProp as any), () => childElement, [childElement])
333
333
 
334
334
  const child = useMemo(() => {
335
335
  if (!childProp) return null
@@ -341,7 +341,7 @@ export const Tooltip = forwardRef(function Tooltip(
341
341
  onMouseLeave: handleMouseLeave,
342
342
  onClick: handleClick,
343
343
  onContextMenu: handleContextMenu,
344
- ref: childRef,
344
+ ref: setChildElement,
345
345
  })
346
346
  }, [
347
347
  childProp,
@@ -465,3 +465,30 @@ function useCloseOnMouseLeave({
465
465
  return () => window.removeEventListener('mousemove', handleMouseMove)
466
466
  }, [onMouseMove, showTooltip])
467
467
  }
468
+
469
+ // Before React 19 accessing `element.props.ref` will throw a warning and suggest using `element.ref`
470
+ // After React 19 accessing `element.ref` does the opposite.
471
+ // https://github.com/facebook/react/pull/28348
472
+ //
473
+ // Access the ref using the method that doesn't yield a warning.
474
+ function getElementRef(element?: React.ReactElement) {
475
+ if (!element) return null
476
+ // React <=18 in DEV
477
+ let getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get
478
+ let mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning
479
+
480
+ if (mayWarn) {
481
+ return (element as any).ref
482
+ }
483
+
484
+ // React 19 in DEV
485
+ getter = Object.getOwnPropertyDescriptor(element, 'ref')?.get
486
+ mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning
487
+
488
+ if (mayWarn) {
489
+ return element.props.ref
490
+ }
491
+
492
+ // Not DEV
493
+ return element.props.ref || (element as any).ref
494
+ }