LinkProvider
Tells the engine's Link which real anchor component to render; TwilightProvider supplies TanStack Router's, and tests can supply a plain one.
import { LinkProvider } from '@salla.sa/twilight-theme-engine/providers';In plain words
The engine's Link does not know which router your theme uses. LinkProvider hands it an adapter: the component that renders the actual anchor. Under TwilightProvider the adapter is TanStack Router's Link, set up for you.
You mount your own LinkProvider only for tests, a component preview tool such as Storybook, or a different router.
Signature
function LinkProvider(props: { adapter: LinkAdapter; children: ReactNode }): JSX.Element
type LinkAdapter = React.ComponentType<LinkProps>; // LinkProps from '/components/common'Try it live
click the linkimport type { ReactNode } from 'react';
import { Link, type LinkProps } from '@salla.sa/twilight-theme-engine/components/common';
import { LinkProvider } from '@salla.sa/twilight-theme-engine/providers';
// A test or a Storybook story: plain anchors, no router needed.
function PlainAnchor({ to, href, preload: _preload, replace: _replace, ...rest }: LinkProps) {
return <a href={to ?? href} {...rest} />;
}
export function WithPlainLinks({ children }: { children: ReactNode }) {
return <LinkProvider adapter={PlainAnchor}>{children}</LinkProvider>;
}
// <WithPlainLinks><Link to="/brands" preload="intent">Brands</Link></WithPlainLinks>
Example
import type { ReactNode } from 'react';
import type { LinkProps } from '@salla.sa/twilight-theme-engine/components/common';
import { LinkProvider } from '@salla.sa/twilight-theme-engine/providers';
function PlainAnchor({ to, href, preload: _preload, replace: _replace, ...rest }: LinkProps) {
return <a href={to ?? href} {...rest} />;
}
export function WithPlainLinks({ children }: { children: ReactNode }) {
return <LinkProvider adapter={PlainAnchor}>{children}</LinkProvider>;
}
How it behaves
With
client.framework: 'tanstack'(the default) the navigation setup mountsLinkProviderwithTanStackLinkAdapteronceisReady. That adapter adds the current locale to internal paths and turnspreload: 'intent'into TanStack's intent preloading.The engine
Linkpassesto(falling back tohref, then/), every other prop anddata-spa-link=""to the adapter, and forwards itsref.The nearest
LinkProviderwins, as with any React context.
Gotchas
LinkAdapteris not exported under that name. Type an adapter withLinkPropsfrom@salla.sa/twilight-theme-engine/components/common.An adapter receives
preloadandreplace. Spreadingreplace={true}onto a DOM anchor makes React warn about a non-boolean attribute; take both out first, as the example does.
Related
Returns the anchor component the nearest LinkProvider supplies, and throws LinkProviderError when there is no provider above.
createLinkWithAdapterBuilds a link component bound to one adapter, which renders without any LinkProvider above it.
TwilightProviderThe component a theme mounts once around its pages; it shares the store, theme, language and navigation with everything inside it.