useTwilight
Reads everything TwilightProvider knows: store, theme, settings, language, direction, current page, login token and the Salla SDK.
import { useTwilight, TwilightContextValue } from '@salla.sa/twilight-theme-engine';In plain words
A hook is a function whose name starts with use, called at the top of a component to get something. useTwilight() hands your component what TwilightProvider knows about the page being shown: the store, the merchant's theme, the language and text direction, the current address, whether a customer is signed in, and the Salla SDK (window.Salla) once the browser has loaded it.
The focused hooks (useStore, useTheme, useTranslation) read better when you need one thing. Reach for useTwilight() when you need several.
Signature
function useTwilight(): TwilightContextValue // throws outside TwilightProvider
interface TwilightContextValue {
isReady: boolean;
store: Store;
theme: Theme;
settings: StoreContext; // the whole store settings response
locale: string; // 'ar'
dir: 'ltr' | 'rtl';
routeId: string;
location: TwilightLocation;
authToken: string | null;
salla: SallaSDK | undefined; // undefined in the server render
i18n: i18n; // the i18next instance
config: TwilightConfig; // { debug }
log: TwilightLog; // styled console helpers
currency: SallaCurrency | null; // always null today
extras?: Record<string, unknown>;
}Try it live
useTwilight() → store: {…} 24 keys
import { useTwilight } from '@salla.sa/twilight-theme-engine';
export function Example() {
const { store } = useTwilight();
return <span>{store.name}</span>;
}
Example
import { useTwilight } from '@salla.sa/twilight-theme-engine';
export function StoreHeading() {
const { store, theme, locale, dir } = useTwilight();
return (
<h1 lang={locale} dir={dir} style={{ color: theme.color.primary }}>
{store.name}
</h1>
);
}
How it behaves
store,theme,isReadyandauthTokenare React state: when they change, every component using the hook renders again.settings,locale,dir,routeId,location,i18nandextrasare getters that read the request context when you access them, so they are current when read but never cause a render themselves.useRouteId()and theuseIs*hooks subscribe for you.storeandthemehold the values the page was first rendered with.useStore().refresh()updatesstore; in development the dev settings widget overlaystheme.settings.sallareadswindow.Sallaon each access:undefinedin the server render and until the SDK script has run.logprints styled console output (info,warn,error,label,dir,trace,group,groupCollapsed,groupEnd) whateverdebugis set to.Hook slot handlers receive this same value as
context.twilight. The hook is also exported from/providers.
Gotchas
authTokenisnullin every server render, even for a signed-in customer: the provider seeds that state from the context orlocalStorageonly in the browser (src/providers/TwilightProvider.tsx). Markup that depends on it differs between server and browser, a hydration mismatch. Decide after hydration, for example withuseIsClient()from@salla.sa/twilight-theme-engine/hooks.currencyis alwaysnull: it is created withuseState(null)and never set (src/providers/twilight-init.ts). Format prices withuseMoney().Reading
settingsthrowsSettingsErrorwhen the request context holds no settings (the getter in src/twilight/context.ts). Inside a rendered page they are always present.
Related
The component a theme mounts once around its pages; it shares the store, theme, language and navigation with everything inside it.
useStoreReads the current store (name, logo, settings, contacts…) and can refresh it from the Salla SDK.
useThemeReads the merchant's theme colors, font, theme settings and whether the page reads right-to-left.
useLocationReads the current address as the router sees it (path, parsed query, raw query, hash), in the server render and in the browser.
Source and docs
- Engine source:
packages/theme-engine/src/providers/twilight-context.ts