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