-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
566 lines (513 loc) · 19.9 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Flotsam</title>
<link
media="all"
rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/sunburst.min.css"
/>
<style>
@import './dist/flotsam.css';
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: inherit;
touch-action: inherit;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
html {
overflow-x: hidden;
overflow-y: scroll;
box-sizing: border-box;
touch-action: manipulation;
scroll-behavior: smooth;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
min-height: 100vh;
font-family: var(--serif-font);
font-size: 100%;
font-kerning: normal;
line-height: 1;
text-rendering: optimizeLegibility;
font-variant-ligatures: common-ligatures;
font-feature-settings: 'kern' on;
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
color: #1a1a1a;
background: #fff;
font-family: SuisseIntl, Helvetica, Arial, sans-serif;
}
.f-h1 {
font-weight: 500;
font-size: 2rem;
line-height: 1.2;
letter-spacing: -0.02em;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
@media screen and (min-width: 650px) {
.f-h1 {
font-size: 2.25rem;
}
}
@media screen and (min-width: 990px) {
.f-h1 {
font-size: 3rem;
}
}
.f-h2 {
font-weight: 500;
font-size: 1.25rem;
line-height: 1.2;
letter-spacing: -0.02em;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
@media screen and (min-width: 990px) {
.f- h2 {
font-size: 1.75rem;
}
}
@media screen and (min-width: 650px) {
.f-h2 {
font-size: 1.5rem;
}
}
.f-body,
input {
font-size: 0.875rem;
line-height: 1.7;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
@media screen and (min-width: 650px) {
.f-body,
input {
font-size: 1rem;
}
}
header {
padding: 0.75rem 2.5rem;
border-bottom: 1px solid #000;
}
main {
flex-grow: 1;
padding: 3.75rem 2.5rem 0;
}
footer {
margin-top: 60px;
padding: 1.5rem 2.5rem;
border-top: 1px solid #000;
}
header h1 {
padding-top: 3.75rem;
}
@media screen and (min-width: 650px) {
header h1 {
padding-top: 5rem;
}
}
@media screen and (min-width: 990px) {
header h1 {
padding-top: 6.25rem;
}
}
header ul,
footer ul {
display: flex;
flex-direction: row;
}
header li,
footer li {
margin-top: 1.25rem;
list-style-type: none;
}
header li:first-child,
footer li:first-child {
margin-right: auto;
}
@media screen and (min-width: 544px) {
header li,
footer li {
margin-top: 0;
margin-right: 1.25rem;
}
}
header a,
footer a {
color: #1a1a1a;
text-decoration: none;
}
header svg,
footer svg {
display: inline-block;
vertical-align: text-bottom;
}
main h2:not(:first-child) {
margin-top: 3.5rem;
border-top: 1px solid #d9d9d9;
padding-top: 3rem;
}
main p {
margin-top: 1rem;
}
form {
margin-top: 2.5rem;
padding: 1.25rem;
background: #f2f2f2;
}
fieldset {
border: 0 none;
}
label {
display: block;
}
input {
width: 100%;
margin-top: 0.5rem;
padding: 0.125rem 0.5rem;
}
.input-container {
position: relative;
}
pre,
code,
kbd {
padding: 0;
font-family: 'Lucida Console', Courier, monospace;
font-size: 0.875rem;
line-height: 1.2;
font-weight: 400;
}
pre {
display: block;
}
code,
kbd {
display: inline-block;
background-color: #eee;
color: #000;
vertical-align: middle;
padding: 2px 6px;
}
pre code {
display: block;
background-color: #1a1a1a;
color: #f8f8f8;
}
.custom-mark {
background-color: #ddd;
}
</style>
</head>
<body>
<header>
<ul class="f-body">
<li>
<a href="https://area17.com/" target="_blank">AREA 17</a>
</li>
<li>
<a href="https://github.com/area17/flotsam">
GitHub
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 1024 1024"
fill="none"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8C0 11.54 2.29 14.53 5.47 15.59C5.87 15.66 6.02 15.42 6.02 15.21C6.02 15.02 6.01 14.39 6.01 13.72C4 14.09 3.48 13.23 3.32 12.78C3.23 12.55 2.84 11.84 2.5 11.65C2.22 11.5 1.82 11.13 2.49 11.12C3.12 11.11 3.57 11.7 3.72 11.94C4.44 13.15 5.59 12.81 6.05 12.6C6.12 12.08 6.33 11.73 6.56 11.53C4.78 11.33 2.92 10.64 2.92 7.58C2.92 6.71 3.23 5.99 3.74 5.43C3.66 5.23 3.38 4.41 3.82 3.31C3.82 3.31 4.49 3.1 6.02 4.13C6.66 3.95 7.34 3.86 8.02 3.86C8.7 3.86 9.38 3.95 10.02 4.13C11.55 3.09 12.22 3.31 12.22 3.31C12.66 4.41 12.38 5.23 12.3 5.43C12.81 5.99 13.12 6.7 13.12 7.58C13.12 10.65 11.25 11.33 9.47 11.53C9.76 11.78 10.01 12.26 10.01 13.01C10.01 14.08 10 14.94 10 15.21C10 15.42 10.15 15.67 10.55 15.59C13.71 14.53 16 11.53 16 8C16 3.58 12.42 0 8 0Z"
transform="scale(64)"
fill="currentColor"
></path>
</svg>
</a>
</li>
</ul>
<h1 class="f-h1">Flotsam</h1>
</header>
<main>
<h2 class="f-h2">Static data</h2>
<form novalidate>
<fieldset>
<div class="input-container">
<label for="q0">Query</label>
<input type="text" id="q0" />
</div>
</fieldset>
</form>
<pre><code>const typeahead = new flotsam({
el: document.querySelectorAll('input'),
data: data, // where data is a list of 999 English words
});</code></pre>
<h2 class="f-h2">Static data, multiple options set</h2>
<p>
No input preview <code>inputPreview: false</code>, <br />
No results marking <code>markResults: false</code>, <br />
No submit on <kbd>return</kbd> press
<code>submitOnReturn: false</code>, <br />
Minimum characters set to 3 <code>minChars: 3</code>.
</p>
<form novalidate>
<fieldset>
<div class="input-container">
<label for="q1">Query</label>
<input type="text" id="q1" />
</div>
</fieldset>
</form>
<pre><code>const typeahead = new flotsam({
el: document.querySelectorAll('input'),
inputPreview: false,
markResults: false,
submitOnReturn: false,
minChars: 3,
data: data, // where data is a list of 999 English words
});</code></pre>
<h2 class="f-h2">Static, translated to French</h2>
<form novalidate>
<fieldset>
<div class="input-container">
<label for="q2">Query</label>
<input type="text" id="q2" />
</div>
</fieldset>
</form>
<pre><code>const typeahead = new flotsam({
el: document.querySelectorAll('input'),
data: dataFrench, // where data is a list of 999 French words
hint: "Lorsque les résultats de la saisie semi-automatique sont disponibles, utilisez les flèches haut et bas pour passer en revue et entrez pour sélectionner. Utilisateurs d'appareils tactiles, explorez au toucher ou avec des gestes de balayage.",
noResultsText: "Désolé, il n'y a pas de résultats pour ::term:: veuillez effectuer une nouvelle recherche",
});</code></pre>
<h2 class="f-h2">
Fuzzy static data search using <code>getData</code> and
<code>fuzzysort</code>
</h2>
<form novalidate>
<fieldset>
<div class="input-container">
<label for="q3">Query</label>
<input type="text" id="q3" />
</div>
</fieldset>
</form>
<pre><code>const typeahead = new flotsam({
el: document.querySelectorAll('input'),
markResults: false,
getData: (textValue) => {
const results = fuzzysort.go(textValue, data).map(result => result.target);
return new Promise((resolve, reject) => resolve(results));
},
});</code></pre>
<p class="f-body">
Uses
<a href="https://github.com/farzher/fuzzysort" target="_blank"
>farzher/fuzzysort</a
>
and its <code>highlight</code> function to add highlights.
<br /><strong>Note:</strong> <code>getData</code> requires you
return a <code>Promise</code>.
</p>
<h2 class="f-h2">
Dynamic <code>getData</code> via <code>Fetch</code>
</h2>
<form novalidate>
<fieldset>
<div class="input-container">
<label for="q4">Query</label>
<input type="text" id="q4" />
</div>
</fieldset>
</form>
<pre><code>let fetchController = new AbortController();
let fetchSignal = fetchController.signal;
const typeahead = new flotsam({
el: document.querySelectorAll('input'),
getData: (textValue) => {
fetchController.abort();
fetchController = new AbortController();
fetchSignal = fetchController.signal;
const url = new URL('http://fakejsonendpoints.dev.area17.com/api/autocomplete.php');
const params = {
q: textValue,
};
url.search = new URLSearchParams(params).toString();
return fetch(
url,
{
method: 'get',
signal: fetchSignal,
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest",
}
}
)
.then(response => {
try {
return response.json();
} catch(error) {
console.log('json error:', error, response);
}
})
.then(data => {
// return data arr
return data;
})
.catch(error => {
console.log('fetch error:', error);
return [];
});
},
});</code></pre>
</main>
<footer>
<ul>
<li>
<a href="https://area17.com/" target="_blank"
>Copyright © AREA 17</a
>
</li>
<li>
<a href="https://github.com/area17/flotsam">
GitHub
<svg
class="inline-block align-text-bottom"
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 1024 1024"
fill="none"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8C0 11.54 2.29 14.53 5.47 15.59C5.87 15.66 6.02 15.42 6.02 15.21C6.02 15.02 6.01 14.39 6.01 13.72C4 14.09 3.48 13.23 3.32 12.78C3.23 12.55 2.84 11.84 2.5 11.65C2.22 11.5 1.82 11.13 2.49 11.12C3.12 11.11 3.57 11.7 3.72 11.94C4.44 13.15 5.59 12.81 6.05 12.6C6.12 12.08 6.33 11.73 6.56 11.53C4.78 11.33 2.92 10.64 2.92 7.58C2.92 6.71 3.23 5.99 3.74 5.43C3.66 5.23 3.38 4.41 3.82 3.31C3.82 3.31 4.49 3.1 6.02 4.13C6.66 3.95 7.34 3.86 8.02 3.86C8.7 3.86 9.38 3.95 10.02 4.13C11.55 3.09 12.22 3.31 12.22 3.31C12.66 4.41 12.38 5.23 12.3 5.43C12.81 5.99 13.12 6.7 13.12 7.58C13.12 10.65 11.25 11.33 9.47 11.53C9.76 11.78 10.01 12.26 10.01 13.01C10.01 14.08 10 14.94 10 15.21C10 15.42 10.15 15.67 10.55 15.59C13.71 14.53 16 11.53 16 8C16 3.58 12.42 0 8 0Z"
transform="scale(64)"
fill="currentColor"
></path>
</svg>
</a>
</li>
</ul>
</footer>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/fuzzysort@2.0.4/fuzzysort.min.js"></script>
<script type="module">
;(async () => {
const data = await import('./data.js')
const { default: flotsam } = await import(
'./dist/flotsam.modern.js'
)
const typeahead0 = new flotsam({
el: document.querySelectorAll('input')[0],
data: data.en,
})
const typeahead1 = new flotsam({
el: document.querySelectorAll('input')[1],
inputPreview: false,
markResults: false,
submitOnReturn: false,
minChars: 3,
inputPreview: false,
data: data.en,
})
typeahead1.on('resultClicked', (instanceData) => {
window.alert(`Selected: ${instanceData.selected}`)
})
const typeahead2 = new flotsam({
el: document.querySelectorAll('input')[2],
data: data.fr,
hint: "Lorsque les résultats de la saisie semi-automatique sont disponibles, utilisez les flèches haut et bas pour passer en revue et entrez pour sélectionner. Utilisateurs d'appareils tactiles, explorez au toucher ou avec des gestes de balayage.",
noResultsText:
"Désolé, il n'y a pas de résultats pour ::term:: veuillez effectuer une nouvelle recherche",
})
const typeahead3 = new flotsam({
el: document.querySelectorAll('input')[3],
data: data.en,
markResults: false,
getData: (textValue) => {
const results = fuzzysort
.go(textValue, data.en)
.map((result) =>
fuzzysort.highlight(
result,
'<mark class="custom-mark">',
'</mark>'
)
)
return new Promise((resolve, reject) =>
resolve(results)
)
},
})
let fetchController = new AbortController()
let fetchSignal = fetchController.signal
const typeahead4 = new flotsam({
el: document.querySelectorAll('input')[4],
getData: (textValue) => {
fetchController.abort()
fetchController = new AbortController()
fetchSignal = fetchController.signal
const url = new URL(
'http://fakejsonendpoints.dev.area17.com/api/autocomplete.php'
)
const params = {
q: textValue,
}
url.search = new URLSearchParams(params).toString()
return fetch(url, {
method: 'get',
signal: fetchSignal,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
})
.then((response) => {
try {
return response.json()
} catch (error) {
console.log('json error:', error, response)
}
})
.then((data) => {
// return data arr
return data
})
.catch((error) => {
console.log('fetch error:', error)
return []
})
},
})
document.querySelectorAll('form').forEach((form) => {
form.addEventListener('submit', (e) => {
e.preventDefault()
})
})
if (window.hljs) {
hljs.highlightAll()
}
})()
</script>
</body>
</html>