@sanity/ui 2.0.0-beta.3 → 2.0.0-beta.4

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.0.0-beta.3",
3
+ "version": "2.0.0-beta.4",
4
4
  "sideEffects": false,
5
5
  "types": "./dist/index.d.ts",
6
6
  "source": "./exports/index.ts",
@@ -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(