Skip to content

Commit

Permalink
add check for css vars in hyphenate properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Zn4rK committed Nov 26, 2023
1 parent 5661932 commit 02d1384
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-vans-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@navita/engine': minor
---

css vars will not be automatically hyphenated
2 changes: 1 addition & 1 deletion packages/engine/src/helpers/declarationsToBlock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hyphenateProperty } from "./hypenateProperty";
import { hyphenateProperty } from "./hyphenateProperty";

// https://github.com/styletron/styletron/blob/b552ddc5050a8cc5eec84a46a299d937d3bb0112/packages/styletron-engine-atomic/src/css.ts#L36
export function declarationsToBlock(style: Record<string, string | number>): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Modified from
// https://github.com/styletron/styletron/blob/b552ddc5050a8cc5eec84a46a299d937d3bb0112/packages/styletron-engine-atomic/src/hyphenate-style-name.ts
const uppercasePattern = /[A-Z]/g;
const msPattern = /^ms-/;
const cssVarPattern = /^--/;
const cache = {};

export function hyphenateProperty(property: string): string {
if (cssVarPattern.test(property)) {
return property;
}

return property in cache
? cache[property]
: (cache[property] = property
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/processStyles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StyleRule } from "@navita/types";
import type { Cache } from "./cache";
import { generateCombinedAtRules } from "./helpers/generateCombinedAtRules";
import { hyphenateProperty } from "./helpers/hypenateProperty";
import { hyphenateProperty } from "./helpers/hyphenateProperty";
import { isMediaQuery } from "./helpers/isMediaQuery";
import { isNestedSelector } from "./helpers/isNestedSelector";
import { isObject } from "./helpers/isObject";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hyphenateProperty } from "../../../src/helpers/hypenateProperty";
import { hyphenateProperty } from "../../../src/helpers/hyphenateProperty";

describe("hypenateProperty", () => {
it("should return the same value if it is already hyphenated", () => {
Expand All @@ -12,4 +12,8 @@ describe("hypenateProperty", () => {
it('ms- prefix should be lowercase', () => {
expect(hyphenateProperty('msBackground')).toBe('-ms-background');
});

it(`it doesn't hyphenate css vars`, () => {
expect(hyphenateProperty('--myVar')).toBe('--myVar');
});
});

0 comments on commit 02d1384

Please sign in to comment.