import type { CSSProperties } from 'react'; import type { FontMap, Theme, ThemeRadius, ThemeStyle } from './types'; /** Keep in step with PlanSchema::STYLE_DEFAULTS. They look like stores did before styles existed. */ export const STYLE_DEFAULTS: ThemeStyle = { buttons: 'solid', cards: 'border', spacing: 'normal', headings: 'normal', backgrounds: 'plain', motion: 'none', }; /** The theme's style with every part filled in. */ export function styleOf(theme: Theme): ThemeStyle { return { ...STYLE_DEFAULTS, ...theme.style }; } /** * Data attributes for the storefront's root element. app.css reads them, so a style changes * every button, card and section at once without each component knowing about it. */ export function styleAttributes(theme: Theme): Record { const style = styleOf(theme); return { 'data-buttons': style.buttons, 'data-cards': style.cards, 'data-spacing': style.spacing, 'data-headings': style.headings, 'data-backgrounds': style.backgrounds, 'data-motion': style.motion, }; } const buttonRadius: Record = { none: '0px', small: '4px', medium: '10px', large: '16px', full: '9999px', }; const cardRadius: Record = { none: '0px', small: '6px', medium: '14px', large: '22px', full: '28px', }; const fallbackFont = 'ui-sans-serif, system-ui, sans-serif'; function family(key: string, fonts: FontMap): string { const name = fonts[key]; return name ? `'${name}', ${fallbackFont}` : fallbackFont; } /** Turn a plan theme into the CSS variables every section reads. */ export function themeStyle(theme: Theme, fonts: FontMap): CSSProperties { return { '--sf-bg': theme.colors.background, '--sf-surface': theme.colors.surface, '--sf-fg': theme.colors.foreground, '--sf-muted': theme.colors.muted, '--sf-primary': theme.colors.primary, '--sf-primary-fg': theme.colors.primaryForeground, '--sf-accent': theme.colors.accent, '--sf-border': theme.colors.border, '--sf-radius': buttonRadius[theme.radius] ?? buttonRadius.medium, '--sf-radius-card': cardRadius[theme.radius] ?? cardRadius.medium, '--sf-font-heading': family(theme.fonts.heading, fonts), '--sf-font-body': family(theme.fonts.body, fonts), } as CSSProperties; } /** Web address that loads the theme's fonts. */ export function fontStylesheetUrl(theme: Theme, fonts: FontMap): string | null { const keys = [...new Set([theme.fonts.heading, theme.fonts.body])].filter( (key) => key in fonts, ); if (keys.length === 0) { return null; } const families = keys.map((key) => `${key}:400,500,600,700`).join('|'); return `https://fonts.bunny.net/css?family=${families}&display=swap`; } const ringgit = new Intl.NumberFormat('en-MY', { style: 'currency', currency: 'MYR', }); export function formatPrice(amount: number): string { return ringgit.format(amount); } /** * Put in front of a page title to say "this is a store's own page". * app.tsx removes it and leaves out our app name. */ export const storefrontTitlePrefix = 'storefront::';