import type { RouterLinkProps, RouterLinkResolved } from '@esmx/router'; import { useMemo } from 'react'; import { useRouter } from './context'; /** * Hook to create reactive link helpers for custom navigation components. * Returns a resolved link object with attributes, state, and event handlers. * * This hook is useful when you need to build custom link components * with full control over rendering while retaining router functionality. * * @param props - RouterLink properties * @returns Resolved link object with attributes, state, and navigation methods * * @example * ```tsx * import { useLink } from '@esmx/router-react'; * * function CustomNavButton({ to, children }) { * const link = useLink({ to, type: 'push', exact: 'include' }); * * return ( * * ); * } * ``` * * @example * ```tsx * // Building a custom navigation card * import { useLink } from '@esmx/router-react'; * * interface NavCardProps { * to: string; * title: string; * description: string; * icon: React.ReactNode; * } * * function NavCard({ to, title, description, icon }: NavCardProps) { * const link = useLink({ to }); * * return ( *
link.navigate(e)} * role="link" * tabIndex={0} * > *
{icon}
*

{title}

*

{description}

* {link.isExternal && } *
* ); * } * ``` * * @example * ```tsx * // Using all resolved properties * import { useLink } from '@esmx/router-react'; * * function DebugLink({ to }) { * const link = useLink({ to, exact: 'exact' }); * * return ( *
* { * e.preventDefault(); * link.navigate(e); * }} * > * Link Text * *
 *         isActive: {String(link.isActive)}
 *         isExactActive: {String(link.isExactActive)}
 *         isExternal: {String(link.isExternal)}
 *         type: {link.type}
 *         tag: {link.tag}
 *       
*
* ); * } * ``` */ export function useLink(props: RouterLinkProps): RouterLinkResolved { const router = useRouter(); const { to, type, replace, exact, activeClass, event, tag, layerOptions, beforeNavigate } = props; return useMemo(() => { return router.resolveLink({ to, type, replace, exact, activeClass, event, tag, layerOptions, beforeNavigate }); }, [ router, to, type, replace, exact, activeClass, event, tag, layerOptions, beforeNavigate ]); }