Merge claude/design-tokens-build (d6675d2): theme on any element, primary-inverse roles
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -42,6 +42,9 @@ export const COLOR_DEFAULTS = {
|
||||
dark: 'color-mix(in oklch, var(--ipal-primary), black 12%)',
|
||||
light: 'color-mix(in oklch, var(--ipal-primary), black 12%)',
|
||||
},
|
||||
// primary inside an inverted section (a dark brand colour vanishes on a dark inverse surface)
|
||||
'primary-inverse': { dark: 'var(--ipal-primary)', light: 'var(--ipal-primary)' },
|
||||
'primary-inverse-text': { dark: 'var(--ipal-primary-text)', light: 'var(--ipal-primary-text)' },
|
||||
'primary-text': { dark: 'oklch(1 0 0)', light: 'oklch(1 0 0)' },
|
||||
secondary: { dark: 'var(--ipal-text-strong)', light: 'var(--ipal-text-strong)' },
|
||||
'secondary-hover': {
|
||||
@@ -76,6 +79,9 @@ export const INVERSE_SCOPE: Partial<Record<ColorRole, string>> = {
|
||||
focus: 'var(--ipal-focus-inverse)',
|
||||
hover: onInverse(12),
|
||||
link: 'var(--ipal-link-inverse)',
|
||||
primary: 'var(--ipal-primary-inverse)',
|
||||
'primary-hover': 'color-mix(in oklch, var(--ipal-primary-inverse), black 12%)',
|
||||
'primary-text': 'var(--ipal-primary-inverse-text)',
|
||||
surface: onInverse(8),
|
||||
text: 'var(--ipal-text-inverse)',
|
||||
'text-muted': onInverse(72),
|
||||
|
||||
@@ -33,7 +33,6 @@ const PAIRS: Pair[] = [
|
||||
{ bg: 'surface-inverse', fg: 'link-inverse', min: 4.5 },
|
||||
{ bg: 'bg', fg: 'focus', min: 3 },
|
||||
{ bg: 'surface-inverse', fg: 'focus-inverse', min: 3 },
|
||||
{ bg: 'surface-inverse', fg: 'primary', level: 'warn', min: 3 },
|
||||
...(['success', 'warning', 'danger', 'info'] as const).flatMap((state) => [
|
||||
{ bg: 'bg' as const, fg: state, min: 4.5 },
|
||||
{ bg: `${state}-surface` as const, fg: state, min: 4.5 },
|
||||
@@ -42,6 +41,9 @@ const PAIRS: Pair[] = [
|
||||
|
||||
/** Content inside an inverted section: muted text and links on the inverse surface. */
|
||||
const INVERSE_PAIRS: Pair[] = [
|
||||
// primary is re-mapped to primary-inverse: a dark brand colour needs a lighter one here
|
||||
{ bg: 'surface-inverse', fg: 'primary', level: 'warn', min: 3 },
|
||||
{ bg: 'primary', fg: 'primary-text', min: 4.5 },
|
||||
{ bg: 'surface-inverse', fg: 'text-muted', min: 4.5 },
|
||||
{ bg: 'surface', fg: 'text', min: 4.5 },
|
||||
{ bg: 'surface', fg: 'link', min: 4.5 },
|
||||
@@ -56,7 +58,15 @@ export function contrastReport(input: TokensInput = {}): ContrastRow[] {
|
||||
const resolved = resolveColors(input)
|
||||
return themes.flatMap((theme) => {
|
||||
const colors: Record<string, string> = resolved[theme]
|
||||
const inverse: Record<string, string> = { ...colors, ...INVERSE_SCOPE }
|
||||
const outer = (name: string) => (name.startsWith('ipal-') ? colors[name.slice(5)] : undefined)
|
||||
// Roles the scope does not declare are inherited, i.e. computed on the ancestor: resolve them there first.
|
||||
const inherited = Object.fromEntries(
|
||||
Object.entries(colors).flatMap(([role, value]) => {
|
||||
const rgb = parseColor(value, outer)
|
||||
return rgb ? [[role, `rgb(${rgb.r * 255} ${rgb.g * 255} ${rgb.b * 255} / ${rgb.a})`]] : []
|
||||
}),
|
||||
)
|
||||
const inverse: Record<string, string> = { ...inherited, ...INVERSE_SCOPE }
|
||||
const check = (map: Record<string, string>, scope: ContrastRow['scope']) => (pair: Pair): ContrastRow => {
|
||||
const lookup = (name: string) => (name.startsWith('ipal-') ? map[name.slice(5)] : undefined)
|
||||
const fg = parseColor(map[pair.fg], lookup)
|
||||
|
||||
+21
-10
@@ -9,10 +9,17 @@ import {
|
||||
import { resolveColors, staticOverrides, type TokensInput } from './define.js'
|
||||
import { fluidClamp, type RemValue } from './fluid.js'
|
||||
|
||||
/** The dark theme. Two parts so it beats a brand's plain `:root` (the brand file comes later). */
|
||||
export const DARK_SELECTOR = ':root[data-theme="dark"]'
|
||||
/**
|
||||
* The themes. `data-theme` works on any element (a dark panel on a light page, a styleguide).
|
||||
* On <html> the dark selector has two parts so it beats the brand's light block (the brand file comes later).
|
||||
*/
|
||||
export const LIGHT_SELECTOR = ':root, [data-theme="light"]'
|
||||
export const DARK_SELECTOR = ':root[data-theme="dark"], [data-theme="dark"]'
|
||||
export const INVERSE_SELECTOR = '[data-surface="inverse"]'
|
||||
|
||||
const SHADOWS_LIGHT = Object.entries(STATIC_DEFAULTS).filter(([name]) => name.startsWith('shadow-'))
|
||||
const STATICS = Object.entries(STATIC_DEFAULTS).filter(([name]) => !name.startsWith('shadow-'))
|
||||
|
||||
type Entries = Iterable<readonly [string, string]>
|
||||
|
||||
function block(selector: string, entries: Entries, indent = ''): string {
|
||||
@@ -45,17 +52,20 @@ export function defaultTokensCss(): string {
|
||||
* @intecion/ipal-kit/tokens.css — neutral defaults for every --ipal-* design token
|
||||
* (generated from src/modules/tokens/catalog.ts; docs/design-tokens.md).
|
||||
*
|
||||
* Light values on :root, dark values on :root[data-theme="dark"] (set by <ThemeScript>,
|
||||
* storage key "theme"). A brand overrides them in src/design/tokens.css, generated by
|
||||
* Light values on :root, dark values on [data-theme="dark"] (on <html> set by <ThemeScript>,
|
||||
* storage key "theme"; on any element for a dark panel). A brand overrides them in src/design/tokens.css, generated by
|
||||
* \`ipal-tokens\` from src/design/tokens.ts and imported after this file.
|
||||
* Components read only the role names, never "-dark": the roles switch with the theme.
|
||||
* The type and space scale is in fluid.css / fluid-wide.css — import one of them as well.
|
||||
*/`,
|
||||
block(':root', [...colorEntries('light'), ...Object.entries(STATIC_DEFAULTS), ['color-scheme', 'light']]),
|
||||
block(':root', STATICS),
|
||||
block(LIGHT_SELECTOR, [...colorEntries('light'), ...SHADOWS_LIGHT, ['color-scheme', 'light']]),
|
||||
block(DARK_SELECTOR, [...colorEntries('dark'), ...Object.entries(DARK_SHADOWS), ...legacyAliases, ['color-scheme', 'dark']]),
|
||||
`/* Inverted section (settings.background = inverted): content inside reads the inverse roles. */`,
|
||||
`/* Inverted section (settings.background = inverted): content inside reads the inverse roles.
|
||||
Put data-surface and data-theme on different elements: on one, the primary re-map is a var() cycle. */`,
|
||||
block(INVERSE_SELECTOR, inverseEntries),
|
||||
block(`${DARK_SELECTOR} ${INVERSE_SELECTOR}`, [['color-scheme', 'light']]),
|
||||
block('[data-theme="dark"] [data-surface="inverse"]', [['color-scheme', 'light']]),
|
||||
block('[data-theme="dark"] [data-theme="light"] [data-surface="inverse"]', [['color-scheme', 'dark']]),
|
||||
`/* No motion for people who asked for none (the a11y widget's "no animations" too). */`,
|
||||
`@media (prefers-reduced-motion: reduce) {\n${block('html:root', reducedMotion, ' ')}\n}`,
|
||||
block('html[data-a11y-motion="reduce"]', reducedMotion),
|
||||
@@ -86,7 +96,8 @@ export function tokensToCss(input: TokensInput, { source = 'src/design/tokens.ts
|
||||
const statics = Object.entries(staticOverrides(input))
|
||||
const parts = [header]
|
||||
if (theme === 'both') {
|
||||
parts.push(block(':root', [...changed('light', light), ...statics, ...fluidEntries(input)]))
|
||||
parts.push(block(':root', [...statics, ...fluidEntries(input)]))
|
||||
parts.push(block(LIGHT_SELECTOR, changed('light', light)))
|
||||
// brand roles set only for light carry over to dark (resolveColors); other roles keep the dark defaults
|
||||
parts.push(block(DARK_SELECTOR, changed('dark', dark)))
|
||||
} else {
|
||||
@@ -98,8 +109,8 @@ export function tokensToCss(input: TokensInput, { source = 'src/design/tokens.ts
|
||||
...Object.fromEntries(Object.entries(shadowSource).filter(([name]) => name.startsWith('shadow-'))),
|
||||
...Object.fromEntries(statics.filter(([name]) => name.startsWith('shadow-'))),
|
||||
})
|
||||
parts.push(block(':root', [...all, ...statics, ...shadows, ...fluidEntries(input), scheme]))
|
||||
parts.push(block(DARK_SELECTOR, [...all, ...shadows, scheme]))
|
||||
parts.push(block(':root', [...statics, ...fluidEntries(input)]))
|
||||
parts.push(block(`${LIGHT_SELECTOR}, ${DARK_SELECTOR}`, [...all, ...shadows, scheme]))
|
||||
parts.push(block(INVERSE_SELECTOR, [['color-scheme', theme === 'dark' ? 'light' : 'dark']]))
|
||||
}
|
||||
return parts.filter(Boolean).join('\n\n') + '\n'
|
||||
|
||||
@@ -43,7 +43,7 @@ type FluidSizeName<P extends string> = FluidSize extends infer K ? (K extends `$
|
||||
|
||||
/** Roles whose light value carries over to dark when the brand sets no dark value. */
|
||||
export const BRAND_ROLES: readonly ColorRole[] = [
|
||||
'primary', 'primary-text', 'secondary', 'secondary-text', 'accent', 'accent-text',
|
||||
'primary', 'primary-text', 'primary-inverse', 'primary-inverse-text', 'secondary', 'secondary-text', 'accent', 'accent-text',
|
||||
]
|
||||
|
||||
function check(keys: Iterable<string>, known: Record<string, unknown>, where: string) {
|
||||
|
||||
@@ -3,7 +3,7 @@ export type { ColorRole, IpalTokenName } from './catalog.js'
|
||||
export { contrastRatio, parseColor } from './color.js'
|
||||
export { contrastReport } from './contrast.js'
|
||||
export type { ContrastRow } from './contrast.js'
|
||||
export { DARK_SELECTOR, defaultTokensCss, INVERSE_SELECTOR, tokensToCss } from './css.js'
|
||||
export { DARK_SELECTOR, defaultTokensCss, INVERSE_SELECTOR, LIGHT_SELECTOR, tokensToCss } from './css.js'
|
||||
export { defineTokens, resolveColors } from './define.js'
|
||||
export type { ColorInput, TokensInput } from './define.js'
|
||||
export { FLUID_RANGES, FLUID_SCALE, fluidClamp } from './fluid.js'
|
||||
|
||||
Reference in New Issue
Block a user