-
-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MenuItem is not processable with enter key when not Link element #1445
Comments
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 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: …}) |
@segunadebayo 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. 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?? |
Perhaps I should have created the issue as a Feature Request instead of a Bug reporting |
🐛 Bug report
When using Menu, the Enter key event is not fired for MenuItem.
This causes an issue if you want to do something as a handler for the Click event.
Specifically, the situation is as follows
💥 Steps to reproduce
In the above example(this is same as codesandbox reproduction code)
CleanShot.2024-04-25.at.14.47.28.mp4
💻 Link to reproduction
CodeSandbox reproduction
🧐 Expected behavior
I expect one of the following
a[href]
🧭 Possible Solution
🌍 System information
📝 Additional information
The text was updated successfully, but these errors were encountered: