Skip to content

Commit

Permalink
refactor: move hashing of vars (--tw-<...>) from preset-tailwind in…
Browse files Browse the repository at this point in the history
…to core
  • Loading branch information
sastan committed Feb 12, 2022
1 parent 80ce410 commit ae979d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/nervous-clocks-drive.md
@@ -0,0 +1,6 @@
---
'@twind/preset-tailwind': patch
'twind': patch
---

refactor: move hashing of vars (`--tw-<...>`) from preset-tailwind into core — this allows var hashing without the tailwind preset
22 changes: 3 additions & 19 deletions packages/preset-tailwind/src/index.ts
Expand Up @@ -5,7 +5,7 @@
* @module
*/

import type { Context, Preset } from 'twind'
import type { Preset } from 'twind'
import type { TailwindTheme } from './types'

import theme from './defaultTheme'
Expand All @@ -23,27 +23,11 @@ export interface TailwindPresetOptions {
export default function presetTailwind({
disablePreflight,
}: TailwindPresetOptions = {}): Preset<TailwindTheme> {
return ({ stringify }) => ({
return {
// allow other preflight to run
preflight: disablePreflight ? undefined : preflight,
theme,
variants,
rules,
// Hash/Tag tailwind custom properties during serialization
stringify(property, value, context) {
return stringify(hashVars(property, context), hashVars(value, context), context)
},
})
}

function hashVars(value: string, { h }: Context<TailwindTheme>): string {
// PERF: check for --tw before running the regexp
// if (value.includes('--tw')) {
return value.replace(
/--(tw-[\w-]+)\b/g,
(_, property: string) => '--' + h(property).replace('#', ''),
)
// }

// return value
}
}
34 changes: 27 additions & 7 deletions packages/twind/src/internal/context.ts
Expand Up @@ -32,6 +32,7 @@ type VariantFunction<Theme extends BaseTheme = BaseTheme> = (
export function createContext<Theme extends BaseTheme = BaseTheme>({
theme,
darkMode,
darkColor,
variants,
rules,
hash,
Expand Down Expand Up @@ -63,20 +64,27 @@ export function createContext<Theme extends BaseTheme = BaseTheme>({
: '@media (prefers-color-scheme:dark)',
])

const h =
typeof hash == 'function'
? (value: string) => hash(value, defaultHash)
: hash
? defaultHash
: identity

return {
theme: makeThemeFunction(theme),

e: escape,

h:
typeof hash == 'function'
? (value) => hash(value, defaultHash)
: hash
? defaultHash
: identity,
h,

s(property, value) {
return stringify(property, value, this)
// Hash/Tag tailwind custom properties during serialization
return stringify(hashVars(property, h), hashVars(value, h), this)
},

d(section, key, color) {
return darkColor?.(section, key, this, color)
},

v(value) {
Expand Down Expand Up @@ -220,3 +228,15 @@ function toCondition(value: string | RegExp): RegExp {
? new RegExp('^' + value + (value.includes('$') || value.slice(-1) == '-' ? '' : '$'))
: value
}

function hashVars(value: string, h: Context['h']): string {
// PERF: check for --tw before running the regexp
// if (value.includes('--tw')) {
return value.replace(
/--(tw-[\w-]+)\b/g,
(_, property: string) => '--' + h(property).replace('#', ''),
)
// }

// return value
}

0 comments on commit ae979d1

Please sign in to comment.