Link
An anchor that moves between store pages without reloading, adding the language (and, on localhost or the preview host, the store) to the path.
import { Link, LinkProps, LinkPreload } from '@salla.sa/twilight-theme-engine/components/common';In plain words
A component is a function that returns a piece of the page. You use it like an HTML tag, and the values you give it (to, className) are its props.
In plain HTML a link is <a href="/cart">, and clicking it makes the browser download a whole new page. Link renders the same <a> tag, but a click swaps only the page content, like an app, which is faster and keeps the header, the open menus and the cart as they are.
It also completes the address for you: to="/cart" becomes /ar/cart on Arabic pages.
Signature
const Link: ForwardRefExoticComponent<LinkProps & RefAttributes<HTMLAnchorElement>>
interface LinkProps {
to?: string; // falls back to href, then '/'
href?: string;
children?: ReactNode;
className?: string;
style?: CSSProperties;
title?: string;
target?: string;
rel?: string;
onClick?: MouseEventHandler<HTMLAnchorElement>;
preload?: LinkPreload; // only 'intent' is passed on under TanStack Router
replace?: boolean; // replace the history entry instead of adding one
'aria-label'?: string;
itemProp?: string;
}
type LinkPreload = 'intent' | 'render' | false;Try it live
import { Link } from '@salla.sa/twilight-theme-engine/components/common';
export function OpenLink() {
return (
<Link
to="/playground/reference/components/image"
className="btn btn--primary"
>
{'Open /playground/reference/components/image'}
</Link>
);
}
Example
import { Link } from '@salla.sa/twilight-theme-engine/components/common';
export function CartLink({ count }: { count: number }) {
return (
<Link to="/cart" className="btn btn--primary" aria-label={`Cart, ${count} items`}>
Cart ({count})
</Link>
);
}
How it behaves
It renders the anchor of the router adapter
TwilightProvidermounts (TanStack Router by default).tofalls back tohref, then to/; every other prop is passed on,data-spa-link=""is added, and therefreaches the real<a>.A path starting with
/gets the current locale in front (/cartbecomes/ar/cart) unless its first segment already is a supported locale (/en/cartstays). On localhost andpreview.salla.designthe router also adds the store segment. Paths without a leading/are left as they are.A full URL (
https://…) renders a plain anchor with no preloading, and the router does not handle its click. Ajavascript:URL gets nohrefat all.The router made by
createRouter()preloads on intent: hovering, focusing or touching a link starts loading the next route.onClickruns before the router navigates; callevent.preventDefault()in it to cancel the navigation.A link whose path is the current path, or a prefix of it, gets
aria-current="page"anddata-status="active".It is eager (not code-split), so it needs no
<Suspense>.@salla.sa/twilight-theme-engine/commonis the same module; the reference theme imports it from there.
Gotchas
preload="render"andpreload={false}change nothing under TanStack Router: its adapter passes on onlyintentand sendsundefinedotherwise, so the router default (intent) applies (src/tanstack/link.tsx). docs/16-client-navigation.md listsrenderas supported. For a link that must never preload, use TanStack Router's ownLinkwithpreload={false}; it takes typed route paths and adds no locale for you.The active match is a prefix match. A Home link (
to="/", which becomes/ar) carriesaria-current="page"on every page of the store, so a nav bar styled on[aria-current="page"]always highlights Home.LinkPropshas noactiveOptions: decide the current item yourself, for example withuseIsHome()from@salla.sa/twilight-theme-engine/providers.Outside
TwilightProvider(a unit test, Storybook) it throwsLinkProviderError. Wrap such renders inLinkProviderfrom@salla.sa/twilight-theme-engine/providers.Never build
tofromwindow.location. On localhost and the preview host the address bar carries the store segment, and the router adds it again (/dev-x/dev-x/cart). Use a path without the locale, or the engineuseLocation().
Related
Tells the engine's Link which real anchor component to render; TwilightProvider supplies TanStack Router's, and tests can supply a plain one.
useNavigateReturns a navigate(path) function that moves to another store page from code, without reloading the whole page.
NavigationInterceptorTurns clicks on ordinary links to this store, including links inside Salla web components, into in-app navigation instead of full reloads.