Skip to content
Widget Library

Theming

The token system of the widgets, the layers a token can be set on, and the API around it.

The look of a widget is set with tokens only. There is no free CSS. The system follows the shadcn model: a few semantic tokens set everything at once, and component tokens override single parts.


Eleven tokens describe a store’s look. Setting only these themes every widget.

Token CSS variable Kind What it sets
foreground --sgf-foreground Colour Text outside the cards: the title. Fallback for text on cards.
primary --sgf-primary Colour Add to basket, badges, the active pagination dot.
primaryForeground --sgf-primary-foreground Colour Text and icons on primary.
secondary --sgf-secondary Colour The slider arrows.
secondaryForeground --sgf-secondary-foreground Colour The icon on secondary.
muted --sgf-muted Colour Inactive pagination dots.
mutedForeground --sgf-muted-foreground Colour The old price.
card --sgf-card Colour The card surface.
cardForeground --sgf-card-foreground Colour Text on cards: name, price, rating. Also the title when foreground is not set.
sale --sgf-sale Colour The discount.
radius --sgf-radius Length Corners of cards and buttons.

Each component token reads its own value first, then the semantic tokens it derives from, then the library default.

Token Derives from Default
fontFamily – inherit
titleColor foreground inherit
titleSize – 20px; 17px when the widget is narrower than 600 px
textColor cardForeground, foreground inherit
priceColor cardForeground, foreground inherit
oldPriceColor mutedForeground inherit
discountColor sale #d62028
ratingColor cardForeground, foreground inherit
badgeBackground primary #111111
badgeColor primaryForeground #ffffff
countdownBackground primary #111111
countdownColor primaryForeground #ffffff
cardBackground card transparent
cardBorder – none
cardRadius radius 8px
buttonBackground primary #111111
buttonColor primaryForeground #ffffff
buttonRadius radius 4px
arrowColor secondaryForeground #111111
arrowBackground secondary #ffffff
arrowSize – 40px
paginationColor muted #c4c4c4
paginationActiveColor primary #111111

inherit takes the value from the page around the widget. In CSS the chain is one expression, built by themeValue():

