-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/configuration-builder/src/utils/useConfigurationExporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useConfiguratorStatusContext } from "../ConfiguratorStatusContext"; | ||
import { ColorToken, colorTokenToRGBA as _colorTokenToRGBA } from "./paletteUtils"; | ||
|
||
function colorTokenToVarName(colorToken: ColorToken): string { | ||
const tokenPart = `${colorToken.colorKey.replace("-", "")}`; | ||
if (colorToken.alpha === 100) { | ||
return tokenPart; | ||
} | ||
return `${tokenPart}_${colorToken.alpha}`; | ||
} | ||
|
||
export function useConfigurationExporter(): () => string { | ||
const { tokens, colors } = useConfiguratorStatusContext().theme; | ||
const colorTokenToRGBA = _colorTokenToRGBA(colors); | ||
return () => { | ||
const prelude = `import { BentoTheme } from "@buildo/bento-design-system";`; | ||
|
||
const usedColors: Record<string, string> = {}; | ||
Object.entries(tokens).forEach(([_, tokensSection]) => { | ||
Object.entries(tokensSection).forEach(([_, colorToken]) => { | ||
const rgba = colorTokenToRGBA(colorToken); | ||
if (rgba) { | ||
usedColors[colorTokenToVarName(colorToken)] = rgba; | ||
} | ||
}); | ||
}); | ||
|
||
const colorConsts = Object.entries(usedColors) | ||
.reduce((acc, [colorKey, color]) => { | ||
return [...acc, `const ${colorKey} = "${color}";`]; | ||
}, [] as string[]) | ||
.join("\n"); | ||
|
||
let themeCode = "export const theme: BentoTheme = {\n"; | ||
Object.entries(tokens).forEach(([key, tokensSection]) => { | ||
themeCode += ` ${key}: {\n`; | ||
Object.entries(tokensSection).forEach(([key2, colorToken]) => { | ||
themeCode += ` ${key2}: ${colorTokenToVarName(colorToken)},\n`; | ||
}); | ||
themeCode += " },\n"; | ||
}); | ||
themeCode += "};"; | ||
|
||
return [prelude, colorConsts, themeCode].join("\n\n"); | ||
}; | ||
} |