question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to unmount Menu.Item

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
RobinMalfaitcommented, Oct 29, 2020

Hey! Thank you for your bug report! Much appreciated! 🙏

The unmount prop feature is not yet released, but unmount={true} is the default behaviour. The idea is that when you add unmount={true} you will unmount the DOM nodes when the Menu.Items should be invisible. When you set unmount={false} then the DOM nodes will still be they will be marked with a hidden attribute and a style={{ display: 'none' }}

That said, the unmount strategy is used in a DOM perspective. So the actual React node will still be there, as far as I know you can’t unmount yourself. Reason being is that once you provide a prop to the Menu.Items component, the logic happening is already inside the component, so you are already too late if that makes sense. So instead we hide/unmount the children of the Menu.Items component.

I know you can do some magic where you tell your parent that you don’t want to be rendered anymore I think. But since we are not always a direct child of the parent then this can get hairy.

I’m not sure if this answers your question.

0reactions
RobinMalfaitcommented, Nov 23, 2020

Hey! Sorry that I didn’t respond to the issue in a while.

Indeed, there is no other (good) solution to this as far as I know. Another solution I know of is looping through all the children recursively and removing them but I don’t think that’s a good solution.

I’m afraid that you will need to keep using the solution with the more verbose syntax you initially used.

If other issues are present, feel free to open a new issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove unused Help menu item from SwiftUI app
You can use the UIMenuBuilder api to do this. "To add and remove menus from the menu bar using the main menu system,...
Read more >
How to remove menu item? · Issue #3221 · Klipper3d/klipper
With the new menu system where list items are automatically appended, it does not appear to be possible to remove an item from...
Read more >
remove_menu_page() | Function
Removes a top-level admin menu. ... To remove only certain menu items include only those you want to hide within the function. To...
Read more >
Wagtail - How To Remove Sub-Menu Items From Settings Menu
Create a Wagtail group (Groups under the Settings menu). When assigning object permissions for the group, make sure Site Settings is not ...
Read more >
Menu items not deleting and not appearing in correct ... - Drupal
... it doesn't remove itself from the menu? And while I am able to disable that menu item in the menu editor, I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found