Skip to content

Commit a5b7b6a

Browse files
committed
compile target
1 parent 6341427 commit a5b7b6a

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

text/0000-cssom.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ While there are many options for authoring CSS, it is super tiresome to convince
1414

1515
### Goals
1616

17+
- CSS scoping (unique selector)
1718
- High-performance rendering of static styles via CSSOM.
1819
- CSS Rules can be reused across components.
1920
- Components have no additional overhead for styling.
@@ -57,12 +58,71 @@ That being said using a new prop like `css` is also an option:
5758
render(<button css={buttonStyle}>x</button>);
5859
```
5960

60-
### Babel plugin
61+
### A new primitive
6162

62-
Babel plugin can be optional and can enable advanced features like:
63+
Once style or css prop accepts an object that contains CSS, we are essentially creating a new primitive. This primitive can be passed arround, reused, overriden and React will know what to do with it. This opens a number of new doors in the future.
64+
65+
### Compile target
66+
67+
This new primitive can be targeted by compilers (babel plugins and co.). In the simplest scenario it will work without any compilation steps. With compilation it would be possible to compile for example CSS modules into something that React can take and render.
68+
69+
Example of CSS modules being rendered by the new primitive:
70+
71+
```js
72+
import styles from "./button.css";
73+
render(<button className={styles.button}>Hi</button>);
74+
```
75+
76+
What happens right now with CSS loader:
77+
78+
```js
79+
// button.js
80+
import styles from "./button.css";
81+
render(<button className={styles.button}>Hi</button>);
82+
```
83+
84+
Compiles `button.css` to:
85+
86+
```js
87+
// button.css
88+
const createStyleNode = (id, content) => {
89+
const style = document.createElement("style");
90+
style.id = id;
91+
style.textContent = content;
92+
document.head.appendChild(style);
93+
};
94+
95+
createStyleNode(
96+
"/src/button.css:-css",
97+
".button-some-suffix {\n color: red;\n}"
98+
);
99+
```
100+
101+
Instead, it could compile to something like this:
102+
103+
```js
104+
// button.css
105+
export default {
106+
button: {
107+
css: ".button-some-suffix {\n color: red;\n}",
108+
id: "/src/button.css:-css",
109+
},
110+
};
111+
```
112+
113+
and
114+
115+
```js
116+
// button.js
117+
import styles from "./button.css";
118+
render(<button style={styles.button}>Hi</button>);
119+
```
120+
121+
Optionally many other compiler-based things can be enabled:
63122

64123
- vendor prefixing
65-
- optimizations
124+
- optimizations for compression
125+
- linting
66126
- other preprocessing options e.g. using PostCSS
67127

68128
After babel plugin the call into `css` tag function can be removed and the result of the above example can be compiled to:
@@ -72,6 +132,25 @@ const buttonStyle = { css: ".css-0 { color: red; }" };
72132
render(<button style={buttonStyle}>x</button>);
73133
```
74134

135+
### Object based styles syntax
136+
137+
It is still possible to use object based syntax like in [JSS](https://github.com/cssinjs/jss/blob/master/docs/jss-syntax.md) or React Native. It is possible because in the end it's still going to be a CSS string which can be passed to React:
138+
139+
```js
140+
const styles = StyleSheet.create({
141+
container: {
142+
flex: 1,
143+
justifyContent: "center",
144+
alignItems: "center",
145+
},
146+
}); // {container: {css: ".r-d23pfw {flex: 1; justify-content: center; align-items: center;"}}
147+
render(
148+
<View style={styles.container}>
149+
<Text>Hi</Text>
150+
</View>
151+
);
152+
```
153+
75154
# Drawbacks
76155

77156
- we are adding a new area of responsibility to React, which will require more work to maintain it, but hopefully can be parallelized
@@ -96,7 +175,7 @@ It is a primitive interface for passing CSS to React to integrate with component
96175

97176
# Unresolved questions
98177

99-
- Unique class names generation algorithm
178+
- Specific algorithm for unique class names generation
100179
- Composition
101180
- Overrides
102181
- Dynamic or state-based styling

0 commit comments

Comments
 (0)