Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add style-dictionary tokens for download #820

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Outlet } from "react-router-dom";
import Notifications from "./components/Notifications";
import { useThemeBuilderStore } from "./store";
import { useEffect } from "react";
import { DefaultColorType } from "./utils/data.ts";
import {
getNonColorCssProperties,
getPaletteOutput,
getSpeakingNames,
} from "./utils/outputs";
import { DefaultColorType } from "./utils/data.ts";
} from "./utils/outputs/web";

const App = () => {
const { speakingNames, luminanceSteps, theme } = useThemeBuilderStore(
Expand Down
38 changes: 35 additions & 3 deletions src/utils/outputs/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ import { generateDensityEnumFile } from "./compose/density.ts";
import { getSketchColorsAsString } from "./sketch.ts";
import { getFontFaces } from "./web/fonts.ts";
import { kebabCase } from "../index.ts";
import { generateCustomColorClass } from "./web/custom-color-class.ts";
import { generateAndroidReadmeFile } from "./compose/readme.ts";
import {
getCssPropertyAsString,
getCssThemeProperties,
getFullColorCss,
getPaletteOutput,
getSpeakingNames,
} from "./index.ts";
import { generateCustomColorClass } from "./web/custom-color-class.ts";
import { generateAndroidReadmeFile } from "./compose/readme.ts";
} from "./web";
import {
getSDColorPalette,
getSDSpeakingColors,
} from "./style-dictionary/colors.ts";
import { getDBNonColorToken } from "./style-dictionary";

const download = (fileName: string, file: Blob) => {
const element = document.createElement("a");
Expand Down Expand Up @@ -67,6 +72,33 @@ export const downloadTheme = async (
const zip = new JSZip();
zip.file(`${fileName}.json`, themeJsonString);

// Style dictionary

const sdFolder: string = "StyleDictionary";
zip.file(
`${sdFolder}/palette-colors.json`,
JSON.stringify(getSDColorPalette(allColors, luminanceSteps)),
);
zip.file(
`${sdFolder}/speaking-colors.json`,
JSON.stringify(getSDSpeakingColors(speakingNames, allColors)),
);
const token = getDBNonColorToken(theme);
const tokenProps = [
"spacing",
"sizing",
"border",
"elevation",
"transition",
"font",
];
for (const prop of tokenProps) {
zip.file(
`${sdFolder}/${prop}s.json`,
JSON.stringify({ [prop]: token[prop] }),
);
}
Comment on lines +95 to +100

Choose a reason for hiding this comment

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

issue: Style Dictionary Tokens need a value property, to be recognised.

This code generates something like this:

{
  "border": {
    "height": {
      "3xs": "0.0625rem",

but we need

{
  "border": {
    "height": {
      "3xs": {
        "value": "0.0625rem",
      }


//Android
const androidFolder: string = "Android";
const androidThemeFolder: string = `${androidFolder}/theme`;
Expand Down
225 changes: 24 additions & 201 deletions src/utils/outputs/index.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,34 @@
import {
DefaultColorType,
HeisslufType,
SpeakingName,
ThemeType,
} from "../data.ts";
import traverse from "traverse";
import { DefaultColorType, HeisslufType } from "../data.ts";
import { getHeissluftColors } from "../generate-colors.ts";

export const prefix = "db";

export const getCssPropertyAsString = (
properties: any,
inRoot?: boolean,
): string => {
let resultString = "";
export const nonRemProperties = ["opacity", "elevation", "transition", "font"];

for (const [key, value] of Object.entries(properties)) {
resultString += `${key}: ${value};\n`;
}

if (inRoot) {
return `:root{${resultString}}`;
}

return resultString;
};

const nonRemProperties = ["opacity", "elevation", "transition", "font"];

const isFontFamily = (path: string[]): boolean =>
export const isFontFamily = (path: string[]): boolean =>
(path[0] === "font" && path[1] === "family") || path[0] !== "font";

export const getNonColorCssProperties = (
theme: ThemeType,
asString?: boolean,
) => {
const resolvedProperties: any = {};
traverse(theme).forEach(function (value) {
if (
this.isLeaf &&
this.path.length > 0 &&
this.path[0] !== "colors" &&
this.path[0] !== "additionalColors" &&
this.path[0] !== "customColors" &&
this.path[0] !== "branding" &&
isFontFamily(this.path) &&
!this.path.includes("_scale")
) {
const key = `--${prefix}-${this.path
.map((path) => path.toLowerCase())
.map((path) => {
if (path === "lineheight") {
return "line-height";
} else if (path === "fontsize") {
return "font-size";
}
return path;
})
.join("-")}`;

resolvedProperties[key] =
!nonRemProperties.includes(this.path[0]) &&
(typeof value === "string" || value instanceof String)
? `${value}rem`
: value;

if (this.path.at(-1) === "fontSize") {
const lineHeightPath = [...this.path];
lineHeightPath[lineHeightPath.length - 1] = "lineHeight";
const fontSizeAsNumber = Number(value);
const lineHeightAsNumber = Number(traverse(theme).get(lineHeightPath));

const remainingIconPath = this.path
.filter((path) => path !== "typography" && path !== "fontSize")
.join("-");
const fontSizing = fontSizeAsNumber * lineHeightAsNumber;
resolvedProperties[
`--${prefix}-base-icon-weight-${remainingIconPath}`
] = fontSizing * 16;
resolvedProperties[
`--${prefix}-base-icon-font-size-${remainingIconPath}`
] = `${fontSizing}rem`;
}
}
});

if (asString) {
return getCssPropertyAsString(resolvedProperties);
}

return resolvedProperties;
};

export const getCssThemeProperties = (theme: ThemeType): string => {
const customTheme = getNonColorCssProperties(theme, true);

return `:root{
${customTheme}
}
`;
};

export const getFullColorCss = (
colorsPalette: string,
colorsSpeakingNames: string,
): string => {
return `:root{
${colorsPalette}
${colorsSpeakingNames}
}

[data-color-scheme="light"] {
color-scheme: light;
}

[data-color-scheme="dark"] {
color-scheme: dark;
}
`;
};
export const isDimensionProperty = (context: any) =>
context.isLeaf &&
context.path.length > 0 &&
context.path[0] !== "colors" &&
context.path[0] !== "additionalColors" &&
context.path[0] !== "customColors" &&
context.path[0] !== "branding" &&
isFontFamily(context.path) &&
!context.path.includes("_scale");

export const getTraverseKey = (path: string[], separator: string = "-") =>
`${path
.map((p) => p.toLowerCase())
.map((p) => {
return p === "lineheight"
? "line-height"
: p === "fontsize"
? "font-size"
: p;
})
.join(separator)}`;

export const getPalette = (
allColors: Record<string, DefaultColorType>,
Expand All @@ -142,90 +52,3 @@ export const getPalette = (
(previousValue, currentValue) => ({ ...previousValue, ...currentValue }),
{},
);

export const getPaletteOutput = (
allColors: Record<string, DefaultColorType>,
luminanceSteps: number[],
): any => {
const palette: Record<string, HeisslufType[]> = getPalette(
allColors,
luminanceSteps,
);
const result: any = {};

Object.entries(allColors).forEach(([unformattedName, color]) => {
const name = unformattedName.toLowerCase();

const hslType: HeisslufType[] = palette[unformattedName];
hslType.forEach((hsl) => {
result[`--${prefix}-${name}-${hsl.index ?? hsl.name}`] = hsl.hex;
});

result[`--${prefix}-${name}-origin`] = color.origin;
result[`--${prefix}-${name}-origin-light-default`] = color.originLight;
result[`--${prefix}-${name}-origin-light-hovered`] =
color.originLightHovered;
result[`--${prefix}-${name}-origin-light-pressed`] =
color.originLightPressed;
result[`--${prefix}-${name}-on-origin-light-default`] = color.onOriginLight;
result[`--${prefix}-${name}-on-origin-light-hovered`] =
color.onOriginLightHovered;
result[`--${prefix}-${name}-on-origin-light-pressed`] =
color.onOriginLightPressed;

result[`--${prefix}-${name}-origin-dark-default`] = color.originDark;
result[`--${prefix}-${name}-origin-dark-hovered`] = color.originDarkHovered;
result[`--${prefix}-${name}-origin-dark-pressed`] = color.originDarkPressed;
result[`--${prefix}-${name}-on-origin-dark-default`] = color.onOriginDark;
result[`--${prefix}-${name}-on-origin-dark-hovered`] =
color.onOriginDarkHovered;
result[`--${prefix}-${name}-on-origin-dark-pressed`] =
color.onOriginDarkPressed;
});

return result;
};

export const getSpeakingNames = (
speakingNames: SpeakingName[],
allColors: Record<string, DefaultColorType>,
): any => {
const result: any = {};
Object.entries(allColors).forEach(([unformattedName]) => {
const name = unformattedName.toLowerCase();
result[`--${prefix}-${name}-origin-default`] =
`light-dark(var(--${prefix}-${name}-origin-light-default),var(--${prefix}-${name}-origin-dark-default))`;
result[`--${prefix}-${name}-origin-hovered`] =
`light-dark(var(--${prefix}-${name}-origin-light-hovered),var(--${prefix}-${name}-origin-dark-hovered))`;
result[`--${prefix}-${name}-origin-pressed`] =
`light-dark(var(--${prefix}-${name}-origin-light-pressed),var(--${prefix}-${name}-origin-dark-pressed))`;
result[`--${prefix}-${name}-on-origin-default`] =
`light-dark(var(--${prefix}-${name}-on-origin-light-default),var(--${prefix}-${name}-on-origin-dark-default))`;
result[`--${prefix}-${name}-on-origin-hovered`] =
`light-dark(var(--${prefix}-${name}-on-origin-light-hovered),var(--${prefix}-${name}-on-origin-dark-hovered))`;
result[`--${prefix}-${name}-on-origin-pressed`] =
`light-dark(var(--${prefix}-${name}-on-origin-light-pressed),var(--${prefix}-${name}-on-origin-dark-pressed))`;

speakingNames.forEach((speakingName) => {
if (
speakingName.transparencyDark !== undefined ||
speakingName.transparencyLight !== undefined
) {
result[`--${prefix}-${name}-${speakingName.name}`] =
`light-dark(color-mix(in srgb, transparent ${
speakingName.transparencyLight
}%, var(--${prefix}-${name}-${
speakingName.light
})),color-mix(in srgb, transparent ${
speakingName.transparencyDark
}%, var(--${prefix}-${name}-${speakingName.dark})))`;
} else {
result[`--${prefix}-${name}-${speakingName.name}`] =
`light-dark(var(--${prefix}-${name}-${
speakingName.light
}),var(--${prefix}-${name}-${speakingName.dark}))`;
}
});
});
return result;
};
Loading
Loading