Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | 3053x 3053x 3053x 8056x 52x 8004x 3001x 4x 17x 17x 17x 2x 14x 53x 1x 80x 80x 80x 3037x 80x 3040x 3040x 3040x 8x 3040x 2992x 80x 80x 2x 2x 10x 10x 10x 80x 2x 2x 10x 10x 10x 80x 1x 5x 5x 5x 80x 2x 2x 6x 6x 6x 80x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 80x 80x 80x 37x 37x 37x 11x 37x 37x 37x 4x 12x 12x 10x 10x 2x 8x 16x 8x | // SPDX-License-Identifier: MIT
/**
* CSS Custom Properties generator for Turbo Themes.
*
* Generates framework-agnostic CSS variables from theme tokens.
* This is the primary output format for platform-agnostic theming.
*
* @packageDocumentation
*/
import type { ThemeFlavor, ThemeTokens } from '@turbocoder13/turbo-themes-core';
import { generateSyntaxVarsFromTokens } from './syntax.js';
// Import centralized mappings from config
import {
CORE_MAPPINGS,
CSS_VAR_PREFIX,
OPTIONAL_GROUPS,
} from '@turbocoder13/turbo-themes-core/css/mappings';
/**
* Resolves a dot-separated path to a value in the tokens object.
*
* @param tokens - The tokens object to traverse
* @param path - Dot-separated path (e.g., 'background.base')
* @returns The resolved value or undefined if path doesn't exist
*/
function resolveTokenPath(tokens: ThemeTokens, path: string): string | undefined {
const parts = path.split('.');
let current: unknown = tokens;
for (const part of parts) {
if (current === null || current === undefined || typeof current !== 'object') {
return undefined;
}
current = (current as Record<string, unknown>)[part];
}
return typeof current === 'string' ? current : undefined;
}
/**
* Trusted font provider domains for web font imports.
* Only HTTPS URLs from these domains are allowed to prevent CSS injection.
*/
const TRUSTED_FONT_DOMAINS = [
'fonts.googleapis.com',
'fonts.gstatic.com',
'use.typekit.net',
'fonts.bunny.net',
'rsms.me', // Inter font
'fonts.cdnfonts.com',
'github.githubassets.com', // GitHub themes
] as const;
/**
* Validates that a web font URL is safe to include in CSS.
* Only allows HTTPS URLs from trusted font provider domains.
*
* @param url - The URL to validate
* @returns true if the URL is from a trusted font provider
*/
function isValidFontUrl(url: string): boolean {
try {
const parsed = new URL(url);
if (parsed.protocol !== 'https:') {
return false;
}
return TRUSTED_FONT_DOMAINS.some(
(domain) => parsed.hostname === domain || parsed.hostname.endsWith(`.${domain}`),
);
} catch {
return false;
}
}
/**
* Generates CSS custom property declarations from theme tokens.
* Uses centralized mapping configuration for consistency.
*
* @param tokens - The theme tokens to convert
* @returns Array of CSS variable declaration lines
*
* @example
* ```ts
* const lines = generateCssVarsFromTokens(theme.tokens);
* // [' --turbo-bg-base: #1e1e2e;', ' --turbo-bg-surface: #313244;', ...]
* ```
*/
export function generateCssVarsFromTokens(tokens: ThemeTokens): string[] {
const lines: string[] = [];
const prefix = CSS_VAR_PREFIX;
const add = (name: string, value: string): void => {
lines.push(` --${prefix}-${name}: ${value};`);
};
// Apply core mappings from centralized config
for (const mapping of CORE_MAPPINGS) {
// Extract variable name from cssVar (remove prefix if present)
const varName = mapping.cssVar.startsWith('--')
? mapping.cssVar.slice(2) // Remove leading --
: mapping.cssVar;
let value = resolveTokenPath(tokens, mapping.tokenPath);
// Try fallback if primary path didn't resolve
if (value === undefined && mapping.fallbackPath) {
value = resolveTokenPath(tokens, mapping.fallbackPath);
}
if (value !== undefined) {
add(varName, value);
}
}
// Optional token groups
const optionalGroups = OPTIONAL_GROUPS;
// Spacing tokens (if available)
if (tokens.spacing && optionalGroups.spacing) {
const { properties } = optionalGroups.spacing;
for (const prop of properties) {
const value = (tokens.spacing as Record<string, string>)[prop];
Eif (value) {
add(`${optionalGroups.spacing.prefix}-${prop}`, value);
}
}
}
// Elevation tokens (if available)
if (tokens.elevation && optionalGroups.elevation) {
const { properties } = optionalGroups.elevation;
for (const prop of properties) {
const value = (tokens.elevation as Record<string, string>)[prop];
Eif (value) {
add(`${optionalGroups.elevation.prefix}-${prop}`, value);
}
}
}
// Animation tokens (if available) - uses custom mappings
if (tokens.animation && optionalGroups.animation?.mappings) {
for (const mapping of optionalGroups.animation.mappings) {
const value = resolveTokenPath(tokens, mapping.tokenPath);
Eif (value) {
add(`${optionalGroups.animation.prefix}-${mapping.cssVar}`, value);
}
}
}
// Opacity tokens (if available)
if (tokens.opacity && optionalGroups.opacity) {
const { properties } = optionalGroups.opacity;
for (const prop of properties) {
const value = (tokens.opacity as Record<string, number>)[prop];
Eif (value !== undefined) {
add(`${optionalGroups.opacity.prefix}-${prop}`, String(value));
}
}
}
// Component tokens (if available)
if (tokens.components) {
const { components } = tokens;
Eif (components.card) {
Eif (components.card.bg) add('card-bg', components.card.bg);
Eif (components.card.border) add('card-border', components.card.border);
}
Eif (components.modal) {
Eif (components.modal.bg) add('modal-bg', components.modal.bg);
Eif (components.modal.cardBg) add('modal-card-bg', components.modal.cardBg);
}
Eif (components.dropdown) {
Eif (components.dropdown.bg) add('dropdown-bg', components.dropdown.bg);
Eif (components.dropdown.border) add('dropdown-border', components.dropdown.border);
Eif (components.dropdown.itemHoverBg) add('dropdown-item-hover', components.dropdown.itemHoverBg);
}
}
// Syntax highlighting tokens
const syntaxLines = generateSyntaxVarsFromTokens(tokens);
lines.push(...syntaxLines);
return lines;
}
/**
* Generates a complete CSS file for a single theme flavor.
*
* @param flavor - The theme flavor to generate CSS for
* @returns Complete CSS string with data-theme selector
*
* @example
* ```ts
* const css = generateThemeCss(catppuccinMocha);
* // [data-theme="catppuccin-mocha"] { --turbo-bg-base: #1e1e2e; ... }
* ```
*/
export function generateThemeCss(flavor: ThemeFlavor): string {
const vars = generateCssVarsFromTokens(flavor.tokens);
const webFonts = flavor.tokens.typography?.webFonts ?? [];
const fontImports = webFonts
.filter(isValidFontUrl)
.map((url) => `@import url('${url}');`)
.join('\n');
const colorScheme = flavor.appearance === 'dark' ? 'dark' : 'light';
const cssContent = `[data-theme="${flavor.id}"] {
${vars.join('\n')}
color-scheme: ${colorScheme};
}`;
return fontImports ? `${fontImports}\n\n${cssContent}\n` : `${cssContent}\n`;
}
/**
* Design system tokens that are theme-agnostic.
* These provide spacing, shadows, radius, and transitions for components.
*/
const DESIGN_SYSTEM_TOKENS = `
/* Spacing */
--space-xs: 0.25rem;
--space-sm: 0.5rem;
--space-md: 1rem;
--space-lg: 1.5rem;
--space-xl: 2rem;
--space-2xl: 3rem;
--space-3xl: 4rem;
--space-4xl: 6rem;
/* Border Radius */
--radius-sm: 0.5rem;
--radius-md: 0.75rem;
--radius-lg: 1rem;
--radius-xl: 1.5rem;
--radius-2xl: 2rem;
--radius-full: 9999px;
/* Shadows */
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
--shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07), 0 2px 4px rgba(0, 0, 0, 0.05);
--shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
--shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.15), 0 10px 10px rgba(0, 0, 0, 0.04);
--shadow-glow: 0 0 30px color-mix(in srgb, var(--turbo-brand-primary) 30%, transparent);
--shadow-glow-sm: 0 0 15px color-mix(in srgb, var(--turbo-brand-primary) 20%, transparent);
/* Transitions */
--transition-fast: 120ms ease-out;
--transition-normal: 200ms ease-out;
--transition-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);
/* Gradients (theme-aware) */
--gradient-primary: linear-gradient(135deg, var(--turbo-brand-primary), var(--turbo-state-info));
--gradient-surface: linear-gradient(180deg, var(--turbo-bg-surface), var(--turbo-bg-base));
`;
/**
* Generates the core CSS file with default variable values.
*
* @param defaultFlavor - The theme flavor to use as defaults
* @returns CSS string with :root selector
*
* @example
* ```ts
* const coreCss = generateCoreCss(catppuccinMocha);
* // :root { --turbo-bg-base: #1e1e2e; ... }
* ```
*/
export function generateCoreCss(defaultFlavor: ThemeFlavor): string {
const vars = generateCssVarsFromTokens(defaultFlavor.tokens);
return `:root {\n${vars.join('\n')}\n${DESIGN_SYSTEM_TOKENS}}\n`;
}
/**
* Generates a combined CSS file with all themes.
*
* @param flavors - Array of all theme flavors
* @param defaultFlavorId - ID of the theme to use as :root defaults
* @returns Complete CSS string with core and all theme selectors
*/
export function generateCombinedCss(
flavors: readonly ThemeFlavor[],
defaultFlavorId: string = 'catppuccin-mocha'
): string {
const defaultFlavor = flavors.find((f) => f.id === defaultFlavorId) || flavors[0];
if (!defaultFlavor) {
throw new Error('No flavors provided');
}
const coreCss = generateCoreCss(defaultFlavor);
const themeCss = flavors.map((flavor) => generateThemeCss(flavor)).join('\n');
return `/* Turbo Themes - Pure CSS Custom Properties */\n/* Generated automatically - do not edit */\n\n${coreCss}\n${themeCss}`;
}
// Re-export the prefix for external use
export { CSS_VAR_PREFIX };
|