Skip to content

Commit

Permalink
fix: handle color function in replacement for theme(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Feb 12, 2022
1 parent 7cb135c commit 9fc5bae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-scissors-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'twind': patch
---

fix: handle color function in replacement for `theme(...)`
22 changes: 20 additions & 2 deletions packages/twind/src/internal/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { CSSObject, Falsey, Context, TwindRule, BaseTheme, MaybeArray } from '../types'
import type {
CSSObject,
Falsey,
Context,
TwindRule,
BaseTheme,
MaybeArray,
ColorValue,
} from '../types'
import type { ParsedRule } from './parse'
import type { ConvertedRule } from './precedence'
import { Layer, moveToLayer } from './precedence'
Expand All @@ -9,6 +17,7 @@ import { stringify } from './stringify'
import { translateWith } from './translate'
import { parse } from './parse'
import { compareTwindRules } from './sorted-insertion-index'
import { toColorValue } from '../colors'

export function serialize<Theme extends BaseTheme = BaseTheme>(
style: CSSObject | Falsey,
Expand Down Expand Up @@ -226,7 +235,16 @@ export function resolveThemeFunction<Theme extends BaseTheme = BaseTheme>(
// if (value.includes('theme')) {
return value.replace(
/theme\((["'`])?(.+?)\1(?:\s*,\s*(["'`])?(.+?)\3)?\)/g,
(_, __, key, ___, value) => context.theme(key, value) as string,
(_, __, key, ___, defaultValue) => {
const value = context.theme(key, defaultValue)

if (typeof value == 'function' && /color|fill|stroke/i.test(key)) {
return toColorValue(value as ColorValue)
}

// TODO: warn if not a string
return value as string
},
)
// }

Expand Down

0 comments on commit 9fc5bae

Please sign in to comment.