This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added conversion scripts for json to css
- Loading branch information
1 parent
7f51e5a
commit f9bcd98
Showing
3 changed files
with
106 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable guard-for-in */ | ||
/* eslint-disable n/no-sync */ | ||
/* eslint-disable import/unambiguous */ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const brandColorsPath = path.resolve( | ||
__dirname, | ||
'../src/figma/brandColors.json', | ||
); | ||
const brandColorsCSSPath = path.resolve( | ||
__dirname, | ||
'../src/css/brand-colors.css', | ||
); | ||
|
||
const brandColors = JSON.parse(fs.readFileSync(brandColorsPath, 'utf8')); | ||
|
||
let cssContent = `/** | ||
* Brand Colors | ||
* Do not use "--brand-colors" in your code | ||
* Instead use the "--color-" variables to ensure | ||
* theme compatible styles | ||
*/ | ||
:root { | ||
`; | ||
|
||
for (const color in brandColors) { | ||
if (color !== 'white' && color !== 'black') { | ||
cssContent += ` /* ${color.charAt(0).toUpperCase() + color.slice(1)} */\n`; | ||
for (const shade in brandColors[color]) { | ||
const variableName = `--brand-colors-${color}-${color}${shade.padStart( | ||
3, | ||
'0', | ||
)}`; | ||
const colorValue = brandColors[color][shade].value; | ||
cssContent += ` ${variableName}: ${colorValue};\n`; | ||
} | ||
} else { | ||
const variableName = `--brand-colors-${color}`; | ||
const colorValue = brandColors[color].value; | ||
cssContent += ` ${variableName}: ${colorValue};\n`; | ||
} | ||
} | ||
|
||
cssContent += `}\n`; | ||
|
||
fs.writeFileSync(brandColorsCSSPath, cssContent, 'utf8'); | ||
console.log('Brand colors CSS file generated successfully.'); |
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,56 @@ | ||
/* eslint-disable guard-for-in */ | ||
/* eslint-disable jsdoc/require-jsdoc */ | ||
/* eslint-disable n/no-sync */ | ||
/* eslint-disable import/unambiguous */ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const lightThemePath = path.resolve(__dirname, '../src/figma/lightTheme.json'); | ||
const darkThemePath = path.resolve(__dirname, '../src/figma/darkTheme.json'); | ||
const lightThemeCSSPath = path.resolve( | ||
__dirname, | ||
'../src/css/light-theme-colors.css', | ||
); | ||
const darkThemeCSSPath = path.resolve( | ||
__dirname, | ||
'../src/css/dark-theme-colors.css', | ||
); | ||
|
||
const lightTheme = JSON.parse(fs.readFileSync(lightThemePath, 'utf8')); | ||
const darkTheme = JSON.parse(fs.readFileSync(darkThemePath, 'utf8')); | ||
|
||
function generateThemeCSS(theme, themeName) { | ||
let cssContent = `/* | ||
* ${themeName} Theme Colors | ||
* Use these css variables in your code. | ||
* This will ensure theme compatibility | ||
*/ | ||
${ | ||
themeName === 'Light' ? ":root, [data-theme='light']" : "[data-theme='dark']" | ||
} { | ||
`; | ||
|
||
for (const section in theme) { | ||
for (const key in theme[section]) { | ||
const variableName = `--color-${section}-${key.replace(/_/gu, '-')}`; | ||
const { value } = theme[section][key]; | ||
const { description } = theme[section][key]; | ||
const cssValue = value.startsWith('{') | ||
? `var(--brand-colors-${value.slice(1, -1).replace('.', '-')})` | ||
: value; | ||
cssContent += ` /* ${description} */\n ${variableName}: ${cssValue};\n`; | ||
} | ||
} | ||
|
||
cssContent += `}\n`; | ||
return cssContent; | ||
} | ||
|
||
fs.writeFileSync( | ||
lightThemeCSSPath, | ||
generateThemeCSS(lightTheme, 'Light'), | ||
'utf8', | ||
); | ||
fs.writeFileSync(darkThemeCSSPath, generateThemeCSS(darkTheme, 'Dark'), 'utf8'); | ||
console.log('Theme colors CSS files generated successfully.'); |
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