Link

Links allow users to navigate to a different location. They can be presented inline inside a paragraph or as standalone text.

installyarn add @react-spectrum/link
version3.0.0-alpha.1
usageimport {Link} from '@react-spectrum/link'

Example#


<Link><a href="https://www.imdb.com/title/tt6348138/" target="_blank">The missing link.</a></Link>

Content#


Links accept content as children. If the child is a native anchor element, or a link component used with a client side router, the Link component will add spectrum styles and event handlers to that element. Actual navigation will be handled by the wrapped element.

<Link><a href="https://www.adobe.com" target="_blank">Adobe.com</a></Link>

To use in React Router

import {Link} from '@react-spectrum/link';
import {Link as RouterLink} from 'react-router-dom';

<Link><RouterLink to="/next-page">Next Page</RouterLink></Link>

If the content is plain text, it will be styled and exposed to assistive technologies as a link. Events will need to be handled in JavaScript with the onPress prop. Note: this will not behave like a native link. Browser features like context menus and open in new tab will not apply.

<Link onPress={() => alert('pressed!')}>A label</Link>

Internationalization#

In order to internationalize a link, a localized string should be passed to the children prop.

Accessibility#

For string-typed children, the link component will expose the accessible role to assistive technology as a "link".

<Link onPress={e => alert(`clicked "${e.target.textContent}" Link`)}>
  I forgot my password
</Link>

For other children, use an element of semantic meaning, or use a role where appropriate.

Be mindful of what the experience is for users navigating with screen readers. Make sure to give enough context within the link about where the link will take the user.

Props#


NameTypeDefaultDescription
variant'primary''secondary''overBackground'"primary"The visual style of the link.
isQuietbooleanWhether the link should be displayed with a quiet style.
childrenReactNodeThe content to display in the link.
UNSAFE_classNamestring
UNSAFE_styleCSSProperties
Events
NameTypeDefaultDescription
onPress(e: PressEvent) => voidHandler that is called when the press is released over the target.
onPressStart(e: PressEvent) => voidHandler that is called when a press interaction starts.
onPressEnd(e: PressEvent) => voidHandler that is called when a press interation ends, either over the target or when the pointer leaves the target.
onPressChange(isPressed: boolean) => voidHandler that is called when the press state changes.
onPressUp(e: PressEvent) => voidHandler that is called when a press is released over the target, regardless of whether it started on the target or not.
Layout
NameTypeDefaultDescription
flexstringnumberboolean
flexGrownumber
flexShrinknumber
flexBasisnumberstring
alignSelf'auto' | 'normal' | 'start' | 'end' | 'flex-start' | 'flex-end' | 'self-start' | 'self-end' | 'center' | 'stretch'
justifySelf'auto' | 'normal' | 'start' | 'end' | 'flex-start' | 'flex-end' | 'self-start' | 'self-end' | 'center' | 'left' | 'right' | 'stretch'
flexOrdernumber
gridAreastring
gridColumnstring
gridRowstring
gridColumnStartstring
gridColumnEndstring
gridRowStartstring
gridRowEndstring
Spacing
NameTypeDefaultDescription
marginDimensionValue
marginTopDimensionValue
marginLeftDimensionValue
marginRightDimensionValue
marginBottomDimensionValue
marginStartDimensionValue
marginEndDimensionValue
marginXDimensionValue
marginYDimensionValue
Sizing
NameTypeDefaultDescription
widthDimensionValue
minWidthDimensionValue
maxWidthDimensionValue
heightDimensionValue
minHeightDimensionValue
maxHeightDimensionValue
Positioning
NameTypeDefaultDescription
position'static' | 'relative' | 'absolute' | 'fixed' | 'sticky'
topDimensionValue
bottomDimensionValue
leftDimensionValue
rightDimensionValue
startDimensionValue
endDimensionValue
zIndexnumber
isHiddenboolean
Accessibility
NameTypeDefaultDescription
rolestring
idstring
tabIndexnumber
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-ownsstringIdentifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship between DOM elements where the DOM hierarchy cannot be used to represent the relationship.
aria-hiddenboolean'false''true'Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.

Visual options#


View Guidelines

Primary#

<p>Would you like to <Link variant="primary">learn more</Link> about this fine component?</p>

Secondary#

<p>Would you like to <Link variant="secondary">learn more</Link> about this fine component?</p>

Over background#

<View backgroundColor="positive" padding="25px">
  <Link variant="overBackground">Learn more here!</Link>
</View>

quiet#

<p>Would you like to <Link isQuiet>learn more</Link> about this fine component?</p>