Skip to content

Commit 433c1d7

Browse files
authored
Merge pull request #7 from syfxlin/docs/license
docs(README.md): update README.md
2 parents 9e21afc + e513944 commit 433c1d7

File tree

1 file changed

+130
-15
lines changed

1 file changed

+130
-15
lines changed

README.md

+130-15
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,164 @@
11
# Reve
22

3-
## Introduction
3+
[vanilla-extract](https://vanilla-extract.style) is a CSS-in-JS compiler that writes styles in TypeScript (or JavaScript) using locale-scoped class names and CSS variables, then generates static CSS files at build time.
44

5-
Provides css-prop-like API and flexible theme system based on vanilla-extract and stylis.
5+
@syfxlin/reve is a library of enhancements to vanilla-extract to improve the experience of using vanilla-extract and reduce unnecessary time consumption due to specific programming paradigms.
6+
7+
## Motivation
8+
9+
vanilla-extract use object style to define styles, which makes good use of the type mechanism for auto-completion and error checking. But this style is not convenient compared to vanilla style. For example, string values need more quotes, CSS functions can't be auto-complete and error-checked, [Emmet](https://docs.emmet.io/css-abbreviations) abbreviation syntax is not supported, and so on.
10+
11+
vanilla-extract each style block can only target a single element, which makes it a pain when we need to define complex nested styles, wasting a lot of time writing global styles or adding classNames to child elements, most typically adding typography styles. The most typical scenario is to add typography styles. For example, here's a comparison of two snippets:
12+
13+
```typescript
14+
// Bad
15+
export const typography = style({
16+
// ...
17+
});
18+
19+
globalStyle(`${typography} h1, ${typography} h2, ${typography} h3`, {
20+
// ...
21+
});
22+
23+
globalStyle(`${typography} h1::before, ${typography} h2::before, ${typography} h3::before`, {
24+
// ...
25+
});
26+
27+
// Good
28+
export const typography = styled.css`
29+
// ... typography style
30+
31+
h1, h2, h3 {
32+
// ... heading style
33+
34+
&::after {
35+
// ... heading ::after pseudo style
36+
}
37+
}
38+
`
39+
```
40+
41+
vanilla-extract developers added this limitation for maintainability, so I don't think they'll remove it, so I use the [stylis](https://github.com/thysultan/stylis) library to parse the vanilla style css into object style css and chunking the styles to ensure that a style chunk targets only and chunking the styles to make sure that a style block is only for one element, styles is finally exported via vanilla-extract globalStyle.
42+
43+
vanilla-extract writes styles based on TypeScript/JavaScript, so it has the ability to execute code, and we can take advantage of this by creating a theme system that calculates values at build time and hardcodes them directly into the CSS, defining only the values that need to be able to change dynamically in theme variables. This way we don't need to define a very large number of theme variables, resulting in a very large CSS file (vanilla-extract generates all defined theme variables into the final CSS file, even if it doesn't use them). For example:
44+
45+
```typescript
46+
// Bad
47+
export const theme = createThemeContract({
48+
color: {
49+
background: null
50+
},
51+
size: {
52+
// ... x1 - x31
53+
x32: null
54+
}
55+
});
56+
57+
createGlobalTheme(".light-theme", theme, {
58+
color: {
59+
background: "lightblue"
60+
},
61+
size: {
62+
// ... x1 - x31
63+
x32: "32px"
64+
}
65+
});
66+
67+
createGlobalTheme(".dark-theme", theme, {
68+
color: {
69+
background: "lightpink"
70+
},
71+
size: {
72+
// ... x1 - x31
73+
x32: "32px"
74+
}
75+
});
76+
77+
export const container = styled.css`
78+
font-size: ${theme.size.x32};
79+
background-color: ${theme.color.background};
80+
`;
81+
// Output:
82+
// .light-theme { --def456: lightblue; /* x1-x31 */; --x32: 32px; }
83+
// .dark-theme { --def456: lightpink; /* x1-x31 */; --x32: 32px; }
84+
// .abc123 { font-size: var(--x32); background-color: var(--def456); }
85+
86+
// Good
87+
export const theme = createReveTheme({
88+
static: {
89+
size: (value: number) => `${value}px`,
90+
},
91+
dynamic: {
92+
".light-theme": {
93+
color: { background: "lightblue" },
94+
},
95+
".dark-theme": {
96+
color: { background: "lightpink" },
97+
},
98+
},
99+
});
100+
101+
export const container = styled.css`
102+
font-size: ${theme.size(32)};
103+
background-color: ${theme.color.background};
104+
`;
105+
// Output:
106+
// .light-theme { --def456: lightblue; }
107+
// .dark-theme { --def456: lightpink; }
108+
// .abc123 { font-size: 32px; background-color: var(--def456); }
109+
```
110+
111+
I started this project as a proof-of-concept to see if I could fix a few of the bad implementations mentioned above, improve my coding efficiency, and save time for other things I want to do.
6112

7113
## Installation
8114

9115
```shell
116+
# NPM
10117
npm i @syfxlin/reve @vanilla-extract/css
118+
# Yarn
119+
yarn add @syfxlin/reve @vanilla-extract/css
120+
# PNPM
121+
pnpm add @syfxlin/reve @vanilla-extract/css
11122
```
12123

13124
## Usage
14125

15126
```typescript
16-
// css-prop-like API
127+
import { styled, createReveTheme } from "@syfxlin/reve";
128+
129+
// vanilla css api
17130
styled.global`
18131
box-sizing: border-box;
19132
`
20133

21134
export const container = styled.css`
22135
font-size: ${theme.size(32)};
23136
background-color: ${theme.color.background};
24-
137+
138+
/* Supports nested syntax for styled-componets styles */
25139
div {
26140
display: flex;
27-
141+
28142
&::before {
29143
}
30144
}
31-
145+
32146
div & {
33147
}
34-
148+
35149
& div {
36150
}
37-
151+
38152
& > div {
39153
}
154+
155+
/* Support media query syntax */
156+
@media (max-width: 1250px) {
157+
/* ... */
158+
}
40159
`;
41160

42-
// theme API
161+
// theme system
43162
export const theme = createReveTheme({
44163
static: {
45164
size: (value: number) => `${value}px`,
@@ -57,12 +176,8 @@ export const theme = createReveTheme({
57176

58177
## Maintainer
59178

60-
reve is written and maintained with the help of [Otstar Lin](https://ixk.me) and the following [contributors](https://github.com/syfxlin/reve/graphs/contributors).
61-
62-
> Otstar Lin - [Personal Website](https://ixk.me/) · [Blog](https://blog.ixk.me/) · [GitHub](https://github.com/syfxlin)
179+
**@syfxlin/reve** is written and maintained with the help of [Otstar Lin](https://github.com/syfxlin) and the following [contributors](https://github.com/syfxlin/reve/graphs/contributors).
63180

64181
## License
65182

66-
![License](https://img.shields.io/github/license/syfxlin/reve.svg?style=flat-square)
67-
68-
Released under the MIT License.
183+
Released under the [MIT](https://opensource.org/licenses/MIT) License.

0 commit comments

Comments
 (0)