Comments (3)
This is by design. The Menu uses virtual focus to navigate the items. Meaning the focus remains on the menu content all the time.
When an is selected, the onSelect is the best way to listen for selection. See the docs here
https://zagjs.com/components/react/menu#listening-for-item-selection
Links are a special case because they need to trigger navigation, hence the reason theyโre manually clicked.
Use the merge props helper to combine the props of the content with your own custom handler
mergeProps(api.contentProps, { onKeyDown: โฆ})
from zag.
@segunadebayo
I know that by passing onSelect to Menu, I can detect the user's selection.
However, this causes the processing to become redundant when handling each item is selected.
For example, let's assume there is a component like the following. Here, you want to call the API when either edit or delete is selected.
import * as menu from "@zag-js/menu"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"
export function Menu() {
const [state, send] = useMachine(menu.machine({ id: useId() }))
const api = menu.connect(state, send, normalizeProps)
return (
<div>
<button {...api.triggerProps}>
Actions <span {...api.indicatorProps}>โพ</span>
</button>
<div {...api.positionerProps}>
<ul {...api.contentProps}>
<li {...api.getItemProps({ value: "edit" })}>Edit</li>
<li {...api.getItemProps({ value: "delete" })}>Delete</li>
</ul>
</div>
</div>
)
}
For this, it is redundant to describe the processing with onSelect.
import * as menu from "@zag-js/menu"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"
export function Menu() {
const [state, send] = useMachine(menu.machine({
id: useId(),
onSelect(details) {
switch(details.value) {
case "edit":
callEditAPI()
case "delete":
callDeleteAPI()
// If there are more menu items, this would continue
}
}
}))
...
}
So I think we should be able to pass props like onSelectItem to Item.
(Just like the Radix UI MenuItem's onSelect
)
export function Menu() {
...
return (
<div>
...
<div {...api.positionerProps}>
<ul {...api.contentProps}>
<li {...api.getItemProps({ value: "edit", onSelectItem: () => callEditAPI() })}>Edit</li>
<li {...api.getItemProps({ value: "delete", onSelectItem: () => callDeleteAPI() })}>Delete</li>
</ul>
</div>
</div>
)
}
How do you think this??
from zag.
Perhaps I should have created the issue as a Feature Request instead of a Bug reporting
from zag.
Related Issues (20)
- tooltip is not interactive by default HOT 1
- Number and Pin Input Fields doesn't Restrict Full-Width Characters in using Japanese Input Method HOT 1
- expose pageSize infos and event in the pagination api HOT 1
- Vanilla example does not actually update props
- [docs] Toast visualization `500` error
- [select] Tab key does not close the open listbox HOT 2
- Slider Log scale
- [toast] No ghost element found in toast. Render the `ghostBefore` and `ghostAfter` elements HOT 1
- [popover] Reset scroll position when a popover is opened
- [popover] `autoFocus` does not work HOT 1
- NumberInput breaks on SSR on solid HOT 1
- win.PointerEvent is not a constructor
- FileUpload triggers onFileAccept when deleting an item from AcceptedFile list HOT 2
- Certain ColorPicker Channel Inputs Not Clamping HOT 2
- Bug: Anchor as menu item hijacks focus HOT 2
- Exclude the user-specified DOM element from dismissalbeElement in Combobox.
- [React][Tooltip]: Tooltip doesn't when when you tab to a button that's outside the viewport HOT 1
- Supercharge the Number Input
- [React][Tooltip] `closeOnScroll` doesn't behave correctly in Safari HOT 1
- [React][TreeView] api throwing "`selectedValue.includes` is not a function" error for several methods HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zag.