From 550fe68d274ad173d05dd9eb5e1669d661b4c195 Mon Sep 17 00:00:00 2001 From: Neretin Artem Date: Tue, 13 Aug 2024 21:25:13 +0500 Subject: [PATCH] feat(core-themes): Add methods for fallback tokens generate --- packages/themes/core-themes/src/generate.ts | 12 ++- .../core-themes/src/utils/fallbackTokens.ts | 74 +++++++++++++++++++ .../themes/core-themes/src/utils/index.ts | 1 + .../themes/core-themes/src/utils/other.ts | 2 + 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 packages/themes/core-themes/src/utils/fallbackTokens.ts diff --git a/packages/themes/core-themes/src/generate.ts b/packages/themes/core-themes/src/generate.ts index 5fc437402f..49abac2dea 100644 --- a/packages/themes/core-themes/src/generate.ts +++ b/packages/themes/core-themes/src/generate.ts @@ -16,7 +16,7 @@ import { generateThemes, generateTokens, } from './generators'; -import { getMetaGrouped, readTheme } from './utils'; +import { getMetaGrouped, readTheme, fallbackCreateColorTokensWrapper } from './utils'; export const generate = async (themes: ThemeRequest[]) => { const themeDir = 'src'; @@ -29,8 +29,14 @@ export const generate = async (themes: ThemeRequest[]) => { const { meta, variations } = themeSource; const metaGrouped = getMetaGrouped(themeSource.meta); - const colorCSSVariables = createColorTokens(variations.color, metaGrouped.color); - const colorJSVariables = createColorTokens(variations.color, metaGrouped.color, isJS); + // TODO: Использовать createColorTokens: https://github.com/salute-developers/plasma/issues/1363 + const colorCSSVariables = fallbackCreateColorTokensWrapper(theme.name, variations.color, metaGrouped.color); + const colorJSVariables = fallbackCreateColorTokensWrapper( + theme.name, + variations.color, + metaGrouped.color, + isJS, + ); const gradientCSSVariables = createGradientTokens(variations.gradient, metaGrouped.gradient); const gradientJSVariables = createGradientTokens(variations.gradient, metaGrouped.gradient, isJS); diff --git a/packages/themes/core-themes/src/utils/fallbackTokens.ts b/packages/themes/core-themes/src/utils/fallbackTokens.ts new file mode 100644 index 0000000000..6a0b030dd8 --- /dev/null +++ b/packages/themes/core-themes/src/utils/fallbackTokens.ts @@ -0,0 +1,74 @@ +// TODO: Убрать использование файла: https://github.com/salute-developers/plasma/issues/1363 + +import { createColorTokens } from '../creators'; +import { TokenType } from '../types'; + +import { camelToKebab } from './other'; + +export const deprecatedColorTokenOnActualToken: Record = { + whitePrimary: 'onDarkTextPrimary', + whiteSecondary: 'onDarkTextSecondary', + whiteTertiary: 'onDarkTextTertiary', + blackPrimary: 'onLightTextPrimary', + blackSecondary: 'onLightTextSecondary', + blackTertiary: 'onLightTextTertiary', + buttonBlack: 'onLightSurfaceSolidDefault', + buttonBlackSecondary: 'onLightSurfaceTransparentSecondary', + buttonWhite: 'onDarkSurfaceSolidDefault', + buttonWhiteSecondary: 'onDarkSurfaceTransparentSecondary', + text: 'textPrimary', + primary: 'textPrimary', + secondary: 'textSecondary', + tertiary: 'textTertiary', + paragraph: 'textParagraph', + background: 'backgroundPrimary', + accent: 'textAccent', + success: 'textPositive', + warning: 'textWarning', + critical: 'textNegative', + overlay: 'overlaySoft', + surfaceLiquid01: 'surfaceTransparentPrimary', + surfaceLiquid02: 'surfaceTransparentSecondary', + surfaceLiquid03: 'surfaceTransparentTertiary', + surfaceSolid01: 'surfaceSolidPrimary', + surfaceSolid02: 'surfaceSolidSecondary', + surfaceSolid03: 'surfaceSolidTertiary', + surfaceCard: 'surfaceTransparentCard', + buttonSecondary: 'surfaceTransparentSecondary', + buttonAccent: 'textAccent', + buttonSuccess: 'surfacePositive', + buttonWarning: 'surfaceWarning', + buttonCritical: 'surfaceNegative', +}; + +export const getFallbackTokens = (isJS?: boolean) => { + const legacyPrefix = 'plasmaColors'; + + return Object.entries(deprecatedColorTokenOnActualToken) + .map(([oldName, actualName]) => { + const oldVarName = `--${camelToKebab(legacyPrefix)}-${camelToKebab(oldName)}`; + const actualVarName = `--${camelToKebab(actualName)}`; + + if (isJS) { + return `/** @deprecated instead use ${actualName} */\nexport const ${oldName} = 'var(${oldVarName}, var(${actualVarName}))';\n`; + } + + return ` ${oldVarName}: var(${actualVarName});`; + }) + .join('\n'); +}; + +export const fallbackCreateColorTokensWrapper = (name: string, color: any, tokens?: Array, isJS = false) => { + const colorVariables = createColorTokens(color, tokens, isJS); + + if (name === 'plasma_web' || name === 'plasma_b2c') { + const fallbackTokens = getFallbackTokens(isJS); + + return { + dark: `${colorVariables.dark}\n${fallbackTokens}`, + light: `${colorVariables.light}\n${fallbackTokens}`, + }; + } + + return colorVariables; +}; diff --git a/packages/themes/core-themes/src/utils/index.ts b/packages/themes/core-themes/src/utils/index.ts index 7469a937f4..021054ad0e 100644 --- a/packages/themes/core-themes/src/utils/index.ts +++ b/packages/themes/core-themes/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './meta'; export * from './theme'; export * from './other'; +export * from './fallbackTokens'; diff --git a/packages/themes/core-themes/src/utils/other.ts b/packages/themes/core-themes/src/utils/other.ts index 829cbfc14a..77fbd60766 100644 --- a/packages/themes/core-themes/src/utils/other.ts +++ b/packages/themes/core-themes/src/utils/other.ts @@ -1,2 +1,4 @@ export const kebabToCamel = (str: string) => str.replace(/-([a-z])/g, (_, group) => group.toUpperCase()).replace(/^./, (char) => char.toLowerCase()); + +export const camelToKebab = (str: string) => str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);