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

Enable sx prop on any components #37

Merged
merged 22 commits into from
May 2, 2024
Merged

Enable sx prop on any components #37

merged 22 commits into from
May 2, 2024

Conversation

siriwatknp
Copy link
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 new feature New feature or request 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
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
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
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
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
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.

},
}}
>
<p sx={{ boxShadow: '0 0 4px 0 rgba(0 0 0 / 0.12)' }}>
Copy link
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
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
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
Member Author

image

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

@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
17 checks passed
@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
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[react] Support the sx prop on all components
3 participants