background: var(--sgf-button-background, var(--sgf-primary, #111111));

The chains live once in the code (THEME_DERIVES_FROM, THEME_DEFAULTS); the stylesheet is built from them and a test keeps them in step.


Four layers, weakest first. Each layer overrides the one before it token by token.

# Layer Set by How
1 Library defaults the library the fallbacks in the widget’s stylesheet
2 Initialisation theme theme.json in the account’s integration repository, edited in the panel setWidgetTheme(theme)
3 Site CSS the site’s developers :root { --sgf-primary: … } in their own stylesheet
4 Campaign theme the panel, per campaign theme in the widget config
import { setWidgetTheme } from '@segmentify/widgets';
setWidgetTheme({ primary: '#1d4ed8', primaryForeground: '#ffffff', radius: '8px' });

It writes one stylesheet into the page’s head, <style data-sgf-theme>, with the tokens under :where(:root). :where() has no specificity, so any :root rule of the site wins over it. Calling it again replaces the theme; an empty theme removes the stylesheet. Invalid values and unknown tokens are dropped, and the tokens that were kept are returned.

Custom properties cross the shadow boundary, so a site can theme the widgets from its own stylesheet, also per section of a page:

:root { --sgf-primary: #166534; }
.checkout { --sgf-radius: 0; }

This is the site’s own CSS and is not validated by the library. A title size set this way keeps the default line height; set through setWidgetTheme or a campaign theme it gets a line height of 1.4.

theme in the config is set on the widget element itself, so it wins over layers 2 and 3. Only the tokens it names are overridden; the rest keep coming from below. An invalid campaign value is ignored and the lower layer’s value stays.

{ "theme": { "primary": "#d62028", "buttonRadius": "999px" } }

A campaign can override a semantic token (primary) or a single component token (buttonBackground).


One JSON file holds a customer’s theme, the way components.json does in shadcn. This format is the standard: a theme is written, exchanged and stored in it, as token values only. CSS, HTML and script are never accepted.

The theme is edited in the panel and kept in the account’s integration repository as theme.json:

  1. The theme form of the panel is saved. The values are validated with the schema and the review rules below; an invalid theme is not saved.
  2. The file is committed to the account’s repository and the account’s files are built for QA.
  3. After the QA result is approved in the panel, the change goes to the main branch and the files are built for production.

So the theme is part of the account’s project and has its history in git, and it is managed from the panel. No theme file is kept on the CDN. The build of the repository checks the file again (check-theme), and the widget checks every value once more before applying it.

// src/main.ts of the integration
import { setWidgetTheme } from '@segmentify/widgets';
import themeFile from '../theme.json';
setWidgetTheme(themeFile.theme);

The panel form, its validation and the commit and build flow are not built yet (SFY-27910, SFY-27911, SFY-27912). Until they are, the file is edited in the repository.

{
"$schema": "https://segmentify.com/schemas/widgets/widget-theme.v1.json",
"schemaVersion": 1,
"name": "Acme Store",
"theme": { "primary": "#1d4ed8", "primaryForeground": "#ffffff", "radius": "8px" }
}
Field Meaning
$schema Optional. Lets an editor check the file while it is written.
schemaVersion 1.
name 1 to 80 characters.
description Optional, up to 300 characters.
theme The tokens. Unknown tokens and fields are refused.

The type is WidgetThemeFile; its JSON Schema is generated from it and shipped as @segmentify/widgets/schema/widget-theme.v1.json. Valid and invalid examples are under fixtures/theme-file/. The template and example themes are under themes/ (see Customer Theme Kit).


Kind Accepted
Colour Hex with 3, 4, 6 or 8 digits; rgb(), rgba(), hsl() or hsla() with three components and an optional alpha, separated by commas or by spaces (alpha after /); transparent, currentColor, inherit; or one of the 148 colour names of CSS in lower case (red, rebeccapurple). Up to 64 characters. No other word and no other function
Length A number with px, rem, em or %, or 0
Font family Families separated by commas. A family is a quoted name (letters, digits, spaces, hyphens, underscores; the quote is closed and holds no comma) or words separated by one space, each starting with a letter
Border none, 0, or a width in px, a style (solid, dashed, dotted) and a colour (hex, a colour name or a colour keyword)

The rules are closed and sound:

  • Closed. url(), var(), calc(), escapes, comments and anything that could end a declaration are refused. Nothing in a value can load a file, run anything or reach another rule.
  • Sound. Whatever is accepted is a value the browser can use. A value that only looks right, such as #12345, rgb(1, 2) or a word that is not a colour, is refused: written into the page it would switch the property off instead of leaving the default.

Some valid CSS is left out on purpose, to keep the rules small enough to state and to apply the same way in every service: colour names in upper case, percentages in rgb(), newer colour functions, units such as vw and pt, a comma inside a quoted font name.

The schema refuses a value on write and the widget checks again before applying, with the same patterns. A service that validates with the schema must use the regular expression rules of ECMAScript: in some other engines $ also matches before a final line break, which would let a value ending in a line break through. The fixtures theme-*-line-break are there to catch that.

Each rule is tested with a list of hostile values, a list of values no browser can use and the values a theme needs, in Chrome, Firefox and WebKit.

The widget does not load fonts. fontFamily names a font the site already loads.


import { reviewTheme } from '@segmentify/widgets';
const { accepted, rejected, warnings } = reviewTheme(file.theme);
Result Content
accepted The tokens that can be applied.
rejected { token, value, reason }; reason is unknown-token, not-a-string, too-long or invalid-format.
warnings { foreground, background, ratio, minimum } for text and background pairs under WCAG 2 contrast: 4.5 for text, 3 for the arrow icon.

The pairs are judged on the value each component token ends up with in this theme alone. A pair is skipped when a side cannot be judged: inherit, transparent, a colour name, hsl() or a colour that is not fully opaque. The title is not judged, because it sits on the site’s page. The panel applies the same rules when the form is saved, and the account’s repository runs them in its build, so a bad edit fails before it is deployed:

import { reviewTheme } from '@segmentify/widgets';
import themeFile from '../theme.json';
it('the theme file is valid and readable', () => {
const review = reviewTheme(themeFile.theme);
expect(review.rejected).toEqual([]);
expect(review.warnings).toEqual([]);
});

The semantic and component tokens, the theme file and the layer order are part of the contract. On mobile there is no CSS: the SDK takes the theme at initialisation (layer 2) and the campaign theme overrides it (layer 4). Scenarios use initTheme and effectiveTheme for this (see Conformance Scenarios).