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/style: Update document for sx-prop. (#2118) #2122

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/docs/src/pages/sx-prop.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,45 @@ import { Box } from 'theme-ui'
</Box>
```

## Work with `className`

`theme-ui` has its own function to build JSX with sx prop. How does it works?
`theme-ui` will use `praseProps` to deal with all props. it will firstly deal
with `sx` and/or `css` props, to get the **new** `css` prop with original props
except the **old** `sx` and/or **old** `css` props. Then it will let
`emtion`'s `jsx` function to finish the other works.

You can click [emotion css-prop][] to get more infomation.

[emotion]: https://emotion.sh/docs/css-prop#style-precedence

So, emtion will deal with the **new** `css` prop and `className`(for example,
`'bar'`) prop. Then make **new** style class with new name(for example,
`'foo'`), and replace old `className` with new `className`.

``` jsx
const Comp = (props) => {
// - theme-ui will turn `sx` prop into `css` prop.
// - emtion will mixin `css` prop and `className` prop to get new `className`
// prop
<div className={props.className} sx={{width: 1}}/>
}

<Comp />
// => <div class="bar" />
// => .bar {
// width: 1px;
// }

// sx prop with turn into `className` prop.
<Comp sx={{height: 1}} />
// => <div class="foo" />
// => .foo {
// width: 1px;
// height: 1px;
// }
```

## Object Styles

If you're new to using JavaScript object notation for authoring styles, see the
Expand Down
2 changes: 2 additions & 0 deletions packages/parse-props/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const getCSS = (props: any) => (theme: any) => {

const parseProps = (props: any) => {
if (!props || (!props.sx && !props.css)) return props

// Now props should be a object with `sx` and/or with `css` prop.
const next: typeof props & { css: Interpolation<any> } = {}
for (let key in props) {
if (key === 'sx') continue
Expand Down