Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/calcite-design-tokens/src/build-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { semantic } from "./build/dictionaries/index.js";
import { dark, light, semantic } from "./build/dictionaries/index.js";

await dark.buildAllPlatforms();
await light.buildAllPlatforms();
await semantic.buildAllPlatforms();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import StyleDictionary from "style-dictionary";
import { register as registerTokenStudioTransformers } from "@tokens-studio/sd-transforms";
import calciteLightConfig from "../../config/calcite/light.js";
import calciteDarkConfig from "../../config/calcite/dark.js";
import calciteLightConfig from "../../config/color/light.js";
import calciteDarkConfig from "../../config/color/dark.js";
import semanticConfig from "../../config/semantic.js";
import { register as registerCalciteHooks } from "../registry/index.js";
import { applyBuiltInOverrides, applyOverrides } from "../overrides/index.js";
Expand All @@ -14,9 +14,9 @@ await registerTokenStudioTransformers(StyleDictionary, {
await registerCalciteHooks(StyleDictionary);
applyOverrides(StyleDictionary);

const semantic = new StyleDictionary(semanticConfig);
const light = new StyleDictionary(calciteLightConfig);
const dark = new StyleDictionary(calciteDarkConfig);
const semantic = new StyleDictionary(semanticConfig);

applyBuiltInOverrides([semantic, light, dark]);

Expand Down
4 changes: 3 additions & 1 deletion packages/calcite-design-tokens/src/build/filter/core.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Filter } from "style-dictionary/types";
import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isLightOrDarkColorToken } from "./light-or-dark.js";

export const filterCoreTokens: Filter["filter"] = (token) => !token.isSource && token.type !== "typography";
export const filterCoreTokens: Filter["filter"] = (token, config) =>
!token.isSource && token.type !== "typography" && !isLightOrDarkColorToken(token, config);

export const registerFilterCoreTokens: RegisterFn = () =>
StyleDictionary.registerFilter({
Expand Down
5 changes: 1 addition & 4 deletions packages/calcite-design-tokens/src/build/filter/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isThemed } from "../utils/token-types.js";

export const filterDarkColorTokens: Filter["filter"] = (token) =>
token.isSource &&
(isThemed(token, { theme: "dark", targetPropName: "filePath" }) ||
isThemed(token, { theme: "dark", targetPropName: "path" }));
export const filterDarkColorTokens: Filter["filter"] = (token) => isThemed(token, { theme: "dark" });

export const registerFilterDarkColorTokens: RegisterFn = () =>
StyleDictionary.registerFilter({
Expand Down
6 changes: 4 additions & 2 deletions packages/calcite-design-tokens/src/build/filter/global-js.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Filter } from "style-dictionary/types";
import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isLightOrDarkColorToken } from "./light-or-dark.js";

/**
* This filter helps match the test snapshot, this can be removed once outputs are consolidated
*
* @param token
* @param config
*/
export const filterGlobalTokensJs: Filter["filter"] = (token) => {
return token.isSource;
export const filterGlobalTokensJs: Filter["filter"] = (token, config) => {
return token.isSource || isLightOrDarkColorToken(token, config);
};

export const registerFilterGlobalTokensJs: RegisterFn = () =>
Expand Down
4 changes: 3 additions & 1 deletion packages/calcite-design-tokens/src/build/filter/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Filter } from "style-dictionary/types";
import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isBreakpoint } from "../utils/token-types.js";
import { state } from "../shared/state.js";
import { isLightOrDarkColorToken } from "./light-or-dark.js";

export const filterGlobalTokens: Filter["filter"] = (token, config) => {
return (
token.isSource && !(token.type === "typography" || isBreakpoint(token) || isLightOrDarkColorToken(token, config))
state.sameValueThemeTokens.has(token.key) ||
(token.isSource && !(token.type === "typography" || isBreakpoint(token) || isLightOrDarkColorToken(token, config)))
);
};

Expand Down
3 changes: 2 additions & 1 deletion packages/calcite-design-tokens/src/build/filter/include.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Filter } from "style-dictionary/types";
import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isThemed } from "../utils/token-types.js";

export const filterIncludeTokens: Filter["filter"] = (token) => !token.isSource;
export const filterIncludeTokens: Filter["filter"] = (token) => !token.isSource && !isThemed(token);

export const registerFilterIncludeTokens: RegisterFn = () => {
StyleDictionary.registerFilter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isThemed } from "../utils/token-types.js";

export const isLightOrDarkColorToken: Filter["filter"] = (token) =>
isThemed(token, { targetPropName: "filePath" }) || isThemed(token, { targetPropName: "path" });
export const isLightOrDarkColorToken: Filter["filter"] = (token) => isThemed(token);

const filterLightOrDarkColorTokens: Filter["filter"] = (token) =>
token.isSource && (isThemed(token, { targetPropName: "filePath" }) || isThemed(token, { targetPropName: "path" }));
const filterLightOrDarkColorTokens: Filter["filter"] = (token) => !token.isSource && isThemed(token);

