@sanity/ui 1.7.2 → 1.7.3

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": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "sideEffects": false,
5
5
  "types": "./dist/index.d.ts",
6
6
  "source": "./src/index.ts",
@@ -70,5 +70,10 @@ export default defineScope({
70
70
  title: 'MenuButton with on close',
71
71
  component: lazy(() => import('./onCloseMenuButton')),
72
72
  },
73
+ {
74
+ name: 'shouldFocus',
75
+ title: 'Menu with shouldFocus',
76
+ component: lazy(() => import('./shouldFocus')),
77
+ },
73
78
  ],
74
79
  })
@@ -0,0 +1,46 @@
1
+ import {useSelect} from '@sanity/ui-workshop'
2
+ import {Fragment, useCallback, useState} from 'react'
3
+ import {Button, Flex, Popover} from '../../../primitives'
4
+ import {LayerProvider} from '../../../utils'
5
+ import {Menu} from '../menu'
6
+ import {MenuDivider} from '../menuDivider'
7
+ import {MenuItem} from '../menuItem'
8
+
9
+ const ITEMS = [...Array(8).keys()].map((num) => ({
10
+ title: `Item ${num + 1}`,
11
+ divider: num === 3,
12
+ }))
13
+
14
+ const OPTIONS = {first: 'first', last: 'last'}
15
+
16
+ export default function ShouldFocusStory() {
17
+ const shouldFocus = useSelect('Should focus', OPTIONS, 'first')
18
+
19
+ const [popoverOpen, setPopoverOpen] = useState<boolean>(false)
20
+ const handleToggleOpen = useCallback(() => setPopoverOpen((v) => !v), [])
21
+
22
+ return (
23
+ <LayerProvider>
24
+ <Flex align="center" justify="center" padding={4}>
25
+ <Popover
26
+ content={
27
+ <Menu shouldFocus={shouldFocus}>
28
+ {ITEMS.map((item) => {
29
+ return (
30
+ <Fragment key={item.title}>
31
+ <MenuItem text={item.title} />
32
+ {item.divider && <MenuDivider />}
33
+ </Fragment>
34
+ )
35
+ })}
36
+ </Menu>
37
+ }
38
+ open={popoverOpen}
39
+ portal
40
+ >
41
+ <Button text="Open menu" onClick={handleToggleOpen} />
42
+ </Popover>
43
+ </Flex>
44
+ </LayerProvider>
45
+ )
46
+ }
@@ -173,9 +173,14 @@ export function useMenuController(props: {
173
173
  )
174
174
 
175
175
  const handleItemMouseLeave = useCallback(() => {
176
+ // Set the active index to -2 to deactivate all menu items
177
+ // when the user moves the mouse away from the menu item.
178
+ // We avoid using -1 because it would focus the first menu item,
179
+ // which would be incorrect when the user hovers over a gap
180
+ // between two menu items or a menu divider.
181
+ setActiveIndex(-2)
176
182
  rootElement?.focus()
177
- setActiveIndex(-1)
178
- }, [rootElement, setActiveIndex])
183
+ }, [setActiveIndex, rootElement])
179
184
 
180
185
  // Set focus on the currently active element
181
186
  useEffect(() => {