HookHandler, HookHandlers, HookDefinition
Types for a slot handler function, the object defineHooks accepts, and a handler as the registry stores it.
import { HookHandler, HookHandlers, HookDefinition } from '@salla.sa/twilight-theme-engine/hooks';In plain words
Type names for the hook system, so your editor can check your handlers. A HookHandler is "a function that receives the slot's context object and returns something React can render, or null". HookHandlers is the object you pass to defineHooks, and HookDefinition is one entry as hookRegistry.getHandlers returns it.
Types disappear when the theme is built: they only help while you write code.
Signature
type HookHandler<T = Record<string, unknown>> = (context: T) => ReactNode;
type HookHandlers = Record<
string,
HookHandler | Array<{ priority?: number; handler: HookHandler }>
>;
interface HookDefinition {
id: number; // registry-wide counter, the React key in HookSlot
handler: HookHandler;
priority: number;
}Example
import type { HookContext, HookDefinition, HookHandler, HookHandlers } from '@salla.sa/twilight-theme-engine/hooks';
import { defineHooks, hookRegistry } from '@salla.sa/twilight-theme-engine/hooks/HookRegistry';
const freeShipping: HookHandler<HookContext> = ({ twilight }) =>
twilight.locale.startsWith('ar') ? <p>شحن مجاني</p> : <p>Free shipping over 200 SAR</p>;
const cartHooks: HookHandlers = {
'cart:summary.end': freeShipping as HookHandler,
'cart:end': [{ handler: () => <hr />, priority: 10 }],
};
defineHooks(cartHooks);
export const summaryHandlers: HookDefinition[] = hookRegistry.getHandlers('cart:summary.end');
How it behaves
The default
TisRecord<string, unknown>, sotwilightisunknowninside an unannotated handler. Annotate the parameter asHookContext(or an interface extending it) to get types.HookHandlersvalues are typed with the defaultT, which is why a handler typed withHookContextneeds a cast inside aHookHandlersobject.hookRegistry.registeris generic and needs none.The same three names are exported from
@salla.sa/twilight-theme-engine(the package root);HookHandleris also exported from/typesand/types/hooks.getHandlersreturns the registry's own array, sorted highest priority first. Copy it before changing it.
Gotchas
A handler may return
undefined(it is a validReactNode), and it renders nothing, the same asnull.