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.

Custom component in `as` prop of `MenuButton` doesn't work

See original GitHub issue

Description

When I supply a component to the as prop of MenuButton I expect the menu to function normally.

The documentation states:

The default MenuButton can be styled using the usual styled-system props, but it starts off plainly styled.

Using the as prop of the MenuButton, you can render a custom component instead of the default MenuButton. For instance, you can use Chakra’s Button component, or your own custom component.

Custom components must take a ref prop which is assigned to the React component that triggers the menu opening. This is so that the MenuList popover can be positioned correctly. Without this, the MenuList will render in an undefined position.

This doesn’t seem to work with a custom component (even if it is wrapped in forward ref).

Link to Reproduction

https://codesandbox.io/embed/youthful-darkness-r6ih8

Steps to reproduce

  1. Create a ref-forwarding custom wrapper around Button:
const CustomButton = forwardRef((props, ref) => {
  return <Button ref={ref}>{props.children}</Button>
})
  1. Pass it as the as prop to MenuButton and build a menu like normal. The menu will not appear to open.

Chakra UI Version

1.6.12

Browser

No response

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
martinblosteincommented, Nov 16, 2021

I see, makes total sense. From the existing documentation I assumed the ref would be used to attach a onclick listener to the element, but that wouldn’t be idiomatic. Thanks.

Basically, the custom component needs to implement the same prop interface as MenuButton. Duh!

0reactions
anubra266commented, Nov 16, 2021

@martinblostein the full return is here

Read more comments on GitHub >

github_iconTop Results From Across the Web

[MenuButton] Can't specify Button specific props with TypeScript
Describe the bug When using TypeScript, I can't specify props of the component I pass in to the as prop of the MenuButton...
Read more >
Problems with ChakraUI Custom Menu Button - Stack Overflow
Im using ChakraUI to create a menu for my website. I want to set my custom Image Component as the MenuButton ...
Read more >
Menu | Amplify UI for React
The default Menu button can be customized by importing the MenuButton component and passing it to the Menu 's trigger prop. MenuButton can...
Read more >
Menu Button - Reach UI
An accessible component for the common dropdown menu button design pattern ... Any props not listed above will be spread onto the underlying...
Read more >
FAQ - Ant Design
Here are the frequently asked questions about Ant Design and antd that you should look up before you ask in the community or...
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