If true
, the link will open in new tab
Link
Link is an accessible element for navigation.
Imports#
import { Link } from '@chakra-ui/react'import { ExternalLinkIcon } from '@chakra-ui/icons'
Usage#
<Link>Chakra UI</Link>
External Link#
<Link href='https://chakra-ui.com' isExternal>Chakra Design system <ExternalLinkIcon mx='2px' /></Link>
Link inline with text#
<Text>Did you know that{' '}<Link color='teal.500' href='#'>links can live inline with text</Link></Text>
Usage with Routing Library#
To use the Link
with a routing library like Reach Router or React Router, all
you need to do is pass the as
prop. It'll replace the rendered a
tag with
Reach's Link
.
import { Link as ReactRouterLink } from 'react-router-dom'import { Link as ChakraLink, LinkProps } from '@chakra-ui/react'<ChakraLink as={ReactRouterLink} to='/home'>Home</ChakraLink>
Usage with Next.js#
As of Next.js 13, the Link
component directly renders an a
element,
therefore its child can no longer be another a
element. The recommended
way is to use the as
property on Chakra UI components used as a link. Here
is an example using the Link
component:
import NextLink from 'next/link'import { Link } from '@chakra-ui/react'<Link as={NextLink} href='/home'>Home</Link>
If you are using Next.js 13 and do not want to use the new behavior, you can
either use the legacyBehavior
prop directly on the Next.js Link
component
or if you want to set this behavior globally you can use the following Next.js
configuration:
{experimental: { newNextLinkBehavior: false }}
If you are using a version of Next.js below the version 13 you can use the Chakra
UI Link
this way:
import NextLink from "next/link"<NextLink href='/home' passHref><Link>Home</Link></NextLink>
Another way to style the new Next.js Link
component is to create a custom
component using the Chakra Factory function:
import NextLink, { type LinkProps as NextLinkProps } from 'next/link'import { chakra } from '@chakra-ui/react'// wrap the NextLink with Chakra UI's factory functionconst MagicLink = chakra<typeof NextLink, NextLinkProps>(NextLink, {// ensure that you're forwarding all of the required props for your caseshouldForwardProp: (prop) => ['href', 'target', 'children', ...].includes(prop),})// use the MagicLink just like you'd use the ordinary Chakra UI link<MagicLinkhref='...'color='...'target='...'>...</MagicLink>
Props#
The Link component composes the Box
component.
isExternal
isExternal
boolean
false
The Link
component is a single part component. All of the styling is applied
directly to the anchor
element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of the Link
component are:
variant
: The visual variant of the Link component.colorScheme
: The color scheme of the Link component.size
: The size of the Link component.
Note: Theming properties for Link component are not implemented in the default theme. You can extend the theme to implement them.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Adding a custom size#
Let's assume we want to include an extra large link size. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const xl = defineStyle({fontSize: 'xl',})export const linkTheme = defineStyleConfig({sizes: { xl },})// Now we can use the new `xl` size<Link size="xl">...</Link>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom branded variant. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const brandPrimary = defineStyle({textDecoration: 'underline',color: 'white',fontFamily: 'serif',fontWeight: 'normal',// let's also provide dark mode alternatives_dark: {color: 'orange.800',}})export const linkTheme = defineStyleConfig({variants: { brandPrimary },})// Now we can use the new `brandPrimary` variant<Link variant="brandPrimary">...</Link>
Using a custom color scheme#
Let's assume we want to use our own custom color scale based on our brand. We'd need to define the color scale first in the main theme file:
import { extendTheme } from '@chakra-ui/react'export const theme = extendTheme({colors: {brand: {50: '#f7fafc',...500: '#718096',...900: '#171923',}}})
Then, we can use the custom color scale as the color scheme for the Link component:
<Link colorScheme='brand'>...</Link>
Setting default properties#
Let's assume that we want to set the default size, variant or color scheme of every link in our app. Here's how we can do that:
import { defineStyleConfig } from '@chakra-ui/react'export const linkTheme = defineStyleConfig({defaultProps: {size: 'xl',variant: 'brandPrimary',colorScheme: 'brand',},})// This saves you time, instead of manually setting the size,// variant and color scheme every time you use a Link component:<Link size="xl" variant="brandPrimary" colorScheme="brand">...</Link>
Showcase#
import { Box, SimpleGrid, IconButton, Link, Text, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; import { ExternalLinkIcon } from "@chakra-ui/icons"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <Link textDecoration={"underline"} href="https://chakra-ui.com"> themed underline link </Link> <Link href="https://chakra-ui.com" isExternal> themed external link <ExternalLinkIcon mx="2px" /> </Link> <Text> Themed Link{" "} <Link href="https://chakra-ui.com"> with inline text </Link> </Text> <Link href="https://chakra-ui.com" variant="custom"> themed link with custom variant </Link> <Link href="https://chakra-ui.com" size="xl"> link size xl </Link> </SimpleGrid> <IconButton rounded="full" aria-label="change theme" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }