Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve camelCased Variable names #835

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/nextjs-example/app/CardTokens.stylex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
import * as stylex from '@stylexjs/stylex';

export const tokens = stylex.defineVars({
arrow_transform: 'translateX(0)',
arrowTransform: 'translateX(0)',
});
4 changes: 2 additions & 2 deletions apps/nextjs-example/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const styles = stylex.create({
transitionDuration: '400ms',
textAlign: 'center',
textDecoration: 'none',
[tokens.arrow_transform]: {
[tokens.arrowTransform]: {
default: 'translateX(0)',
':hover': 'translateX(4px)',
},
Expand All @@ -85,7 +85,7 @@ const styles = stylex.create({
span: {
display: 'inline-block',
transitionProperty: 'transform',
transform: tokens.arrow_transform,
transform: tokens.arrowTransform,
transitionDuration: {
default: '200ms',
[REDUCE_MOTION]: '0s',
Expand Down
47 changes: 47 additions & 0 deletions packages/babel-plugin/__tests__/stylex-transform-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1652,4 +1652,51 @@ describe('@stylexjs/babel-plugin', () => {
`);
});
});

describe('[transform] setting vars with stylex.create()', () => {
test('preserves kebab-case in CSS variable names', () => {
// NOTE: the generated variable name is a little weird, but valid.
expect(
transform(`
import stylex from 'stylex';
export const styles = stylex.create({
default: {
'--background-color': 'red',
},
});
`),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(".xgau0yw{--background-color:red}", 1);
export const styles = {
default: {
"--background-color": "xgau0yw",
$$css: true
}
};"
`);
});

test('preserves camelCase in CSS variable names', () => {
expect(
transform(`
import stylex from 'stylex';
const styles = stylex.create({
default: {
'--myCustomVar': 'red',
'--anotherCamelVar': '10px',
},
});
`),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(".x1ujxqga{--myCustomVar:red}", 1);
_inject2(".x1g24lt9{--anotherCamelVar:10px}", 1);"
`);
});
});
});
2 changes: 1 addition & 1 deletion packages/shared/src/convert-to-className.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function convertStyleToClassName(
): StyleRule {
const { classNamePrefix = 'x', debug = false } = options;
const [key, rawValue] = objEntry;
const dashedKey = dashify(key);
const dashedKey = key.startsWith('--') ? key : dashify(key);

let value: string | $ReadOnlyArray<string> = Array.isArray(rawValue)
? rawValue.map((eachValue) => transformValue(key, eachValue, options))
Expand Down
Loading