Repository navigation
Conversation
Bundle size
PerformanceTotal duration: 1,269.84 ms +78.47 ms(+6.6%) | Renders: 50 (+0) | Paint: 1,918.54 ms +96.22 ms(+5.3%)
10 tests within noise — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
To help the team a bit (and your team on Claude), here is a reproduction that seems to reproduce the problem: https://stackblitz.com/edit/va1fcbzq?file=src%2FApp.tsx (repro based on this demo): Screen.Recording.2026-05-13.at.00.37.47.movIt's not clear that this should be solved with a new API; this feels like a bug: I would expect the same behavior regardless of how the menu is open: with a trigger, with the For example, in https://mui.com/material-ui/react-menu/, the focus is placed on the first menu item when the menu opens (with the keyboard) but the behavior can be configured with a prop. Let's wait for team inputs. |
60e54e8 to
e1cad93
Compare
e1cad93 to
60e54e8
Compare
|
Thanks @oliviertassinari and good call! I opened #4816 as well so we can compare this approach to that one. Will defer to the team on which they'd prefer. |
commit: |
|
Thanks for the PR @chsmc-ant Makes sense as controlled-opens are assumed to always be pointer-driven, but a keyboard-driven controlled open is also valid in which case the highlight should be set on the first item. An imperative action also makes more sense than a prop like The only issue is naming: I'm also not sure if exposing the index is a good idea, because 1) disabled items should be skipped on initial open, which should pass through the internal logic to skip them, and 2) it's likely hard to keep track of which index is the last item if the controlled open is an Possible alternative: cc: @colmtuite Side note: in case you need this behavior before release, this technically already works, if hacky: firstItem.focus();
// or firstItem.dispatchEvent(new MouseEvent('mousemove', { bubbles: true })) |
|
We have another imperative API that has the same issue: |
|
Thanks for the feedback @atomiks @michaldudak — pushed 974094c which:
PR description updated to match. Happy to bikeshed |
3f70508 to
d8abaf8
Compare
| pendingFocusItem, | ||
| onPendingFocusItemChange() { | ||
| store.set('pendingFocusItem', null); | ||
| }, |
There was a problem hiding this comment.
Folded this into useListNavigation as it removes the duplicate effect, and AriaCombobox will likely want this behavior as well: #4745
d8abaf8 to
77d7be1
Compare
|
I think |
|
thanks @atomiks happy to go ahead and implement that rename if you think it's the right approach |
|
Hey @chsmc-ant 👋 Apologies for jumping in at the last minute here, but I'm curious about this API. It seems that this might be solvable already with the detached triggers API? Also, I'm struggling to imagine a use case where you might want to highlight the last item? Pressing up/down on an isolated trigger seems like a strange case to me, I'd expect that only Space/Enter would work. And when opening programatically, I'm also struggling to imagine any use case where you'd want to highlight anything other than the first item. So I'm wondering if this should be handled automatically, somehow. Or whether it's already achievable with our detached triggers API. Are you able to share more about exactly what your use case is? |
|
Side note, I wondered if it would make sense to have a global logic to record the modality in the background, e.g. somethigg like this: // Keep track of the current interaction modality
let currentModality = 'pointer';
window.addEventListener('keydown', (event) => {
if (!event.metaKey && !event.altKey && !event.ctrlKey) {
currentModality = 'keyboard';
}
}, { capture: true });
window.addEventListener('mousedown', () => {
currentModality = 'pointer';
}, { capture: true });
window.addEventListener('touchstart', () => {
currentModality = 'pointer';
}, { capture: true });or (assuming .matches(':focus-visible') doesn't kill performance) const AppState = {
modality: 'pointer' // Default fallback ('pointer' or 'keyboard')
};
window.addEventListener('focusin', (event) => {
// Use a microtask to let the browser update the pseudo-class state
queueMicrotask(() => {
if (event.target.matches(':focus-visible')) {
AppState.modality = 'keyboard';
} else {
AppState.modality = 'pointer';
}
});
});This way, when there is a programmatic change of state from a prop (where there is no access to the |
|
@colmtuite thanks for taking a look — happy to share the use case. Use case: A vertical app sidebar where each row has an associated flyout menu. Desired keyboard UX: Why detached triggers don't cover it: The row is a On On handling it automatically: Would love that. @oliviertassinari's modality-tracking idea would make this zero-config for the common case. This PR was scoped as the minimal escape hatch that doesn't commit to a heuristic; if the team would rather pursue auto-detection instead, happy to close this in favour of that (or land this now and treat auto-detection as a follow-up that makes the explicit call unnecessary in most cases). |
Addresses review feedback: - Rename to focusItem and use directional targets instead of a raw index, so disabled/hidden items are skipped via getMinListIndex/getMaxListIndex. - Add an optional focusItem parameter to MenuHandle.open() with the same values.
c555d20 to
c082ad5
Compare
|
@chsmc-ant thanks for the explanation. Would users know On @oliviertassinari's point: this seems interesting. I wonder if On the (original) implementation: I'm okay with ditching I also think, for future-proofing/clarity, we should make So the API looks like:
But again, maybe |
They don't need to — The
I'd be happy if it were — it'd cover the
Both sound good to me — happy to push those changes if we're going ahead with the imperative API. |
Some historical context: We had this in Material UI for a while (at the time where Oops, closed by mistake, wrong button on my phone. |
|
We already use Suggestion: try adding this to the floating props in onFocus(event) {
const target = getTarget(event.nativeEvent) as Element | null;
if (
focusItemOnOpenRef.current === 'auto' &&
open &&
store.select('activeIndex') == null &&
keyRef.current == null &&
target === event.currentTarget &&
matchesFocusVisible(target)
) {
indexRef.current = getMinListIndex(listRef, disabledIndicesRef.current);
onNavigate(event);
}
} |
|
This pull request has been inactive for 30 days. Please remove the stale label or leave a comment to keep it open. Otherwise, it will be closed in 15 days. |
|
This pull request has been closed due to 15 days of inactivity after being marked stale. |
When a menu is opened programmatically (via controlled
open, orMenuHandle.open(), oronOpenChange(true)from a custom interaction that isn't one ofMenu.Trigger's built-in open keys),useListNavigationleavesactiveIndexasnull. This is correct for pointer-driven opens, but for custom keyboard shortcuts it means every item renders withtabindex="-1"andFloatingFocusManagermoves focus to the popup container rather than the first item — the user then has to press ↓ once more before arrow navigation works.This PR adds an imperative
highlightItem(target: 'first' | 'last' | 'none')action so callers can ask the menu to highlight an item once it opens:Menu.RootactionsRef— alongsideunmount/close:MenuHandle.open(triggerId, highlightItem?)— same values as a second parameter:The requested target is stored as
pendingHighlightItemonMenuStoreand resolved insideuseListNavigation's initial-sync layout effect once the list is populated, usinggetMinListIndex/getMaxListIndexso hidden items are skipped — the same logic used for the built-infocusItemOnOpenpath.useListNavigationthen moves DOM focus to the resolved item (for Menu; virtual-focus consumers like Combobox would only updateactiveIndex).A
Menu.Root.HighlightItemtype alias is exported for the'first' | 'last' | 'none'union.Preview: https://deploy-preview-4815--base-ui.netlify.app/react/components/menu#controlled-mode-with-multiple-triggers