diff --git a/packages/@stylexjs/babel-plugin/__tests__/transform-stylex-props-test.js b/packages/@stylexjs/babel-plugin/__tests__/transform-stylex-props-test.js index 5d6012368..bc090cfb5 100644 --- a/packages/@stylexjs/babel-plugin/__tests__/transform-stylex-props-test.js +++ b/packages/@stylexjs/babel-plugin/__tests__/transform-stylex-props-test.js @@ -103,6 +103,43 @@ describe('@stylexjs/babel-plugin', () => { `); }); + test('Test that sx attribute can be used instead of ...stylex.props', () => { + expect( + transform( + ` + import stylex from 'stylex'; + const styles = stylex.create({ + red: { + color: 'red', + } + }); + function Foo() { + return ( + <> +
Hello World
+
Hello World
+
Hello World
+ + ); + } + `, + options, + ), + ).toMatchInlineSnapshot(` + "import _inject from "@stylexjs/stylex/lib/stylex-inject"; + var _inject2 = _inject; + import stylex from 'stylex'; + _inject2(".color-x1e2nbdu{color:red}", 3000); + function Foo() { + return <> +
Hello World
+
Hello World
+
Hello World
+ ; + }" + `); + }); + test('local dynamic styles', () => { expect( transform( diff --git a/packages/@stylexjs/babel-plugin/src/index.js b/packages/@stylexjs/babel-plugin/src/index.js index fe80ddace..d67e460de 100644 --- a/packages/@stylexjs/babel-plugin/src/index.js +++ b/packages/@stylexjs/babel-plugin/src/index.js @@ -278,6 +278,40 @@ function styleXTransform(): PluginObj<> { }, }, + JSXOpeningElement(path: NodePath) { + const node = path.node; + if ( + node.name.type !== 'JSXIdentifier' || + typeof node.name.name !== 'string' || + node.name.name[0] !== node.name.name[0].toLowerCase() + ) { + return; + } + // console.log(path.node.attributes); + const relevantAttribute = path + .get('attributes') + .find( + (attr: NodePath) => + attr.isJSXAttribute() && + attr.get('name').isJSXIdentifier() && + attr.get('name').node.name === 'sx' && + attr.get('value').isJSXExpressionContainer(), + ); + if (relevantAttribute == null) { + console.log('no relevant attribute'); + return; + } + const value = relevantAttribute.get('value').get('expression').node; + relevantAttribute.replaceWith( + t.jsxSpreadAttribute( + t.callExpression( + t.memberExpression(t.identifier('stylex'), t.identifier('props')), + [value], + ), + ), + ); + }, + CallExpression(path: NodePath) { if (path.parentPath.isVariableDeclarator()) { const parentPath = path.parentPath;