Skip to content

Commit 24fd1a5

Browse files
authored
Expose isPressed on useLink hook (#1767)
1 parent 269980f commit 24fd1a5

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

packages/@react-aria/link/docs/useLink.mdx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,22 @@ This example shows a client handled link using press events. It sets `elementTyp
9494
so that `useLink` returns the proper ARIA attributes to expose the element as a link to
9595
assistive technology.
9696

97+
In addition, this example shows usage of the `isPressed` value returned by `useLink` to properly
98+
style the links's active state. You could use the CSS `:active` pseudo class for this, but `isPressed`
99+
properly handles when the user drags their pointer off of the link, along with keyboard support and better
100+
touch screen support.
101+
97102
```tsx example
98103
function Link(props) {
99104
let ref = React.useRef();
100-
let {linkProps} = useLink({...props, elementType: 'span'}, ref);
105+
let {linkProps, isPressed} = useLink({...props, elementType: 'span'}, ref);
101106

102107
return (
103108
<span
104109
{...linkProps}
105110
ref={ref}
106111
style={{
107-
color: 'var(--blue)',
112+
color: isPressed ? 'var(--blue)' : 'var(--spectrum-global-color-blue-700)',
108113
textDecoration: 'underline',
109114
cursor: 'pointer'
110115
}}>

packages/@react-aria/link/src/useLink.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export interface AriaLinkOptions extends AriaLinkProps {
2727

2828
export interface LinkAria {
2929
/** Props for the link element. */
30-
linkProps: HTMLAttributes<HTMLElement>
30+
linkProps: HTMLAttributes<HTMLElement>,
31+
/** Whether the link is currently pressed. */
32+
isPressed: boolean
3133
}
3234

3335
/**
@@ -55,10 +57,11 @@ export function useLink(props: AriaLinkOptions, ref: RefObject<HTMLElement>): Li
5557
};
5658
}
5759

58-
let {pressProps} = usePress({onPress, onPressStart, onPressEnd, isDisabled, ref});
60+
let {pressProps, isPressed} = usePress({onPress, onPressStart, onPressEnd, isDisabled, ref});
5961
let domProps = filterDOMProps(otherProps, {labelable: true});
6062

6163
return {
64+
isPressed, // Used to indicate press state for visual
6265
linkProps: mergeProps(domProps, {
6366
...pressProps,
6467
...linkProps,

0 commit comments

Comments
 (0)