Skip to content

Commit c6dca96

Browse files
committed
Improvements and documentation.
1 parent 5cfb776 commit c6dca96

File tree

11 files changed

+309
-18
lines changed

11 files changed

+309
-18
lines changed

README.md

Lines changed: 292 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,307 @@
11
# RunCSS
22

3-
RunCSS is a runtime version of TailwindCSS. It has no build. RunCSS provides all the same CSS utility class names that we know and love from TailwindCSS.
3+
[RunCSS, A Runtime Version of TailwindCSS and Beyond](https://dev.to/mudgen/runcss-a-runtime-version-of-tailwindcss-5dic)
44

5-
RunCSS is batteries included. It has feature parity with TailwindCSS and beyond. RunCSS defaults are the same as TailwindCSS defaults plus TailwindCSS's [additional variants](https://tailwindcss.com/docs/configuring-variants). By default all variants such as hover, active, visited, group-hover etc. and responsive variants such as sm, lg etc work with all class names.
5+
RunCSS is a runtime version of TailwindCSS. It has no build. RunCSS provides all the same CSS utility class names that we know and love from TailwindCSS.
6+
7+
RunCSS is batteries included. It has feature parity with TailwindCSS and beyond. RunCSS defaults are the same as TailwindCSS defaults plus TailwindCSS's [additional variants](https://tailwindcss.com/docs/configuring-variants), plus more. By default all variants such as `hover`, `active`, `visited`, `group-hover` etc. and responsive variants such as `sm`, `lg` etc work with all class names.
68

79
RunCSS is possible because it is a Javascript file that generates CSS at runtime.
810

9-
The tradeoff to using RunCSS is a small amount of Javascript execution to generate CSS at runtime. The necessary CSS is generated once for each class name when it is first encountered. CSS is only generated for class names that are actually used.
11+
> The primary difference between TailwindCSS and RunCSS is that TailwindCSS generates CSS at build time and RunCSS generates CSS at runtime.
1012
11-
# Installation
13+
RunCSS has no build. Just use it. Off to the races!
1214

13-
Add a CSS reset or base CSS file, such as TailwindCSS's preflight, to your web application:
15+
The tradeoff to using RunCSS is a small amount of Javascript execution to generate CSS at runtime. The necessary CSS for each class name is generated one time as it is encountered. CSS is only generated for class names that are actually used.
1416

15-
```
17+
### How to use RunCSS
18+
19+
__Step 1.__ Add a CSS reset or base CSS file, such as TailwindCSS's [preflight](https://unpkg.com/tailwindcss@%5E1/dist/base.css), to your web application:
20+
```html
1621
<link href="https://unpkg.com/runcss@^0/dist/preflight.css"
1722
rel="stylesheet">
1823
```
24+
__Step 2.__ Import the RunCSS Javascript file into your application:
25+
```javascript
26+
import processClasses from 'https://unpkg.com/runcss@^0/dist/runcss.modern.js'
27+
```
28+
__Step 3.__ Call the `processClasses` function on CSS class names. It is possible to integrate RunCSS into existing Javascript libraries so that `processClass` is called automatically when CSS class names are used. RunCSS ignores class names it has already generated CSS for so `processClasses` can be called repeatedly on the same class names.
29+
### Example
30+
Here is an example that integrates RunCSS with [Webscript](https://mudgen.github.io/webscript/docs/) and creates the same card example given on TailwindCSS's homepage:
31+
32+
```javascript
33+
// Importing Webscript
34+
import builders from 'https://unpkg.com/webscript@^0/dist/webscript.modern.js'
35+
import createDOMElement from 'https://unpkg.com/webscript@^0/dist/createDOMElement.modern.js'
36+
// Importing RunCSS
37+
import processClasses from 'https://unpkg.com/runcss@^0/dist/runcss.modern.js'
38+
39+
// Integrating RunCSS with Webscript
40+
function createElement (type, props, ...children) {
41+
if (props.class) {
42+
processClasses(props.class)
43+
}
44+
return createDOMElement(type, props, ...children)
45+
}
46+
47+
// Create the builders used to build DOM elements
48+
const { div, img, h2 } = builders(createElement)
49+
50+
// Card display
51+
const card =
52+
div.class`md:flex bg-white rounded-lg p-6`(
53+
img.class`h-16 w-16 md:h-24 md:w-24 rounded-full mx-auto md:mx-0 md:mr-6`.src`./avatar.jpg`,
54+
div.class`text-center md:text-left`(
55+
h2.class`text-lg``Erin Lindford`,
56+
div.class`text-purple-500``Customer Support`,
57+
div.class`text-gray-600``[email protected]`,
58+
div.class`text-gray-600``(555) 765-4321`))
59+
```
60+
61+
Here is the result of the above code:
62+
![Result of above code](https://dev-to-uploads.s3.amazonaws.com/i/zfjfxvjwg96y8njyo5wl.png)
63+
64+
## RunCSS File Size
65+
66+
[runcss.modern.js](https://github.com/mudgen/runcss/blob/master/dist/runcss.modern.js) is 8kb compressed and 20kb raw. It has no dependencies.
67+
68+
## Optional Node.js Package
69+
70+
RunCSS can optionally be installed like this:
71+
```shell
72+
npm install runcss
73+
```
74+
75+
## Going Beyond TailwindCSS
76+
77+
Because RunCSS doesn't have build-time constraints it can easily go beyond TailwindCSS and it does. RunCSS provides all the same utility CSS class names that TailwindCSS does plus many more.
78+
79+
For example, by default, TailwindCSS's margin classes have holes in them. There is `m-6`, but no `m-7`. There are no margin classes between `m-24` and `m-32`. The margin classes stop at `m-64`. With TailwindCSS it is possible to plug these holes by manually adding configuration to the TailwindCSS build configuration file. RunCSS doesn't require configuration and has no such holes and the class names don't end. RunCSS includes `m-65` and `m-66` and so on forever or until the browser can't take it anymore.
80+
81+
But there is more. RunCSS accepts any valid CSS length unit in many class names. For example, you could use `m-5%` or `m-1.25rem` or `m-25px` or whatever valid CSS length unit you want to use.
82+
83+
One of the benefits of using utility classes is "designing with constraints". It is easier to build consistent visual designs if you pick your styles from a limited set. With RunCSS this can be done by convention and enforced, if desired, by a linter. In addition with RunCSS you can go outside your design system in special cases where you need maximum control.
84+
85+
Many of the following sections show RunCSS's extended capabilities.
86+
87+
## Configuration
88+
RunCSS provides the `configure` function that can be used to configure parts of RunCSS. The following sections in this article that can use `configure` show how to use it.
89+
90+
## Colors
91+
RunCSS provides the same [default color palette](https://tailwindcss.com/docs/customizing-colors#default-color-palette) as TailwindCSS.
92+
93+
These colors can be used in all the same class names as can be used in TailwindCSS. They can be used in text, borders, placeholders, divides, and backgrounds.
94+
95+
### Color Example:
96+
```javascript
97+
// Using Webscript with RunCSS
98+
div.class`bg-blue-500 border-3 border-yellow-700`(
99+
p.class`text-white``Example Colors`
100+
)
101+
```
102+
Did you know that CSS specifications and browsers support [150 color keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value)? RunCSS supports them all too. From `black` to [rebeccapurple](https://codepen.io/trezy/post/honoring-a-great-man).
103+
104+
RunCSS supports all valid CSS color formats. For example hex, rgb/rgba and hsl/hsla formats
105+
106+
Here is an example that uses various color formats:
107+
```javascript
108+
div.class`bg-rebeccapurple border-10 border-rgba(200,10,10,0.1)`(
109+
p.class`text-hsl(120,100%,60%) xl:text-#ecc94b``Example Colors`
110+
)
111+
```
112+
> *Note: Make sure there are no spaces in your class names because class names are separated by spaces.*
113+
114+
It is possible to make your own color palette by configuring colors with the `configure` function. You can create your own color keywords.
115+
116+
Here is an example that sets the 'blue' keyword to the color red and sets some banana colors:
117+
```javascript
118+
// import the configure function
119+
import processClasses, { configure } from 'https://unpkg.com/runcss@^0/dist/runcss.modern.js'
120+
121+
// create our own color palette
122+
configure({
123+
colors: {
124+
blue: '#ff0000',
125+
banana: {
126+
100: '#FFFFF0',
127+
200: '#FEFCBF',
128+
300: '#FAF089'
129+
}
130+
}
131+
})
132+
```
133+
> Note that the CSS for these color classes is only generated if they are used.
134+
135+
> Note that only hex values can be used in the `configure` function for colors.
136+
137+
## Responsive Design
138+
139+
Responsive design with RunCSS works the same way as it does with TailwindCSS. [Checkout Tailwind's documentation about it.](https://tailwindcss.com/docs/responsive-design/)
140+
141+
By default RunCSS provides the same responsive breakpoints as TailwindCSS:
142+
```javascript
143+
{ sm: '640px', md: '768px', lg: '1024px', xl: '1280px' }
144+
```
145+
146+
Just like TailwindCSS all RunCSS classes can use the breakpoint prefixes without any configuration. __However, in addition, any CSS class not generated and coming from RunCSS can use them too!__
147+
148+
For example, if you create your own CSS file with some custom CSS you don't have to create media queries for different breakpoints. Just use the responsive prefixes from RunCSS.
149+
150+
### Example
151+
152+
Here is a custom CSS file. Notice there are no media queries for responsive versions of the class:
153+
```css
154+
.myclass {
155+
margin: 0 10px;
156+
background-color: red;
157+
border-radius: 0.5rem;
158+
}
159+
```
160+
Go ahead and make it responsive by using RunCSS's responsive prefixes in your DOM building code:
161+
```javascript
162+
div.class`lg:myclass`(
163+
p`Example text`
164+
)
165+
```
166+
RunCSS only generates CSS for responsive breakpoint classes that are used.
167+
168+
### Configure Your Own Responsive Breakpoints
169+
170+
You can set your own responsive breakpoints and prefixes by calling RunCSS's `configure` function. Here is an example:
171+
172+
```javascript
173+
configure({
174+
screens: {
175+
watch: '300px',
176+
phone: '340px',
177+
tablet: '640px'
178+
}
179+
})
180+
```
181+
> Note: Make sure you configure screens before you start processing CSS class names with `processClasses`.
182+
183+
## Pseudo-Class Variants
184+
185+
Pseudo-class variants like `hover`, `focus` etc. work with RunCSS class names the same way they do with TailwindCSS class names.
186+
187+
TailwindCSS [provides a number of pseduo-class variants](https://tailwindcss.com/docs/pseudo-class-variants) that are not enabled by default due to file-size constraints.
188+
189+
RunCSS, not having build file-size constraints, has enabled, by default, all of TailwindCSS's pseudo-class variants.
190+
191+
RunCSS only generates the needed CSS for the class names and variants that are actually used.
192+
193+
By default, RunCSS also provides and has enabled all [psuedo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes#Index_of_standard_pseudo-classes) and [psuedo-element](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements#Index_of_standard_pseudo-elements) variants that are supported by web browsers.
194+
195+
Just like RunCSS responsive prefixes can be used by CSS class names from third-party CSS style sheets, RunCSS's psuedo-class and psuedo-element prefixes can be used by CSS class names from third-party CSS style sheets.
196+
197+
### Example
198+
199+
Here is a custom CSS file. Notice there are no psuedo-class versions of the class name:
200+
```css
201+
.myclass {
202+
margin: 0 10px;
203+
background-color: red;
204+
border-radius: 0.5rem;
205+
}
206+
```
207+
Go ahead and apply a RunCSS pseudo-class prefix to it:
208+
```javascript
209+
div.class`hover:myclass`(
210+
p`Example text`
211+
)
212+
```
213+
No configuration for pseudo-classes and pseudo-elements is needed because they are all available.
214+
215+
## Extracting Components
216+
217+
RunCSS provides the `component` function to create CSS components. This is a way to create your own CSS utilities or components using RunCSS class names and/or CSS properties.
218+
219+
The `component(name, classNames, properties)` function takes three strings as arguments. The third argument is optional.
220+
221+
CSS will be generated using the last two arguments.
222+
223+
### Component Example
224+
```javascript
225+
import processClasses, { component } from 'https://unpkg.com/runcss@^0/dist/runcss.modern.js'
226+
227+
component(
228+
'btn', // new class name
229+
'p-2 bg-blue text-white hover:text-green-500 text-base lg:text-lg', // extracting CSS from class names
230+
'box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.5); outline: none;' // using CSS properties
231+
)
232+
233+
// Use the CSS component
234+
const button = button.class`btn text-yellow``Click Me`
235+
```
236+
RunCSS utility class names will override CSS components. This enables you to customize or specialize CSS components when they are used.
237+
238+
You can think of CSS components as default styles that can be overridden with utility classes.
239+
240+
In the example above the `text-yellow` class overrides the `text-white` class that is defined in the CSS component.
241+
242+
## Increasing Specificity with Important
243+
244+
You can increase specificity of your RunCSS utilities by calling `configure` with `{important: true}`. That will add `!important` to RunCSS styles.
245+
246+
If you want more specificity but less than `!important` then give the important option a selector. Like this: `{important: '#app'}`. And make sure that your RunCSS classes are added under an element with the 'app' id or whatever you specified.
247+
248+
## Prefix
249+
250+
It is possible to add a prefix to all RunCSS utilities by calling `configure` with a prefix option.
251+
252+
Here is an example:
253+
```Javascript
254+
configure({ prefix: 'run-' })
255+
256+
div.class`run-text-blue hover:run-text-yellow`(
257+
p`My test`
258+
)
259+
```
260+
261+
## Separator
262+
263+
Instead of using `:` to separate variants such as `hover`, `sm`, `focus` and the rest you can use a different separator. Call `configure` with the `separator` option. Here is an example:
264+
265+
```Javascript
266+
configure({separator: '$'})
267+
268+
div.class`run-text-blue hover$run-text-yellow`(
269+
p`My test`
270+
)
271+
```
272+
273+
## No Build Movement
274+
275+
RunCSS is another tool that's part of the No Build Movement.
276+
277+
The No Build Movement is a change in web development that favors building web applications without build tools except for minification of resources.
278+
279+
## Project Home
280+
281+
The RunCSS project can be found here:
282+
{% github mudgen/runcss %}
283+
284+
[Follow me on twitter.](https://twitter.com/mudgen)
285+
286+
287+
288+
289+
290+
291+
292+
293+
294+
295+
296+
297+
298+
299+
300+
301+
302+
303+
304+
19305

20-
Import the RunCSS Javascript file into your application:
21306

22307

dist/runcss.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.modern.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.modern.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runcss.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ configure({
2323
tablet: '640px'
2424
}
2525
})
26+
2627
*/
28+
configure({ prefix: 'run-', separator: '&' })
29+
2730
// Second Part
2831
function createElement (type, props, ...children) {
2932
if (props.class) {
@@ -59,7 +62,7 @@ const { body, div, p, ul, li, input, span, button } = builders((type, props, ...
5962

6063
component(
6164
'btn',
62-
'p-2 rounded bg-blue hover:text-green-500 text-base lg:text-lg',
65+
'r-p-2 r-rounded r-bg-blue hover:r-text-green-500 text-base lg:text-lg',
6366
'box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.5);outline: none;'
6467
)
6568

@@ -71,6 +74,9 @@ component(
7174

7275
const app =
7376
div.id`app`.class`mt-5 ml-1.7rem`(
77+
div.class`run-text-blue hover&run-text-yellow`(
78+
p`My test`
79+
),
7480
div.class`lg:myclass`(
7581
button.class`xl:btn text-green``Click Me`,
7682
button.class`btn text-yellow``Click Me`
@@ -80,7 +86,7 @@ const app =
8086
card
8187
),
8288
ul(
83-
li.class`pl-1.9em lg:pl-100 tracking-widest``hello`,
89+
li.class`r-pl-1.9em lg:r-pl-100 r-tracking-widest``hello`,
8490
li.class`mt-30 text-pink-700 text-opacity-25``Great`,
8591
li.class`px-50 lg:hidden``fine`,
8692
li.class`px-5 text-pink-700``Another little test`,

0 commit comments

Comments
 (0)