disable Link component #21396
Replies: 16 comments 19 replies
-
I also want to disable Next.js link but still show the url. |
Beta Was this translation helpful? Give feedback.
-
I passed empty object in
|
Beta Was this translation helpful? Give feedback.
-
You can create a wrapper component for the Link to disable it conditionally like following
|
Beta Was this translation helpful? Give feedback.
-
I just changed the
|
Beta Was this translation helpful? Give feedback.
-
This suggestion is a little bit hacky, and only works in some scenarios but I just change styling if a value is passed through <Link style={{
textDecoration: href ? "underline" : "none",
cursor: href ? "pointer" : "default"
}} href={href ? href : {}}>
<Text_Component
font_size={font_size}
line_height={line_height}
font_weight={font_weight}
color={color}
text={text}
align={align}
hover={href}
>
</Text_Component>
</Link> Of course this is not the best way and the best way would be to return a different element if |
Beta Was this translation helpful? Give feedback.
-
Yeah I wrote something like this for a shadcn button / link that can be disabled type PaginationButtonProps =
| (ComponentProps<typeof Link> & { disabled?: false })
| (ComponentPropsWithoutRef<'button'> & { disabled: true })
function PaginationButton(props: PaginationButtonProps) {
if (props.disabled) {
const { disabled, ...rest } = props
return (
<Button disabled={disabled} asChild>
<button {...rest} />
</Button>
)
}
return (
<Button asChild>
<Link {...props} />
</Button>
)
} Used like this for example: <ButtonGroup>
<PaginationButton
href={`?page=${page - 1}`}
disabled={isFirstPage}
>
Previous
</PaginationButton>
<PaginationButton
href={`?page=${page + 1}`}
disabled={isLastPage}
>
Next
</PaginationButton>
</ButtonGroup> |
Beta Was this translation helpful? Give feedback.
-
Using css: a.disabled {
pointer-events: none;
} Then component: <Link className={clsx(p.disabled && "disabled")} aria-disabled={p.disabled}>...</Link> Alt using tailwind utilities: <Link className={clsx(p.disabled && "pointer-events-none")} aria-disabled={p.disabled}>...</Link> Alt. using style: <Link style={{pointerEvents: "none"}} aria-disabled={p.disabled}>...</Link>
|
Beta Was this translation helpful? Give feedback.
-
Update: <Link
key={title}
href={route}
className={`btn-primary flex items-center ${
pathname === route && 'pointer-events-none opacity-20'
}`}
>
<span className='hidden md:inline mr-1'>{title}</span>
<button.icon className='inline' fontSize={20} />
</Link> |
Beta Was this translation helpful? Give feedback.
-
My solution:
Use mode:
|
Beta Was this translation helpful? Give feedback.
-
Maybe as a solution:
|
Beta Was this translation helpful? Give feedback.
-
This works for me.
|
Beta Was this translation helpful? Give feedback.
-
we can use e.Preventdefault() and can apply condition to diable the link on onClicking event. { if (loading) e.preventDefault(); }} > Forgot Password? |
Beta Was this translation helpful? Give feedback.
-
Why is this so hard |
Beta Was this translation helpful? Give feedback.
-
You can use Tailwind to conditionally ignore pointer event:
|
Beta Was this translation helpful? Give feedback.
-
Try pointerEvents , it disables the link
|
Beta Was this translation helpful? Give feedback.
-
works for me with the shadcn button |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to disable the Link component? I thought if
href
is#
, it would disable the Link component. However, very surprisingly, the actual link in this becomes thepathname
of the current page and it leads on a runtime error on click the link.Beta Was this translation helpful? Give feedback.
All reactions