tailwindlabs / headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
https://headlessui.com
MIT License
25.81k stars 1.07k forks source link

Inconsistent focus behavior of the MenuItems component #3488

Open lukejagodzinski opened 6 days ago

lukejagodzinski commented 6 days ago

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v2.1.8

What browser are you using?

Chrome Version 129.0.6668.58 (Official Build) (arm64)

Reproduction URL

https://codesandbox.io/p/devbox/5g77y7

Describe your issue

When opening (using keyboard) dropdown menu for the first and second time there is inconsistent focus behavior. After opening menu for the first time, neither of the items is focused. However, when I open it for the second time the first element gets focused.

I've included CodeSandBox reproduction but also pasting code and reproduction steps here.

<div className="flex flex-row w-full gap-2 justify-center items-center">
  <Button autoFocus className="border p-2 bg-gray-100">
    Before
  </Button>
  <Menu>
    <MenuButton className="border p-2 bg-gray-100">My account</MenuButton>
    <MenuItems anchor="bottom" className="bg-gray-100 border">
      <MenuItem className="data-[focus]:bg-gray-600 data-[focus]:text-white p-2">
        <div>Settings</div>
      </MenuItem>
      <MenuItem className="data-[focus]:bg-gray-600 data-[focus]:text-white p-2">
        <div>Support</div>
      </MenuItem>
      <MenuItem className="data-[focus]:bg-gray-600 data-[focus]:text-white p-2">
        <div>License</div>
      </MenuItem>
    </MenuItems>
  </Menu>
  <button className="border p-2 bg-gray-100">After</button>
</div>

Steps to reproduce: