Skip to content

Commit

Permalink
fix(stylex.create): preserve camelCased variable names when being ass…
Browse files Browse the repository at this point in the history
…igned a value
  • Loading branch information
Dwlad90 committed Jan 4, 2025
1 parent d9d580e commit f42f8f5
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs-example/app/CardTokens.stylex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,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 @@ -59,7 +59,7 @@ const styles = stylex.create({
transitionDuration: '400ms',
textAlign: 'center',
textDecoration: 'none',
[tokens.arrow_transform]: {
[tokens.arrowTransform]: {
default: 'translateX(0)',
':hover': 'translateX(4px)',
},
Expand All @@ -76,7 +76,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
2 changes: 1 addition & 1 deletion apps/nextjs-postcss-example/app/CardTokens.stylex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,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-postcss-example/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const styles = stylex.create({
transitionDuration: '400ms',
textAlign: 'center',
textDecoration: 'none',
[tokens.arrow_transform]: {
[tokens.arrowTransform]: {
default: 'translateX(0)',
':hover': 'translateX(4px)',
},
Expand All @@ -84,7 +84,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
2 changes: 1 addition & 1 deletion apps/nextjs-swc-example/app/CardTokens.stylex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,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-swc-example/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const styles = stylex.create({
transitionDuration: '400ms',
textAlign: 'center',
textDecoration: 'none',
[tokens.arrow_transform]: {
[tokens.arrowTransform]: {
default: 'translateX(0)',
':hover': 'translateX(4px)',
},
Expand All @@ -76,7 +76,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn convert_style_to_class_name(
let (key, raw_value) = obj_entry;

let dashed_key = if key.starts_with("--") {
key.to_lowercase()
key.to_string()
} else {
dashify(key)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//__stylex_metadata_start__[{"class_name":"x1ujxqga","style":{"rtl":null,"ltr":".x1ujxqga{--myCustomVar:red}"},"priority":1},{"class_name":"x1g24lt9","style":{"rtl":null,"ltr":".x1g24lt9{--anotherCamelVar:10px}"},"priority":1}]__stylex_metadata_end__
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//__stylex_metadata_start__[{"class_name":"xgau0yw","style":{"rtl":null,"ltr":".xgau0yw{--background-color:red}"},"priority":1}]__stylex_metadata_end__
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
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod stylex_create_call_pseudo_elements;
mod stylex_create_call_queries;
mod stylex_create_call_queries_with_functions;
mod stylex_create_call_queries_with_properties;
mod stylex_create_call_setting_vars;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use stylex_shared::{shared::structures::plugin_pass::PluginPass, StyleXTransform};
use swc_core::ecma::{
parser::{Syntax, TsSyntax},
transforms::testing::test,
};

test!(
Syntax::Typescript(TsSyntax {
tsx: true,
..Default::default()
}),
|tr| StyleXTransform::new_test_force_runtime_injection_with_pass(
tr.comments.clone(),
PluginPass::default(),
None
),
preserves_kebab_case_in_css_variable_names,
r#"
import stylex from 'stylex';
export const styles = stylex.create({
default: {
'--background-color': 'red',
},
});
"#
);


test!(
Syntax::Typescript(TsSyntax {
tsx: true,
..Default::default()
}),
|tr| StyleXTransform::new_test_force_runtime_injection_with_pass(
tr.comments.clone(),
PluginPass::default(),
None
),
preserves_camel_case_in_css_variable_names,
r#"
import stylex from 'stylex';
const styles = stylex.create({
default: {
'--myCustomVar': 'red',
'--anotherCamelVar': '10px',
},
});
"#
);
Original file line number Diff line number Diff line change
Expand Up @@ -1475,4 +1475,44 @@ describe('@stylexjs/babel-plugin', ()=>{
`);
});
});
describe('[transform] setting vars with stylex.create()', ()=>{
test('preserves kebab-case in CSS variable names', ()=>{
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/postcss-plugin/app/CardTokens.stylex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,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 packages/postcss-plugin/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const styles = stylex.create({
transitionDuration: '400ms',
textAlign: 'center',
textDecoration: 'none',
[tokens.arrow_transform]: {
[tokens.arrowTransform]: {
default: 'translateX(0)',
':hover': 'translateX(4px)',
},
Expand All @@ -84,7 +84,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

0 comments on commit f42f8f5

Please sign in to comment.