published on in news

useLink

Provides the behavior and accessibility implementation for a link component. A link allows a user to navigate to another page or resource within a web page or application.

useLink(props: AriaLinkProps): LinkAria

Links can be created in HTML with the <a> element with an href attribute. However, if the link does not have an href, and is handled client side with JavaScript instead, it will not be exposed to assistive technology properly. useLink helps achieve accessible links with either native HTML elements or custom element types.

  • Support for mouse, touch, and keyboard interactions
  • Support for navigation links via <a> elements or custom element types via ARIA
  • Support for disabled links

A link consists of a pressable area usually containing a textual label or an icon that users can click or tap to navigate to another page or resource. In addition, keyboard users may activate links using the Enter key.

useLink returns props to be spread onto the link element:

NameTypeDescription
linkPropsHTMLAttributes<HTMLDivElement>Props for the link element.

If a visual label is not provided (e.g. an icon or image only link), then an aria-label or aria-labelledby prop must be passed to identify the link to assistive technology.

This example shows a basic link using a native <a> element.

function Link(props) { let {linkProps} = useLink(props); return ( <a {...linkProps} href={props.href} target={props.target} style={{color: 'dodgerblue'}}> {props.children} </a> ); } <Link href="https://adobe.com" target="_blank">Adobe</Link>

This example shows a client handled link using press events. It sets elementType to span so that useLink returns the proper ARIA attributes to expose the element as a link to assistive technology.

function Link(props) { let {linkProps} = useLink({...props, elementType: 'span'}); return ( <span {...linkProps} style={{ color: 'dodgerblue', textDecoration: 'underline', cursor: 'pointer' }}> {props.children} </span> ); } <Link onPress={() => alert('Pressed link')}>Adobe</Link>

A link can be disabled by passing the isDisabled property. This will work with both native link elements as well as client handled links. Native navigation will be disabled, and the onPress event will not be fired. The link will be exposed as disabled to assistive technology with ARIA.

function Link(props) { let {linkProps} = useLink(props); return ( <a {...linkProps} href={props.href} target={props.target} style={{ color: props.isDisabled ? 'gray' : 'dodgerblue', cursor: props.isDisabled ? 'default' : 'pointer' }}> {props.children} </a> ); } <Link href="https://adobe.com" target="_blank" isDisabled>Disabled link</Link>
NameTypeDescription
isDisabledbooleanWhether the link is disabled.
elementTypestringThe HTML element used to render the link, e.g. "a", or "span".
refRefObject<HTMLElementnull>A ref to the link element.
childrenReactNodeThe content to display in the link.
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.
idstring
tabIndexnumber
rolestring
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.
NameTypeDescription
type'pressstart' | 'pressend' | 'pressup' | 'press'The type of press event being fired.
pointerTypePointerTypeThe pointer type that triggered the press event.
targetHTMLElementThe target element of the press event.
shiftKeybooleanWhether the shift keyboard modifier was held during the press event.
ctrlKeybooleanWhether the ctrl keyboard modifier was held during the press event.
metaKeybooleanWhether the meta keyboard modifier was held during the press event.
'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual'
NameTypeDescription
linkPropsHTMLAttributes<HTMLDivElement>Props for the link element.

ncG1vNJzZmiqlZawtb%2FPnpqtqqWie6O4zptlnKeimnu4tc2dprCrXqOytXvRnpicrKOlsqTA0a6kaG5kan1xgMGeb2xtZWivcYWVbJmea2Vnf3KDl26dnJpma4V5gpRxmXJnlKSwtHvRnpicrF2Wv6qtjq6qnoSZo7hvtNOmow%3D%3D