Skip to content

Add a quick trigger action to the Menu, Listbox and Combobox components #3700

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

Merged
merged 12 commits into from
Apr 24, 2025

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Apr 22, 2025

This PR adds a new quick trigger feature to the Menu. Not sure what the best
name for this is, but essentially this is the behavior:

Recently we made sure that the Menu opens on mousedown (not just click).

This means that we can perform the following quick action:

  1. mousedown on the MenuButton — this will open the Menu
  2. Without releasing the mouse button yet, move your mouse over one of the MenuItems — this will highlight the currently active MenuItem.
  3. Release the mouse button — this will invoke the currently active MenuItem and close the Menu.

This now means that you can perform actions very quickly.

What this PR doesn't do yet is if you have a scrollable list, then it won't scroll up or down when you reach the ends of the list. For this we would need to introduce some new elements. The native Menu items on macOS show a little placeholder arrow. If you put your cursor in that area, it starts scrolling:

image

Test plan

  1. Everything still works as expected
  2. Quick release has been added:

For consistency
Scenario:
1. Mousedown on the MenuButton to open the menu
2. Hold down, and drag your mouse over a MenuItem
3. Release the mouse (mouseup/pointerup) on the MenuItem
4. MenuItem is "invoked"
Copy link

vercel bot commented Apr 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 24, 2025 1:33pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 24, 2025 1:33pm

@RobinMalfait RobinMalfait changed the title Add a quick trigger action to the Menu component Add a quick trigger action to the Menu, Listbox and Combobox components Apr 24, 2025
// Source: https://github.com/testing-library/react-testing-library/issues/838#issuecomment-735259406
//
// Polyfill the PointerEvent class for JSDOM
class PointerEvent extends Event {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add this to make the tests pass

Comment on lines +1001 to +1029
let enableQuickRelease = comboboxState === ComboboxState.Open
useQuickRelease(enableQuickRelease, {
trigger: localButtonElement,
action: useCallback(
(e) => {
if (localButtonElement?.contains(e.target)) {
return QuickReleaseAction.Ignore
}

if (inputElement?.contains(e.target)) {
return QuickReleaseAction.Ignore
}

let option = e.target.closest('[role="option"]:not([data-disabled])')
if (option !== null) {
return QuickReleaseAction.Select(option as HTMLElement)
}

if (optionsElement?.contains(e.target)) {
return QuickReleaseAction.Ignore
}

return QuickReleaseAction.Close
},
[localButtonElement, inputElement, optionsElement]
),
close: machine.actions.closeCombobox,
select: machine.actions.selectActiveOption,
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to suggestions for the API here. But essentially what we want to know is:

  1. What is the trigger element where it all starts. E.g.: MenuButton
  2. Depending on the current target when we release the pointerup, what action should we take? Options are:
  3. Ignore — essentially do nothing. In this case it means keep the Menu open and that's it.
  4. Select — select the current DOM node. In the Listbox and Combobox we can rely on the selectActiveOption. In the Menu we don't have that, so instead we just look for the [role="menuitem"]. If it exists and it's not disabled then we can select this option.
  5. Close — this should close the dropdown again. This will happen if you drag off the MenuButton for example.
  6. We need a way to close the current dropdown
  7. We need a way to select the current element. This will receive the element you provided via Select(…).

The reason I didn't just call close() and select() as part of the action is because we also have to make sure that we take the ~100ms delay into account. This threshold is only relevant for if a dropdown opens on top of the current cursor position (e.g.: https://catalyst.tailwindui.com/docs/listbox) because then we don't want to select the item.

If we check for the time differences for any action, then it could result in the menu not closing if you were quick enough.

@RobinMalfait RobinMalfait marked this pull request as ready for review April 24, 2025 11:03
@RobinMalfait RobinMalfait merged commit 1461b65 into main Apr 24, 2025
8 checks passed
@RobinMalfait RobinMalfait deleted the feat/quick-trigger branch April 24, 2025 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant