@sanity/ui 2.0.0-beta.1 → 2.0.0-beta.10

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 (85) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +6 -3
  3. package/dist/index.cjs.mjs +11 -36
  4. package/dist/index.d.ts +248 -268
  5. package/dist/index.esm.js +494 -354
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.js +503 -567
  8. package/dist/index.js.map +1 -1
  9. package/dist/theme.cjs.mjs +1 -1
  10. package/dist/theme.d.ts +81 -114
  11. package/dist/theme.esm.js +299 -252
  12. package/dist/theme.esm.js.map +1 -1
  13. package/dist/theme.js +299 -252
  14. package/dist/theme.js.map +1 -1
  15. package/package.json +6 -3
  16. package/src/core/__workshop__/constants.ts +3 -2
  17. package/src/core/_compat.ts +236 -87
  18. package/src/core/components/menu/__workshop__/selectedItem.tsx +7 -2
  19. package/src/core/components/menu/menuButton.tsx +12 -1
  20. package/src/core/constants.ts +18 -0
  21. package/src/core/hooks/useMediaIndex/useMediaIndex.test.tsx +6 -3
  22. package/src/core/middleware/origin.ts +47 -0
  23. package/src/core/primitives/avatar/__workshop__/asButton.tsx +1 -1
  24. package/src/core/primitives/avatar/__workshop__/stack.tsx +1 -1
  25. package/src/core/primitives/avatar/avatar.tsx +14 -4
  26. package/src/core/primitives/avatar/avatarCounter.tsx +16 -5
  27. package/src/core/primitives/avatar/avatarStack.tsx +2 -2
  28. package/src/core/primitives/badge/badge.tsx +1 -3
  29. package/src/core/primitives/kbd/kbd.tsx +2 -0
  30. package/src/core/primitives/popover/popover.tsx +67 -13
  31. package/src/core/primitives/popover/popoverCard.tsx +26 -15
  32. package/src/core/primitives/select/select.tsx +1 -1
  33. package/src/core/primitives/switch/styles.ts +6 -1
  34. package/src/core/primitives/tooltip/tooltip.tsx +47 -57
  35. package/src/core/primitives/tooltip/tooltipCard.tsx +104 -0
  36. package/src/core/styles/card/_cardColorStyle.ts +2 -0
  37. package/src/core/styles/margin/marginsStyle.test.ts +2 -2
  38. package/src/core/styles/padding/paddingStyle.test.ts +2 -2
  39. package/src/core/theme/__workshop__/build/Root.tsx +20 -3
  40. package/src/core/theme/__workshop__/build/story.tsx +16 -10
  41. package/src/core/theme/__workshop__/canvas.tsx +7 -7
  42. package/src/core/theme/__workshop__/debug/story.tsx +159 -0
  43. package/src/core/theme/__workshop__/index.ts +5 -0
  44. package/src/core/theme/theme.test.tsx +2 -2
  45. package/src/core/types/avatar.ts +1 -1
  46. package/src/core/types/button.ts +1 -2
  47. package/src/core/{primitives/tooltip → utils/conditionalWrapper}/conditionalWrapper.tsx +3 -0
  48. package/src/core/utils/conditionalWrapper/index.ts +1 -0
  49. package/src/core/utils/index.ts +1 -0
  50. package/src/theme/build/_deprecated/color/factory.ts +3 -3
  51. package/src/theme/build/buildColorTheme.ts +16 -16
  52. package/src/theme/build/colorToken/colorToken.ts +19 -0
  53. package/src/theme/build/colorToken/compileColorToken.ts +21 -0
  54. package/src/theme/build/colorToken/index.ts +2 -0
  55. package/src/theme/build/colorToken/types.ts +6 -0
  56. package/src/theme/build/renderColorTheme.ts +14 -15
  57. package/src/theme/build/renderColorValue.ts +9 -2
  58. package/src/theme/config/system.ts +12 -10
  59. package/src/theme/config/tokens/color/types.ts +12 -1
  60. package/src/theme/config/tokens/parseTokenKey.ts +1 -0
  61. package/src/theme/config/tokens/parseTokenValue.test.ts +13 -16
  62. package/src/theme/config/tokens/parseTokenValue.ts +45 -14
  63. package/src/theme/config/tokens/types.ts +3 -2
  64. package/src/theme/defaults/colorTokens.ts +219 -211
  65. package/src/theme/defaults/config.ts +2 -1
  66. package/src/theme/defaults/fonts.ts +13 -5
  67. package/src/theme/index.ts +0 -7
  68. package/src/theme/system/color/_constants.ts +5 -2
  69. package/src/theme/system/color/_system.ts +2 -1
  70. package/src/theme/system/color/avatar.ts +2 -12
  71. package/src/theme/system/color/button.ts +4 -20
  72. package/src/theme/system/color/input.ts +3 -11
  73. package/src/theme/system/color/selectable.ts +3 -14
  74. package/src/theme/system/shadow.ts +0 -6
  75. package/src/theme/system/styles.ts +0 -6
  76. package/src/theme/system/theme.ts +2 -0
  77. package/src/theme/system/v0/avatar.ts +1 -1
  78. package/src/theme/system/v0/color/button.ts +4 -4
  79. package/src/theme/system/v0/color/card.ts +2 -2
  80. package/src/theme/system/v0/color/input.ts +3 -3
  81. package/src/theme/system/v0/color/muted.ts +2 -2
  82. package/src/theme/system/v0/color/solid.ts +2 -2
  83. package/src/theme/versioning/themeColor_v0_v2.ts +2 -2
  84. package/src/theme/versioning/v2_v0.ts +2 -2
  85. package/src/theme/build/colorToken.ts +0 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/ui",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.10",
