Component hooks
Run your code when a salla-* web component loads, block an add to cart, or change the items a product list shows, with Salla.hooks.
Read this when you need to change how a salla-* web component behaves, from your theme or an app script.
Component hooks let your code step into a web component at fixed points: when it loads, before it adds to the cart, before it shows its products. They are not slots: a slot adds content to a page, a component hook changes a component.
const unsubscribe = Salla.hooks.registerHook('salla-add-product-button', 'componentDidLoad', (button) => {
console.log('Add button for product', button.productId);
});
// later, to stop:
unsubscribe();Register a hook
Salla.hooks.registerHook(tag, hookName, callback, options?) returns a function that removes the hook.
| Option | Does |
|---|---|
priority | A number. Higher runs first. 0 by default. |
once | true to run once, then remove itself. Salla.hooks.registerHookOnce(...) does the same. |
The callback may be async: the component waits for it. An error in your callback is caught and logged, so it never breaks the component.
Components and hooks
Only these tags and hooks can be registered. Anything else is ignored with a console warning.
| Tag | Hook | Callback receives | You can change |
|---|---|---|---|
salla-add-product-button | componentDidLoad | the component | The element |
salla-add-product-button | validate | { isValid, component } | isValid: set it to false to stop the add |
salla-products-list | componentDidLoad | the component | The element |
salla-products-list | beforeBuildListUrl | { component, url } | url, to change the request |
salla-products-list | beforeAppendItems | { component, items } | items, to filter or replace the products shown |
salla-products-slider | componentDidLoad, beforeAppendItems | as above | as above |
salla-menu | afterFetchMenuItems | the component | component.menus, the loaded menu |
salla-product-options, salla-hook, salla-scopes, salla-user-menu | componentDidLoad | the component | The element |
On salla-add-product-button, the component has productId, productType, productStatus and quantity.
Salla.hooks.registerHook('salla-products-list', 'beforeAppendItems', (ctx: { items: Array<{ is_available: boolean }> }) => {
ctx.items = ctx.items.filter((item) => item.is_available);
});
Salla.hooks.registerHook('salla-products-list', 'beforeBuildListUrl', (ctx: { url: string }) => {
const url = new URL(ctx.url);
url.searchParams.set('custom_filter', 'my_value');
ctx.url = url.toString();
});Late registration
You do not have to register before the component loads. A new componentDidLoad hook also runs at once for every component of that tag already on the page,
then for each one that loads later. Each element runs it only once.
Remove hooks
| Call | Removes |
|---|---|
The function registerHook returned | That one hook |
Salla.hooks.removeHook(tag, hookName, callback) | That one hook |
Salla.hooks.removeHook(tag, hookName) | Every hook of that name on the tag |
Salla.hooks.clear(tag?) | Every hook on the tag, or every hook on the page |
In a React component, register in useEffect and return the remove function as the cleanup.
Gotchas
validateruns only when the button is not a form's submit button and the product'ssalla-product-optionsis on the page. The engine's product page submits a form, so therevalidatedoes not run. To check an add there, use your own submit handler.- Register after the SDK has started: inside
Salla.onReady(() => { ... }), or inuseEffect. - Objects are passed by reference. Change the fields of
ctx; do not replacectxitself.