From 8bd2335b95f010f22b442b2b9f8e4ce86baee03c Mon Sep 17 00:00:00 2001 From: Charles de Dreuille Date: Sun, 30 Nov 2025 09:01:03 +0000 Subject: [PATCH] Improve Link component to add title Signed-off-by: Charles de Dreuille --- packages/ui/src/components/Link/Link.tsx | 56 ++++++++++++++++++------ packages/ui/src/components/Link/types.ts | 7 +++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/packages/ui/src/components/Link/Link.tsx b/packages/ui/src/components/Link/Link.tsx index 56b0cae5bb..fbd77bdaff 100644 --- a/packages/ui/src/components/Link/Link.tsx +++ b/packages/ui/src/components/Link/Link.tsx @@ -14,8 +14,9 @@ * limitations under the License. */ -import { forwardRef } from 'react'; -import { Link as AriaLink, RouterProvider } from 'react-aria-components'; +import { forwardRef, useRef } from 'react'; +import { useLink } from 'react-aria'; +import { RouterProvider } from 'react-aria-components'; import clsx from 'clsx'; import { useStyles } from '../../hooks/useStyles'; import { LinkDefinition } from './definition'; @@ -37,29 +38,56 @@ export const Link = forwardRef((props, ref) => { }, ); - const { className, href, ...restProps } = cleanedProps; + const { + className, + href, + title, + children, + onPress, + variant, + weight, + color, + truncate, + slot, + ...restProps + } = cleanedProps; const isExternal = isExternalLink(href); + const internalRef = useRef(null); + const linkRef = (ref || internalRef) as React.RefObject; - const component = ( - + // Use useLink hook to get link props + const { linkProps } = useLink( + { + href, + onPress, + ...restProps, + }, + linkRef, ); - // If it's an external link, render AriaLink without RouterProvider + const anchorElement = ( + + {children} + + ); + + // If it's an external link, render without RouterProvider if (isExternal) { - return component; + return anchorElement; } // For internal links, use RouterProvider return ( - {component} + {anchorElement} ); }); diff --git a/packages/ui/src/components/Link/types.ts b/packages/ui/src/components/Link/types.ts index 33c33b26c0..796676e593 100644 --- a/packages/ui/src/components/Link/types.ts +++ b/packages/ui/src/components/Link/types.ts @@ -22,6 +22,7 @@ import type { TextWeights, } from '../../types'; import type { LinkProps as AriaLinkProps } from 'react-aria-components'; +import type { ReactNode } from 'react'; /** @public */ export interface LinkProps extends AriaLinkProps { @@ -32,4 +33,10 @@ export interface LinkProps extends AriaLinkProps { | TextColorStatus | Partial>; truncate?: boolean; + + // This is used to set the title attribute on the link + title?: string; + + // This is used to set the children of the link + children?: ReactNode; }