Skip to content

Enable sx prop on any components#37

Merged
siriwatknp merged 22 commits into
masterfrom
poc/sx-runtime
May 2, 2024
Merged

Enable sx prop on any components#37
siriwatknp merged 22 commits into
masterfrom
poc/sx-runtime

Conversation

@siriwatknp
Copy link
Copy Markdown
Member

@siriwatknp siriwatknp commented Apr 26, 2024

closes #35

  • transform sx prop using runtime {…sx()} spread (see the test cases)
  • Remove ForwardSx
  • Remove allowTransformSx check (to transform any component)
  • Remove sx related logic from runtime styled (I like this so much because sx is not independent of styled)
  • Remove double class CSS specificity

Tested in next-app, it works with HTML tags and Material UI components.


@siriwatknp siriwatknp added the type: new feature Expand the scope of the product to solve a new problem. label Apr 26, 2024
@siriwatknp siriwatknp marked this pull request as draft April 26, 2024 04:32
@siriwatknp siriwatknp marked this pull request as ready for review April 26, 2024 05:59
"import/no-unresolved": "off"
}
"import/no-unresolved": "off",
"react/no-unknown-property": ["error", { "ignore": ["sx"] }],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore sx prop on JSX html tag.

Comment on lines +9 to +11
interface HTMLAttributes<T> {
sx?: any;
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to test that we can modify the types of JSX elements.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, we can use the SxProps tough, no? :) I would recommend adding a documentation about this in the README.md too, so we can test everything, including typings and make sure we haven't forgot something.

Copy link
Copy Markdown
Member Author

@siriwatknp siriwatknp Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for POC at this point. There is one issue to fix from Pigment side which is delegating sx logic to the theme. Currently, Pigment is using styleFunctionSx from MUI System which is impossible to customize.

Once that's done, I will add docs for it.

/**
* For JSX calls, replace the sx prop with runtime sx
*/
this.replacer((_tagPath) => {
Copy link
Copy Markdown
Member Author

@siriwatknp siriwatknp Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting type like this will fail this.replacer((_tagPath: NodePath<CallExpression>) => { because Type 'Node' is not assignable to type 'CallExpression'.

So I have to create a new variable instead.

Comment thread packages/pigment-css-react/src/processors/sx.ts
Comment thread packages/pigment-css-react/src/processors/sx.ts Outdated
},
}}
>
<p sx={{ boxShadow: '0 0 4px 0 rgba(0 0 0 / 0.12)' }}>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ❤️ this!

@brijeshb42
Copy link
Copy Markdown
Contributor

I haven't tested this but how would conditional sx work here ->

<div sx={props.isPrimary ? {color: 'red', background: 'blue'} : {fontWeight: '400'}} />

@siriwatknp
Copy link
Copy Markdown
Member Author

siriwatknp commented Apr 26, 2024

I haven't tested this but how would conditional sx work here ->

<div sx={props.isPrimary ? {color: 'red', background: 'blue'} : {fontWeight: '400'}} />

It will be:

<div {…props.isPrimary ? sx() : sx() } />

The logic that you had implemented remains the same. What I added is the conversion of sx prop to spread {…sx()} call.

The runtime sx() will always return { className: '...', style: { … } }.

@siriwatknp
Copy link
Copy Markdown
Member Author

image

I have to add @pigment-css/react to the root package.json

Comment thread packages/pigment-css-react/tests/sx/fixtures/sx-jsx.output.js Outdated
Comment thread packages/pigment-css-react/tests/sx/fixtures/sx-jsx.output.js Outdated
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Apr 30, 2024
@siriwatknp siriwatknp requested a review from brijeshb42 May 1, 2024 02:26
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label May 1, 2024
@siriwatknp siriwatknp merged commit df751ab into master May 2, 2024
@siriwatknp siriwatknp deleted the poc/sx-runtime branch May 2, 2024 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[react] Support the sx prop on all components

3 participants