Repository navigation
[menu] Fix SubmenuTrigger not respecting disabled state from render prop - #3858
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
Greptile OverviewGreptile SummaryFixed Key Changes:
Implementation Quality:
Confidence Score: 5/5
Important Files Changed
|
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
| const renderElementDisabled = Boolean( | ||
| render && | ||
| React.isValidElement<{ disabled?: boolean | undefined }>(render) && | ||
| render.props.disabled, | ||
| ); |
There was a problem hiding this comment.
This should probably be done the other way around: if the trigger is DOM disabled, have a warning logic in dev mode to detect it and ask developers to move the logic to the parent.
I don't see how we could do React tree introspection, the Base UI component wouldn't even know if that "disabled" prop defined user land maps to an actual disabled behavior, or something else that the user has defined.
There was a problem hiding this comment.
This also doesn't support the function form of render. It seems like the event handlers in useMenuItem could inspect the disabled state of the ref to prevent themselves from executing instead, as we can't determine it in the render body of the component
There was a problem hiding this comment.
Thanks for the feedback! I've implemented the approach of checking the disabled state via refs in event handlers, and also a warning when the trigger resolves to a DOM-disabled element, suggesting that the disabled state be moved to the Base UI component rather than being applied only at the rendered element level.
The core issue (submenu opening on hover while disabled) is addressed by the guard in useHoverReferenceInteraction.ts. I also added disabled checks inside useMenuItem.ts, which seemed conceptually reasonable, but I’m not fully confident they are strictly necessary.
Do you consider the additional guards in useMenuItem.ts valuable, or should they be removed to keep the fix minimal and scoped solely to preventing submenu activation?
There was a problem hiding this comment.
I'm in favor of removing all checks in production mode. I'm also not very keen on development-time warnings. Disabling a Base UI component's logic should be separate from rendering a custom component (so we can, for example, render Tooltips on disabled buttons).
There was a problem hiding this comment.
@michaldudak To make sure I’m aligning the PR with this direction, could you clarify what you’d like to see as the concrete outcome?
Based on your comment, should I:
- Remove all DOM-disabled checks from event handlers (including the one in
useHoverReferenceInteraction) and rely solely on the component-leveldisabledstate? - Remove the dev-time warning in
MenuSubmenuTriggerentirely? - Keep any minimal guard to prevent submenu opening when a trigger resolves to a disabled DOM element, or is that behavior intentionally unsupported?
Regarding
Disabling a Base UI component's logic should be separate from rendering a custom component (so we can, for example, render Tooltips on disabled buttons).
From an accessibility standpoint, most guidance suggests that tooltips should only be attached to focusable/hoverable elements; disabled buttons and plain text are not focusable, which makes tooltip content unreachable for keyboard and screen reader users:
A common workaround is to render a focusable wrapper but i also found this approach to work:
<Tooltip.Trigger
render={(props) => (
<Button focusableWhenDisabled {...props} disabled>
<BoldIcon aria-label="Bold" />
</Button>
)}
/>(StackBlitz: https://stackblitz.com/edit/ljb99a3s?file=src%2FApp.tsx)
There was a problem hiding this comment.
My point is that I'm pretty fine with the current state of things. We may show a warning in dev for submenu triggers, but not for other components.
From an accessibility standpoint, most guidance suggests that tooltips should only be attached to focusable/hoverable elements; disabled buttons and plain text are not focusable, which makes tooltip content unreachable for keyboard and screen reader users.
We believe tooltips are inherently inaccessible and should be visual-only components.
BTW, you can make a disabled Button focusable using the focusableWhenDisabled prop.
There was a problem hiding this comment.
Thnks, I reverted the submenu changes. But I kept the dev warning, the disabled state styles in the demos and some of the tests for the disabled submenu trigger.
| useIsoLayoutEffect(() => { | ||
| const element = triggerElementRef.current; | ||
| if (element && isElementDisabled(element) && !disabledProp) { | ||
| warn( |
There was a problem hiding this comment.
It would be great to include the component stack trace, as in #3861
There was a problem hiding this comment.
Added stack trace to warn message:
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useIsoLayoutEffect(() => {
const element = triggerElementRef.current;
if (element && isElementDisabled(element) && !disabledProp) {
const ownerStackMessage = SafeReact.captureOwnerStack?.() || '';
warn(
`A disabled element was detected on <Menu.SubmenuTrigger>. To properly disable the trigger, use the \`disabled\` prop on the component instead of setting it on the rendered element.${ownerStackMessage}`,
);
}
});
}
mj12albert
left a comment
There was a problem hiding this comment.
The demos don't actually contain disabled items, are the additional styles necessary?
I think it’s a good enhancement for the sandboxes, even if the demos don’t include disabled items. |
| checked={showMinimap} | ||
| onCheckedChange={setShowMinimap} | ||
| className="grid cursor-default grid-cols-[0.75rem_1fr] items-center gap-2 py-2 pr-8 pl-2.5 text-sm leading-4 outline-hidden select-none data-[highlighted]:relative data-[highlighted]:z-0 data-[highlighted]:text-gray-50 data-[highlighted]:before:absolute data-[highlighted]:before:inset-x-1 data-[highlighted]:before:inset-y-0 data-[highlighted]:before:z-[-1] data-[highlighted]:before:rounded-xs data-[highlighted]:before:bg-gray-900" | ||
| className="grid cursor-default grid-cols-[0.75rem_1fr] items-center gap-2 py-2 pr-8 pl-2.5 text-sm leading-4 outline-hidden select-none data-[highlighted]:relative data-[highlighted]:z-0 data-[highlighted]:text-gray-50 data-[highlighted]:before:absolute data-[highlighted]:before:inset-x-1 data-[highlighted]:before:inset-y-0 data-[highlighted]:before:z-[-1] data-[highlighted]:before:rounded-xs data-[highlighted]:before:bg-gray-900 data-[disabled]:text-gray-400 data-[disabled]:data-[highlighted]:before:bg-gray-300" |
There was a problem hiding this comment.
data-[highlighted]:before:rounded-xs
Note: rounded-xs doesnt exist in tailwind v3.
There was a problem hiding this comment.
We're moving away from Tailwind v3 and showcase just v4 in the docs.
michaldudak
left a comment
There was a problem hiding this comment.
This looks good. Merge in the latest master and we'll be able to close this PR.
|
Thanks for your contribution! |
Fixes an issue where
Menu.SubmenuTriggerdid not respect thedisabledattribute when a disabled button was provided via therenderprop. Also adds visual disabled styles to Menu demos to match existing patterns in experiments components.Changes
Menu.SubmenuTriggernot detecting thedisabledstate when a disabled button is provided via therenderprop (e.g.,render={<button disabled />}). The component now correctly derives the disabled state from the rendered element and applies it to submenu trigger behavior.Problem
When using
Menu.SubmenuTriggerwith a render prop that contains a disabled button:The disabled state was not detected, causing the submenu to still open via mouse hover, even though the underlying button was disabled.
Additionally, Menu demos lacked disabled styling. There was no visual distinction between enabled and disabled menu items when highlighted.
Before
After
Added
data-[disabled]styles to all Menu demos.CSS Modules
Tailwind CSS
Reviewer Notes
This follows the existing pattern used by experiments components where disabled state is expressed via
data-[disabled]and styling is driven from that attribute.Although there is a possible workaround today (passing
disableddirectly toMenu.SubmenuTrigger), this change ensures the render-prop pattern behaves consistently with developer expectations.Tests
packages/react/src/menu/submenu-trigger/MenuSubmenuTrigger.test.tsxdata-disabled,aria-disabled)Related
Experiment Path: docs/src/app/(private)/experiments/menu/menu-disabled-submenu-trigger.tsx
Demo with Error: https://stackblitz.com/edit/aktrg8j2?file=src%2FApp.tsx
Fixes #3850