The Menu auto-close behaviour when wrapping a Link #2942
-
I'm trying to use custom Link component with import { Menu } from '@headlessui/react';
import { Link } from "@remix-run/react";
<Menu.Item
key={item.name}
as={Link}
to={item.href}>
{item.name}
</Menu.Item> This works fine. Above this, I'm looking to wrap the import { RemixLinkProps } from "@remix-run/react/dist/components";
import { forwardRef } from 'react';
type MyLinkProps = RemixLinkProps & React.RefAttributes<HTMLAnchorElement>;
const MyLink = forwardRef(
function MyLink({ children, to }: MyLinkProps, ref: React.ForwardedRef<HTMLAnchorElement>) {
return (
<Link to={to} ref={ref}>
{children}
</Link>
);
}
); I start using the wrapper: <Menu.Item
key={item.name}
as={MyLink}
to={item.href}>
{item.name}
</Menu.Item> At this point, click |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I managed to resolve this myself. We need to pass the rest of the parameters to the link component: const MyLink = forwardRef(
function MyLink({ children, to, ...restProps }: MyLinkProps, ref: React.ForwardedRef<HTMLAnchorElement>) {
return (
<Link to={to} ref={ref} { ...restProps }>
{children}
</Link>
);
}
); I'm leaving the thread here, in case the powers that be deems this to be worth leaving. Otherwise, please remove this. |
Beta Was this translation helpful? Give feedback.
I managed to resolve this myself. We need to pass the rest of the parameters to the link component:
I'm leaving the thread here, in case the powers that be deems this to be worth leaving. Otherwise, please remove this.