Skip to content

Commit 38e057b

Browse files
committed
remove theme config from classname
1 parent a7afc2a commit 38e057b

File tree

5 files changed

+16
-30
lines changed

5 files changed

+16
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "inan-tailwind-parser",
2+
"name": "tailwind-parser",
33
"private": false,
44
"version": "1.1.20",
55
"type": "module",

src/classname.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import get from 'lodash/get'
2-
31
import { PluginNotFoundException } from './exceptions/plugin-not-found-exception'
42
import { functionalPlugins, namedPlugins } from './plugins'
5-
import { getTailwindTheme } from './theme'
3+
64
import { buildModifier } from './utils/build-modifier'
75
import { calculateHexFromString } from './utils/calculate-hex-from-string'
86
import { findTailwindColorFromHex } from './utils/find-tailwind-color-from-hex'
@@ -11,8 +9,11 @@ import { StringBuilder } from './utils/string-builder'
119

1210
export const EMPTY_CLASSNAME = ''
1311

14-
export const classname = (ast, config) => {
15-
const theme = getTailwindTheme(config)
12+
export const classname = (ast) => {
13+
if ([null, undefined, ''].includes(ast.value)) {
14+
return EMPTY_CLASSNAME
15+
}
16+
1617
const stringBuilder = new StringBuilder()
1718
let negative = ast.negative || false
1819
stringBuilder.appendVariants(...ast.variants || [])
@@ -40,20 +41,12 @@ export const classname = (ast, config) => {
4041
}
4142

4243
stringBuilder.addRoot(root)
43-
if (isColor(ast.value, theme)) {
44+
if (isColor(ast.value)) {
4445
const matchedPlugin = possiblePlugins.find(plugin => plugin.type === 'color')
4546
if (!matchedPlugin) {
4647
throw new PluginNotFoundException(ast.property)
4748
}
4849

49-
const tailwindThemeColor = get(theme[matchedPlugin.scaleKey || 'colors'], ast.value.split('-').join('.'))
50-
if (tailwindThemeColor && typeof tailwindThemeColor !== 'object') {
51-
return stringBuilder
52-
.appendModifier(buildModifier(ast.modifier, theme.opacity))
53-
.addValue(ast.value)
54-
.toString()
55-
}
56-
5750
const isRgba = ast.value.includes('rgba')
5851
if (isRgba) {
5952
return stringBuilder
@@ -67,9 +60,9 @@ export const classname = (ast, config) => {
6760
}
6861

6962
return stringBuilder
70-
.appendModifier(buildModifier(color.alpha || ast.modifier, theme.opacity))
63+
.appendModifier(buildModifier(color.alpha || ast.modifier))
7164
.addValue(
72-
findTailwindColorFromHex(color.hex, theme[matchedPlugin.scaleKey || 'colors']) || StringBuilder.makeArbitrary(color.hex),
65+
findTailwindColorFromHex(color.hex) || StringBuilder.makeArbitrary(color.hex),
7366
)
7467
.toString()
7568
}
@@ -96,7 +89,7 @@ const findTailwindValueByUnit = (unit, matchedPlugin = {}) => {
9689
return undefined
9790
}
9891

99-
if (matchedPlugin.type === 'image') {
92+
if (matchedPlugin.type === 'image' && !unit.includes('url')) {
10093
unit = `url(http://github.com/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E$%7B%3C/span%3E%3Cspan%20class=pl-s1%3Eunit%3C/span%3E%3Cspan%20class=pl-kos%3E%7D%3C/span%3E%3C/span%3E)`
10194
}
10295

src/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { parseVariant } from "./parse-variant";
55
import { inferDataType } from "./utils/infer-data-type";
66
import { getValue, type Value } from "./utils/value";
77
import { getTailwindTheme } from "./theme";
8-
import { CalculateHexFromString } from "./utils/calculate-hex-from-string";
8+
import { calculateHexFromString } from "./utils/calculate-hex-from-string";
99
import { findTailwindColorFromHex } from "./utils/find-tailwind-color-from-hex";
1010
import { buildModifier } from "./utils/build-modifier";
1111
import { isColor } from "./utils/is-color";
@@ -115,7 +115,7 @@ export const parse = (input: string, config?: any): AST | Error => {
115115
let associatedPluginByType = availablePlugins!.find(plugin => plugin.type === unitType)
116116

117117
if (unitType === "color") {
118-
const color = CalculateHexFromString(arbitraryValue)
118+
const color = calculateHexFromString(arbitraryValue)
119119
if (!color) {
120120
return {
121121
root: base,

src/utils/is-color.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,10 @@ const NAMED_COLORS = new Set([
200200

201201
const IS_COLOR_FN = /^(rgba?|hsla?|hwb|color|(ok)?(lab|lch)|light-dark|color-mix)\(/i
202202

203-
export function isColor(value: string, theme?: any): boolean {
203+
export function isColor(value: string): boolean {
204204
if (!value) return false
205205

206-
let isThemeColor = false
207-
208-
if (theme) {
209-
const [trueValue,] = segment(value, '/')
210-
isThemeColor = !!get(theme.colors as any, trueValue.split('-').join('.'))
211-
}
212-
213206
return (
214-
value.charCodeAt(0) === HASH || IS_COLOR_FN.test(value) || NAMED_COLORS.has(value.toLowerCase()) || isThemeColor
207+
value.charCodeAt(0) === HASH || IS_COLOR_FN.test(value) || NAMED_COLORS.has(value.toLowerCase())
215208
)
216209
}

src/utils/string-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class StringBuilder {
5252
}
5353

5454
static makeArbitrary(input) {
55-
if (input.includes('url') || /[0-9]/.test(input) && (!input.includes('-') || !input.indexOf('-'))) {
55+
if (input.includes('(') || /[0-9]/.test(input) && (!input.includes('-') || !input.indexOf('-'))) {
5656
return `[${StringBuilder.makeInputArbitraryFormat(input)}]`
5757
}
5858
return input

0 commit comments

Comments
 (0)