'use client'; import { type FocusableElement } from '@react-types/shared'; import React, { ForwardedRef, forwardRef } from 'react'; import { mergeProps, useFocusRing, useLink, useObjectRef } from 'react-aria'; import { ArrowRightIcon } from '../icon/index.js'; import { styles as linkStyles } from './link.styles.js'; import { LinkComponent, LinkImplementationProps } from './link.types.js'; export function BaseLink( { className, children, href, tag: Tag, iconBefore: IconBefore, iconAfter: IconAfter, iconSize = 'small', target, type = 'standalone', underline = true, autoFocus, elementType, onBlur, onClick, onFocus, onFocusChange, onKeyDown, onKeyUp, onPress, onPressChange, onPressEnd, onPressStart, onPressUp, download, ping, referrerPolicy, rel, routerOptions, ...componentProps }: LinkImplementationProps, ref: ForwardedRef, ) { const Component = Tag ?? 'a'; const ariaElementType = typeof Component === 'string' ? Component : (elementType ?? 'a'); const linkRef = useObjectRef(ref as ForwardedRef); const { linkProps } = useLink( { ...componentProps, autoFocus, download, elementType: ariaElementType, href: typeof href === 'string' ? href : undefined, onBlur, onClick, onFocus, onFocusChange, onKeyDown, onKeyUp, onPress, onPressChange, onPressEnd, onPressStart, onPressUp, ping, referrerPolicy, rel, routerOptions, target, }, linkRef, ); const { isFocusVisible, focusProps } = useFocusRing(); const styles = linkStyles({ type, underline, isFocusVisible }); if (type === 'standalone' && !IconBefore && !IconAfter) { IconBefore = ArrowRightIcon; } // Custom components (e.g. NextLink) resolve their own href, so keep it untransformed by RouterProvider's useHref const customComponentProps = typeof Component === 'string' ? {} : { href }; return ( {IconBefore && } {children} {IconAfter && } ); } export const Link = forwardRef(BaseLink) as unknown as LinkComponent;