export const registerFilterLightOrDarkColorTokens: RegisterFn = () =>
StyleDictionary.registerFilter({
Expand Down
5 changes: 1 addition & 4 deletions packages/calcite-design-tokens/src/build/filter/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../types/interfaces.js";
import { isThemed } from "../utils/token-types.js";

export const filterLightColorTokens: Filter["filter"] = (token) =>
token.isSource &&
(isThemed(token, { theme: "light", targetPropName: "filePath" }) ||
isThemed(token, { theme: "light", targetPropName: "path" }));
export const filterLightColorTokens: Filter["filter"] = (token) => isThemed(token, { theme: "light" });

export const registerFilterLightColorTokens: RegisterFn = () =>
StyleDictionary.registerFilter({
Expand Down
31 changes: 16 additions & 15 deletions packages/calcite-design-tokens/src/build/format/index-file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import prettierSync from "@prettier/sync";
import { Dictionary, FormatFn, FormatFnArguments, TransformedTokens } from "style-dictionary/types";
import { Dictionary, FormatFn, FormatFnArguments } from "style-dictionary/types";
import { fileHeader, formattedVariables } from "style-dictionary/utils";
import StyleDictionary from "style-dictionary";
import { RegisterFn, Stylesheet } from "../types/interfaces.js";
import { fromTokens } from "../utils/dictionary.js";
import { isThemed } from "../utils/token-types.js";
import { dark, light } from "../dictionaries/index.js";

export const registerFormatIndex: RegisterFn = () => {
StyleDictionary.registerFormat({
Expand All @@ -13,7 +15,7 @@ export const registerFormatIndex: RegisterFn = () => {
};

export const formatIndexFile: FormatFn = async (args) => {
const { dictionary, file, options } = args;
const { file, options } = args;

if (options.fileExtension !== ".css" && options.fileExtension !== ".scss") {
throw new Error("Only .css and .scss file extensions are supported.");
Expand All @@ -23,22 +25,21 @@ export const formatIndexFile: FormatFn = async (args) => {
const themes = ["light", "dark"] as const;
const format = options.fileExtension.replace(".", "") as Stylesheet;

const themedTokens = dictionary.allTokens.reduce(
(acc: { light: TransformedTokens; dark: TransformedTokens }, token) => {
if (token.path.includes("light")) {
acc.light[token.name] = token;
} else if (token.path.includes("dark")) {
acc.dark[token.name] = token;
}
return acc;
},
{ light: {}, dark: {} },
);
const darkDictionary = await dark.getPlatformTokens(options.platform, { cache: true });
const lightDictionary = await light.getPlatformTokens(options.platform, { cache: true });

const commonVarFormat = "css";
const varLists = {
light: createVarList(commonVarFormat, fromTokens(themedTokens.light), args),
dark: createVarList(commonVarFormat, fromTokens(themedTokens.dark), args),
light: createVarList(
commonVarFormat,
fromTokens(lightDictionary.allTokens.filter((token) => isThemed(token, { theme: "light" }))),
args,
),
dark: createVarList(
commonVarFormat,
fromTokens(darkDictionary.allTokens.filter((token) => isThemed(token, { theme: "dark" }))),
args,
),
} as const;

const classGroupStrategy = format === "scss" ? "@mixin " : ".";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { registerPreprocessorStoreSameValueThemeTokens } from "./store-same-value-theme-tokens.js";
import { registerPreprocessorStorePostMergeDictionary } from "./store-post-merge-dictionary.js";

export function registerPreprocessors(): void {
registerPreprocessorStorePostMergeDictionary();
registerPreprocessorStoreSameValueThemeTokens();
}

export * from "./store-same-value-theme-tokens.js";
export * from "./store-post-merge-dictionary.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import StyleDictionary from "style-dictionary";
import { DesignToken } from "style-dictionary/types";
import { dark, light } from "../dictionaries/index.js";
import { isThemed } from "../utils/token-types.js";
import { state } from "../shared/state.js";

export function registerPreprocessorStoreSameValueThemeTokens(): void {
StyleDictionary.registerPreprocessor({
name: PreprocessorStoreSameValueThemeTokens,
preprocessor: async (dictionary) => {
const sameValueThemeTokens = light.allTokens
.filter((token) => isThemed(token))
.filter((token, index) => token.value === dark.allTokens[index].value);
const keyToToken = new Map<string, DesignToken>();

sameValueThemeTokens.forEach((token) => {
if (token.key) {
keyToToken.set(token.key, token);
}
});
Comment on lines +11 to +20
Copy link
Copy Markdown
Contributor

@benelan benelan Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const sameValueThemeTokens = light.allTokens
.filter((token) => isThemed(token))
.filter((token, index) => token.value === dark.allTokens[index].value);
const keyToToken = new Map<string, DesignToken>();
sameValueThemeTokens.forEach((token) => {
if (token.key) {
keyToToken.set(token.key, token);
}
});
const keyToToken = new Map<string, DesignToken>();
light.allTokens.forEach((token, index) => {
if (isThemed(token) && token.value === dark.allTokens[index].value && token.key) {
keyToToken.set(token.key, token);
}
});

I think this could be simplified, can you confirm?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'll add that to #11860.


state.sameValueThemeTokens = keyToToken;

return dictionary;
},
});
}

export const PreprocessorStoreSameValueThemeTokens = "calcite/preprocessor/store-same-value-theme-tokens";
13 changes: 10 additions & 3 deletions packages/calcite-design-tokens/src/build/shared/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const sharedBuildState: any = {
postMergeDictionary: undefined,
} as const;
import { DesignToken, PreprocessedTokens } from "style-dictionary/types";

type State = {
postMergeDictionary: PreprocessedTokens;
sameValueThemeTokens: Map<string, DesignToken>;
};

const sharedBuildState = {
// populated by preprocessors
} as unknown as State;

export { sharedBuildState as state };
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Filter, ValueTransform } from "style-dictionary/types";
import { ArrayValues } from "type-fest";
import StyleDictionary from "style-dictionary";
import { RegisterFn } from "../../types/interfaces.js";
import { isBreakpoint } from "../../utils/token-types.js";

const stringValueTokenTypes = ["fontWeight", "dimension", "lineHeight", "opacity", "z-index"] as const;
const filterTypes: Filter["filter"] = (token) =>
stringValueTokenTypes.includes(token.type as ArrayValues<typeof stringValueTokenTypes>);
stringValueTokenTypes.includes(token.type as ArrayValues<typeof stringValueTokenTypes>) && !isBreakpoint(token);

const transformValueEnsureType: ValueTransform["transform"] = async (token) => {
return `${token.value}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ const transformValueMergeValues: ValueTransform["transform"] = async (token, con
);

if (tokenIndex > -1) {
return {
dark: dictionaries.dark.allTokens[tokenIndex].value,
light: dictionaries.light.allTokens[tokenIndex].value,
};
const lightValue = dictionaries.light.allTokens[tokenIndex].value;
const darkValue = dictionaries.dark.allTokens[tokenIndex].value;

if (lightValue !== darkValue) {
return {
light: lightValue,
dark: darkValue,
};
}
}

return token.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ function throwSizeError(name: string, value: string, unitType: string) {
throw `Invalid Number: '${name}: ${value}' is not a valid number, cannot transform to '${unitType}' \n`;
}

function isNotBorderOrFixed(token: TransformedToken) {
return !token.path.some((path) => ["border", "fixed"].includes(path));
function isStaticPx(token: TransformedToken) {
return !token.path.some((path) => ["base", "border", "fixed", "px"].includes(path));
}

function filter(token: TransformedToken, options: Config) {
return (
isSource(token) &&
!isBreakpoint(token) &&
isNotBorderOrFixed(token) &&
isStaticPx(token) &&
isDimension(token, options) &&
!isFontRelated(token) &&
!isCornerRadius(token) &&
Expand Down
6 changes: 3 additions & 3 deletions packages/calcite-design-tokens/src/build/utils/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Dictionary, TransformedTokens } from "style-dictionary/types";
import { Dictionary } from "style-dictionary/types";
import { convertTokenData } from "style-dictionary/utils";

export function fromTokens(tokens: TransformedTokens): Dictionary {
export function fromTokens(tokens: Parameters<typeof convertTokenData>[0]): Dictionary {
return {
tokens: tokens,
tokens: convertTokenData(tokens, { output: "object" }),
allTokens: convertTokenData(tokens, { output: "array" }),
tokenMap: convertTokenData(tokens, { output: "map" }),
};
Expand Down
7 changes: 3 additions & 4 deletions packages/calcite-design-tokens/src/build/utils/token-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ export function isTypography(token: TransformedToken): boolean {

interface IsThemedOptions {
theme?: "light" | "dark";
targetPropName?: keyof Pick<TransformedToken, "filePath" | "path">;
}

export function isThemed(token: TransformedToken, options?: IsThemedOptions): boolean {
const { theme, targetPropName } = options || {};
const targetProp = token[targetPropName ?? "filePath"];
export function isThemed(token: DesignToken, options?: IsThemedOptions): boolean {
const { theme } = options || {};
const targetProp = token.filePath;

return (
token.type === "color" &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { expandTypesMap as sdTypes } from "@tokens-studio/sd-transforms";
import { transformers, filters, headers, formats } from "../../build/registry/index.js";

const config: Config = {
source: ["src/tokens/semantic/calcite/dark.json"],
include: ["src/tokens/core/[!$]*.json"],
source: ["src/tokens/semantic/color/dark.json"],
include: ["src/tokens/semantic/color/dark.json", "src/tokens/core/[!$]*.json"],
preprocessors: ["tokens-studio"],
platforms: {
scss: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { expandTypesMap as sdTypes } from "@tokens-studio/sd-transforms";
import { transformers, filters, headers, formats } from "../../build/registry/index.js";

const config: Config = {
source: ["src/tokens/semantic/calcite/light.json"],
include: ["src/tokens/core/[!$]*.json"],
source: ["src/tokens/semantic/color/light.json"],
include: ["src/tokens/semantic/color/light.json", "src/tokens/core/[!$]*.json"],
preprocessors: ["tokens-studio"],
platforms: {
scss: {
Expand Down
Loading
Loading