Skip to content

Commit fdbddcd

Browse files
authored
Merge pull request #4 from dev-five-git/fix-var-name-issue
Fix var name issue
2 parents 1fcd313 + a9738a2 commit fdbddcd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/__tests__/Element.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ describe('Element', () => {
100100
expect(await element.render()).toEqual(
101101
'<Box w="60px">\n <Box w="100%" />\n</Box>',
102102
)
103+
{
104+
const element = createElement('RECTANGLE', {
105+
border: 'solid 1px var(--containerBackground, #FFF)',
106+
fills: [],
107+
})
108+
expect(await element.getComponentType()).toEqual('Box')
109+
expect(await element.render()).toEqual(
110+
'<Box border="solid 1px $containerBackground" />',
111+
)
112+
}
113+
{
114+
const element = createElement('RECTANGLE', {
115+
border: 'solid 1px var(--containerBackground)',
116+
fills: [],
117+
})
118+
expect(await element.getComponentType()).toEqual('Box')
119+
expect(await element.render()).toEqual(
120+
'<Box border="solid 1px $containerBackground" />',
121+
)
122+
}
103123
})
104124

105125
describe('Image', () => {

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function space(depth: number) {
116116
function extractVariableName(value: string) {
117117
if (!value.startsWith('var(--')) return value
118118
const match = value.match(/var\(--(\w+)/)
119-
return '$' + match?.[1]
119+
return '$' + match?.[1].split(',')[0].trim()
120120
}
121121

122122
export function propsToComponentProps(
@@ -431,6 +431,8 @@ export function organizeProps(props: Record<string, string>) {
431431
if (ret[key].startsWith('"') && ret[key].endsWith('"'))
432432
ret[key] = ret[key].slice(1, -1)
433433
if (ret[key].includes('/*')) ret[key] = ret[key].split('/*')[0].trim()
434+
if (ret[key].includes('var(--'))
435+
ret[key] = ret[key].replace(/var\(--.*\)/g, extractVariableName)
434436
}
435437
for (const key in CONVERT_PROPS_VALUE_MAP) {
436438
if (!ret[key]) continue

0 commit comments

Comments
 (0)