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

[docs] Add sx prop to readme (part 1) #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented May 8, 2024

NOT included in this PR: For part 2

  • The shorthand properties (currently, the shorthand works but it's tied to MUI System styleFunctionSx). There is a plan to move to theme.unstable_sx so that developer can configure their own which will be a different PR.
  • SxProps types for augmenting TypeScript HTMLAttribute interface to be able to use sx prop on an HTML element. I think this can be a separate PR. For now, we document the value as any.

@siriwatknp siriwatknp added the docs Improvements or additions to the documentation label May 8, 2024
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

The shorthand properties (currently, the shorthand works but it's tied to MUI System styleFunctionSx). There is a plan to move to theme.unstable_sx so that developer can configure their own which will be a different PR.

This is very important, I would even wait with adding documentation until we have this. The sx prop currently is tight to a theme structure, that people may not be aware of. If we don't have a documentation about this, it looks like magic.


The value provided to `sx` prop can be one of the following:

- a plain style object (recommended)
Copy link
Member

Choose a reason for hiding this comment

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

A why would be nice.

- a callback function that receives the [theme object](#theming) then return a plain style object:

```js
<div sx={(theme) => ({ color: theme.colors.primary })} />
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can add example that is not possible with an object, so that people are aware of what's the different use-case.

- an array of plain style objects or callback functions. This is useful for applying conditional styles based on other variables:

```js
<div
Copy link
Member

Choose a reason for hiding this comment

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

Great example!


The value provided to `sx` prop can be one of the following:

- a plain style object (recommended)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also mention static values.

<div sx={{ display: 'flex', flexDirection: 'column' }}>
```

For a React component, it must spread the `className` and `style` to the underlying DOM element, otherwise the styles won't be applied.
Copy link
Member

Choose a reason for hiding this comment

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

nit: "spread" is just a way of passing several props at once. I think "pass" (or another word?) would work best here since some devs might think they're forced to spread props.

Suggested change
For a React component, it must spread the `className` and `style` to the underlying DOM element, otherwise the styles won't be applied.
For a React component, it must pass the `className` and `style` props to the underlying DOM element, otherwise the styles won't be applied.


### Shorthand properties

> ⏳ **Coming soon**: We are working on the shorthand properties for `sx` prop.
Copy link
Member

Choose a reason for hiding this comment

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

This part seems to take for granted that users know about what shorthand properties are. Maybe a longer explanation, an example, or a link to an existing resource of why shorthand properties are useful is helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can link to docs on mui.com

<div sx={{ display: 'flex', flexDirection: 'column' }}>
```

For a React component, it must spread the `className` and `style` to the underlying DOM element, otherwise the styles won't be applied.
Copy link
Contributor

Choose a reason for hiding this comment

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

This line doesn't seem applicable. We can do without it because users don't have to care about the spread. Also, if they have their own className being passed to the element/component, we are the ones transforming that to take care of passing both the existing class and the generated class.
Also, we are typechecking it (when using typescript). So sx prop willl only be passed on components that actually accept it. This means, that component is aware of what to do with it.

<AnyComponent sx={{ fontSize: 12, color: 'red' }} />;
```

> 💡 **Gotcha**: You don't need to pass `sx` prop to underlying component or DOM because it will be replaced with `className` and `style` props.
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this mean ?

@NeilTheFisher NeilTheFisher mentioned this pull request May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants