Skip to content

Ability to unmount Menu.Item #134

@rijk

Description

@rijk

From the docs I'm not sure what the unmount parameter is supposed to do, so I can't say for sure this is a bug, but here goes:

Going by this example: https://github.com/tailwindlabs/headlessui/blob/develop/packages/%40headlessui-react/pages/menu/menu-with-framer-motion.tsx, you need a render prop to get framer-motion exit animations to work:

<Menu>
  {({ open }) => (
    <>
      <Menu.Button>...</Menu.Button>

      <AnimatePresence>
        {open && (
          <Menu.Items
            static
            as={motion.div}
            initial={{ opacity: 0, y: 0 }}
            animate={{ opacity: 1, y: '0.5rem' }}
            exit={{ opacity: 0, y: 0 }}
          >
            ...
          </Menu.Items>
        )}
      </AnimatePresence>
    </>
  )}
</Menu>

However, since framer-motion does its own mount/unmount detection, we actually don't need this, and could (in theory) just do this:

<Menu>
  <Menu.Button>...</Menu.Button>

  <AnimatePresence>
    <Menu.Items
      unmount
      as={motion.div}
      initial={{ opacity: 0, y: 0 }}
      animate={{ opacity: 1, y: '0.5rem' }}
      exit={{ opacity: 0, y: 0 }}
    >
      ...
    </Menu.Items>
  </AnimatePresence>
</Menu>

Which is of course a lot simpler and looks better in my opinion.

However, this doesn't work as the <Menu.Items> element is never unmounted from the tree, even with the unmount parameter:

image

This blocks <AnimatePresence> from briefly keeping the element in DOM and firing the exit animation.

I would expect the <Menu.Items> to disappear from the React DOM, especially when the unmount parameter is set. It's unclear to me why it hangs around, especially as the linked example removes it from the DOM as well (with the {open && (), and it seems to work just fine. Also, the behaviour doesn't seem to change when I switch to unmount={false}. So that's why I reason it's probably a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions