Back to Chakra Ui

Link

apps/www/content/docs/components/link.mdx

0.3.0-beta1.7 KB
Original Source
<ExampleTabs name="link-basic" />

Usage

jsx
import { Link } from "@chakra-ui/react"
jsx
<Link href="...">Click here</Link>

Examples

Variants

Use the variant prop to change the appearance of the Link component

<ExampleTabs name="link-with-variants" />

Within Text

Use Link within a text to create a hyperlink

<ExampleTabs name="link-within-text" />

External

Add an external link icon to the Link component

<ExampleTabs name="link-with-external" />

Guides

Routing Library

Use the asChild prop to compose Link with framework links like (Next.js)

jsx
import { Link as ChakraLink } from "@chakra-ui/react"
import NextLink from "next/link"

const Demo = () => {
  return (
    <ChakraLink asChild>
      <NextLink href="/about">Click here</NextLink>
    </ChakraLink>
  )
}

Use the _currentPage condition to style active links when using aria-current="page".

jsx
<Link
  href="/home"
  aria-current="page"
  _currentPage={{ color: "blue.500", fontWeight: "bold" }}
>
  Home
</Link>

With routing libraries, set aria-current based on the current route:

jsx
import { Link as ChakraLink } from "@chakra-ui/react"
import NextLink from "next/link"
import { usePathname } from "next/navigation"

const NavLink = ({ href, children }) => {
  const pathname = usePathname()
  const isActive = pathname === href

  return (
    <ChakraLink asChild>
      <NextLink
        href={href}
        aria-current={isActive ? "page" : undefined}
        _currentPage={{ color: "blue.500", fontWeight: "bold" }}
      >
        {children}
      </NextLink>
    </ChakraLink>
  )
}

Props

<PropTable component="Link" part="Link" />