@sanity/ui 3.0.11 → 3.0.12-canary.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.
@@ -98,22 +98,25 @@ export const Tooltip = forwardRef(function Tooltip(
98
98
  const {
99
99
  animate: _animate = false,
100
100
  arrow: arrowProp = false,
101
- boundaryElement = boundaryElementContext?.element,
101
+ boundaryElement: _boundaryElement,
102
102
  children: childProp,
103
103
  content,
104
104
  disabled,
105
- fallbackPlacements: fallbackPlacementsProp = props.fallbackPlacements ??
106
- DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom'],
105
+ fallbackPlacements: _fallbackPlacementsProp,
107
106
  padding = 2,
108
107
  placement: placementProp = 'bottom',
109
108
  portal: portalProp,
110
109
  radius = 2,
111
110
  scheme,
112
111
  shadow = 2,
113
- zOffset = layer.tooltip.zOffset,
112
+ zOffset: _zOffset,
114
113
  delay,
115
114
  ...restProps
116
115
  } = props
116
+ const boundaryElement = _boundaryElement ?? boundaryElementContext?.element
117
+ const fallbackPlacementsProp =
118
+ _fallbackPlacementsProp ?? DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom']
119
+ const zOffset = _zOffset ?? layer.tooltip.zOffset
117
120
  const prefersReducedMotion = usePrefersReducedMotion()
118
121
  const animate = prefersReducedMotion ? false : _animate
119
122
  const fallbackPlacements = useArrayProp(fallbackPlacementsProp)
@@ -129,44 +132,14 @@ export const Tooltip = forwardRef(function Tooltip(
129
132
  const portalElement =
130
133
  typeof portalProp === 'string' ? portal.elements?.[portalProp] || null : portal.element
131
134
 
132
- const middleware = useMemo(() => {
133
- const ret: Middleware[] = []
134
-
135
- // Flip the floating element when leaving the boundary box
136
- ret.push(
137
- flip({
138
- boundary: boundaryElement || undefined,
139
- fallbackPlacements,
140
- padding: DEFAULT_TOOLTIP_PADDING,
141
- rootBoundary,
142
- }),
143
- )
144
-
145
- // Define distance between reference and floating element
146
- ret.push(offset({mainAxis: DEFAULT_TOOLTIP_DISTANCE}))
147
-
148
- // Shift the tooltip so its sits with the boundary element
149
- ret.push(
150
- shift({
151
- boundary: boundaryElement || undefined,
152
- rootBoundary,
153
- padding: DEFAULT_TOOLTIP_PADDING,
154
- }),
155
- )
156
-
157
- // Place arrow
158
- if (arrowProp) {
159
- ret.push(arrow({element: arrowRef, padding: DEFAULT_TOOLTIP_PADDING}))
160
- }
161
-
162
- // Determine the origin to scale from.
163
- // Must be placed after `@sanity/ui/size` and `shift` middleware.
164
- if (animate) {
165
- ret.push(origin)
166
- }
167
-
168
- return ret
169
- }, [animate, arrowProp, boundaryElement, fallbackPlacements])
135
+ const middleware = useMiddleware({
136
+ animate,
137
+ arrowProp,
138
+ arrowRef,
139
+ boundaryElement,
140
+ fallbackPlacements,
141
+ rootBoundary,
142
+ })
170
143
 
171
144
  const {floatingStyles, placement, middlewareData, refs, update} = useFloating({
172
145
  middleware,
@@ -417,6 +390,61 @@ export const Tooltip = forwardRef(function Tooltip(
417
390
  })
418
391
  Tooltip.displayName = 'ForwardRef(Tooltip)'
419
392
 
393
+ function useMiddleware({
394
+ animate,
395
+ arrowProp,
396
+ arrowRef,
397
+ boundaryElement,
398
+ fallbackPlacements,
399
+ rootBoundary,
400
+ }: {
401
+ animate: boolean
402
+ arrowProp: boolean
403
+ arrowRef: React.RefObject<HTMLDivElement | null>
404
+ boundaryElement: HTMLElement | null
405
+ fallbackPlacements: Placement[]
406
+ rootBoundary: RootBoundary
407
+ }) {
408
+ return useMemo(() => {
409
+ const ret: Middleware[] = []
410
+
411
+ // Flip the floating element when leaving the boundary box
412
+ ret.push(
413
+ flip({
414
+ boundary: boundaryElement || undefined,
415
+ fallbackPlacements,
416
+ padding: DEFAULT_TOOLTIP_PADDING,
417
+ rootBoundary,
418
+ }),
419
+ )
420
+
421
+ // Define distance between reference and floating element
422
+ ret.push(offset({mainAxis: DEFAULT_TOOLTIP_DISTANCE}))
423
+
424
+ // Shift the tooltip so its sits with the boundary element
425
+ ret.push(
426
+ shift({
427
+ boundary: boundaryElement || undefined,
428
+ rootBoundary,
429
+ padding: DEFAULT_TOOLTIP_PADDING,
430
+ }),
431
+ )
432
+
433
+ // Place arrow
434
+ if (arrowProp) {
435
+ ret.push(arrow({element: arrowRef, padding: DEFAULT_TOOLTIP_PADDING}))
436
+ }
437
+
438
+ // Determine the origin to scale from.
439
+ // Must be placed after `@sanity/ui/size` and `shift` middleware.
440
+ if (animate) {
441
+ ret.push(origin)
442
+ }
443
+
444
+ return ret
445
+ }, [animate, arrowProp, arrowRef, boundaryElement, fallbackPlacements, rootBoundary])
446
+ }
447
+
420
448
  /**
421
449
  * As `useEffectEvent` should never be passed to other components or hooks, this custom hook groups together the `useEffectEvent` and the `useEffect` hook using it.
422
450
  * @see https://19.react.dev/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events:~:text=Never%20pass%20them%20to%20other%20components%20or%20Hooks
@@ -1,4 +1,4 @@
1
- import {forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react'
1
+ import {forwardRef, useEffect, useImperativeHandle, useRef, useState} from 'react'
2
2
  import {styled} from 'styled-components'
3
3
 
4
4
  import {_isScrollable} from '../../helpers'
@@ -131,31 +131,20 @@ export const VirtualList = forwardRef(function VirtualList(
131
131
  onChange({fromIndex, gap: space[gap], itemHeight, scrollHeight, scrollTop, toIndex})
132
132
  }, [fromIndex, gap, itemHeight, onChange, scrollHeight, scrollTop, space, toIndex])
133
133
 
134
- const children = useMemo(() => {
135
- if (!renderItem || items.length === 0) return null
136
-
137
- if (itemHeight === -1) {
138
- return [<ItemWrapper key={0}>{renderItem(items[0])}</ItemWrapper>]
139
- }
140
-
141
- return items.slice(fromIndex, toIndex).map((item, _itemIndex) => {
142
- const itemIndex = fromIndex + _itemIndex
143
- const node = renderItem(item)
144
- const key = getItemKey ? getItemKey(item, itemIndex) : itemIndex
145
-
146
- return (
147
- <ItemWrapper key={key} style={{top: itemIndex * (itemHeight + space[gap])}}>
148
- {node}
149
- </ItemWrapper>
150
- )
151
- })
152
- }, [fromIndex, gap, getItemKey, itemHeight, items, renderItem, space, toIndex])
153
-
154
- const wrapperStyle = useMemo(() => ({height}), [height])
134
+ const children = useChildren({
135
+ fromIndex,
136
+ gap,
137
+ itemHeight,
138
+ space,
139
+ toIndex,
140
+ getItemKey,
141
+ items,
142
+ renderItem,
143
+ })
155
144
 
156
145
  return (
157
146
  <StyledVirtualList as={as} data-ui="VirtualList" {...restProps} ref={ref}>
158
- <div ref={wrapperRef} style={wrapperStyle}>
147
+ <div ref={wrapperRef} style={{height}}>
159
148
  {children}
160
149
  </div>
161
150
  </StyledVirtualList>
@@ -163,6 +152,42 @@ export const VirtualList = forwardRef(function VirtualList(
163
152
  })
164
153
  VirtualList.displayName = 'ForwardRef(VirtualList)'
165
154
 
155
+ function useChildren({
156
+ fromIndex,
157
+ gap,
158
+ getItemKey,
159
+ itemHeight,
160
+ items,
161
+ renderItem,
162
+ space,
163
+ toIndex,
164
+ }: Pick<VirtualListProps, 'getItemKey' | 'renderItem'> &
165
+ Required<Pick<VirtualListProps, 'items'>> & {
166
+ fromIndex: number
167
+ gap: number
168
+ itemHeight: number
169
+ space: number[]
170
+ toIndex: number
171
+ }) {
172
+ if (!renderItem || items.length === 0) return null
173
+
174
+ if (itemHeight === -1) {
175
+ return [<ItemWrapper key={0}>{renderItem(items[0])}</ItemWrapper>]
176
+ }
177
+
178
+ return items.slice(fromIndex, toIndex).map((item, _itemIndex) => {
179
+ const itemIndex = fromIndex + _itemIndex
180
+ const node = renderItem(item)
181
+ const key = getItemKey ? getItemKey(item, itemIndex) : itemIndex
182
+
183
+ return (
184
+ <ItemWrapper key={key} style={{top: itemIndex * (itemHeight + space[gap])}}>
185
+ {node}
186
+ </ItemWrapper>
187
+ )
188
+ })
189
+ }
190
+
166
191
  function findScrollable(parentNode: ParentNode | null) {
167
192
  let _scrollEl = parentNode
168
193