Skip to content

Commit a9812a3

Browse files
authored
Add groupedMenusRef prop to menu to support better UX in a group of menu (#1606)
1 parent 779106d commit a9812a3

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

packages/components/src/Menu/Menu.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ export interface MenuProps {
5050
* Use for controlled menu
5151
*/
5252
setOpen?: (isOpen: boolean) => void
53+
54+
/**
55+
* This prop exposes the groupedPopoversRef prop of the wrapped Popover component rendered by MenuList.
56+
* This allows transitions between menus in a group without clicking twice
57+
* (first to dismiss the current menu, and again to open the target menu)
58+
*/
59+
groupedMenusRef?: RefObject<HTMLElement>
5360
}
5461

5562
export const Menu: FC<MenuProps> = ({
5663
children,
5764
disabled,
65+
groupedMenusRef,
5866
hoverDisclosureRef,
5967
id: propsID,
6068
isOpen: controlledIsOpen = false,
@@ -68,6 +76,7 @@ export const Menu: FC<MenuProps> = ({
6876

6977
const context = {
7078
disabled,
79+
groupedMenusRef,
7180
id,
7281
isOpen: isControlled.current ? controlledIsOpen : isOpen,
7382
setOpen: isControlled.current ? controlledSetOpen : setOpen,

packages/components/src/Menu/MenuContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626

27-
import { createContext, KeyboardEvent } from 'react'
27+
import { createContext, KeyboardEvent, RefObject } from 'react'
2828
import noop from 'lodash/noop'
2929

3030
export interface MenuContextProps {
@@ -35,6 +35,7 @@ export interface MenuContextProps {
3535
setOpen?: (isOpen: boolean) => void
3636
triggerElement?: HTMLElement | null
3737
triggerCallbackRef?: (node: HTMLElement | null) => void
38+
groupedMenusRef?: RefObject<HTMLElement>
3839
}
3940

4041
export interface MenuItemContextProps {

packages/components/src/Menu/MenuList.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ export const MenuListInternal = forwardRef(
101101
{ children, compact, disabled, pin, placement, ...props }: MenuListProps,
102102
forwardedRef: Ref<HTMLUListElement>
103103
) => {
104-
const { id, isOpen, setOpen, triggerElement } = useContext(MenuContext)
104+
const { groupedMenusRef, id, isOpen, setOpen, triggerElement } = useContext(
105+
MenuContext
106+
)
105107

106108
const [renderIconPlaceholder, setRenderIconPlaceholder] = useState(false)
107109

@@ -146,6 +148,7 @@ export const MenuListInternal = forwardRef(
146148
const isMenu = isOpen !== undefined
147149
const { popover } = usePopover({
148150
content: menuList,
151+
groupedPopoversRef: groupedMenusRef,
149152
isOpen,
150153
pin,
151154
placement,

www/src/documentation/components/overlays/menu.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The entire menu can be disabled via the `disabled` prop.
1818
- `onFocus` - and `onMouseOver` - show the tooltip
1919
- `onBlur` - and `onMouseOut` - hides the tooltip
2020
- `ref` - tooltip and popover placement
21+
- `groupedMenusRef` - reference to the container of a group of menu
2122

2223
```jsx
2324
<Space>
@@ -108,6 +109,37 @@ Control the state of the menu with the React `useState` hook.
108109
}
109110
```
110111

112+
## Grouped Menus
113+
114+
```jsx
115+
;() => {
116+
const containerRef = React.useRef()
117+
return (
118+
<Space ref={containerRef}>
119+
<Menu groupedMenusRef={containerRef}>
120+
<MenuDisclosure tooltip="Select your favorite kind">
121+
<Button>Cheese</Button>
122+
</MenuDisclosure>
123+
<MenuList>
124+
<MenuItem icon="FavoriteOutline">Gouda</MenuItem>
125+
<MenuItem icon="FavoriteOutline">Swiss</MenuItem>
126+
</MenuList>
127+
</Menu>
128+
129+
<Menu groupedMenusRef={containerRef}>
130+
<MenuDisclosure tooltip="Select your favorite pet">
131+
<Button>Pet</Button>
132+
</MenuDisclosure>
133+
<MenuList>
134+
<MenuItem icon="FavoriteOutline">Dog</MenuItem>
135+
<MenuItem icon="FavoriteOutline">Cat</MenuItem>
136+
</MenuList>
137+
</Menu>
138+
</Space>
139+
)
140+
}
141+
```
142+
111143
## MenuList
112144

113145
<Props of="MenuList" />

0 commit comments

Comments
 (0)