-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from ably/automate-color-computation
[WEB-4025] replace hard-coded TW tables with build-generated alternative
- Loading branch information
Showing
8 changed files
with
248 additions
and
863 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 |
---|---|---|
|
@@ -8,4 +8,5 @@ yarn-error.log | |
/reset | ||
.idea/* | ||
types | ||
index.d.ts | ||
index.d.ts | ||
computed-colors-*.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { | ||
numericalColors, | ||
variants, | ||
prefixes, | ||
Theme, | ||
ComputedColors, | ||
} from "../src/core/styles/colors/types"; | ||
|
||
const computeColors = (base: Theme) => { | ||
if (base !== "dark" && base !== "light") { | ||
throw new Error(`Invalid base theme: ${base}. Expected "dark" or "light".`); | ||
} | ||
|
||
const colors = {} as ComputedColors; | ||
|
||
variants.forEach((variant) => | ||
prefixes.forEach((property) => | ||
numericalColors.forEach((colorSet) => | ||
colorSet.map((color, index) => { | ||
if (base === "dark") { | ||
colors[`${variant}${property}-${colorSet[index]}`] = { | ||
light: `${variant}${property}-${colorSet[colorSet.length - index - 1]}`, | ||
}; | ||
} else if (base === "light") { | ||
colors[`${variant}${property}-${colorSet[index]}`] = { | ||
dark: `${variant}${property}-${colorSet[colorSet.length - index - 1]}`, | ||
}; | ||
} | ||
}), | ||
), | ||
), | ||
); | ||
|
||
return colors; | ||
}; | ||
|
||
const darkOutputPath = path.join( | ||
__dirname, | ||
"../src/core/styles/colors", | ||
"computed-colors-dark.json", | ||
); | ||
const lightOutputPath = path.join( | ||
__dirname, | ||
"../src/core/styles/colors", | ||
"computed-colors-light.json", | ||
); | ||
|
||
async function writeComputedColors() { | ||
try { | ||
await Promise.all([ | ||
fs.promises.writeFile( | ||
darkOutputPath, | ||
JSON.stringify(computeColors("dark"), null, 2), | ||
"utf-8", | ||
), | ||
fs.promises.writeFile( | ||
lightOutputPath, | ||
JSON.stringify(computeColors("light"), null, 2), | ||
"utf-8", | ||
), | ||
]); | ||
console.log( | ||
`🎨 Tailwind theme classes have been computed and written to JSON files.`, | ||
); | ||
} catch { | ||
console.error(`Error persisting computed colors.`); | ||
} | ||
} | ||
|
||
writeComputedColors(); |
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
Oops, something went wrong.