Category, MenuItem, Breadcrumb, Page
Describes categories, menu links, breadcrumb steps, and the Page record a route loader returns to name the current page.
import { Category, MenuItem, Breadcrumb, Page } from '@salla.sa/twilight-theme-engine/types';In plain words
Four small shapes that help shoppers find their way. A Category is a product category, possibly with sub_categories. A MenuItem is one link in a menu the merchant built, with its own children. A Breadcrumb is one step of a trail such as "Home › Chairs".
Page describes the page being shown: a slug naming its kind (such as product.single), a title, and optionally its breadcrumbs. Each engine route has a loader, a function that fetches the page's data before it renders, and every loader returns a page.
Signature
interface Category {
id: number | string; // the hashed id, e.g. 'QlayPG'
id_?: number; // the numeric id, e.g. 1309167683
name: string; url: string;
description?: string; icon?: string | null; image?: string | null;
products_count?: number;
sub_categories?: Category[];
items?: null;
}
interface MenuItem {
id: number | string;
title: string;
url: string;
children?: MenuItem[];
has_children?: boolean;
}
interface Breadcrumb { name: string; url: string }
interface Page {
title: string;
slug: string; // a RouteId value, e.g. 'product.index'
description?: string;
id?: string | number;
url?: string;
parent?: { id: string | number; name: string; url: string } | null;
breadcrumbs?: Breadcrumb[];
}Example
import { RouteId } from '@salla.sa/twilight-theme-engine';
import type { Breadcrumb, Category, Page } from '@salla.sa/twilight-theme-engine/types';
/** The page record a category listing's loader returns next to its own data. */
export function categoryPage(category: Category, homeLabel: string): Page {
const breadcrumbs: Breadcrumb[] = [
{ name: homeLabel, url: '/' },
{ name: category.name, url: category.url },
];
return {
slug: RouteId.PRODUCT_INDEX,
id: category.id_ ?? category.id,
title: category.name,
breadcrumbs,
};
}
How it behaves
category.list()andcategory.find(id)(@salla.sa/twilight-theme-engine/api/category) returnCategory;menu.header()andmenu.footer()(/api/menu) returnMenuItem[]. Thecategoryon a product is the smallerProductCategory.After a route loads, the engine takes the
pageof the deepest route match that returned one, stores it asgetTwilightContext().page, and hands it to the Salla SDK withSalla.config.set('page', …), addingurlfrom the address bar when the page has none.useBreadcrumbs(page)returnspage.breadcrumbswhen the loader set them, and otherwise builds Home ›page.title(nothing on the home page). TheBreadcrumbcomponent takes apageand renders that trail.Category and menu
urls are absolute store addresses (https://demostore.salla.sa/ar/dev-vgckq3fssfhjewwi/offerson the demo store). The engineMainMenupassesitem.urltoLinkunchanged; do the same rather than cutting the address apart.
Gotchas
Category.idis the hashed id ("QlayPG"on the demo store). The numeric id isid_, and onlyid_works as a productssourceValue.Menu item ids are not always numbers, or present: header items use slugs such as
"offers", and some footer items have noidat all (demo store).key={item.id}then produces duplicate keys; useitem.id ?? item.url.Menu items also carry
target,attrs,link_attrsandorder, which the type leaves out (demo store).targetis the link target the merchant chose; reading it needs a cast.A route whose loader returns no
pagedoes not clear the previous one: after a client-side navigation,getTwilightContext().pageand the SDK's page config still describe the last page that had one. Return apagefrom every page loader.
Related
Reads the store's category tree, or a single category, from the Salla API; the tree is cached per branch.
menuFetches the header and footer menus the merchant built in the Salla dashboard, in the language of the current page.
useBreadcrumbsBuilds a page's breadcrumb trail from its loader data, with a Home-plus-title fallback and a remembered referrer on product pages.
BreadcrumbDraws a page's breadcrumb trail from its loader data, with structured data for search engines, unless the merchant turned breadcrumbs off.
Route modulesEvery built-in page is an object with a loader that fetches its data, a head that sets its tags, and a Component that draws it.