-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
277 lines (239 loc) · 7.57 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width" name="viewport">
<link href="favicon.svg" rel="icon" type="image/svg+xml">
<link crossorigin="anonymous" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/atom-one-light.min.css" integrity="sha512-o5v54Kh5PH0dgnf9ei0L+vMRsbm5fvIvnR/XkrZZjN4mqdaeH7PW66tumBoQVIaKNVrLCZiBEfHzRY4JJSMK/Q==" referrerpolicy="no-referrer" rel="stylesheet" />
<link href="./mui-ripple.css" rel="stylesheet">
<link href="./mui-input.css" rel="stylesheet">
<link href="./pulse-on-change.css" rel="stylesheet">
<link href="./hover.css" rel="stylesheet">
<title>Web Frontend Tricks by Eric Fortis</title>
<style>
:root {
--accent: dodgerblue;
--boxShadowIndex1: 0 1px 3px 0 rgba(0, 0, 0, 0.2), 0 1px 1px 0 rgba(0, 0, 0, 0.14), 0 2px 1px -1px rgba(0, 0, 0, 0.12);
}
html,
body {
width: 90%;
max-width: 960px;
margin: 0 auto;
font-family: Avenir Next, system-ui, sans-serif;
font-size: 16px;
scroll-behavior: smooth;
}
ul li {
line-height: 2;
}
h2 {
margin-top: 6rem;
position: relative;
left: -5px;
padding: 5px 0 5px 5px;
border-radius: 10px;
}
a {
color: var(--accent);
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a[href^="#"]::after {
display: inline-block;
margin-left: 3px;
color: #af85bb;
content: '↴';
transform: scale(.8) translateY(2px);
}
button {
padding: 8px 20px;
color: white;
background: var(--accent);
border-radius: 40px;
cursor: pointer;
}
button:active {
cursor: grabbing;
}
output {
padding: 0 6px;
}
pre {
font-size: .8rem;
tab-size: 2;
}
pre code {
box-shadow: var(--boxShadowIndex1);
}
footer {
padding: 20px 0;
margin-top: 9rem;
color: #999;
}
.hljs-selector-class {
color: #a626a4
}
.hljs-attribute {
color: #4078f2
}
.hljs-return {
padding-right: 4px;
padding-bottom: 1px;
padding-left: 8px;
background: #a626a4;
border-bottom-left-radius: 10px;
border-top-left-radius: 10px;
color: white;
}
</style>
</head>
<body>
<h1>Web Frontend Tricks</h1>
<p>
Tips and tricks for web front-end development.
</p>
<ul id="toc"></ul>
<h2 id="-pulse-on-change">Pulse on change</h2>
<input type="number" id="numA" value="1" /> +
<input type="number" id="numB" value="2" /> =
<output for="numA numB" id="outputAB">3</output>
<pre id="pulse-on-change.css"></pre>
<pre id="pulse-on-change.js"></pre>
<h2 id="-sleep">Sleep</h2>
<pre id="sleep.js"></pre>
<textarea cols="70" rows="2"></textarea>
<h2 id="-css-mui-input-animation">CSS-only Material UI Input Animation</h2>
<input class="muiInput" placeholder="Click me" />
<pre id="mui-input.css"></pre>
<h2 id="-css-mui-ripple">CSS-only Material UI Ripple</h2>
<button autofocus class="ripple" type="button">Focus</button>
<pre id="mui-ripple.css"></pre>
<h2 id="-css-hover">Hover (Out, In, In-Out) Transitions</h2>
<ul class="HoverDemo">
<li class="itemA">Item A (Hover Out)</li>
<li class="itemB">Item B (Hover In)</li>
<li class="itemC">Item C (Hover In Out)</li>
</ul>
<pre id="hover.css"></pre>
<h2 id="-play-note">Play Note</h2>
<button class="ripple" onclick="playNote()" type="button">Play Note</button>
<pre id="play-note.js"></pre>
<h2 id="-email-obfuscator">Email Deobfuscator</h2>
<a id="EmailLink">Contact</a>
<pre id="email-deobfuscator.js"></pre>
<h2 id="-rel-noopener">Auto-inject rel="noopener" to anchors</h2>
<pre id="rel-noopener.js"></pre>
<h2 id="-form-data-to-json">Form Data to JSON</h2>
<pre id="form-data-to-json.js"></pre>
<p>
See also, <a href="https://github.com/uxtely/js-utils/blob/main/fetch-with-retry.js">fetch-with-retry.js</a>
</p>
<h2 id="-more">More…</h2>
<ul>
<li>
<a href="https://ericfortis.github.io/web-animations/" target="_blank">Web Animations</a>
</li>
<li>
<a href="https://github.com/uxtely/js-utils" target="_blank">JavaScript Utils</a>
</li>
<li>
<a href="https://blog.uidrafter.com" target="_blank">Blog Posts</a>
</li>
</ul>
<footer>
<a target="_blank" aria-label="Twitter" href="//twitter.com/efortis">
<svg width="48" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">
<path fill="#3eabff" d="M45.7 10.5a17.8 17.8 0 01-5.1 1.4A9 9 0 0044.5 7a17.8 17.8 0 01-5.7 2.2 8.9 8.9 0 00-15.1 8A25.3 25.3 0 015.3 8a8.9 8.9 0 002.8 11.9 8.9 8.9 0 01-4-1.1 9 9 0 007 8.8 9 9 0 01-4 .2 9 9 0 008.4 6.2 17.9 17.9 0 01-13.2 3.7 25.2 25.2 0 0013.6 4 25.2 25.2 0 0025.4-25.4v-1.1a18.1 18.1 0 004.4-4.7z" />
</svg>
</a>
<br>
© 2022 Eric Fortis
</footer>
<script>
window.addEventListener('load', function () {
/* Build Table of Contents */
for (const h2 of document.querySelectorAll('h2[id^="-"]')) {
const li = document.createElement('li')
const a = document.createElement('a')
a.href = '#' + h2.id
a.innerText = h2.innerText
li.append(a)
document.getElementById('toc').append(li)
}
/* Inject and Style Code Snippets */
const allSnippets = document.querySelectorAll('pre')
const nSnippets = allSnippets.length
let nLoadedSnippets = 0
for (let i = 0; i < nSnippets; i++) {
const snippet = allSnippets[i]
const filename = snippet.id
if (filename) {
const code = document.createElement('code')
if (filename.endsWith('.js'))
code.className = 'language-javascript'
else if (filename.endsWith('.css'))
code.className = 'language-css'
snippet.append(code)
readFromGithub(code, filename)
.then(() => { // Otherwise, the vertical offset wouldn't account for the snippets height
if (nSnippets === ++nLoadedSnippets)
flashHighlightWhenScrollingToFragments()
})
}
}
function readFromGithub(targetEl, file) {
// return fetch(`./${file}`)
return fetch(`https://raw.githubusercontent.com/ericfortis/web-tricks/main/${file}`)
.then(res => res.text())
.then(text => {
targetEl.textContent = text
hljs.highlightElement(targetEl)
for (const span of targetEl.querySelectorAll('.hljs-keyword'))
if (span.innerText === 'return' || span.innerText === 'throw')
span.classList.add('hljs-return')
})
.catch(console.error)
}
function flashHighlightWhenScrollingToFragments() {
const FLASH_BG_COLOR = '#77ffc4'
let elPendingHighlightOff = null
let highlightTimer = null
if (location.hash) {
const snippet = document.querySelector(location.hash)
window.scrollTo(0, snippet.offsetTop)
highlightSectionTitle(location.hash)
}
for (const anchor of document.querySelectorAll('a'))
if (anchor.hash)
anchor.addEventListener('click', highlightSectionTitle.bind(null, anchor.hash))
function highlightSectionTitle(fragmentName) {
try {
const el = document.querySelector(fragmentName)
if (elPendingHighlightOff || el === elPendingHighlightOff) {
elPendingHighlightOff.style.background = ''
clearTimeout(highlightTimer)
}
el.style.background = FLASH_BG_COLOR
elPendingHighlightOff = el
highlightTimer = setTimeout(function () {
el.style.background = ''
elPendingHighlightOff = null
}, 2100)
}
catch (_) {
setTimeout(function () {})
}
}
}
})
</script>
<script src="sleep.js"></script>
<script src="play-note.js"></script>
<script src="email-deobfuscator.js"></script>
<script src="pulse-on-change.js"></script>
<script crossorigin="anonymous" integrity="sha512-yUUc0qWm2rhM7X0EFe82LNnv2moqArj5nro/w1bi05A09hRVeIZbN6jlMoyu0+4I/Bu4Ck/85JQIU82T82M28w==" referrerpolicy="no-referrer" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
</body>
</html>