-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MenuLink with NextJS's next/link #951
Comments
Thanks for your question! Would you mind implementing your example in a workable CodeSandbox Template so we can take a look at it? |
@indiesquidge voila! apologies for the delay. have a little something working here. let me know if anything is broken. the relevant link logic is in https://codesandbox.io/s/mystifying-mendel-457b7x one thing i noticed was that the |
@indiesquidge ever get the chance to take a look through this? |
@sgrund14 thanks for creating an example sandbox! I can look into this in the next few days |
awesome, thanks @indiesquidge ! |
@sgrund14 I think this is a matter of your wrapping component swallowing the props that const WrappedLinkWithForwardRef = React.forwardRef<
HTMLAnchorElement,
WrappedLinkProps
>(function WrappedLink(props, ref) {
+ const { href, children, ...rest } = props;
return (
- <Link href={props.href}>
+ <Link href={href}>
- <a ref={ref}>{props.children}</a>
+ <a {...rest} ref={ref}>{children}</a>
</Link>
);
}); |
Building on @indiesquidge's solution, here's a complete example from @proper-px: import Link from 'next/link';
import { forwardRef } from 'react';
import {
Menu,
MenuList,
MenuButton,
MenuItem,
MenuLink,
} from '@reach/menu-button';
interface MenuLinkA11yProps {
href: string;
children: React.ReactNode;
}
const MenuLinkA11y = forwardRef<HTMLAnchorElement, MenuLinkA11yProps>(
({ href, children, ...rest }, ref) => {
return (
// The Reach UI <Link> component is required for client-side routing to
// work with the Next.js 12 "Pages Router".
<Link href={href}>
<a ref={ref} {...rest}>
{children}
</a>
</Link>
);
}
);
const signOut = () => {
// ...
};
const renderMenu = () => (
<Menu>
<MenuButton>
Open menu
</MenuButton>
<MenuList>
<MenuLink as={MenuLinkA11y} href="/foo">
Foo
</MenuLink>
<MenuLink as={MenuLinkA11y} href="/bar">
Bar
</MenuLink>
<MenuItem onSelect={signOut}>Sign Out</MenuItem>
</MenuList>
</Menu>
); For me, the above is fully keyboard accessible and works with client-side routing that uses the Next.js Pages Router. I'm running Next.js version |
❓Question
Hello! I am having a bit of trouble getting the
MenuLink
component to work with the nextJS Link component, which does not currently support the forwardRef API so I've been messing around a little bit following this thread vercel/next.js#9784This code renders my menu fine, but it is no longer wrapped in the [data-reach-menu-item] div and doesn't receive focus when tabbing through the menu :(
Any ideas / experience using nextjs and reach ui together?
The text was updated successfully, but these errors were encountered: