-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylus.html
306 lines (289 loc) · 9.68 KB
/
stylus.html
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
<!DOCTYPE html>
<html>
<head>
<title>🌘 CSS Scope Inline Test Page</title>
<script>
function stylToStyle(string){
let cursor=0
// Remove comments, extra lines.
string = string.replace(/(\/\*[\s\S]*?\*\/|\/\/[^\n]*)/g, '').replace(/^\s*$/gm, '').replace(/ +$/gm, '')
console.log(string)
// Remove excess indentation (dedent).
let indent=''
while (cursor < string.length) {
if (string[cursor] == "\n") { // Line.
cursor += 1 // Next.
continue
}
if (string[cursor] == "\t" || string[cursor] == " ") {
indent += string[cursor]
cursor += 1 // Next.
continue
}
string = string.replaceAll("\n"+indent, "\n") // Remove.
break
}
string = " "+string
// Add curly braces.
let level_from=0
let level_to=0
while (cursor < string.length) {
if (string[cursor] == "\n") { // Line.
level_to = 0
cursor += 1 // Next.
continue
}
if (string[cursor] == "\t") { // Indent.
level_to += 1
cursor += 1 // Next.
continue
}
while (level_to > level_from) { // Real start.
level_from += 1
// Cursor #2 finds last real character for the insert.
let cursor_2 = cursor-1
while (cursor_2 > 0 && ["\n", "\t", " "].includes(string[cursor_2])) {
cursor_2 -= 1
}
cursor_2 += 1
string = string.slice(0, cursor_2) +' {'+ string.slice(cursor_2)
cursor += 2 // Skip ' {'
//cursor += 1 // Next.
continue
}
while (level_from > level_to) { // Real start.
level_from -= 1
// Cursor #2 finds last real character for the insert.
let cursor_2 = cursor-1
while (cursor_2 > 0 && ["\n", "\t", " "].includes(string[cursor_2])) {
cursor_2 -= 1
}
cursor_2 += 1
string = string.slice(0, cursor_2) +'} '+ string.slice(cursor_2)
cursor += 2 // Skip '} '
//cursor += 0 // Next.
continue
}
cursor += 1 // Next.
}
// End of file. Close remaining levels.
while (level_from > 0) {
string += '}'
level_from -= 1
}
function alphanum(c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
}
// Add semicolons.
cursor = 0
while (cursor < string.length) {
if (string[cursor] === "\n") { // Line.
if (cursor > 2) { // Should we add a semicolon?
let c = string[cursor-1]
if (c !== '{' && c !== '}' &&
(c === "\'" || c === '"' || c === ')' || c === '%' || c === '-' || c === '_' || alphanum(c))) {
string = string.slice(0, cursor) +';'+ string.slice(cursor)
}
}
}
cursor += 1 // Next.
}
return string
}
</script>
<script>
// 🌘 CSS Scope Inline (https://github.com/gnat/css-scope-inline)
window.cssScopeCount ??= 1 // Let extra copies share the scope count.
window.cssScope ??= new MutationObserver(mutations => { // Allow 1 observer.
// Styl Support
document?.body?.querySelectorAll('styl').forEach(node => {
let e = document.createElement('style')
e.textContent = stylToStyle(node.textContent)
node.parentNode.replaceChild(e, node)
})
document?.body?.querySelectorAll('style:not([ready])').forEach(node => { // Faster than walking MutationObserver results when recieving subtree (DOM swap, htmx, ajax, jquery).
let scope = 'me__'+(window.cssScopeCount++) // Ready. Make unique scope, example: .me__1234
node.parentNode.classList.add(scope)
node.textContent = node.textContent
.replace(/(?:^|\.|(\s|[^a-zA-Z0-9\-\_]))(me|this|self)(?![a-zA-Z])/g, '$1.'+scope) // Can use: me this self
.replace(/((@keyframes|animation:|animation-name:)[^{};]*)\.me__/g, '$1me__') // Optional. Removes need to escape names, ex: "\.me"
.replace(/(?:@media)\s(xs-|sm-|md-|lg-|xl-|sm|md|lg|xl|xx)/g, // Optional. Responsive design. Mobile First (above breakpoint): 🟢 None sm md lg xl xx 🏁 Desktop First (below breakpoint): 🏁 xs- sm- md- lg- xl- None 🟢 *- matches must be first!
(match, part1) => { return '@media '+({'sm':'(min-width: 640px)','md':'(min-width: 768px)', 'lg':'(min-width: 1024px)', 'xl':'(min-width: 1280px)', 'xx':'(min-width: 1536px)', 'xs-':'(max-width: 639px)', 'sm-':'(max-width: 767px)', 'md-':'(max-width: 1023px)', 'lg-':'(max-width: 1279px)', 'xl-':'(max-width: 1535px)'}[part1]) }
)
node.setAttribute('ready', '')
})
}).observe(document.documentElement, {childList: true, subtree: true})
</script>
<styl>
*,*::before,*::after
margin: 0; box-sizing: border-box /* Reset */
html
font-size: 10px /* Root size. Usage: 1.8rem = 18px (1rem = 10px) */
text-size-adjust: none /* Reset auto size. */
-webkit-text-size-adjust: none /* Fix safari. */
background: #222
color: #ccc
body
font-size: 2rem /* 2rem = 20px in our setup. */
html button
font-size: 2rem
cursor: pointer
color: #fff
text-shadow: 0 2px #000000AA
padding: 2rem 2.4rem
margin: 2rem
background: hsl(345 100% 47%)
border: 0
border-bottom: 0.5rem solid hsl(345 100% 28%)
border-radius: 12px
transition: all 0.3s
html button:hover
background: hsl(345 100% 60%)
transform: translateY(-0.2rem)
html button:active
transform: translateY(0.2rem)
</styl>
</head>
<body>
<div>💩 No style.</div>
<div>
<styl>
me /* Simple example. */
margin: 20px
me div
font-size: 5rem
</styl>
<div>👻</div>
</div>
<div>
<styl>
/* Flat, 1 selector style. Really easy. We recommend it! */
me
font-family: 'Noto Sans', sans-serif
background: hsl(264 62% 20%)
color: #fff
me ol
padding: 5px 0
me ol li
list-style: disc
padding: 4px
border-radius: 12px
margin: 4px 20px
/* ✨ this and self alias me */
this ol li[n1]
background: hsl(264 50% 50%)
self ol li[n2]
background: hsl(264 80% 50%)
/* Nested, 1 selector style. Possible! But visually busy. */
me
border:none
border-radius: 12px
box-shadow: 0 2px 5px #00000044
padding: 10px 20px
margin: 20px
& span
background: hsl(264 90% 90%)
color: hsl(264 30% 40%)
padding: 16px 20px
border-radius: 8px
box-shadow: 0 0 5px hsl(0 0% 0% / 20%)
&::before
content:'🔮'
padding: 0 1ch 0 0
/* Animations */
me
animation: me 2s ease-in-out infinite
@keyframes me
/* ♻️ Looped animation @keyframes can be scoped! .. prefix with me- or me_ for more names! */
0%
transform: translateY(0px)
50%
transform: translateY(20px)
100%
transform: translateY(0px)
/* Plain CSS works untouched! */
h1, h2, h3
font-size: 3rem
margin: 20px 0
</styl>
<h1>🛸 Scoped style using me</h1>
<ol>
<li>Use modern <span>Nested CSS</span>
<li n1>👽 Using this
<li n2>💀 Using self
<li>⚡ Use live editing in the browser inspector to speed up your workflow, again!
</ol>
</div>
<br />
<div>
<styl>
/* Scoped CSS variables work fine! */
me { --color: hsl(264 80% 50%); font-family: sans-serif; margin: 20px }
me div { margin: 20px 0 }
me div ul { margin: 0; padding: 0 }
me div ul li { display: inline-block; width: 110px; text-align: center; background: #444; border-radius: 5px; padding: 10px 2px; margin: 10px 10px }
</styl>
<h2>Responsive Design 🔛 Resize the window!</h2>
🟢 = No breakpoint. Default.
<div>
<styl>
me ul
li[n1]
background: var(--color)
@media sm
me ul li[n2]
background: var(--color)
@media md
me ul li[n3]
background: var(--color)
@media lg
me ul li[n4]
background: var(--color)
@media xl
me ul li[n5]
background: var(--color)
@media xx
me ul li[n6]
background: var(--color)
</styl>
<div>Mobile First (<strong>above</strong> breakpoint)</div>
<ul><li n1><strong>🟢 None</strong><li n2>sm<li n3>md<li n4>lg<li n5>xl<li n6>🏁 xx</ul>
</div>
<div>
<styl>
/* You can also nest @media styles! */
me ul li
&[n1] {
background: var(--color);
}
@media xl- { &[n2] { background: var(--color); } }
@media lg- { &[n3] { background: var(--color); } }
@media md- { &[n4] { background: var(--color); } }
@media sm- { &[n5] { background: var(--color); } }
@media xs- { &[n6] { background: var(--color); } }
</styl>
<div>Desktop First (<strong>below</strong> breakpoint)</div>
<ul><li n6>🏁 xs-<li n5>sm-<li n4>md-<li n3>lg-<li n2>xl-<li n1><strong>🟢 None</strong></ul>
</div>
<br />
<h2>HTMX Test</h2>
<script src="https://unpkg.com/[email protected]/dist/htmx.min.js" defer></script>
<div>Swapped DOM elements are styled instantly when they arrive.</div>
<button garlic hx-get="https://gnat.github.io/css-scope-inline/example.html" hx-select="button[garlic]" hx-swap="afterend">
<styl>
me { margin: 4px; padding: 10px 30px; min-width: 80px; background: hsl(264 80% 47%); border-bottom: 0.5rem solid hsl(264 80% 20%); }
me:hover { background: hsl(264 80% 60%); }
</styl>
🧄
</button>
<button vampire hx-get="https://gnat.github.io/css-scope-inline/example.html" hx-select="button[vampire]" hx-swap="afterend">
<styl>
me { margin: 4px; padding: 10px 30px; min-width: 80px; background: hsl(150 80% 47%); border-bottom: 0.5rem solid hsl(150 80% 28%); }
me:hover { background: hsl(150 80% 60%); }
</styl>
🧛
</button>
</div>
<button onClick="window.open('https://github.com/gnat/css-scope-inline/blob/main/example.html');" style="font-weight: bold; font-family: sans-serif;">🐱 View code for this page.</button>
</body>
</html>