@adyen/adyen-shared-web
v0.6.0
Published
Downloads
15,394
Readme
adyen-shared-web
Shared utilities for the Adyen npm libraries.
Usage
Install the package and create a ThemeGenerator to generate and inject semantic CSS color variables for your application:
import { ThemeGenerator } from "@adyen/adyen-shared-web";
const themeGenerator = new ThemeGenerator();
themeGenerator.create({
primary: "#0066ff",
background: "#ffffff",
neutral: "#f2f4f7",
label: "#101828",
outline: "#98a2b3",
highlight: "#006bd7",
roundness: "medium",
});Every input is optional. The generator only emits the tokens that the supplied inputs affect:
primarysets the brand color (background-inverse-primary, links, selection and emphasized outlines). Links get at least 4.5:1 contrast against the background. The text on the primary (label-inverse-primary) is light or dark, whichever is readable on the primary and its hover and active states. The hover and active states move away from that text, so they are at least as readable as the primary itself; a primary close to black or white keeps the Bento direction. The other inverse surfaces that carry this text, such as applied filter chips and avatars, are derived from the primary so the text stays readable on them: deep shades with light text, and shades close to the primary with dark text. In light mode, dark text on the primary also makeslabel-on-colordark, because Bento uses it for the active step number on the primary.backgroundsets the page background. Surfaces, status colors (critical, success, warning), highlight colors and, withoutoutline, outlines and separators adapt to it. With the default background, they keep their Bento values.neutralsets the secondary surfaces and separators. It also tints the inverse outlines and, withoutprimary, the inverse surfaces. Both keep their Bento lightness.labelsets the primary text color. Secondary and tertiary labels are derived from it with a minimum contrast.highlightsets the informative color (label-highlight). Without it, the Bento informative blue is used. Highlight and selected colors (background-selected*, the same color asbackground-highlight-weakin Bento) are never derived fromprimary.outlinesets the outline colors.labelOnPrimarysets the text on the primary (label-inverse-primary) instead of the automatic light or dark text. It is used exactly, and the inverse surfaces and, in light mode with dark text,label-on-colorfollow it. It has an effect only together withprimary.
Input colors are never changed. Derived text and outline colors (secondary labels, links, status and highlight colors) keep their minimum contrast on the primary, secondary and tertiary background surfaces, because text also sits on secondary surfaces such as cards, drawers and the selected Adyen Web payment method. In development builds, the generator warns when an input color has low contrast. It also warns when the text on the primary is not the Bento default and is used on other colors where it has low contrast: critical and success colors, and checked read-only toggles, whose icon uses the label color as background.
Set roundness to "small", "medium", or "large" to set border-radius-m to 4px, 8px, or 12px. The xs, s, l, and xl radius tokens keep the Bento proportions around it. Pass dark: true when generating a dark theme. Call themeGenerator.destroy() when the theme is no longer needed to remove the generated styles.
To generate CSS without accessing or modifying the DOM, use generateCSS(). It targets :root by default and accepts a custom selector for scoped themes:
const css = themeGenerator.generateCSS(
{
primary: "#0066ff",
dark: true,
},
{
selector: "[data-adyen-pe-theme-root='core-id']",
},
);generateCSS() returns null when no supplied theme input produces CSS variables.
Theme documents
A theme editor works with a ThemeDocument: the seeds of both modes and one roundness, in the form that is stored and shared.
import {
applyTheme,
createThemeDocument,
parseTheme,
resolveTheme,
serializeTheme,
} from "@adyen/adyen-shared-web";
const theme = createThemeDocument(); // { v: 1, light: {}, dark: {} }
theme.light.primary = "#0066ff";
theme.dark.primary = "#84adff";
theme.roundness = "large";
const props = resolveTheme(theme, "dark"); // ThemeProps for one mode, with dark: true
const stored = serializeTheme(theme); // JSON string
const restored = parseTheme(stored); // never throws
const cleanup = applyTheme(previewElement, theme, "light");THEME_SEEDSlists the color inputs in editor order, andDEFAULT_THEME_SEEDSholds the values an editor shows for unset seeds. Defaults are never written into a document: an unset seed keeps the Bento default.resolveTheme()andserializeTheme()drop empty and invalid colors, so a half-typed value never makesgenerateCSS()throw.parseTheme()accepts a JSON string or an object. It ignores unknown keys, invalid colors and invalid roundness, and returns an empty document for anything that is not a theme. A flatThemePropsobject is read as the seeds of its mode (dark: truemeans dark).applyTheme()themes one container and everything inside it. Each container gets its owndata-adyen-theme-rootattribute and its own<style>, in the container's shadow root or the document head, so containers never overwrite each other. Calling it again for the same container updates that style. It also setscolor-schemeon the container, accepts aprefixoption like theThemeGeneratorconstructor, and returns a cleanup that removes the style, the attribute and thecolor-scheme.applyTheme()suits SDKs that read the--adyen-sdk-*variables from an ancestor, such as Adyen Web. Platform Experience and KYC take the theme through their own options, so pass themresolveTheme()output instead.
Publishing a new release on npm
- Tag the
mainbranch with the new semantic version:git tag vX.Y.Zandgit push origin vX.Y.Z - Create a new release on GitHub (GitHub will automatically populate this with a changelog based on merged PRs).
- (Optional) If you want this to be a pre-release, check the "Set as pre-release" option.
- Publish the release!
Staged publishing is intentionally disabled at the moment.
