-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·455 lines (318 loc) · 12.4 KB
/
index.html
File metadata and controls
executable file
·455 lines (318 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE html>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>reproCSS</title>
<style>
* {
box-sizing: border-box;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-kerning: auto;
}
html {
font-size: 12pt;
line-height: 1.4;
font-weight: 400;
font-family: sans-serif;
}
body {
padding: 1em;
margin: 0 auto;
max-width: 800px;
}
img[src*=svg] {
display: block;
max-width: 300px;
margin: 4em auto 2em;
}
code,
pre,
blockquote {
padding: .1em .3em;
background: rgba(0,0,0,.1);
}
code,
pre {
font-family: monospace;
}
pre {
padding: 1em;
white-space: pre-wrap;
}
h1, h2, h3, h4, h5, h6 {
margin: 2em 0 .5em 0;
line-height: 1.2;
letter-spacing: -.02em;
}
h1 {
margin-top: 0;
}
footer {
opacity: .4;
padding: 2em 0;
text-align: center;
transition: opacity .2s ease-in-out;
}
footer:hover {
opacity: 1;
}
p,
li {
font-size: 14pt;
}
@media (min-width: 600px) {
h1 { font-size: 300%; }
h2 { font-size: 200%; }
h3 { font-size: 160%; }
h4 { font-size: 140%; }
h5 { font-size: 120%; }
h6 { font-size: 110%; }
}
</style>
<img src=reprocss-logo.svg>
<h1>reproCSS</h1>
<p><strong>A CSS reprocessor using <code><style></code> tags</strong></p>
<p>Can you imagine if you could interpolate JS inside CSS with the <code>${}</code> syntax, and also control when and how frequently that CSS reprocessed with a <code>process=""</code> attribute on the <code><style></code> tag:</p>
<pre><style process="none"></style>
<style process="once"></style>
<style process="auto"></style>
<style process="touchstart mousedown"></style></pre>
<p>If you are using reproCSS with custom events, you may also optionally use a <code>selector</code> attribute specify a list of one or more CSS selectors you would like to add event listeners for. If no <code>selector</code> attribute is found all custom events will be applied to window.</p>
<pre><style process="click" selector="#any, .css, [selector]"></style></pre>
<p>You can add the CSS you want <code>reprocss.js</code> to apply to your HTML in <code><style></code> tags with the following values on the <code>process</code> attribute:
<ul>
<li><code>none</code> means no reprocessing
<li><code>once</code> means process immediately and never again
<li><code>auto</code> runs every <code>resize</code>, <code>input</code>, and <code>click</code> event on window
<li>any space-separated list of JS events you wish to listen for
</ul>
<h2 id=howto>How to use reproCSS</h2>
<h3 id=github>Github</h3>
<p>To view reproCSS on Github visit: <a href=https://github.com/tomhodgins/reprocss>github.com/tomhodgins/reprocss</a></p>
<p>You can include the <code>reprocss.js</code> JavaScript plugin in your HTML:</p>
<pre><script src="reprocss.js"></script></pre>
<h3 id=npm>npm</h3>
<p>If you are using reproCSS on NPM you can include it in your JS modules with a line like this:</p>
<pre>const reprocss = require('reprocss')</pre>
<h2 id=writing>How to write reproCSSed CSS</h2>
<p>To evaluate JavaScript inside the CSS as it's being reprocessed by <code>reprocss.js</code> you can use the <code>${}</code> interpolation syntax. The following <code><style></code> tag would always ensure the <code><div></code> in this example was half of the window's height:</p>
<pre><style process="auto">
div {
height: calc(${innerHeight}px / 2);
}
</style></pre>
<p>When the browser is 1000px tall the <code>${innerHeight}</code> in our CSS will be output as <code>500</code>, leading to the following output:</p>
<pre><style process="auto">
div {
height: calc(500px / 2);
}
</style></pre>
<p>Currently this plugin only supports <code><style></code> tags, but it may be possible to support CSS loaded via <code><link></code> with a similar technique.</p>
<h2 id=examples>Examples</h2>
<h3 id=pseudo>Interpolating JS-supplied values in CSS <code>content:;</code></h3>
<pre><div>Hello</div>
<script>
var user = 'Username'
</script>
<style process="once">
div:after {
content: ' ${user}';
}
</style></pre>
<h3 id=element-queries>Element Queries via a JS Selector Resolver</h3>
<pre><div id="demo">
<p>Hello</p>
</div>
<style process="resize">
${demo.offsetWidth > 400 && "#demo"} {
background: lime;
}
${demo.offsetWidth > 400 && "#demo"} p {
color: red;
}
</style></pre>
<h3 id=interpolation>JS interpolation in CSS</h3>
<pre><textarea id="demo"></textarea>
<style process="input">
#demo {
background: hsl(${demo.value.length}, 50%, 50%)
}
</style></pre>
<h2 id=demos>Demos</h2>
<ul>
<li><a href=test/element-queries.html>Element Queries with reproCSS</a>
<li><a href=https://codepen.io/tomhodgins/pen/ZyraEQ>Min/Max Font Size</a>
<li><a href=https://codepen.io/tomhodgins/pen/QgQqwx>Attribute Values as Numbers</a>
<li><a href=https://codepen.io/tomhodgins/pen/MoQmdY>Regex Search on Attribute Value</a>
<li><a href=https://codepen.io/tomhodgins/pen/MoQmLY>Cursor Tracking</a>
<li><a href=https://codepen.io/tomhodgins/pen/awqWNz>Scalable Iframe</a>
<li><strong><a href=https://codepen.io/search/pens/?q=reprocss>View reproCSS demos on CodePen</a></strong>
</ul>
<h2 id=mixins>Mixins</h2>
<p>Writing mixins for reproCSS is easy, any JavaScript function that outputs code that can be used in CSS can be called from anywhere in the stylesheet reproCSS is processing using JS interpolation with <code>${}</code>.</p>
<p>An example of a common mixin template might look like this:</p>
<pre>function mixin(selector, rule) {
// Find tags in document matching selector
var tag = document.querySelectorAll(selector)
// Begin with an empty style
var style = ''
// Begin counting matching tags at 0
var count = 0
// For each tag matching our selector
for (var i=0; i<tag.length; i++) {
// Create an identifier based on the selector used
var attr = selector.replace(/\W+/g, '')
// Mark tag with a custom attribute containing identifier and count
tag[i].setAttribute('data-mixin-' + attr, count)
// Add a copy of the CSS rule to the style using a selector for this tag's unique attribute
style += '\n[data-mixin-' + attr + '="' + count + '"] {\n'
+ ' ' + rule + '\n'
+ '}\n'
// Increment the tag counter by +1
count++
}
// Return all generated styles as CSS text
return style
}</pre>
<p>If you were going to create a mixin starting from the template above the first thing you'd want to do is change the function name (currently <code>mixin()</code>) to something unique, as well as update the mentions of <code>mixin</code> inside the mixin logic where it's used to name the elements the mixin is styling, <code>data-mixin-</code>.</p>
<p>Once you have changed the name of the function, you can pass a CSS selector or a list of CSS selectors into to the plugin, along with CSS properties and values as a string to be processed and added to new rules. This basic template can be extended in many ways to support different things. Here are some examples of reproCSS mixins and helper functions:</p>
<h3 id=aspect-ration-mixin>Aspect Ratio Mixin</h3>
<p>This mixin lets you to define an aspect ratio for elements.</p>
<h4>syntax</h4>
<pre>aspectRatio('iframe', 16/9)</pre>
<h4>output</h4>
<pre>/* iframe { aspect-ratio: 1.77; } */
[data-aspect-ratio-unique=0] {
height: 503px;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=test/aspect-ratio-mixin.html>Aspect Ratio Mixin Demo</a>
</ul>
<h3 id=xpath-selector-mixin>XPath Select Mixin</h3>
<p>This mixin lets you use XPath as a selector for CSS rules.</p>
<h4>syntax</h4>
<pre>xpath('//*', `
border: 1px solid red;
`)</pre>
<h4>outpue</h4>
<pre>/*
//* {
border: 1px solid red;
}
*/
[data-xpath-unique=0] {
border: 1px solid red;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=test/xpath-selector-mixin.html>XPath Selector Mixin Demo</a>
</ul>
<h3 id=auto-expand-mixin>Auto Expand Mixin</h3>
<p>This mixin lets you choose between auto-expanding an element's width and height to match its <code>scrollWidth</code> or <code>scrollHeight</code>. Available keywords are <code>width,</code>, <code>height</code>, and <code>both</code>.</p>
<h4>syntax</h4>
<pre>autoExpand('textarea', 'height')</pre>
<h4>output</h4>
<pre>/* textarea { height: auto-expand; } */</pre>
<h4>demo</h4>
<ul>
<li><a href=test/auto-expand-mixin.html>Auto Expand Mixin Demo</a>
</ul>
<h3 id=container-queries-mixin>Container Queries Mixin</h3>
<p>This mixin lets you define a 'container' using a CSS selector, run a JavaScript test against matching tags that match the container's selector, and to apply a CSS rule to that container or its children.</p>
<h4>syntax</h4>
<pre>container('div', 'this.offsetWidth > 500', 'span', 'background: lime;')</pre>
<h4>output</h4>
<pre>/* div(this.offsetWidth > 500) span */
[data-container-unique="0"] {
background: lime;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/container-queries-mixin.html>Container Queries Mixin Demo</a>
</ul>
<h3 id=scoped-eval-mixin>Scoped Eval() Mixin</h3>
<p>This mixin lets you define a CSS selector list, and to output CSS rules with JS interpolation from the context of each element in the document matching the selector.</p>
<h4>syntax</h4>
<pre>scoped('div', `
margin: 1em;
background: lime;
height: eval(this.offsetWidth / (16/9))px;
`)</pre>
<h4>output</h4>
<pre>/* Scope: div */
[data-scoped-unique="0"] {
margin: 1em;
background: lime;
height: 144.5625px;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/scoped-eval-mixin.html>Scoped Eval() Mixin Demo</a>
</ul>
<h3 id=parent-selector-mixin>Parent Selector Mixin</h3>
<p>This mixin lets you define a CSS selector list and apply a CSS rule to the parent node of any matching tags in your document.</p>
<h4>syntax</h4>
<pre>parent('li', 'border: 1px solid red;')</pre>
<h4>output</h4>
<pre>/* li:parent */
[data-parent-unique="0"] {
border: 1px solid red;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/parent-selector-mixin.html>Parent Selector Mixin Demo</a>
</ul>
<h3 id=prev-selector-mixin>Prev Selector Mixin</h3>
<p>This mixin lets you define a CSS selector list and apply a CSS rule to the previous sibling node of any matching tags in your document.</p>
<h4>syntax</h4>
<pre>prev('li:nth-of-type(2)', 'background: lime;')</pre>
<h4>output</h4>
<pre>/* li:prev */
[data-prev-unique="0"] {
background: lime;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/parent-selector-mixin.html>Prev Selector Mixin Demo</a>
</ul>
<h3 id=closest-selector-mixin>Closest Selector Mixin</h3>
<p>This mixin lets CSS authors apply styles to the nearest element matching a CSS selector to another element matching a given CSS selector. You can use this to find the nearest matching ancestor.</p>
<h4>syntax</h4>
<pre>closest('#start', '.target', `border-color: lime`)</pre>
<h4>output</h4>
<pre>/* #start:closest(.target) */
[data-closest-unique="0"] {
border-color: lime
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/closest-selector-mixin.html>Closest Selector Mixin Demo</a>
</ul>
<h3 id=ancestor-selector-mixin>Ancestor Selector Mixin</h3>
<p>This mixin lets CSS authors apply styles to all ancestor elements matching a CSS selector to another element matching a given CSS selector. You can use this to style all matching ancestors.</p>
<h4>syntax</h4>
<pre>ancestor('#start', '.target', `border-color: lime`)</pre>
<h4>output</h4>
<pre>/* #start:ancestor(.target) */
[data-ancestor-unique="0"] {
border-color: lime;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/ancestor-selector-mixin.html>Ancestor Selector Mixin Demo</a>
</ul>
<h3 id=elder-selector-mixin>Elder Selector Mixin</h3>
<p>This mixin lets CSS authors apply styles to all elder siblings of elements matching a CSS selector.</p>
<h4>syntax</h4>
<pre>elder('.target', 'background: lime;')</pre>
<h4>output</h4>
<pre>[data-elder-unique="0"] {
background: lime;
}</pre>
<h4>demo</h4>
<ul>
<li><a href=https://tomhodgins.github.io/reprocss/test/elder-selector-mixin.html>Elder Selector Mixin Demo</a>
</ul>
<footer>Made with ♥ by <a href=http://twitter.com/innovati>@innovati</a></footer>