@sanity/ui 2.9.0-canary.0 → 2.9.0-canary.2

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.0",
3
+ "version": "2.9.0-canary.2",
4
4
  "keywords": [
5
5
  "sanity",
6
6
  "ui",
@@ -305,13 +305,15 @@ export const Dialog = forwardRef(function Dialog(
305
305
  onFocus,
306
306
  padding: paddingProp = 3,
307
307
  portal: portalProp,
308
- position: positionProp = dialog.position || 'fixed',
308
+ position: _positionProp,
309
309
  scheme,
310
310
  width: widthProp = 0,
311
- zOffset: zOffsetProp = dialog.zOffset || layer.dialog.zOffset,
311
+ zOffset: _zOffsetProp,
312
312
  animate: _animate = false,
313
313
  ...restProps
314
314
  } = props
315
+ const positionProp = _positionProp ?? (dialog.position || 'fixed')
316
+ const zOffsetProp = _zOffsetProp ?? (dialog.zOffset || layer.dialog.zOffset)
315
317
  const prefersReducedMotion = usePrefersReducedMotion()
316
318
  const animate = prefersReducedMotion ? false : _animate
317
319
  const portal = usePortal()
@@ -57,10 +57,12 @@ export const Menu = forwardRef(function Menu(
57
57
  originElement,
58
58
  padding = 1,
59
59
  registerElement,
60
- shouldFocus = (props.focusFirst && 'first') || (props.focusLast && 'last') || null,
60
+ shouldFocus: _shouldFocus,
61
61
  space = 1,
62
62
  ...restProps
63
63
  } = props
64
+ const shouldFocus =
65
+ _shouldFocus ?? ((props.focusFirst && 'first') || (props.focusLast && 'last') || null)
64
66
 
65
67
  const ref = useRef<HTMLDivElement | null>(null)
66
68
 
@@ -53,9 +53,10 @@ export function MenuGroup(
53
53
  onClickOutside,
54
54
  onEscape,
55
55
  onItemClick,
56
- onItemMouseEnter = menu.onMouseEnter,
56
+ onItemMouseEnter: _onItemMouseEnter,
57
57
  registerElement,
58
58
  } = menu
59
+ const onItemMouseEnter = _onItemMouseEnter ?? menu.onMouseEnter
59
60
  const [rootElement, setRootElement] = useState<HTMLButtonElement | HTMLDivElement | null>(null)
60
61
  const [open, setOpen] = useState(false)
61
62
  const [shouldFocus, setShouldFocus] = useState<'first' | 'last' | null>(null)
@@ -72,9 +72,11 @@ export const MenuItem = forwardRef(function MenuItem(
72
72
  activeElement,
73
73
  mount,
74
74
  onItemClick,
75
- onItemMouseEnter = menu.onMouseEnter,
76
- onItemMouseLeave = menu.onMouseLeave,
75
+ onItemMouseEnter: _onItemMouseEnter,
76
+ onItemMouseLeave: _onItemMouseLeave,
77
77
  } = menu
78
+ const onItemMouseEnter = _onItemMouseEnter ?? menu.onMouseEnter
79
+ const onItemMouseLeave = _onItemMouseLeave ?? menu.onMouseLeave
78
80
  const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
79
81
  const active = Boolean(activeElement) && activeElement === rootElement
80
82
  const ref = useRef<HTMLDivElement | null>(null)
@@ -1,5 +1,5 @@
1
1
  import {getTheme_v2} from '@sanity/ui/theme'
2
- import {Children, cloneElement, forwardRef, isValidElement, useMemo} from 'react'
2
+ import {Children, cloneElement, forwardRef, isValidElement} from 'react'
3
3
  import {styled, css} from 'styled-components'
4
4
  import {EMPTY_RECORD} from '../../constants'
5
5
  import {useArrayProp} from '../../hooks'
@@ -65,10 +65,7 @@ export const AvatarStack = forwardRef(function AvatarStack(
65
65
  size: sizeProp = 1,
66
66
  ...restProps
67
67
  } = props
68
- const children = useMemo<React.ReactElement[]>(
69
- () => Children.toArray(childrenProp).filter(isValidElement),
70
- [childrenProp],
71
- )
68
+ const children: React.ReactElement[] = Children.toArray(childrenProp).filter(isValidElement)
72
69
  const maxLength = Math.max(maxLengthProp, 0)
73
70
  const size = useArrayProp(sizeProp)
74
71
 
@@ -21,8 +21,10 @@ import {
21
21
  useCallback,
22
22
  useEffect,
23
23
  useImperativeHandle,
24
+ useLayoutEffect,
24
25
  useMemo,
25
26
  useRef,
27
+ useState,
26
28
  } from 'react'
27
29
  import {useArrayProp, useElementSize, useMediaIndex, usePrefersReducedMotion} from '../../hooks'
28
30
  import {origin} from '../../middleware/origin'
@@ -126,15 +128,14 @@ export const Popover = memo(
126
128
  __unstable_margins: margins = DEFAULT_POPOVER_MARGINS,
127
129
  animate: _animate = false,
128
130
  arrow: arrowProp = false,
129
- boundaryElement = boundaryElementContext.element,
131
+ boundaryElement: _boundaryElement,
130
132
  children: childProp,
131
133
  constrainSize = false,
132
134
  content,
133
135
  disabled,
134
- fallbackPlacements = props.fallbackPlacements ??
135
- DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom'],
136
+ fallbackPlacements: _fallbackPlacements,
136
137
  matchReferenceWidth,
137
- floatingBoundary = props.boundaryElement ?? boundaryElementContext.element,
138
+ floatingBoundary: _floatingBoundary,
138
139
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
139
140
  onActivate,
140
141
  open,
@@ -144,16 +145,23 @@ export const Popover = memo(
144
145
  portal,
145
146
  preventOverflow = true,
146
147
  radius: radiusProp = 3,
147
- referenceBoundary = props.boundaryElement ?? boundaryElementContext.element,
148
+ referenceBoundary: _referenceBoundary,
148
149
  referenceElement,
149
150
  scheme,
150
151
  shadow: shadowProp = 3,
151
152
  tone = 'inherit',
152
153
  width: widthProp = 'auto',
153
- zOffset: zOffsetProp = layer.popover.zOffset,
154
+ zOffset: _zOffsetProp,
154
155
  updateRef,
155
156
  ...restProps
156
157
  } = props
158
+ const boundaryElement = _boundaryElement ?? boundaryElementContext.element
159
+ const fallbackPlacements =
160
+ _fallbackPlacements ?? DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom']
161
+ const floatingBoundary = _floatingBoundary ?? boundaryElement ?? boundaryElementContext.element
162
+ const referenceBoundary =
163
+ _referenceBoundary ?? boundaryElement ?? boundaryElementContext.element
164
+ const zOffsetProp = _zOffsetProp ?? layer.popover.zOffset
157
165
  const prefersReducedMotion = usePrefersReducedMotion()
158
166
  const animate = prefersReducedMotion ? false : _animate
159
167
  const boundarySize = useElementSize(boundaryElement)?.border
@@ -202,7 +210,7 @@ export const Popover = memo(
202
210
  const referenceWidthRef = useRef<number>()
203
211
 
204
212
  // Force apply width & max width to floating element
205
- useEffect(() => {
213
+ useLayoutEffect(() => {
206
214
  const floatingElement = ref.current
207
215
 
208
216
  if (!open || !floatingElement) return
@@ -210,7 +218,7 @@ export const Popover = memo(
210
218
  const referenceWidth = referenceWidthRef.current
211
219
 
212
220
  if (matchReferenceWidth) {
213
- if (referenceWidth !== undefined) {
221
+ if (referenceWidthRef.current !== undefined) {
214
222
  floatingElement.style.width = `${referenceWidth}px`
215
223
  }
216
224
  } else if (width !== undefined) {
@@ -350,36 +358,24 @@ export const Popover = memo(
350
358
  [refs],
351
359
  )
352
360
 
361
+ const [childElement, setChildElement] = useState<HTMLElement | null>(null)
353
362
  const setReference = useCallback(
354
363
  (node: HTMLElement | null) => {
355
364
  refs.setReference(node)
356
-
357
- const childRef = getElementRef(childProp as any)
358
-
359
- if (typeof childRef === 'function') {
360
- childRef(node)
361
- } else if (childRef) {
362
- childRef.current = node
363
- }
365
+ setChildElement(node)
364
366
  },
365
- [childProp, refs],
367
+ [refs],
366
368
  )
367
369
 
370
+ useImperativeHandle(getElementRef(childProp as any), () => childElement, [childElement])
371
+
368
372
  const child = useMemo(() => {
369
373
  if (!childProp || referenceElement) return null
370
374
 
371
375
  return cloneElement(childProp, {ref: setReference})
372
376
  }, [childProp, referenceElement, setReference])
373
377
 
374
- useEffect(() => {
375
- if (updateRef) {
376
- if (typeof updateRef === 'function') {
377
- updateRef(update)
378
- } else if (updateRef) {
379
- updateRef.current = update
380
- }
381
- }
382
- }, [update, updateRef])
378
+ useImperativeHandle(updateRef, () => update, [update])
383
379
 
384
380
  useEffect(() => {
385
381
  if (child) return
@@ -447,7 +443,8 @@ Popover.displayName = 'Memo(ForwardRef(Popover))'
447
443
  // https://github.com/facebook/react/pull/28348
448
444
  //
449
445
  // Access the ref using the method that doesn't yield a warning.
450
- function getElementRef(element: React.ReactElement) {
446
+ function getElementRef(element?: React.ReactElement) {
447
+ if (!element) return null
451
448
  // React <=18 in DEV
452
449
  let getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get
453
450
  let mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning
@@ -97,22 +97,25 @@ export const Tooltip = forwardRef(function Tooltip(
97
97
  const {
98
98
  animate: _animate = false,
99
99
  arrow: arrowProp = false,
100
- boundaryElement = boundaryElementContext?.element,
100
+ boundaryElement: _boundaryElement,
101
101
  children: childProp,
102
102
  content,
103
103
  disabled,
104
- fallbackPlacements: fallbackPlacementsProp = props.fallbackPlacements ??
105
- DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom'],
104
+ fallbackPlacements: _fallbackPlacementsProp,
106
105
  padding = 2,
107
106
  placement: placementProp = 'bottom',
108
107
  portal: portalProp,
109
108
  radius = 2,
110
109
  scheme,
111
110
  shadow = 2,
112
- zOffset = layer.tooltip.zOffset,
111
+ zOffset: _zOffset,
113
112
  delay,
114
113
  ...restProps
115
114
  } = props
115
+ const boundaryElement = _boundaryElement ?? boundaryElementContext?.element
116
+ const fallbackPlacementsProp =
117
+ _fallbackPlacementsProp ?? DEFAULT_FALLBACK_PLACEMENTS[props.placement ?? 'bottom']
118
+ const zOffset = _zOffset ?? layer.tooltip.zOffset
116
119
  const prefersReducedMotion = usePrefersReducedMotion()
117
120
  const animate = prefersReducedMotion ? false : _animate
118
121
  const fallbackPlacements = useArrayProp(fallbackPlacementsProp)
@@ -25,12 +25,10 @@ export interface ThemeProviderProps {
25
25
  */
26
26
  export function ThemeProvider(props: ThemeProviderProps): React.ReactElement {
27
27
  const parentTheme = useContext(ThemeContext)
28
- const {
29
- children,
30
- scheme = parentTheme?.scheme || 'light',
31
- theme: rootTheme = parentTheme?.theme || null,
32
- tone = parentTheme?.tone || 'default',
33
- } = props
28
+ const {children} = props
29
+ const scheme = props.scheme ?? (parentTheme?.scheme || 'light')
30
+ const rootTheme = props.theme ?? (parentTheme?.theme || null)
31
+ const tone = props.tone ?? (parentTheme?.tone || 'default')
34
32
 
35
33
  const themeContext: ThemeContextValue | null = useMemo(() => {
36
34
  if (!rootTheme) return null
@@ -21,7 +21,8 @@ export const ElementQuery = forwardRef(function ElementQuery(
21
21
  forwardedRef: React.ForwardedRef<HTMLDivElement>,
22
22
  ) {
23
23
  const theme = useTheme_v2()
24
- const {children, media = theme.media, ...restProps} = props
24
+ const {children, media: _media, ...restProps} = props
25
+ const media = _media ?? theme.media
25
26
 
26
27
  const ref = useRef<HTMLDivElement | null>(null)
27
28
  const [element, setElement] = useState<HTMLDivElement | null>(null)