-
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.
feat: add useTheming hook, reduce generated TW classes
- Loading branch information
1 parent
5c05eb2
commit f943999
Showing
12 changed files
with
210 additions
and
160 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 |
---|---|---|
|
@@ -9,4 +9,4 @@ yarn-error.log | |
.idea/* | ||
types | ||
index.d.ts | ||
computed-colors-*.json | ||
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 |
---|---|---|
@@ -1,72 +1,69 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { | ||
numericalColors, | ||
variants, | ||
prefixes, | ||
Theme, | ||
ComputedColors, | ||
} from "../src/core/styles/colors/types"; | ||
import { invertTailwindClassVariant } from "../src/core/styles/colors/utils"; | ||
import { prefixes, variants } 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 directoryPath = path.join(__dirname, "../src"); | ||
const outputPath = path.join( | ||
__dirname, | ||
"../src/core/styles/colors", | ||
"computed-colors.json", | ||
); | ||
|
||
const colors = {} as ComputedColors; | ||
const joinedVariants = variants.join("|"); | ||
const joinedPrefixes = prefixes.join("|"); | ||
const colors = [ | ||
"neutral", | ||
"orange", | ||
"blue", | ||
"yellow", | ||
"green", | ||
"violet", | ||
"pink", | ||
].join("|"); | ||
|
||
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]}`, | ||
}; | ||
} | ||
}), | ||
), | ||
), | ||
); | ||
const findStringInFiles = (dir: string) => { | ||
const results: string[] = []; | ||
|
||
const readDirectory = (dir: string) => { | ||
const files = fs.readdirSync(dir); | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat.isDirectory()) { | ||
readDirectory(filePath); | ||
} else if (filePath.endsWith(".tsx")) { | ||
const content = fs.readFileSync(filePath, "utf-8"); | ||
const regex = new RegExp( | ||
`themeColor\\("((${joinedVariants}${joinedPrefixes})-(${colors})-(000|[1-9]00|1[0-3]00))"\\)`, | ||
"g", | ||
); | ||
const matches = [...content.matchAll(regex)].map((match) => match[1]); | ||
|
||
if (matches.length > 0) { | ||
results.push(...matches); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
return colors; | ||
readDirectory(dir); | ||
return Array.from(new Set(results)).sort(); | ||
}; | ||
|
||
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", | ||
const matches = findStringInFiles(directoryPath); | ||
|
||
const flippedMatches = matches.map((match) => | ||
invertTailwindClassVariant(match), | ||
); | ||
|
||
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.`); | ||
} | ||
try { | ||
fs.writeFileSync(outputPath, JSON.stringify(flippedMatches)); | ||
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
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.