Style Elements Checkout
Make Stripe's elements match your paywall: appearance variables and rules from your design tokens, fonts that load, and styles for the card fields.
Stripe draws its fields in its own frame, so they can't read your paywall's CSS. To make them match, read your design tokens once, resolve each to a plain colour, and hand them to Stripe through <StripeCheckout options>. This page builds that, the way the elements-checkout example does in components/appearance.ts.
Requirements
- A paywall with
checkout: "elements", see Elements Checkout. - Design tokens as CSS variables, such as
--surfaceand--fginapp/theme.css.
Read your tokens as colours
Stripe needs plain colours, not var(--surface) or oklch(). Resolve each token in the browser and turn it into rgb():
const TOKENS = {
surface: "--surface",
raised: "--raised",
fg: "--fg",
fgSecondary: "--fg-secondary",
fgMuted: "--fg-muted",
border: "--border",
accent: "--accent",
danger: "--destructive-ink",
} as const;
type Palette = Record<keyof typeof TOKENS, string>;
const FALLBACK: Palette = {
surface: "#ffffff",
raised: "#f5f5f5",
fg: "#111111",
fgSecondary: "#555555",
fgMuted: "#888888",
border: "rgba(0, 0, 0, 0.12)",
accent: "#0a7c72",
danger: "#c62828",
};
const readPalette = (): Palette => {
if (typeof document === "undefined") return FALLBACK;
const probe = document.createElement("span");
probe.style.display = "none";
document.body.append(probe);
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 1;
const context = canvas.getContext("2d", { willReadFrequently: true });
if (!context) {
probe.remove();
return FALLBACK;
}
const palette = {} as Palette;
for (const [name, variable] of Object.entries(TOKENS) as [keyof Palette, string][]) {
probe.style.color = `var(${variable})`;
context.clearRect(0, 0, 1, 1);
context.fillStyle = getComputedStyle(probe).color;
context.fillRect(0, 0, 1, 1);
const [r, g, b, a] = context.getImageData(0, 0, 1, 1).data;
palette[name] = a === 255 ? `rgb(${r}, ${g}, ${b})` : `rgba(${r}, ${g}, ${b}, ${(a / 255).toFixed(2)})`;
}
probe.remove();
return palette;
};Read the palette again when the colour scheme changes. The provider switches the root's light or dark class in its own effect, so wait for the class to change:
import { useEffect, useState } from "react";
import { useColorScheme } from "superwall/hooks";
const usePalette = (): Palette => {
const scheme = useColorScheme();
const [palette, setPalette] = useState<Palette>(readPalette);
useEffect(() => {
setPalette(readPalette());
const observer = new MutationObserver(() => setPalette(readPalette()));
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
return () => observer.disconnect();
}, [scheme]);
return palette;
};Set appearance variables and rules
Give the fields the same surface as your plan rows, so the form looks like part of the paywall. Variables set the colours, font and corners; rules give every field the same border and focus edge as your own inputs:
import { useMemo } from "react";
import type { StripeCheckoutOptions } from "superwall/checkout/stripe";
export const useStripeOptions = (): StripeCheckoutOptions => {
const p = usePalette();
return useMemo(() => {
const hairline = `1px solid ${p.border}`;
const flat = { boxShadow: "none" };
return {
fonts: FONTS,
appearance: {
theme: "flat",
labels: "above",
variables: {
colorPrimary: p.accent,
colorBackground: p.surface,
colorText: p.fg,
colorTextSecondary: p.fgSecondary,
colorTextPlaceholder: p.fgMuted,
colorDanger: p.danger,
fontFamily: "Manrope, system-ui, sans-serif",
fontSizeBase: "16px",
borderRadius: "3px",
focusBoxShadow: "none",
},
rules: {
".Input": { ...flat, border: hairline, backgroundColor: p.surface, fontSize: "16px" },
".Input:focus": { ...flat, borderColor: p.accent },
".Input--invalid": { ...flat, borderColor: p.danger },
".Tab": { ...flat, border: hairline, backgroundColor: p.surface },
".Tab--selected": { ...flat, borderColor: p.accent },
".AccordionItem": { ...flat, border: hairline, backgroundColor: p.surface },
".Block": { ...flat, border: hairline, backgroundColor: p.surface },
},
},
};
}, [p]);
};.Block is Link's "save my information" box. Stripe lists every variable and class in its Appearance API.
Pass the result to the form:
const options = useStripeOptions();
<StripeCheckout product={plan} options={options}>
{/* the form */}
</StripeCheckout>;Keep the options object stable
Build options with useMemo. A new object on every render makes Stripe redraw every field, and the shopper loses focus as they type.
Load your font
Give fonts the font file itself, not a stylesheet:
const MANROPE = "url(https://fonts.gstatic.com/s/manrope/v23/xn7gYHE41ni1AdIRggexSvfedN4.woff2)";
const FONTS = ["400", "500", "600"].map((weight) => ({
family: "Manrope",
src: MANROPE,
weight,
display: "swap",
}));Stripe fetches a cssSrc stylesheet from your paywall's page, and the hosted page's Content Security Policy blocks Google Fonts stylesheets there. A font file loads inside Stripe's own frame instead.
Style the card fields
CardElement, CardNumberElement, CardExpiryElement and CardCvcElement ignore appearance. They take options.style, and their outer frame is an element of yours, so give it your own input class:
import { CardNumberElement } from "superwall/checkout/stripe";
const useCardStyle = () => {
const p = usePalette();
return useMemo(
() => ({
base: {
color: p.fg,
iconColor: p.fgMuted,
fontFamily: "Manrope, system-ui, sans-serif",
fontSize: "16px",
"::placeholder": { color: p.fgMuted },
},
invalid: { color: p.danger, iconColor: p.danger },
}),
[p],
);
};
const style = useCardStyle();
<CardNumberElement className="input" options={{ style, showIcon: true }} />;The card fields still read their font from fonts on <StripeCheckout options>.
Keep fields at 16px
iOS Safari zooms into any focused field with text under 16px. Keep fontSizeBase, the .Input rule and the card fields' fontSize at 16px or more, and your own inputs too.
Limitations
- No CSS reaches inside Stripe's frame. Only what
appearance,fontsandstyleallow can change. - Wallet buttons follow their brand.
ExpressCheckoutElementtakesbuttonHeight,buttonThemeandbuttonType, but Apple Pay and Google Pay keep their own look.
Related
- Elements Checkout: build the form
- Elements Checkout Reference: what
optionstakes and what Superwall sets - Elements Checkout Recipes: layouts built with these styles
How is this guide?
Elements Checkout Events
Which purchase callbacks fire, in what order, for an Elements form, its wallets, Superwall's Apple Pay sheet and the payment sheet.
Elements Checkout Recipes
Ready-made Elements Checkout layouts: wallets and card, Apple Pay with card fields, card first, a one-line card, wallets only, billing address and promo codes.