useLinkAdapter
Returns the anchor component the nearest LinkProvider supplies, and throws LinkProviderError when there is no provider above.
import { useLinkAdapter, LinkProviderError } from '@salla.sa/twilight-theme-engine/providers';In plain words
The engine's Link calls useLinkAdapter() to find the real anchor component. Call it yourself when a component needs that anchor directly, with its own props.
With no LinkProvider above, it throws a LinkProviderError whose message says what to fix.
Signature
function useLinkAdapter(): LinkAdapter // React.ComponentType<LinkProps>
class LinkProviderError extends Error {
constructor();
// name: 'LinkProviderError'
// message: 'Link component must be used within LinkProvider. Ensure TwilightProvider
// is configured with client.framework option.'
}Try it live
useLinkAdapter() → TanStackLinkAdapter
Outside a LinkProvider the hook throws LinkProviderError: “Link component must be used within LinkProvider. Ensure TwilightProvider is configured with client.framework option.”
import type { ReactNode } from 'react';
import { useLinkAdapter } from '@salla.sa/twilight-theme-engine/providers';
// A menu item that must render the router's own anchor, with its own props.
export function MenuLink({ to, children }: { to: string; children: ReactNode }) {
const Anchor = useLinkAdapter();
return (
<Anchor to={to} preload="intent" className="menu-link">
{children}
</Anchor>
);
}
// <MenuLink to="/playground/reference/core">…</MenuLink>
Example
import type { ReactNode } from 'react';
import { useLinkAdapter } from '@salla.sa/twilight-theme-engine/providers';
export function MenuLink({ to, children }: { to: string; children: ReactNode }) {
const Anchor = useLinkAdapter();
return (
<Anchor to={to} preload="intent" className="menu-link">
{children}
</Anchor>
);
}
How it behaves
Linkfrom/components/commonisuseLinkAdapter()plus prop defaults, so it throws the same error in the same places.
Gotchas
Places with no
LinkProviderin a working theme:TwilightProvider'sskeleton(rendered before the navigation setup), anything underclient={false}, and component tests rendered without a provider.