# Link API

> Explore the API of Cimpress UI's link component.

Source: https://ui.cimpress.io/components/link/api/

## Import

```tsx
import { Link } from '@cimpress-ui/react';
```

## Accessibility notes

`Link` component can be activated by pressing the Enter key, similar to a native HTML link.

## Client-side routing

`Link` uses the client-side router configured with `RouterProvider`.
If you need features from your router's own link component, such as prefetching, use the `render` prop to render that component instead.
See the [client-side routing guide](/dev-guides/client-side-routing#rendering-router-links) for details.

```tsx
import NextLink from 'next/link';

<Link href="/about" render={(props) => ('href' in props ? <NextLink {...props} prefetch /> : <span {...props} />)}>
  About
</Link>;
```

## Style inheritance

Links rendered within the [text component](/components/text) will inherit the style of the surrounding text.
Certain styles like color and text decoration will not be inherited, so that links remain visually distinct.

The example below renders two paragraphs of text with nested links. Notice that the links match the style of the containing paragraph, even though the link markup doesn't change.

```tsx
import { Link, Stack, Text } from '@cimpress-ui/react';

export default function Demo() {
  return (
    <Stack gap={16}>
      <Text as="p" variant="title-5" tone="base">
        This paragraph contains a <Link href="#">nested link</Link> that inherits its style.
      </Text>

      <Text as="p" variant="medium" tone="subtle" fontStyle="italic">
        This paragraph also contains a <Link href="#">nested link</Link> that inherits its style.
      </Text>
    </Stack>
  );
}
```

## API reference

### Link

Displays a textual link that allows users to navigate to another page or resource.

See [link usage guidelines](https://ui.cimpress.io/components/link/).

- `ref`: `Ref<HTMLAnchorElement>`

  The `ref` type for this component.

**Props: `LinkProps`**

- `href` (required): `string`

  A URL to link to.

- `children` (required): `ReactNode`

  Text to display. Allows nesting other typography components.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `routerOptions`: `undefined`

  Options for the configured client side router.

- `iconStart`: `ReactNode`

  An icon displayed before the link text.

- `iconEnd`: `ReactNode`

  An icon displayed after the link text.

- `render`: `(props: (DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>) | (DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement>), renderProps: LinkRenderProps) => ReactElement`

  Renders a custom element, e.g. your router's link component. Render an `<a>` if `'href' in props`, otherwise a `<span>`.

  See [client-side routing guide](https://ui.cimpress.io/dev-guides/client-side-routing/#rendering-router-links).

- `variant`: `'small' | 'medium' | 'title-1' | 'title-2' | 'title-3' | 'title-4' | 'title-5' | 'title-6' | 'body' | 'body-semibold' | 'medium-semibold' | 'small-semibold'`

  Determines the visual variant of text.

- `fontStyle`: `'normal' | 'italic'`

  Whether the font should be styled with a normal or italic face.

- `alignment`: `'center' | 'end' | 'start' | 'justify'`

  Horizontal alignment of text.

- `hrefLang`: `string`

  Hints at the human language of the linked URL.
  See[MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#hreflang).

- `target`: `HTMLAttributeAnchorTarget`

  The target window for the link. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target).

- `rel`: `string`

  The relationship between the linked resource and the current page. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).

- `download`: `string | boolean`

  Causes the browser to download the linked URL. A string may be provided to suggest a file name.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#download).

- `ping`: `string`

  A space-separated list of URLs to ping when the link is followed. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#ping).

- `referrerPolicy`: `HTMLAttributeReferrerPolicy`

  How much of the referrer to send when following the link. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#referrerpolicy).

- `onHoverStart`: `(e: HoverEvent) => void`

  Handler that is called when a hover interaction starts.

- `onHoverEnd`: `(e: HoverEvent) => void`

  Handler that is called when a hover interaction ends.

- `onPress`: `(e: PressEvent) => void`

  Handler that is called when the press is released over the target.

- `isDisabled`: `boolean`

  Whether the link is disabled.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
