Design tokens: ipal-tokens CLI, components on single names, templates, tests
- bin ipal-tokens: src/design/tokens.ts → src/design/tokens.css, --check, WCAG contrast report (fails on AA errors; --report, --warn-only) - CookieBanner / CookieButton read the single --ipal-* roles; the fallback follows color-scheme via light-dark() and still honours the legacy -dark variables - lightbox fade reads --ipal-duration / --ipal-ease-out - DESIGN.md and tokens.ts templates in ipal-setup/resources Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9511b12d19
commit
ca8e93eaf4
@@ -88,9 +88,10 @@ export type CookieBannerClassNames = {
|
||||
}
|
||||
|
||||
/**
|
||||
* Colours and radius come from CSS custom properties with built-in fallbacks,
|
||||
* so the banner looks right with no setup, and re-skinning per client means
|
||||
* declaring a few variables — no imports, no props, no specificity fights:
|
||||
* Colours and radius come from the design tokens (docs/design-tokens.md) with
|
||||
* built-in fallbacks, so the banner looks right with no setup, and re-skinning
|
||||
* per client means declaring a few variables — no imports, no props, no
|
||||
* specificity fights:
|
||||
*
|
||||
* ```css
|
||||
* :root {
|
||||
@@ -99,22 +100,24 @@ export type CookieBannerClassNames = {
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* Available: --ipal-surface, --ipal-border, --ipal-text, --ipal-text-strong,
|
||||
* Read: --ipal-surface, --ipal-border, --ipal-text, --ipal-text-strong,
|
||||
* --ipal-text-muted, --ipal-primary, --ipal-primary-hover, --ipal-primary-text,
|
||||
* --ipal-hover, --ipal-radius. Each has a `-dark` counterpart used under
|
||||
* Tailwind's `dark:` variant (e.g. --ipal-surface-dark).
|
||||
* --ipal-hover, --ipal-radius. With @intecion/ipal-kit/tokens.css they switch
|
||||
* with the theme on their own. Without it, the fallback follows the page's
|
||||
* color-scheme (light-dark()) and the legacy `-dark` variables
|
||||
* (e.g. --ipal-surface-dark) still set the dark fallback.
|
||||
*
|
||||
* Layout stays in Tailwind: spacing and flow aren't things a brand changes, and
|
||||
* exposing them as tokens would mean re-inventing CSS one variable at a time.
|
||||
* For a different layout use classNames (every element has a slot).
|
||||
*/
|
||||
const surface = 'bg-[var(--ipal-surface,#fff)] dark:bg-[var(--ipal-surface-dark,#171717)]'
|
||||
const border = 'border-[var(--ipal-border,#e5e5e5)] dark:border-[var(--ipal-border-dark,#262626)]'
|
||||
const surface = 'bg-[var(--ipal-surface,light-dark(#fff,var(--ipal-surface-dark,#171717)))]'
|
||||
const border = 'border-[var(--ipal-border,light-dark(#e5e5e5,var(--ipal-border-dark,#262626)))]'
|
||||
const radius = 'rounded-[var(--ipal-radius,0.375rem)]'
|
||||
const accent = 'text-[var(--ipal-primary,#2563eb)]'
|
||||
const bodyText = 'text-[var(--ipal-text,#404040)] dark:text-[var(--ipal-text-dark,#d4d4d4)]'
|
||||
const strongText = 'text-[var(--ipal-text-strong,#171717)] dark:text-[var(--ipal-text-strong-dark,#f5f5f5)]'
|
||||
const mutedText = 'text-[var(--ipal-text-muted,#737373)] dark:text-[var(--ipal-text-muted-dark,#a3a3a3)]'
|
||||
const bodyText = 'text-[var(--ipal-text,light-dark(#404040,var(--ipal-text-dark,#d4d4d4)))]'
|
||||
const strongText = 'text-[var(--ipal-text-strong,light-dark(#171717,var(--ipal-text-strong-dark,#f5f5f5)))]'
|
||||
const mutedText = 'text-[var(--ipal-text-muted,light-dark(#737373,var(--ipal-text-muted-dark,#a3a3a3)))]'
|
||||
const primaryBg = 'bg-[var(--ipal-primary,#2563eb)]'
|
||||
|
||||
const defaults: Required<CookieBannerClassNames> = {
|
||||
@@ -128,7 +131,7 @@ const defaults: Required<CookieBannerClassNames> = {
|
||||
categoryCard: `flex flex-col gap-3 ${radius} border p-4 ${border}`,
|
||||
categoryDescription: `text-xs ${mutedText}`,
|
||||
categoryTitle: `text-sm font-medium ${strongText}`,
|
||||
closeButton: `shrink-0 ${radius} p-1 ${mutedText} hover:bg-[var(--ipal-hover,#f5f5f5)] dark:hover:bg-[var(--ipal-hover-dark,#262626)]`,
|
||||
closeButton: `shrink-0 ${radius} p-1 ${mutedText} hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]`,
|
||||
content: 'flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between',
|
||||
control: 'mt-1 h-4 w-4 shrink-0 accent-[var(--ipal-primary,#2563eb)]',
|
||||
controlThumb: 'pointer-events-none inline-block size-5 rounded-full bg-white shadow transition-transform translate-x-0.5 data-[checked=true]:translate-x-[1.375rem]',
|
||||
@@ -148,7 +151,7 @@ const defaults: Required<CookieBannerClassNames> = {
|
||||
rejectButton: '',
|
||||
root: `fixed inset-x-0 bottom-0 z-50 border-t p-4 shadow-lg ${surface} ${border}`,
|
||||
saveButton: '',
|
||||
secondaryButton: `${radius} px-3 py-1.5 text-sm font-medium text-[var(--ipal-text,#404040)] hover:bg-[var(--ipal-hover,#f5f5f5)] dark:text-[var(--ipal-text-dark,#e5e5e5)] dark:hover:bg-[var(--ipal-hover-dark,#262626)]`,
|
||||
secondaryButton: `${radius} px-3 py-1.5 text-sm font-medium text-[var(--ipal-text,light-dark(#404040,var(--ipal-text-dark,#d4d4d4)))] hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]`,
|
||||
settingsActions: 'flex justify-end gap-2',
|
||||
settingsButton: '',
|
||||
settingsDescription: `text-sm ${bodyText}`,
|
||||
|
||||
@@ -9,9 +9,9 @@ import { useIsMounted } from './useIsMounted.js'
|
||||
* after deciding (GDPR: withdrawing consent must be as easy as giving it).
|
||||
* Hidden while the banner is showing.
|
||||
*
|
||||
* Colours follow the same CSS custom properties as CookieBanner
|
||||
* (--ipal-surface, --ipal-border, --ipal-hover, --ipal-text), so a client's
|
||||
* palette applies to both without touching either component. Pass className to
|
||||
* Colours follow the same design tokens as CookieBanner (--ipal-surface,
|
||||
* --ipal-border, --ipal-hover, --ipal-text), so a client's palette applies to
|
||||
* both without touching either component. Pass className to
|
||||
* replace the styling outright.
|
||||
*/
|
||||
export function CookieButton({ className }: { className?: string } = {}) {
|
||||
@@ -26,8 +26,7 @@ export function CookieButton({ className }: { className?: string } = {}) {
|
||||
const cls =
|
||||
className ??
|
||||
'fixed bottom-4 left-4 z-40 flex h-11 w-11 items-center justify-center rounded-full border shadow-md transition-colors ' +
|
||||
'bg-[var(--ipal-surface,#fff)] border-[var(--ipal-border,#e5e5e5)] hover:bg-[var(--ipal-hover,#f5f5f5)] ' +
|
||||
'dark:bg-[var(--ipal-surface-dark,#171717)] dark:border-[var(--ipal-border-dark,#262626)] dark:hover:bg-[var(--ipal-hover-dark,#262626)]'
|
||||
'bg-[var(--ipal-surface,light-dark(#fff,var(--ipal-surface-dark,#171717)))] border-[var(--ipal-border,light-dark(#e5e5e5,var(--ipal-border-dark,#262626)))] hover:bg-[var(--ipal-hover,light-dark(#f5f5f5,var(--ipal-hover-dark,#262626)))]'
|
||||
|
||||
return (
|
||||
<button
|
||||
@@ -40,7 +39,7 @@ export function CookieButton({ className }: { className?: string } = {}) {
|
||||
>
|
||||
<Cookie
|
||||
aria-hidden="true"
|
||||
className="h-5 w-5 text-[var(--ipal-text,#525252)] dark:text-[var(--ipal-text-dark,#d4d4d4)]"
|
||||
className="h-5 w-5 text-[var(--ipal-text,light-dark(#525252,var(--ipal-text-dark,#d4d4d4)))]"
|
||||
/>
|
||||
</button>
|
||||
)
|
||||
|
||||
@@ -195,7 +195,7 @@ export function LightboxDialog({
|
||||
const style = (key: keyof typeof STYLES, extra?: CSSProperties): CSSProperties | undefined =>
|
||||
unstyled ? undefined : { ...(key === 'close' || key === 'next' || key === 'previous' ? STYLES.button : {}), ...STYLES[key], ...extra }
|
||||
const motion = prefersReducedMotion()
|
||||
const dialogStyle = style('dialog', motion ? undefined : { opacity: visible ? 1 : 0, transition: `opacity ${v('duration', '200ms')} ease` })
|
||||
const dialogStyle = style('dialog', motion ? undefined : { opacity: visible ? 1 : 0, transition: `opacity ${v('duration', 'var(--ipal-duration, 200ms)')} ${v('easing', 'var(--ipal-ease-out, ease)')}` })
|
||||
const canPrev = loop || current > 0
|
||||
const canNext = loop || current < total - 1
|
||||
const step = (delta: number) => onIndexChange(lightboxIndex(current, delta, total, loop))
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, relative, resolve } from 'node:path'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
|
||||
import { contrastReport, type ContrastRow } from './contrast.js'
|
||||
import { tokensToCss } from './css.js'
|
||||
import { defineTokens, type TokensInput } from './define.js'
|
||||
|
||||
export const TOKENS_CLI_USAGE = `Usage: ipal-tokens [--in src/design/tokens.ts] [--out src/design/tokens.css] [--check] [--report] [--warn-only]
|
||||
Generates the brand CSS from the tokens file and checks WCAG contrast.
|
||||
--check fail when the CSS file is out of date (CI, ipal-doctor); writes nothing
|
||||
--report print every checked pair (for DESIGN.md), not only the failures
|
||||
--warn-only report contrast failures without failing`
|
||||
|
||||
type Log = (line: string) => void
|
||||
|
||||
function arg(args: string[], name: string, fallback: string): string {
|
||||
const i = args.indexOf(name)
|
||||
const value = i >= 0 ? args[i + 1] : undefined
|
||||
if (i >= 0 && (!value || value.startsWith('-'))) {throw new Error(`${name} needs a path`)}
|
||||
return value ?? fallback
|
||||
}
|
||||
|
||||
/** Contrast rows as aligned text lines; failures marked ✗ (error) or ! (warning). */
|
||||
export function formatContrastReport(rows: ContrastRow[]): string[] {
|
||||
return rows.map((r) => {
|
||||
const mark = r.pass ? '✓' : r.level === 'warn' ? '!' : '✗'
|
||||
const ratio = r.ratio === null ? 'n/a (colour not parsed)' : `${r.ratio.toFixed(2)}:1`
|
||||
const where = `${r.theme}${r.scope === 'inverse' ? ' · inverse' : ''}`
|
||||
return `${mark} ${`${r.fg} on ${r.bg}`.padEnd(36)} ${ratio.padEnd(10)} min ${r.min}:1 (${where})`
|
||||
})
|
||||
}
|
||||
|
||||
async function loadTokens(file: string): Promise<TokensInput> {
|
||||
if (file.endsWith('.json')) {return JSON.parse(readFileSync(file, 'utf8')) as TokensInput}
|
||||
try {
|
||||
const mod = (await import(pathToFileURL(file).href)) as { default?: TokensInput; tokens?: TokensInput }
|
||||
const tokens = mod.tokens ?? mod.default
|
||||
if (!tokens) {throw new Error(`${file} must export \`tokens\` (or a default export)`)}
|
||||
return tokens
|
||||
} catch (error) {
|
||||
if ((error as { code?: string }).code === 'ERR_UNKNOWN_FILE_EXTENSION') {
|
||||
throw new Error(`this Node cannot import .ts files: use Node 22.18+ (or run with --experimental-strip-types)`)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/** The `ipal-tokens` command. Returns the exit code. */
|
||||
export async function runTokensCli(args: string[], { cwd = process.cwd(), log = console.log }: { cwd?: string; log?: Log } = {}): Promise<number> {
|
||||
if (args.includes('--help') || args.includes('-h')) {
|
||||
log(TOKENS_CLI_USAGE)
|
||||
return 0
|
||||
}
|
||||
try {
|
||||
const input = resolve(cwd, arg(args, '--in', 'src/design/tokens.ts'))
|
||||
const output = resolve(cwd, arg(args, '--out', 'src/design/tokens.css'))
|
||||
if (!existsSync(input)) {
|
||||
log(`ipal-tokens: ${relative(cwd, input)} not found (copy the template from .agents/skills/ipal-setup/resources/tokens.ts)`)
|
||||
return 1
|
||||
}
|
||||
const tokens = defineTokens(await loadTokens(input))
|
||||
const css = tokensToCss(tokens, { source: relative(cwd, input) })
|
||||
const current = existsSync(output) ? readFileSync(output, 'utf8') : null
|
||||
let code = 0
|
||||
if (args.includes('--check')) {
|
||||
if (current !== css) {
|
||||
log(`✗ ${relative(cwd, output)} is out of date — run \`pnpm exec ipal-tokens\``)
|
||||
code = 1
|
||||
} else {log(`✓ ${relative(cwd, output)} is up to date`)}
|
||||
} else if (current !== css) {
|
||||
mkdirSync(dirname(output), { recursive: true })
|
||||
writeFileSync(output, css)
|
||||
log(`wrote ${relative(cwd, output)}`)
|
||||
} else {log(`${relative(cwd, output)} unchanged`)}
|
||||
|
||||
const rows = contrastReport(tokens)
|
||||
const errors = rows.filter((r) => !r.pass && r.level === 'error')
|
||||
const shown = args.includes('--report') ? rows : rows.filter((r) => !r.pass || r.ratio === null)
|
||||
log(`\nWCAG contrast: ${rows.filter((r) => r.pass && r.ratio !== null).length} of ${rows.length} pairs pass${args.includes('--report') ? ' (paste into DESIGN.md → Colour)' : ''}`)
|
||||
for (const line of formatContrastReport(shown)) {log(` ${line}`)}
|
||||
if (errors.length) {
|
||||
log(`\n${errors.length} pair(s) below the WCAG AA minimum${args.includes('--warn-only') ? ' (--warn-only: not failing)' : ''}`)
|
||||
if (!args.includes('--warn-only')) {code = 1}
|
||||
}
|
||||
return code
|
||||
} catch (error) {
|
||||
log(`ipal-tokens: ${(error as Error).message}`)
|
||||
return 1
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ export function tokensToCss(input: TokensInput, { source = 'src/design/tokens.ts
|
||||
const { dark, light } = resolveColors(input)
|
||||
const header = `/*
|
||||
* GENERATED by \`ipal-tokens\` from ${source} — do not edit; change ${source} and run
|
||||
* \`pnpm exec ipal-tokens\`. Import it after @intecion/ipal-kit/tokens.css.
|
||||
* \`pnpm exec ipal-tokens\`. Import it in styles.css after @intecion/ipal-kit/tokens.css (and tailwind.css).
|
||||
*/`
|
||||
const statics = Object.entries(staticOverrides(input))
|
||||
const parts = [header]
|
||||
|
||||
Reference in New Issue
Block a user