Skip to content

Commit 6cdaaa3

Browse files
authored
Allow CSS variables in no-hardcoded-color rule (#278)
2 parents e74119e + 376fd83 commit 6cdaaa3

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

.changeset/allow-css-variables.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-panda": patch
3+
---
4+
5+
Allow CSS variables in no-hardcoded-color rule. CSS variables like `var(--something-here)` are now recognized as valid color values and will not trigger the no-hardcoded-color rule error.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
**/node_modules/**
22
node_modules
3+
package-lock.json
34

45
dist
56
styled-system

plugin/src/rules/no-hardcoded-color.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ const rule: Rule = createRule({
8282
return tokens.length > 0
8383
}
8484

85+
const isCssVariable = (value: string): boolean => {
86+
if (!value) return false
87+
return value.trim().startsWith('var(')
88+
}
89+
8590
const isValidColorToken = (value: string): boolean => {
8691
if (!value) return false
8792
const [colorToken, opacity] = value.split('/')
@@ -104,6 +109,7 @@ const rule: Rule = createRule({
104109
const checkColorValue = (node: TSESTree.Node, value: string, attributeName: string) => {
105110
if (!isColorAttribute(attributeName)) return
106111
if (isTokenFunctionUsed(value)) return
112+
if (isCssVariable(value)) return
107113
if (isValidColorToken(value)) return
108114

109115
reportInvalidColor(node, value)

plugin/tests/no-hardcoded-color.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ import { Circle } from './panda/jsx';
3333
3434
function App(){
3535
return <Circle _hover={{ borderColor: 'gray.100' }} />;
36+
}`,
37+
},
38+
39+
{
40+
code: javascript`
41+
import { css } from './panda/css';
42+
43+
const styles = css({ color: 'var(--my-color)' })`,
44+
},
45+
46+
{
47+
code: javascript`
48+
import { css } from './panda/css';
49+
50+
const styles = css({ backgroundColor: 'var(--bg-color)' })`,
51+
},
52+
53+
{
54+
code: javascript`
55+
import { Circle } from './panda/jsx';
56+
57+
function App(){
58+
return <Circle borderColor='var(--border-color)' />;
59+
}`,
60+
},
61+
62+
{
63+
code: javascript`
64+
import { css } from './panda/css';
65+
66+
function App(){
67+
return <div className={css({ color: 'var(--text-primary)' })} />;
3668
}`,
3769
},
3870
]

0 commit comments

Comments
 (0)