| 54 | * or application. |
| 55 | */ |
| 56 | export function useLink(props: AriaLinkOptions, ref: RefObject<FocusableElement | null>): LinkAria { |
| 57 | let { |
| 58 | elementType = 'a', |
| 59 | onPress, |
| 60 | onPressStart, |
| 61 | onPressEnd, |
| 62 | onClick, |
| 63 | isDisabled, |
| 64 | ...otherProps |
| 65 | } = props; |
| 66 | |
| 67 | let linkProps: DOMAttributes = {}; |
| 68 | if (elementType !== 'a') { |
| 69 | linkProps = { |
| 70 | role: 'link', |
| 71 | tabIndex: !isDisabled ? 0 : undefined |
| 72 | }; |
| 73 | } |
| 74 | let {focusableProps} = useFocusable(props, ref); |
| 75 | let {pressProps, isPressed} = usePress({ |
| 76 | onPress, |
| 77 | onPressStart, |
| 78 | onPressEnd, |
| 79 | onClick, |
| 80 | isDisabled, |
| 81 | ref |
| 82 | }); |
| 83 | let domProps = filterDOMProps(otherProps, {labelable: true}); |
| 84 | let interactionHandlers = mergeProps(focusableProps, pressProps); |
| 85 | let router = useRouter(); |
| 86 | let routerLinkProps = useLinkProps(props); |
| 87 | |
| 88 | return { |
| 89 | isPressed, // Used to indicate press state for visual |
| 90 | linkProps: mergeProps(domProps, routerLinkProps, { |
| 91 | ...interactionHandlers, |
| 92 | ...linkProps, |
| 93 | 'aria-disabled': isDisabled || undefined, |
| 94 | 'aria-current': props['aria-current'], |
| 95 | onClick: (e: React.MouseEvent<HTMLAnchorElement>) => { |
| 96 | pressProps.onClick?.(e); |
| 97 | handleLinkClick(e, router, props.href, props.routerOptions); |
| 98 | } |
| 99 | }) |
| 100 | }; |
| 101 | } |