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: enhance our theme building process by implementing support for various base font sizes #386

Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/utils/cli/bin/spark-setup-themes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ if (!configFileIsMJS) writeFileSync(jsFilePath, jsFileContents)

import(jsFilePath)
.then(module => {
const { tailwindThemeConfigPath, tailwindCSSPath, themes } = module.default
const { tailwindThemeConfigPath, tailwindCSSPath, themes, htmlFontSize = 16 } = module.default

createTailwindThemeConfigFile(tailwindThemeConfigPath)
createCSSTokensFile(tailwindCSSPath, themes)
createCSSTokensFile(tailwindCSSPath, themes, htmlFontSize)

const child = spawn(process.execPath, [jsFilePath], {
stdio: 'inherit',
Expand Down
23 changes: 17 additions & 6 deletions packages/utils/cli/src/setup-themes/utils/createCSSTokenfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { join } from 'node:path'
import hexRgb from 'hex-rgb'

import { DEFAULT_KEY } from './constants.js'
import { doubleHyphensRegex, isHex, isObject, isStringOrNumber, toKebabCase } from './utils.js'
import {
doubleHyphensRegex,
getRemEquivalentValue,
isHex,
isObject,
isStringOrNumber,
toKebabCase,
} from './utils.js'

function toCSSVars(_theme, className) {
function toCSSVars(_theme, className, htmlFontSize) {
const flattenedTheme = {}

function flatten(theme, paths = []) {
Expand All @@ -23,6 +30,10 @@ function toCSSVars(_theme, className) {
return `${red} ${green} ${blue}`
}

if (/rem$/gi.test(value)) {
return getRemEquivalentValue(value, htmlFontSize)
}

return value
}

Expand All @@ -49,9 +60,9 @@ const toStringifiedTheme = theme =>
.map(([k, v]) => `${k}:${v}`)
.join(';')

const getStringifiedThemes = themeRecord =>
const getStringifiedThemes = (themeRecord, htmlFontSize) =>
Object.keys(themeRecord).map(key => {
const { className, ...rest } = toCSSVars(themeRecord[key], key)
const { className, ...rest } = toCSSVars(themeRecord[key], key, htmlFontSize)

return key === 'default'
? `:root{${toStringifiedTheme(rest)}}`
Expand All @@ -78,15 +89,15 @@ const getStringifiedThemes = themeRecord =>
*
* createCSSTokensFile('somePath.css', themes) // will generate a "somePath.css" file in the relative location from which it was called
*/
export function createCSSTokensFile(path, themeRecord) {
export function createCSSTokensFile(path, themeRecord, htmlFontSize) {
try {
appendFileSync(
join(process.cwd(), path),
`
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {${getStringifiedThemes(themeRecord).join('')}}
@layer base {${getStringifiedThemes(themeRecord, htmlFontSize).join('')}}
`,
{
flag: 'w',
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/cli/src/setup-themes/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function hasNumber(value) {
return /\d/.test(value)
}

function getRemEquivalentValue(remValue, htmlFontSize) {
const defaultBrowserBase = 16
const pxValue = parseFloat(remValue) * defaultBrowserBase

return `${pxValue / htmlFontSize}rem`
}

const doubleHyphensRegex = /(?<!var\()--+/g

export {
Expand All @@ -37,5 +44,6 @@ export {
isObject,
isCamelCase,
hasNumber,
getRemEquivalentValue,
doubleHyphensRegex,
}
1 change: 1 addition & 0 deletions packages/utils/theme/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export interface Theme {

export interface ThemeConfig {
tailwindThemeConfigPath: string
htmlFontSize?: number
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a JSDoc comment for each keys in that interface :D

That way it will shows in IDE's for developers.

The base value for `1rem` is `16px`.
Use this option if your codebase uses a different value (ex: `htmlFontSize: 10`)

tailwindCSSPath: string
themes: RequireAtLeastOne<Record<string, Theme>, 'default'>
}