diff --git a/packages/hast-util-to-babel-ast/src/index.test.ts b/packages/hast-util-to-babel-ast/src/index.test.ts
index 5e72b10a..30e30547 100644
--- a/packages/hast-util-to-babel-ast/src/index.test.ts
+++ b/packages/hast-util-to-babel-ast/src/index.test.ts
@@ -83,4 +83,16 @@ describe('hast-util-to-babel-ast', () => {
`";"`,
)
})
+
+ it('properly converts style with variables', () => {
+ const code = ``
+ expect(transform(code)).toMatchInlineSnapshot(`
+ ";"
+ `)
+ })
})
diff --git a/packages/hast-util-to-babel-ast/src/stringToObjectStyle.ts b/packages/hast-util-to-babel-ast/src/stringToObjectStyle.ts
index 2d7f4a27..bf9b4965 100644
--- a/packages/hast-util-to-babel-ast/src/stringToObjectStyle.ts
+++ b/packages/hast-util-to-babel-ast/src/stringToObjectStyle.ts
@@ -2,21 +2,28 @@
import * as t from '@babel/types'
import { hyphenToCamelCase, isNumeric, trimEnd } from './util'
+const PX_REGEX = /^\d+px$/
+const MS_REGEX = /^-ms-/
+const VAR_REGEX = /^--/
+
/**
* Determines if the CSS value can be converted from a
* 'px' suffixed string to a numeric value.
*/
const isConvertiblePixelValue = (value: string) => {
- return /^\d+px$/.test(value)
+ return PX_REGEX.test(value)
}
/**
* Format style key into JSX style object key.
*/
const formatKey = (key: string) => {
+ if (VAR_REGEX.test(key)) {
+ return t.stringLiteral(key)
+ }
key = key.toLowerCase()
// Don't capitalize -ms- prefix
- if (/^-ms-/.test(key)) key = key.substr(1)
+ if (MS_REGEX.test(key)) key = key.substr(1)
return t.identifier(hyphenToCamelCase(key))
}