4
4
  "sideEffects": false,
5
5
  "types": "./dist/index.d.ts",
6
6
  "source": "./exports/index.ts",
@@ -72,7 +72,7 @@
72
72
  },
73
73
  "dependencies": {
74
74
  "@floating-ui/react-dom": "^2.0.4",
75
- "@sanity/color": "3.0.0-beta.8",
75
+ "@sanity/color": "3.0.0-beta.9",
76
76
  "@sanity/icons": "^2.7.0",
77
77
  "csstype": "^3.1.2",
78
78
  "framer-motion": "^10.16.5",
@@ -171,7 +171,10 @@
171
171
  "keywords": [
172
172
  "sanity",
173
173
  "ui",
174
- "components"
174
+ "primitives",
175
+ "react",
176
+ "components",
177
+ "design-system"
175
178
  ],
176
179
  "author": "Sanity.io <hello@sanity.io>",
177
180
  "homepage": "https://www.sanity.io/",
@@ -17,9 +17,10 @@ import {
17
17
  import {ThemeColorSpotKey, ThemeColorToneKey, ThemeFontWeightKey} from '@sanity/ui/theme'
18
18
 
19
19
  export const WORKSHOP_AVATAR_SIZE_OPTIONS: {[key: string]: AvatarSize} = {
20
- '0 (default)': 0,
21
- '1': 1,
20
+ '0': 0,
21
+ '1 (default)': 1,
22
22
  '2': 2,
23
+ '3': 3,
23
24
  }
24
25
 
25
26
  export const WORKSHOP_BADGE_MODE_OPTIONS: {[key: string]: BadgeMode} = {
@@ -1,73 +1,23 @@
1
- // re-export for backwards compatibility
2
- export {
3
- COLOR_CONFIG_CARD_KEYS,
4
- COLOR_CONFIG_CARD_TONES,
5
- COLOR_CONFIG_BLEND_KEYS,
6
- COLOR_CONFIG_STATE_KEYS,
7
- COLOR_CONFIG_STATE_TONES,
8
- THEME_COLOR_CARD_TONES,
9
- THEME_COLOR_BLEND_MODES,
10
- THEME_COLOR_BUTTON_MODES,
11
- THEME_COLOR_STATES,
12
- THEME_COLOR_STATE_TONES,
13
- buildTheme,
14
- createColorTheme,
15
- hexToRgb,
16
- hslToRgb,
17
- isColorBlendModeValue,
18
- isColorButtonMode,
19
- isColorConfigBaseKey,
20
- isColorConfigBaseTone,
21
- isColorConfigBlendKey,
22
- isColorConfigStateKey,
23
- isColorConfigStateTone,
24
- isColorHueKey,
25
- isColorOpacityValue,
26
- isColorTintKey,
27
- isColorTokenValue,
28
- isColorValue,
29
- multiply,
30
- parseColor,
31
- parseTokenKey,
32
- parseTokenValue,
33
- rgbToHex,
34
- rgbToHsl,
35
- rgba,
36
- rgbaToRGBA,
37
- screen,
38
- defaultTheme as studioTheme,
39
- type BaseTheme,
40
- type BoxShadow,
41
- type CSSObject,
42
- type ColorBlendModeTokenValue,
43
- type ColorConfigCardKey,
44
- type ColorConfigCardTone,
45
- type ColorConfigBlendKey,
46
- type ColorConfigOpacityValue,
47
- type ColorConfigStateKey,
48
- type ColorConfigStateTone,
49
- type ColorConfigValue,
50
- type HSL,
1
+ // This file re-exports API members which existed in the `@sanity/ui` export in v1.
2
+
3
+ import {
4
+ type BaseTheme as _BaseTheme,
5
+ type ThemeBoxShadow,
6
+ type HSL as _HSL,
51
7
  type PartialThemeColorBuilderOpts,
52
- type RGB,
53
- type RGBA,
54
- type RootTheme,
55
- type Theme,
8
+ type RGB as _RGB,
9
+ type RootTheme as _RootTheme,
10
+ type ThemeStyles,
11
+ type Theme as _Theme,
56
12
  type ThemeAvatar,
57
13
  type ThemeColor,
58
14
  type ThemeColorBase,
59
- type ThemeColorBaseTokens,
60
- type ThemeColorCardToneKey,
61
- type ThemeColorBlendModeKey,
62
15
  type ThemeColorBuilderOpts,
63
16
  type ThemeColorButton,
64
- type ThemeColorButtonModeKey,
65
- type ThemeColorButtonState,
17
+ type ThemeColorButtonModeKey as _ThemeColorButtonModeKey,
66
18
  type ThemeColorButtonStates,
67
- type ThemeColorButtonTokens,
68
19
  type ThemeColorButtonTones,
69
20
  type ThemeColorCard,
70
- type ThemeColorCardState,
71
21
  type ThemeColorGenericState,
72
22
  type ThemeColorInput,
73
23
  type ThemeColorInputState,
@@ -76,40 +26,239 @@ export {
76
26
  type ThemeColorMutedTone,
77
27
  type ThemeColorName,
78
28
  type ThemeColorScheme,
79
- type ThemeColorSchemeKey,
29
+ type ThemeColorSchemeKey as _ThemeColorSchemeKey,
80
30
  type ThemeColorSchemes,
81
31
  type ThemeColorSelectable,
32
+ type ThemeColorSelectableStates,
33
+ type ThemeColorSolid,
34
+ type ThemeColorSolidTone,
35
+ type ThemeColorSpot,
36
+ type ThemeColorSpotKey,
37
+ type ThemeColorSyntax as _ThemeColorSyntax,
38
+ type ThemeColorToneKey,
39
+ type ThemeFont as _ThemeFont,
40
+ type ThemeFontKey as _ThemeFontKey,
41
+ type ThemeFontSize as _ThemeFontSize,
42
+ type ThemeFontWeight as _ThemeFontWeight,
43
+ type ThemeFontWeightKey as _ThemeFontWeightKey,
44
+ type ThemeFonts as _ThemeFonts,
45
+ type ThemeInput,
46
+ type ThemeLayer as _ThemeLayer,
47
+ type ThemeShadow as _ThemeShadow,
48
+ type ThemeColorButtonState,
49
+ type ThemeColorCardState,
82
50
  type ThemeColorSelectableState,
51
+ buildTheme,
52
+ } from '@sanity/ui/theme'
53
+
54
+ /**
55
+ * @public
56
+ * @deprecated Use `BaseTheme` from `@sanity/ui/theme` instead.
57
+ */
58
+ export type BaseTheme = _BaseTheme
59
+
60
+ /**
61
+ * @public
62
+ * @deprecated Use `ThemeBoxShadow` from `@sanity/ui/theme` instead.
63
+ */
64
+ export type BoxShadow = ThemeBoxShadow
65
+
66
+ /**
67
+ * @public
68
+ * @deprecated Use `HSL` from `@sanity/ui/theme` instead.
69
+ */
70
+ export type HSL = _HSL
71
+
72
+ /**
73
+ * @public
74
+ * @deprecated Use `RGB` from `@sanity/ui/theme` instead.
75
+ */
76
+ export type RGB = _RGB
77
+
78
+ /**
79
+ * @public
80
+ * @deprecated Use `RootTheme` from `@sanity/ui/theme` instead.
81
+ */
82
+ export type RootTheme = _RootTheme
83
+
84
+ /**
85
+ * @public
86
+ * @deprecated Use `ThemeStyles` from `@sanity/ui/theme` instead.
87
+ */
88
+ export type Styles = ThemeStyles
89
+
90
+ /**
91
+ * @public
92
+ * @deprecated Use `Theme` from `@sanity/ui/theme` instead.
93
+ */
94
+ export type Theme = _Theme
95
+
96
+ /**
97
+ * @public
98
+ * @deprecated Use `ThemeColorButtonModeKey` from `@sanity/ui/theme` instead.
99
+ */
100
+ export type ThemeColorButtonModeKey = _ThemeColorButtonModeKey
101
+
102
+ /**
103
+ * @public
104
+ * @deprecated Use `ThemeColorSchemeKey` from `@sanity/ui/theme` instead.
105
+ */
106
+ export type ThemeColorSchemeKey = _ThemeColorSchemeKey
107
+
108
+ /**
109
+ * @public
110
+ * @deprecated Use `ThemeColorSyntax` from `@sanity/ui/theme` instead.
111
+ */
112
+ export type ThemeColorSyntax = _ThemeColorSyntax
113
+
114
+ /**
115
+ * @public
116
+ * @deprecated Use `ThemeFont` from `@sanity/ui/theme` instead.
117
+ */
118
+ export type ThemeFont = _ThemeFont
119
+
120
+ /**
121
+ * @public
122
+ * @deprecated Use `ThemeFontKey` from `@sanity/ui/theme` instead.
123
+ */
124
+ export type ThemeFontKey = _ThemeFontKey
125
+
126
+ /**
127
+ * @public
128
+ * @deprecated Use `ThemeFontSize` from `@sanity/ui/theme` instead.
129
+ */
130
+ export type ThemeFontSize = _ThemeFontSize
131
+
132
+ /**
133
+ * @public
134
+ * @deprecated Use `ThemeFontWeight` from `@sanity/ui/theme` instead.
135
+ */
136
+ export type ThemeFontWeight = _ThemeFontWeight
137
+
138
+ /**
139
+ * @public
140
+ * @deprecated Use `ThemeFontWeightKey` from `@sanity/ui/theme` instead.
141
+ */
142
+ export type ThemeFontWeightKey = _ThemeFontWeightKey
143
+
144
+ /**
145
+ * @public
146
+ * @deprecated Use `ThemeFonts` from `@sanity/ui/theme` instead.
147
+ */
148
+ export type ThemeFonts = _ThemeFonts
149
+
150
+ /**
151
+ * @public
152
+ * @deprecated Use `ThemeLayer` from `@sanity/ui/theme` instead.
153
+ */
154
+ export type ThemeLayer = _ThemeLayer
155
+
156
+ /**
157
+ * @public
158
+ * @deprecated Use `ThemeShadow` from `@sanity/ui/theme` instead.
159
+ */
160
+ export type ThemeShadow = _ThemeShadow
161
+
162
+ export {
163
+ type PartialThemeColorBuilderOpts,
164
+ type ThemeAvatar,
165
+ type ThemeColor,
166
+ type ThemeColorBase,
167
+ type ThemeColorBuilderOpts,
168
+ type ThemeColorButton,
169
+ type ThemeColorButtonStates,
170
+ type ThemeColorButtonTones,
171
+ type ThemeColorCard,
172
+ type ThemeColorGenericState,
173
+ type ThemeColorInput,
174
+ type ThemeColorInputState,
175
+ type ThemeColorInputStates,
176
+ type ThemeColorMuted,
177
+ type ThemeColorMutedTone,
178
+ type ThemeColorName,
179
+ type ThemeColorScheme,
180
+ type ThemeColorSchemes,
181
+ type ThemeColorSelectable,
83
182
  type ThemeColorSelectableStates,
84
183
  type ThemeColorSolid,
85
184
  type ThemeColorSolidTone,
86
185
  type ThemeColorSpot,
87
186
  type ThemeColorSpotKey,
88
- type ThemeColorStateKey,
89
- type ThemeColorStateTokens,
90
- type ThemeColorStateToneKey,
91
- type ThemeColorStatesTokens,
92
- type ThemeColorSyntax,
93
- type ThemeColorTokenValue,
94
- type ThemeColorTokens,
95
187
  type ThemeColorToneKey,
96
- type ThemeConfig,
97
- type ThemeFocusRing,
98
- type ThemeFont,
99
- type ThemeFontKey,
100
- type ThemeFontSize,
101
- type ThemeFontWeight,
102
- type ThemeFontWeightKey,
103
- type ThemeFonts,
104
188
  type ThemeInput,
105
- type ThemeLayer,
106
- type ThemeShadow,
107
- type ThemeStyles,
108
- type TokenBaseKeyNode,
109
- type TokenBlendModeValueNode,
110
- type TokenButtonKeyNode,
111
- type TokenColorValueNode,
112
- type TokenHueValueNode,
113
- type TokenKeyNode,
114
- type TokenValueNode,
189
+ type ThemeColorButtonState,
190
+ type ThemeColorCardState,
191
+ type ThemeColorSelectableState,
192
+ }
193
+
194
+ import {
195
+ createColorTheme as _createColorTheme,
196
+ hexToRgb as _hexToRgb,
197
+ hslToRgb as _hslToRgb,
198
+ multiply as _multiply,
199
+ parseColor as _parseColor,
200
+ rgbToHex as _rgbToHex,
201
+ rgbToHsl as _rgbToHsl,
202
+ rgba as _rgba,
203
+ screen as _screen,
115
204
  } from '@sanity/ui/theme'
205
+
206
+ /**
207
+ * @public
208
+ * @deprecated Use `createColorTheme` from `@sanity/ui/theme` instead.
209
+ */
210
+ export const createColorTheme = _createColorTheme
211
+
212
+ /**
213
+ * @public
214
+ * @deprecated Use `hexToRgb` from `@sanity/ui/theme` instead.
215
+ */
216
+ export const hexToRgb = _hexToRgb
217
+
218
+ /**
219
+ * @public
220
+ * @deprecated Use `hslToRgb` from `@sanity/ui/theme` instead.
221
+ */
222
+ export const hslToRgb = _hslToRgb
223
+
224
+ /**
225
+ * @public
226
+ * @deprecated Use `multiply` from `@sanity/ui/theme` instead.
227
+ */
228
+ export const multiply = _multiply
229
+
230
+ /**
231
+ * @public
232
+ * @deprecated Use `parseColor` from `@sanity/ui/theme` instead.
233
+ */
234
+ export const parseColor = _parseColor
235
+
236
+ /**
237
+ * @public
238
+ * @deprecated Use `rgbToHex` from `@sanity/ui/theme` instead.
239
+ */
240
+ export const rgbToHex = _rgbToHex
241
+
242
+ /**
243
+ * @public
244
+ * @deprecated Use `rgbToHsl` from `@sanity/ui/theme` instead.
245
+ */
246
+ export const rgbToHsl = _rgbToHsl
247
+
248
+ /**
249
+ * @public
250
+ * @deprecated Use `rgba` from `@sanity/ui/theme` instead.
251
+ */
252
+ export const rgba = _rgba
253
+
254
+ /**
255
+ * @public
256
+ * @deprecated Use `screen` from `@sanity/ui/theme` instead.
257
+ */
258
+ export const screen = _screen
259
+
260
+ /**
261
+ * @public
262
+ * @deprecated Use `buildTheme` from `@sanity/ui/theme` instead.
263
+ */
264
+ export const studioTheme = buildTheme()
@@ -16,8 +16,10 @@ const POPOVER_PROPS: MenuButtonProps['popover'] = {
16
16
  matchReferenceWidth: true,
17
17
  }
18
18
 
19
+ const INITIAL_INDEX = 1
20
+
19
21
  export default function SelectedItemStory() {
20
- const [selectedIndex, setSelectedIndex] = useState(0)
22
+ const [selectedIndex, setSelectedIndex] = useState(INITIAL_INDEX)
21
23
 
22
24
  return (
23
25
  <Box padding={[4, 5, 6]}>
@@ -26,12 +28,13 @@ export default function SelectedItemStory() {
26
28
 
27
29
  <MenuButton
28
30
  button={<Button text="Open menu" />}
29
- id="selected-item-example"
31
+ id="menu-button"
30
32
  menu={
31
33
  <Menu>
32
34
  <MenuItem
33
35
  icon={SearchIcon}
34
36
  iconRight={selectedIndex === 0 ? CheckmarkIcon : undefined}
37
+ id="menu-item-1"
35
38
  onClick={() => setSelectedIndex(0)}
36
39
  pressed={selectedIndex === 0}
37
40
  selected={selectedIndex === 0}
@@ -40,6 +43,7 @@ export default function SelectedItemStory() {
40
43
  <MenuItem
41
44
  icon={ClockIcon}
42
45
  iconRight={selectedIndex === 1 ? CheckmarkIcon : undefined}
46
+ id="menu-item-2"
43
47
  onClick={() => setSelectedIndex(1)}
44
48
  pressed={selectedIndex === 1}
45
49
  selected={selectedIndex === 1}
@@ -49,6 +53,7 @@ export default function SelectedItemStory() {
49
53
  <MenuItem
50
54
  icon={ExpandIcon}
51
55
  iconRight={selectedIndex === 2 ? CheckmarkIcon : undefined}
56
+ id="menu-item-3"
52
57
  onClick={() => setSelectedIndex(2)}
53
58
  pressed={selectedIndex === 2}
54
59
  selected={selectedIndex === 2}
@@ -99,6 +99,16 @@ export const MenuButton = forwardRef(function MenuButton(
99
99
  setShouldFocus(null)
100
100
  }, [])
101
101
 
102
+ // Prevent mouse event propagation when the menu is open.
103
+ // This is to ensure that `handleBlur` isn't triggered when clicking the menu button whilst open,
104
+ // which can lead to `setOpen` being triggered multiple times (once by `handleBlur`, and again by `handleButtonClick`).
105
+ const handleMouseDown = useCallback(
106
+ (event: PointerEvent) => {
107
+ if (open) event.preventDefault()
108
+ },
109
+ [open],
110
+ )
111
+
102
112
  const handleButtonKeyDown = useCallback((event: React.KeyboardEvent<HTMLButtonElement>) => {
103
113
  // On `ArrowDown`, `Enter` and `Space`
104
114
  // - Opens menu and moves focus to first menuitem
@@ -231,13 +241,14 @@ export const MenuButton = forwardRef(function MenuButton(
231
241
  id,
232
242
  onClick: handleButtonClick,
233
243
  onKeyDown: handleButtonKeyDown,
244
+ onMouseDown: handleMouseDown,
234
245
  'aria-haspopup': true,
235
246
  'aria-expanded': open,
236
247
  ref: setButtonRef,
237
248
  selected: open,
238
249
  })
239
250
  : null,
240
- [buttonProp, handleButtonClick, handleButtonKeyDown, id, open, setButtonRef],
251
+ [buttonProp, handleButtonClick, handleButtonKeyDown, handleMouseDown, id, open, setButtonRef],
241
252
  )
242
253
 
243
254
  const popoverProps: MenuButtonProps['popover'] = useMemo(
@@ -1,3 +1,5 @@
1
+ import {Transition, Variant} from 'framer-motion'
2
+
1
3
  /**
2
4
  * @internal
3
5
  */
@@ -8,6 +10,22 @@ export const EMPTY_ARRAY: never[] = []
8
10
  */
9
11
  export const EMPTY_RECORD: Record<string, never> = {}
10
12
 
13
+ /**
14
+ * Shared `framer-motion` variants used by `Popover` and `Tooltip` components.
15
+ * @internal
16
+ */
17
+ export const POPOVER_MOTION_PROPS: {
18
+ animate: Variant
19
+ initial: Variant
20
+ exit: Variant
21
+ transition: Transition
22
+ } = {
23
+ initial: {opacity: 0.5, scale: 0.97},
24
+ animate: {opacity: 1, scale: 1},
25
+ exit: {opacity: 0, scale: 0.97},
26
+ transition: {duration: 0.4, type: 'spring'},
27
+ }
28
+
11
29
  /**
12
30
  * @internal
13
31
  * @deprecated No longer used.
@@ -1,9 +1,12 @@
1
1
  /** @jest-environment node */
2
- import {defaultTheme} from '@sanity/ui/theme'
2
+
3
+ import {buildTheme} from '@sanity/ui/theme'
3
4
  import {renderToString, renderToStaticMarkup} from 'react-dom/server'
4
5
  import {ThemeProvider} from '../../theme'
5
6
  import {useMediaIndex} from './useMediaIndex'
6
7
 
8
+ const theme = buildTheme()
9
+
7
10
  function Log() {
8
11
  const mediaIndex = useMediaIndex()
9
12
 
@@ -14,7 +17,7 @@ describe('useMediaIndex', () => {
14
17
  it(`SSR to static markup returns 0`, () => {
15
18
  expect(
16
19
  renderToStaticMarkup(
17
- <ThemeProvider theme={defaultTheme}>
20
+ <ThemeProvider theme={theme}>
18
21
  <Log />
19
22
  </ThemeProvider>,
20
23
  ),
@@ -23,7 +26,7 @@ describe('useMediaIndex', () => {
23
26
  it(`SSR to markup for hydration doesn't throw`, () => {
24
27
  expect(
25
28
  renderToString(
26
- <ThemeProvider theme={defaultTheme}>
29
+ <ThemeProvider theme={theme}>
27
30
  <Log />
28
31
  </ThemeProvider>,
29
32
  ),
@@ -0,0 +1,47 @@
1
+ import {Middleware} from '@floating-ui/react-dom'
2
+
3
+ /**
4
+ * Custom floating-ui middleware which calculates transform-origin X + Y offsets
5
+ * based on the current floating rect's dimensions and shift offset.
6
+ *
7
+ * Scaling popovers with these transform-origin offsets will give the effect of
8
+ * popvers slightly 'growing' from the origin/reference element.
9
+ *
10
+ * This middleware must be applied after both `@sanity/ui/size` and `shift` middlewares.
11
+ */
12
+ export const origin: Middleware = {
13
+ name: '@sanity/ui/origin',
14
+ fn({middlewareData, placement, rects}) {
15
+ const [side] = placement.split('-')
16
+
17
+ const floatingWidth = rects.floating.width
18
+ const floatingHeight = rects.floating.height
19
+
20
+ const shiftX = middlewareData.shift?.x || 0
21
+ const shiftY = middlewareData.shift?.y || 0
22
+
23
+ if (floatingWidth <= 0 || floatingHeight <= 0) {
24
+ return {}
25
+ }
26
+
27
+ const isVerticalPlacement = ['bottom', 'top'].includes(side)
28
+
29
+ const {originX, originY}: {originX: number; originY: number} = isVerticalPlacement
30
+ ? {
31
+ originX: clamp(0.5 - shiftX / floatingWidth, 0, 1),
32
+ originY: side === 'bottom' ? 0 : 1,
33
+ }
34
+ : {
35
+ originX: side === 'left' ? 1 : 0,
36
+ originY: clamp(0.5 - shiftY / floatingHeight, 0, 1),
37
+ }
38
+
39
+ return {
40
+ data: {originX, originY},
41
+ }
42
+ },
43
+ }
44
+
45
+ function clamp(num: number, min: number, max: number) {
46
+ return Math.min(Math.max(num, min), max)
47
+ }
@@ -3,7 +3,7 @@ import {useSelect} from '@sanity/ui-workshop'
3
3
  import {WORKSHOP_AVATAR_SIZE_OPTIONS} from '../../../__workshop__/constants'
4
4
 
5
5
  export default function AsButtonStory() {
6
- const size = useSelect('Size', WORKSHOP_AVATAR_SIZE_OPTIONS, 0, 'Props')
6
+ const size = useSelect('Size', WORKSHOP_AVATAR_SIZE_OPTIONS, 1, 'Props')
7
7
 
8
8
  return (
9
9
  <Flex align="center" height="fill" justify="center">
@@ -3,7 +3,7 @@ import {useSelect} from '@sanity/ui-workshop'
3
3
  import {WORKSHOP_AVATAR_SIZE_OPTIONS} from '../../../__workshop__/constants'
4
4
 
5
5
  export default function StackStory() {
6
- const size = useSelect('Size', WORKSHOP_AVATAR_SIZE_OPTIONS, 0, 'Props')
6
+ const size = useSelect('Size', WORKSHOP_AVATAR_SIZE_OPTIONS, 1, 'Props')
7
7
 
8
8
  return (
9
9
  <Flex align="center" height="fill" justify="center">
@@ -69,12 +69,12 @@ export const Avatar = forwardRef(function Avatar(
69
69
  arrowPosition: arrowPositionProp,
70
70
  animateArrowFrom,
71
71
  status = 'online',
72
- size: sizeProp = 0,
72
+ size: sizeProp = 1,
73
73
  ...restProps
74
74
  } = props
75
+ const {avatar} = useTheme_v2()
75
76
  const as = ReactIs.isValidElementType(asProp) ? asProp : 'div'
76
77
  const size = useArrayProp(sizeProp)
77
- const {avatar} = useTheme_v2()
78
78
 
79
79
  // @todo: remove this
80
80
  const avatarSize = avatar.sizes[size[0]] || avatar.sizes[0]
@@ -111,7 +111,17 @@ export const Avatar = forwardRef(function Avatar(
111
111
  }
112
112
  }, [onImageLoadError])
113
113
 
114
- const initialsSize = useMemo(() => size.map((s) => (s === 0 ? 0 : s * 2)), [size])
114
+ const initialsSize = useMemo(
115
+ () =>
116
+ size.map((s) => {
117
+ if (s === 1) return 1
118
+ if (s === 2) return 3
119
+ if (s === 3) return 5
120
+
121
+ return 0
122
+ }),
123
+ [size],
124
+ )
115
125
 
116
126
  return (
117
127
  <Root
@@ -175,7 +185,7 @@ export const Avatar = forwardRef(function Avatar(
175
185
  {(imageFailed || !src) && initials && (
176
186
  <>
177
187
  <Initials>
178
- <InitialsLabel forwardedAs="span" size={initialsSize}>
188
+ <InitialsLabel forwardedAs="span" size={initialsSize} weight="medium">
179
189
  {initials}
180
190
  </InitialsLabel>
181
191
  </Initials>
@@ -5,7 +5,7 @@ import {EMPTY_RECORD} from '../../constants'
5
5
  import {useArrayProp} from '../../hooks'
6
6
  import {rem, _responsive, ThemeProps} from '../../styles'
7
7
  import {AvatarSize} from '../../types'
8
- import {Text} from '../text'
8
+ import {Label} from '../label'
9
9
 
10
10
  function _responsiveAvatarCounterSizeStyle(props: {$size: AvatarSize[]} & ThemeProps) {
11
11
  const {avatar, media} = getTheme_v2(props.theme)
@@ -67,15 +67,26 @@ export const AvatarCounter = forwardRef(function AvatarCounter(
67
67
  props: AvatarCounterProps,
68
68
  ref: React.Ref<HTMLDivElement>,
69
69
  ) {
70
- const {count, size: sizeProp = 0} = props
70
+ const {count, size: sizeProp = 1} = props
71
71
  const size = useArrayProp(sizeProp)
72
- const counterSize = useMemo(() => size.map((s) => (s === 0 ? 0 : s + 1)), [size])
72
+
73
+ const fontSize = useMemo(
74
+ () =>
75
+ size.map((s) => {
76
+ if (s === 1) return 1
77
+ if (s === 2) return 3
78
+ if (s === 3) return 5
79
+
80
+ return 0
81
+ }),
82
+ [size],
83
+ )
73
84
 
74
85
  return (
75
86
  <Root $size={size} data-ui="AvatarCounter" ref={ref}>
76
- <Text as="span" size={counterSize} weight="medium">
87
+ <Label as="span" size={fontSize} weight="medium">
77
88
  {count}
78
- </Text>
89
+ </Label>
79
90
  </Root>
80
91
  )
81
92
  })
@@ -63,7 +63,7 @@ export const AvatarStack = forwardRef(function AvatarStack(
63
63
  const {
64
64
  children: childrenProp,
65
65
  maxLength: maxLengthProp = 4,
66
- size: sizeProp = 0,
66
+ size: sizeProp = 1,
67
67
  ...restProps
68
68
  } = props
69
69
  const children = childrenToElementArray(childrenProp).filter(
@@ -81,7 +81,7 @@ export const AvatarStack = forwardRef(function AvatarStack(
81
81
  <Root data-ui="AvatarStack" {...restProps} ref={ref} $size={size}>
82
82
  {len === 0 && (
83
83
  <div>
84
- <AvatarCounter count={len} />
84
+ <AvatarCounter count={len} size={size} />
85
85
  </div>
86
86
  )}
87
87