Created
August 22, 2022 03:11
-
-
Save lawrencecchen/f9a1d1c6c91ea22a4303c02d61a26b9e to your computer and use it in GitHub Desktop.
a next.js link that can be disabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm i clsx react-aria | |
import clsx from 'clsx'; | |
import Link, { LinkProps } from 'next/link'; | |
import React, { AnchorHTMLAttributes, useRef } from 'react'; | |
import { type AriaLinkOptions, useLink } from 'react-aria'; | |
export default function LLink( | |
props: AnchorHTMLAttributes<HTMLAnchorElement> & { | |
children: React.ReactNode; | |
next?: LinkProps; | |
aria?: AriaLinkOptions; | |
} | |
) { | |
const { children, next, aria, ...attrs } = props; | |
const ref = useRef<HTMLAnchorElement | null>(null); | |
const { linkProps } = useLink(aria ?? {}, ref); | |
const className = clsx(props.className, 'transition', { | |
'opacity-50 cursor-default': props.aria?.isDisabled, | |
}); | |
if (next) { | |
return ( | |
<Link {...attrs} {...linkProps} {...next} className={className}> | |
{children} | |
</Link> | |
); | |
} | |
return ( | |
<a {...attrs} {...linkProps} className={className}> | |
{children} | |
</a> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment