Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 148fe2e

Browse files
authored
feat: introduce universal system (#117)
1 parent e4f4adc commit 148fe2e

File tree

14 files changed

+449
-49
lines changed

14 files changed

+449
-49
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
'no-plusplus': 'off',
2222
'no-return-assign': 'off',
2323
'no-param-reassign': 'off',
24+
'no-nested-ternary': 'off',
2425
'no-shadow': 'off',
2526
'default-case': 'off',
2627
'no-underscore-dangle': ['off', { allow: ['__smoothUI', '__scTheme'] }],

docs/system/GettingStarted.mdx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
name: Getting Started
3+
menu: System
4+
order: 1
5+
---
6+
7+
import { Playground } from 'docz'
8+
import { Box, system, styled } from '@smooth-ui/core-sc'
9+
10+
# System
11+
12+
Smooth UI has a powerful style system that gives you a real styling power, it ensure a consistency in your design and reduce the amount of code to produce things.
13+
14+
## System Props
15+
16+
All Smooth UI components support system props, styling a component can be done by setting props:
17+
18+
<Playground>
19+
<Box
20+
backgroundColor="primary"
21+
width={{ sm: 1, md: 0.5 }}
22+
height={100}
23+
mx="auto"
24+
p={2}
25+
/>
26+
</Playground>
27+
28+
The above example create a red `div` with:
29+
30+
- A background with color "primary"
31+
- A width of `100%` on mobile, `50%` on desktop
32+
- A height of `100px`
33+
- A padding of `16px`
34+
- A `marginLeft` and a `marginRight` of `50%`
35+
36+
[Read more about creating components with system props](/docs-system-props)
37+
38+
## Styled System
39+
40+
The system can also be used to create styled components, it is like an alternative to the traditional CSS. It gives you more power and more consistency by automatically reading values from theme.
41+
42+
<Playground>
43+
{() => {
44+
const MyBox = styled.div(
45+
system({
46+
backgroundColor: 'primary',
47+
width: { sm: 1, md: 0.5 },
48+
height: 100,
49+
mx: 'auto',
50+
p: 2,
51+
}),
52+
)
53+
return <MyBox />
54+
}}
55+
</Playground>
56+
57+
The above example create a `div` with:
58+
59+
- A background with color "primary"
60+
- A width of `100%` on mobile, `50%` on desktop
61+
- A height of `100px`
62+
- A padding of `16px`
63+
- A `marginLeft` and a `marginRight` of `50%`
64+
65+
[Read more about using system to create components](/docs-styled-system)

docs/system/Props.mdx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
name: System props
3+
menu: System
4+
order: 2
5+
---
6+
7+
import { Box } from '@smooth-ui/core-sc'
8+
9+
# System props
10+
11+
System props is an efficient way to style a component without writing any line of CSS. System props components can be created in several ways.
12+
13+
## Use `Box` component (or any Smooth UI component)
14+
15+
The easiest solution is to import the `Box` component from Smooth UI. It is just a simple `div` with system props.
16+
17+
```js
18+
import { Box } from '@smooth-ui/core-sc'
19+
20+
;<Box
21+
backgroundColor="primary"
22+
width={{ sm: 1, md: 0.5 }}
23+
height={100}
24+
mx="auto"
25+
p={2}
26+
/>
27+
```
28+
29+
The above example create a red `div` with:
30+
31+
- A background with color "primary"
32+
- A width of `100%` on mobile, `50%` on desktop
33+
- A height of `100px`
34+
- A padding of `16px`
35+
- A `marginLeft` and a `marginRight` of `50%`
36+
37+
> All Smooth UI components implement the system.
38+
39+
## Create a custom component
40+
41+
To create a custom component that implements system props, style a `Box` or use system utilities.
42+
43+
### Style a Box
44+
45+
A `Box` can be styled like any other component, a styled `Box` inherits the system.
46+
47+
```js
48+
import { Box } from '@smooth-ui/core-sc'
49+
import styled from 'styled-component'
50+
51+
const RedBox = styled(Box)`
52+
background-color: red;
53+
`
54+
55+
<RedBox border="1px solid blue" />
56+
// Will display a red box with a blue border
57+
```
58+
59+
### Use system utilities
60+
61+
All system utilities are accessible in `@smooth-ui/system`. They can be applied on a styled components and give them system props power!
62+
63+
```js
64+
import system, { borders } from '@smooth-ui/system'
65+
66+
// This component supports all system utilities
67+
const Box = styled.div`
68+
${system.props}
69+
`
70+
71+
// This component supports only border utilities
72+
const BorderBox = styled.div`
73+
${borders.props}
74+
`
75+
76+
<BorderBox border="1px solid blue" />
77+
<Box backgroundColor="red" />
78+
```
79+
80+
[All system utilities](/docs-system-utilities) are documented.
81+
82+
### System on complex components
83+
84+
Even complex components can benefit from the system, it can be applied on a specific part of the component.
85+
86+
```js
87+
import { spaces, typography } from '@smooth-ui/system'
88+
89+
const Container = styled.div`
90+
${spaces.props}
91+
92+
input {
93+
${typography.props}
94+
}
95+
`
96+
97+
function RichInput() {
98+
return (
99+
<Container>
100+
<input />
101+
</Container>
102+
)
103+
}
104+
105+
;<RichInput
106+
fontSize={10} // Applied on `input`
107+
px={1} // Applied on `div`
108+
/>
109+
```

docs/system/Styled.mdx

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
name: System components
3+
menu: System
4+
order: 3
5+
---
6+
7+
import { Playground } from 'docz'
8+
import { Box, system, styled } from '@smooth-ui/core-sc'
9+
10+
# System components
11+
12+
System can be use to create styled components, it is like an alternative to the traditional CSS. It gives you more power and more consistency by automatically reading values from theme.
13+
14+
## Use system to create a styled component
15+
16+
```js
17+
import styled from 'styled-components'
18+
import system from '@smooth-ui/system'
19+
20+
const Box = styled.div(
21+
system({
22+
backgroundColor: 'primary',
23+
width: { sm: 1, md: 0.5 },
24+
height: 100,
25+
mx: 'auto',
26+
p: 2,
27+
}),
28+
)
29+
```
30+
31+
The above example create a red `div` with:
32+
33+
- A background with color "primary"
34+
- A width of `100%` on mobile, `50%` on desktop
35+
- A height of `100px`
36+
- A padding of `16px`
37+
- A `marginLeft` and a `marginRight` of `50%`
38+
39+
> All CSS properties are supported.
40+
41+
## Use props in system
42+
43+
The `system()` returns a function that accepts props. To access props, wrap this function.
44+
45+
```js
46+
import styled from 'styled-components'
47+
import system from '@smooth-ui/system'
48+
49+
const Box = styled.div(props =>
50+
system({
51+
backgroundColor: props.backgroundColor,
52+
}),
53+
)
54+
```
55+
56+
## Use CSS and system together
57+
58+
Sometimes, using CSS can be useful (media queries, better syntax). System and CSS fits well together.
59+
60+
```js
61+
import styled from 'styled-components'
62+
import system from '@smooth-ui/system'
63+
64+
const Box = styled.div`
65+
${system({ px: 1 })}
66+
67+
@media (min-width: 700px) {
68+
${system({ px: 2 })}
69+
}
70+
`
71+
```
72+
73+
## Use in CSS prop
74+
75+
System can be used with [emotion css prop](https://emotion.sh/docs/css-prop).
76+
77+
```js
78+
import system, { cx } from '@smooth-ui/system'
79+
import { css } from '@emotion/core'
80+
81+
const buttonStyle = css`
82+
background-color: hotpink;
83+
`
84+
85+
function Button(props) {
86+
return (
87+
<button css={cx([system({ color: 'primary' }), buttonStyle])} {...props} />
88+
)
89+
}
90+
```
91+
92+
> `cx` is required because emotion does not support function in style array.

0 commit comments

Comments
 (0)