pushEqualHeightConfig
Copies the store's equal-height product card settings into Google Tag Manager's window.dataLayer; WidgetHead calls it for you.
import { pushEqualHeightConfig } from '@salla.sa/twilight-theme-engine/components/layout';In plain words
Salla stores can make product cards the same height, and scripts loaded through Google Tag Manager read that choice from window.dataLayer, a plain array on the page. pushEqualHeightConfig copies the three settings from the store settings into that array.
WidgetHead already calls it on every page with the store's settings. Call it yourself only to push values of your own, such as a theme setting.
Signature
function pushEqualHeightConfig(settings: Record<string, unknown>): void
// pushes, when the enabled key is present:
// { equalHeight: { isEnabled: boolean; max: number; classType: string } }Try it live
import { useEffect } from 'react';
import { pushEqualHeightConfig } from '@salla.sa/twilight-theme-engine/components/layout';
// WidgetHead already pushes the store's own settings; push values of your own like this.
export function EqualHeightConfig() {
useEffect(() => {
// Browser only: it writes to window.dataLayer.
pushEqualHeightConfig({
'products::card.equal-height.enabled': true,
'products::card.equal-height.max-pixel': 300,
'products::card.equal-height.type': 'min',
});
}, []);
return null;
}
Example
import { useEffect } from 'react';
import { pushEqualHeightConfig } from '@salla.sa/twilight-theme-engine/components/layout';
import { useTheme } from '@salla.sa/twilight-theme-engine/hooks/useTheme';
// A theme that offers equal-height cards as one of its own twilight.json settings.
export function ThemeEqualHeight() {
const { settings } = useTheme();
const values = settings as Record<string, unknown>;
const enabled = values.equal_height_cards === true;
const maxPixel = Number(values.equal_height_max ?? 300);
useEffect(() => {
// Browser only: it writes to window.dataLayer.
pushEqualHeightConfig({
'products::card.equal-height.enabled': enabled,
'products::card.equal-height.max-pixel': maxPixel,
'products::card.equal-height.type': 'min',
});
}, [enabled, maxPixel]);
return null;
}
How it behaves
It reads three flat keys:
products::card.equal-height.enabled,products::card.equal-height.max-pixelandproducts::card.equal-height.type.When the enabled key is
undefinedornullit does nothing. Otherwise it createswindow.dataLayerif needed and pushes{ equalHeight: { isEnabled: Boolean(enabled), max: Number(maxPixel ?? 300), classType: String(type ?? "min") } }.It mirrors the PHP storefront's
HeadWidget::run().The demo store's
store.settingscontains none of these keys, so on these pagesWidgetHeadpushes nothing.
Gotchas
It uses
windowwithout checking for it, so calling it during a server render throwsReferenceError: window is not definedwhenever the enabled key is set. Call it in an effect, as the example does.The enabled value goes through
Boolean(), so the strings"false"and"0"count as enabled. Pass a real boolean.
Related
Source and docs
- Engine source:
packages/theme-engine/src/components/layout/WidgetHead.tsx