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.
install | yarn add @react-aria/link |
---|---|
version | 3.0.0-alpha.1 |
usage | import {useLink} from '@react-aria/link' |
API#
useLink(props: AriaLinkProps): LinkAria
Features#
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
Anatomy#
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:
Name | Type | Description |
linkProps | HTMLAttributes<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.
Example#
This example shows a basic link using a native <a>
element.
function Link(props) {
let {linkProps} = useLink(props);
return (
<a
...linkProps
href= propshref
target= propstarget
style={color: 'dodgerblue'}>
propschildren
</a>
);
}
<Link href="https://adobe.com" target="_blank">Adobe</Link>
Client handled links#
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'
}>
propschildren
</span>
);
}
<Link onPress=() => alert('Pressed link')>Adobe</Link>
Disabled links#
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= propshref
target= propstarget
style={
color: propsisDisabled ? 'gray' : 'dodgerblue'
cursor: propsisDisabled ? 'default' : 'pointer'
}>
propschildren
</a>
);
}
<Link href="https://adobe.com" target="_blank" isDisabled>Disabled link</Link>
Name | Type | Description |
isDisabled | boolean | Whether the link is disabled. |
elementType | string | The HTML element used to render the link, e.g. "a", or "span". |
ref | RefObject<HTMLElement | null> | A ref to the link element. |
children | ReactNode | The content to display in the link. |
onPress | (e: PressEvent) => void | Handler that is called when the press is released over the target. |
onPressStart | (e: PressEvent) => void | Handler that is called when a press interaction starts. |
onPressEnd | (e: PressEvent) => void | Handler that is called when a press interation ends, either over the target or when the pointer leaves the target. |
onPressChange | (isPressed: boolean) => void | Handler that is called when the press state changes. |
onPressUp | (e: PressEvent) => void | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
id | string | |
tabIndex | number | |
role | string | |
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-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-owns | string | Identifies 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-hidden | boolean | 'false' | 'true' | Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable. |
Name | Type | Description |
linkProps | HTMLAttributes<HTMLDivElement> | Props for the link element. |