Pass `id` prop to the menu item's DOM element
- Dominant language
- TypeScript
- Stars
- 15.9k
- Forks
- 1.6k
- Avg merge
- 3d 9m
- Merged PRs (30d)
- 59
Description
### Provide a general summary of the feature here
Could you please consider adding a way to set an `id` attribute on the DOM element rendered by menu items?
### 🤔 Expected Behavior?
Currently, when you pass an `id` to `MenuItem`, it is not applied to the DOM element that the menu item renders. This is similar to the behavior of the `Menu` component, which accepts an `id` but does not use it as expected.
### 😯 Current Behavior
The `id` prop is filtered out of the DOM props and instead, the value from the custom `useId` hook is used.
### 💁 Possible Solution
From what I've seen, the current logic filters out the `id` prop due to special handling of the submenu triggers. A naive fix would be to fall back to user-provided ID if none is provided by the `MenuItemContext`:
https://github.com/adobe/react-spectrum/blob/4b2c6e76fa97d2e00a478a152972837c1cb76938/packages/react-aria-components/src/Menu.tsx#L369
```diff
let {menuItemProps, labelProps, descriptionProps, keyboardShortcutProps, ...states} = useMenuItem({
...props,
- id,
+ id: id ?? props.id,
key: item.key,
selectionManager
}, state, ref);
```
### 🔦 Context
> What are you trying to accomplish?
I was testing the `Autocomplete` component and needed to determine which menu item had virtual focus through `AutocompleteStateContext`. Unfortunately, the `focusedNodeId` property doesn't point to my provided `id` on the menu item, but instead to the internally generated one from `useId`.
### 💻 Examples
_No response_
### 🧢 Your Company/Team
_No response_
### 🕷 Tracking Issue
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.