Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
783 lines (720 loc) · 47.6 KB
/
Copy pathexamples-basic.html
File metadata and controls
783 lines (720 loc) · 47.6 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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic Usage — MultiSelect</title>
<!-- Shared example styles -->
<link rel="stylesheet" href="examples-shared.css">
</head>
<body>
<div class="container">
<header>
<a href="index.html" class="back-link">← Back to Examples</a>
<h1>Basic Usage</h1>
<p class="subtitle">The essentials, each as ONE live picker you configure with control switches — selection & input mode, content & display, groups, the scroll-to API, and search — plus a declarative, no-JavaScript setup.</p>
</header>
<!-- ============================================================= -->
<!-- BU01 · Selection & input mode -->
<!-- ============================================================= -->
<div class="card">
<h2>BU01 · Selection & Input Mode</h2>
<p>
One picker, many behaviors. Flip between multi- and single-select, add the inline
clear (✕) and the count badge, change the search-input mode (a <code>readonly</code>
or <code>hidden</code> search gives keyboard-only navigation), toggle
<code>close-on-select</code>, and switch text direction (LTR / RTL).
</p>
<div class="controls">
<label><input type="checkbox" id="sel-multiple" checked> <code>multiple</code></label>
<label><input type="checkbox" id="sel-clear"> <code>show-clear</code> (✕)</label>
<label><input type="checkbox" id="sel-counter"> <code>show-counter</code></label>
<label><input type="checkbox" id="sel-close"> <code>close-on-select</code></label>
</div>
<div class="controls">
<span><code>search-input-mode</code>:</span>
<label><input type="radio" name="sel-sim" value="normal" checked> normal</label>
<label><input type="radio" name="sel-sim" value="readonly"> readonly</label>
<label><input type="radio" name="sel-sim" value="hidden"> hidden</label>
</div>
<div class="controls">
<span><code>dir</code>:</span>
<label><input type="radio" name="sel-dir" value="ltr" checked> ltr</label>
<label><input type="radio" name="sel-dir" value="rtl"> rtl</label>
</div>
<div class="demo-area">
<web-multiselect
id="ex-selection"
value-member="value"
display-value-member="label"
icon-member="icon"
search-placeholder="Search…">
</web-multiselect>
</div>
</div>
<!-- ============================================================= -->
<!-- BU02 · Content & display -->
<!-- ============================================================= -->
<div class="card">
<h2>BU02 · Content & Display</h2>
<p>
Icons and subtitles on the option rows, plus how the current selection is shown in
the control: the <code>badges-display-mode</code> and where badges sit
(<code>badges-position</code>).
</p>
<div class="controls">
<label><input type="checkbox" id="disp-icons" checked> icons</label>
<label><input type="checkbox" id="disp-subtitles" checked> subtitles</label>
</div>
<div class="controls">
<span><code>badges-display-mode</code>:</span>
<label><input type="radio" name="disp-mode" value="badges" checked> badges</label>
<label><input type="radio" name="disp-mode" value="count"> count</label>
<label><input type="radio" name="disp-mode" value="compact"> compact</label>
<label><input type="radio" name="disp-mode" value="partial"> partial</label>
<label><input type="radio" name="disp-mode" value="none"> none</label>
<label id="disp-maxvisible-wrap" style="margin-inline-start:1rem; display:none"><code>badges-max-visible</code>:
<input type="number" id="disp-maxvisible" min="1" max="10" value="3" style="width:3.5rem">
</label>
</div>
<div class="controls">
<span><code>badges-position</code>:</span>
<label><input type="radio" name="disp-pos" value="bottom" checked> bottom</label>
<label><input type="radio" name="disp-pos" value="top"> top</label>
<label><input type="radio" name="disp-pos" value="left"> left</label>
<label><input type="radio" name="disp-pos" value="right"> right</label>
</div>
<div class="controls">
<span><code>selected-order</code>:</span>
<label><input type="radio" name="disp-order" value="as-selected" checked> as-selected</label>
<label><input type="radio" name="disp-order" value="label-asc"> label A→Z</label>
<label><input type="radio" name="disp-order" value="label-desc"> label Z→A</label>
<label><input type="radio" name="disp-order" value="member"> member (year)</label>
<label><input type="radio" name="disp-order" value="custom"> custom (year ↓)</label>
<span class="muted" style="margin-inline-start:0.5rem">selected badges reorder; the value stays as-picked</span>
</div>
<div class="demo-area">
<web-multiselect
id="ex-display"
value-member="value"
display-value-member="label"
icon-member="icon"
subtitle-member="subtitle"
search-placeholder="Search frameworks…">
</web-multiselect>
</div>
<p style="margin-top:1.5rem">
<strong>Reserved icon gutter.</strong> When only <em>some</em> rows carry an icon, the
icon-less rows pull their label into the gutter and the list looks ragged. Turn on
<code>reserve-icon-slot</code> to keep a fixed-width icon gutter on <em>every</em> row, so
all labels share one start column. It's option-level — the same knob works in tree mode.
</p>
<div class="controls">
<label><input type="checkbox" id="disp-reserve"> <code>reserve-icon-slot</code></label>
</div>
<div class="demo-area">
<web-multiselect
id="ex-reserve"
value-member="value"
display-value-member="label"
icon-member="icon"
search-placeholder="Mixed icon / no-icon rows — open me…">
</web-multiselect>
</div>
</div>
<!-- ============================================================= -->
<!-- BU03 · Groups -->
<!-- ============================================================= -->
<div class="card">
<h2>BU03 · Groups</h2>
<p>
Group options under headers with a <code>group</code> member. Each group header
shows a <strong>count of that group's selected items</strong> (in any mode — open the
dropdown to see it). Turn on <strong>per-group select-all</strong>
(<code>group-select-mode="cascade"</code>) to also put a tristate checkbox on each
header that toggles the whole group — the group name is never itself a value.
Turning on <strong>custom labels</strong> renders the header via
<code>renderGroupLabelContentCallback</code>, whose 2nd arg carries the per-group
selection — here it writes <code>GROUP — N remaining</code>
(<code>selectableCount − selectedCount</code>).
</p>
<div class="controls">
<span><code>group-select-mode</code>:</span>
<label><input type="radio" name="grp-mode" value="none" checked> none</label>
<label><input type="radio" name="grp-mode" value="cascade"> cascade</label>
<label style="margin-inline-start:1rem"><input type="checkbox" id="grp-custom"> custom labels (<code>renderGroupLabelContentCallback</code>)</label>
</div>
<div class="controls">
<span><code>getCountLabelCallback</code>:</span>
<label><input type="radio" name="grp-count" value="bracket" checked> <code>[x]</code> (default)</label>
<label><input type="radio" name="grp-count" value="ratio"> <code>x/y</code></label>
<span class="muted" style="margin-inline-start:0.5rem">drives the in-input counter <em>and</em> each group's count</span>
</div>
<div class="demo-area">
<web-multiselect
id="ex-groups"
value-member="value"
display-value-member="label"
icon-member="icon"
group-member="group"
show-counter
search-placeholder="Search…">
</web-multiselect>
</div>
</div>
<!-- ============================================================= -->
<!-- BU04 · Declarative usage (no JS) — see the full card below -->
<!-- ============================================================= -->
<div class="card">
<h2>BU04 · Declarative Usage (No JavaScript)</h2>
<p>
For simple cases you don't need any script at all — put plain
<code><option></code> / <code><optgroup></code> children inside the element
and the component parses them once from the light DOM. It reads each option's
<code>value</code>, its text, the <code>selected</code> attribute, and
<code>data-icon</code> / <code>data-subtitle</code>. Single-select is
<code>multiple="false"</code>. Even custom styling stays script-free — the
<code>custom-styles</code> attribute injects raw CSS into the shadow DOM (the
declarative twin of <code>customStylesCallback</code>).
</p>
<div class="grid">
<div class="form-group">
<label>Simple choice (single-select)</label>
<web-multiselect id="decl-single" multiple="false" search-placeholder="Select your answer…">
<option value="yes">Yes</option>
<option value="no">No</option>
<option value="maybe" selected>Maybe / I don't know</option>
</web-multiselect>
<small class="form-text">No JavaScript — pure HTML. <code>selected</code> pre-picks an option.</small>
</div>
<div class="form-group">
<label>Multiple fruits (icons + <code>custom-styles</code>)</label>
<web-multiselect id="decl-fruits" search-placeholder="Select fruits…"
custom-styles=".ms__badge { border-radius: 999px; font-weight: 600; }">
<option value="apple" data-icon="🍎">Apple</option>
<option value="banana" data-icon="🍌" selected>Banana</option>
<option value="orange" data-icon="🍊">Orange</option>
<option value="grape" data-icon="🍇" selected>Grape</option>
<option value="strawberry" data-icon="🍓">Strawberry</option>
</web-multiselect>
<small class="form-text">Row icons from <code>data-icon</code>; the pill-shaped badges are pure <code>custom-styles</code> — no JS.</small>
</div>
</div>
<div class="grid mt-1">
<div class="form-group">
<label>Grouped options (<code><optgroup></code>)</label>
<web-multiselect id="decl-grouped" search-placeholder="Select languages…">
<optgroup label="Frontend">
<option value="js" data-icon="🟨">JavaScript</option>
<option value="ts" data-icon="🔷">TypeScript</option>
<option value="html" data-icon="📄">HTML</option>
</optgroup>
<optgroup label="Backend">
<option value="python" data-icon="🐍" selected>Python</option>
<option value="java" data-icon="☕">Java</option>
<option value="go" data-icon="🐹">Go</option>
</optgroup>
</web-multiselect>
<small class="form-text">The <code><optgroup></code> <code>label</code> becomes the group header.</small>
</div>
<div class="form-group">
<label>With subtitles (<code>data-subtitle</code>)</label>
<web-multiselect id="decl-subtitles" search-placeholder="Select framework…">
<option value="react" data-icon="⚛️" data-subtitle="A JavaScript library for building UIs">React</option>
<option value="vue" data-icon="🖖" data-subtitle="The Progressive JavaScript Framework">Vue.js</option>
<option value="svelte" data-icon="🔥" data-subtitle="Cybernetically enhanced web apps" selected>Svelte</option>
</web-multiselect>
<small class="form-text">Second-line descriptions come from <code>data-subtitle</code>.</small>
</div>
</div>
<details class="mt-1">
<summary>Show code</summary>
<pre><code><!-- Single-select, one option pre-picked -->
<web-multiselect multiple="false">
<option value="yes">Yes</option>
<option value="maybe" selected>Maybe</option>
</web-multiselect>
<!-- Grouped, with icons + subtitles + no-JS styling -->
<web-multiselect custom-styles=".ms__badge { border-radius: 999px; }">
<optgroup label="Frontend">
<option value="js" data-icon="🟨">JavaScript</option>
<option value="react" data-icon="⚛️"
data-subtitle="A JS library for building UIs">React</option>
</optgroup>
</web-multiselect></code></pre>
</details>
</div>
<!-- ============================================================= -->
<!-- BU05 · Scroll-to API -->
<!-- ============================================================= -->
<div class="card">
<h2>BU05 · Scroll-to API — <code>scrollToGroup</code> / <code>scrollToValue</code> / <code>scrollToIndex</code></h2>
<p>
Imperative commands that bring an option or group into view. Pair with <code>open()</code>
for an "open + jump" gesture. They work in floating, mobile fullscreen, virtual-scroll
and tree modes, and return <code>false</code> when the target isn't in the current
filtered list. <code>clearSearch()</code> reveals a filtered-out option so you can then
scroll to it.
</p>
<div class="grid">
<div class="form-group">
<label>Grouped (headers)</label>
<div style="display:flex; gap:1rem; align-items:flex-start; flex-wrap:wrap;">
<div style="flex:1 1 220px; min-width:220px;">
<web-multiselect id="scroll-grouped"
value-member="value" display-value-member="label"
icon-member="icon" group-member="group"></web-multiselect>
</div>
<div style="display:flex; flex-direction:column; gap:0.5rem; flex:0 0 auto; min-width:190px;">
<button type="button" data-scroll-grouped="group:backend">▶ group “backend”</button>
<button type="button" data-scroll-grouped="group:database">▶ group “database”</button>
<button type="button" data-scroll-grouped="value:redis">◎ value “redis”</button>
<button type="button" data-scroll-grouped="index:0">⇱ index 0</button>
</div>
</div>
<small class="form-text"><code>scrollToGroup</code> centers the group header.</small>
</div>
<div class="form-group">
<label>Long list + virtual scroll</label>
<div style="display:flex; gap:1rem; align-items:flex-start; flex-wrap:wrap;">
<div style="flex:1 1 220px; min-width:220px;">
<web-multiselect id="scroll-virtual"
value-member="value" display-value-member="label"
enable-virtual-scroll></web-multiselect>
</div>
<div style="display:flex; flex-direction:column; gap:0.5rem; flex:0 0 auto; min-width:190px;">
<button type="button" data-scroll-virtual="value:jp">Japan</button>
<button type="button" data-scroll-virtual="value:us">United States</button>
<button type="button" data-scroll-virtual="value:vn">Vietnam (last)</button>
<button type="button" data-scroll-virtual="clear">clearSearch()</button>
</div>
</div>
<small class="form-text">Virtual mode scrolls by index math — the row need not be rendered yet.</small>
</div>
</div>
<div id="scroll-log" class="log-panel"><div class="muted">scrollTo* return values log here…</div></div>
<details class="mt-1">
<summary>Show code</summary>
<pre><code>el.open();
el.scrollToGroup('backend'); // header centered (or the group's first row in virtual mode)
el.scrollToValue('redis'); // → false if filtered out by the current search
el.scrollToIndex(0);
// Reveal a filtered-out option, then scroll to it:
el.clearSearch();
el.scrollToValue('jp');</code></pre>
</details>
</div>
<!-- ============================================================= -->
<!-- BU06 · Search -->
<!-- ============================================================= -->
<div class="card">
<h2>BU06 · Search</h2>
<p>
<strong>Filter</strong> mode hides non-matches; <strong>navigate</strong> mode keeps
every option visible and jumps focus between matches (<kbd>Ctrl</kbd>/<kbd>Cmd</kbd> +
<kbd>↑</kbd>/<kbd>↓</kbd>). Add a search hint, require a minimum number of characters
before searching, and debounce the query.
</p>
<div class="controls">
<span><code>search-mode</code>:</span>
<label><input type="radio" name="srch-mode" value="filter" checked> filter</label>
<label><input type="radio" name="srch-mode" value="navigate"> navigate</label>
<label style="margin-inline-start:1rem"><input type="checkbox" id="srch-hint"> search hint</label>
</div>
<div class="controls">
<label><code>min-search-length</code>:
<select id="srch-min">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</label>
<label><code>search-debounce</code> (ms):
<select id="srch-deb">
<option value="0" selected>0</option>
<option value="150">150</option>
<option value="300">300</option>
<option value="600">600</option>
</select>
</label>
</div>
<div class="demo-area">
<web-multiselect
id="ex-search"
search-mode="filter"
value-member="id"
display-value-member="name"
icon-member="flag"
search-placeholder="Type to search countries…">
</web-multiselect>
</div>
<div class="note">
<p><strong>When to use each mode:</strong></p>
<ul>
<li><strong>Filter</strong>: large lists you want to narrow down (product catalogs, user lists).</li>
<li><strong>Navigate</strong>: quick selection from a known list (country / state selectors).</li>
</ul>
</div>
</div>
<!-- ============================================================= -->
<!-- BU07 · Search matching -->
<!-- ============================================================= -->
<div class="card">
<h2>BU07 · Search matching — <code>search-behavior</code> & <code>search-match</code></h2>
<p>
Two independent switches for the built-in search. <code>search-behavior</code> decides how text
is <strong>normalized</strong>: <code>normalized</code> (the default) ignores case and accents, so
<code>cesky</code> finds <em>Český Krumlov</em>; <code>case-insensitive</code> ignores case only;
<code>exact</code> ignores nothing. <code>search-match</code> decides <strong>where</strong> the
term must appear. Both are live: flip them with a term typed and the list re-filters in place.
Each option's search value is normalized once and cached, not on every keystroke.
</p>
<div class="controls">
<span><code>search-behavior</code>:</span>
<label><input type="radio" name="sm-behavior" value="normalized" checked> normalized</label>
<label><input type="radio" name="sm-behavior" value="case-insensitive"> case-insensitive</label>
<label><input type="radio" name="sm-behavior" value="exact"> exact</label>
</div>
<div class="controls">
<label><code>search-match</code>:
<select id="sm-match">
<option value="contains" selected>contains</option>
<option value="equals">equals</option>
<option value="starts-with">starts-with</option>
<option value="word-starts-with">word-starts-with</option>
<option value="ends-with">ends-with</option>
<option value="word-ends-with">word-ends-with</option>
<option value="regex">regex</option>
</select>
</label>
</div>
<div class="demo-area">
<web-multiselect
id="ex-search-match"
value-member="value"
display-value-member="label"
search-placeholder="Try cesky, nad, kralove, ^(Ústí|Nové)…">
</web-multiselect>
</div>
<div class="note">
<p><strong>Things to try:</strong></p>
<ul>
<li><code>cesky</code>: matches under <code>normalized</code>; switch to <code>case-insensitive</code> and it's gone (<code>česk</code> still works).</li>
<li><code>brno</code> with <code>equals</code>: only the whole value counts (and case doesn't), so <code>brn</code> finds nothing.</li>
<li><code>nad</code>: <code>contains</code> / <code>word-starts-with</code> find the "…nad Labem" towns; <code>starts-with</code> finds nothing.</li>
<li><code>kralove</code>: <code>ends-with</code> finds <em>Hradec Králové</em>; <code>word-ends-with</code> adds <em>Dvůr Králové nad Labem</em>.</li>
<li><code>^(Ústí|Nové)</code> with <code>regex</code>. Keep typing an unfinished pattern like <code>^(Ústí</code> and the last valid result stays.</li>
</ul>
<p><strong>Regex skips normalization:</strong> the pattern runs against the raw text, so
<code>search-behavior</code> doesn't apply. <code>^ces</code> finds nothing; <code>^Čes</code> does.</p>
</div>
<details class="mt-1">
<summary>Show code</summary>
<pre><code><web-multiselect search-behavior="normalized" search-match="word-starts-with"></web-multiselect>
// live, e.g. from a page-level switch
el.searchBehavior = 'case-insensitive';
el.searchMatch = 'regex';
// the same normalization, for your own code
import { normalizeSearchText } from '@keenmate/web-multiselect';
normalizeSearchText('Český Krumlov'); // 'cesky krumlov'</code></pre>
</details>
</div>
<!-- ============================================================= -->
<!-- BU08 · Preparing the term -->
<!-- ============================================================= -->
<div class="card">
<h2>BU08 · Preparing the term — <code>getSearchTermCallback</code></h2>
<p>
<code>getSearchTermCallback</code> turns what the user typed into what search should look for.
Return a string (used as-is) or a <code>RegExp</code>. Here part numbers are written with all sorts
of separators; the callback turns <code>ha-324/56</code> into a pattern that ignores them, so it
finds <em>HA 324-56</em>, <em>HA32456</em> and <em>HA-324/56</em>. Untick it to compare:
plain search finds only the one written exactly that way.
</p>
<div class="controls">
<label><input type="checkbox" id="pn-term-cb" checked> <code>getSearchTermCallback</code> (separator-agnostic regex)</label>
</div>
<div class="demo-area">
<web-multiselect
id="ex-part-numbers"
value-member="value"
display-value-member="label"
subtitle-member="subtitle"
search-placeholder="Try ha-324/56, xk 100 2, ha32…">
</web-multiselect>
</div>
<details class="mt-1">
<summary>Show code</summary>
<pre><code>el.getSearchTermCallback = (term) =>
new RegExp(term.split(/[^a-z0-9]+/i).filter(Boolean).join('[^a-z0-9]*'), 'i');
// "ha-324/56" → /ha[^a-z0-9]*324[^a-z0-9]*56/i
// A returned RegExp is tested against the raw search value. A returned string is used
// as-is, so normalize it yourself (normalizeSearchText) to match the prepared values.</code></pre>
</details>
</div>
</div>
<script type="module">
import { persistControls } from './examples-controls-store.js';
// ── Datasets ─────────────────────────────────────────────────────
const languages = [
{ value: 'js', label: 'JavaScript', icon: '🟨' },
{ value: 'ts', label: 'TypeScript', icon: '🔷' },
{ value: 'python', label: 'Python', icon: '🐍' },
{ value: 'java', label: 'Java', icon: '☕' },
{ value: 'csharp', label: 'C#', icon: '💜' },
{ value: 'php', label: 'PHP', icon: '🐘' },
{ value: 'ruby', label: 'Ruby', icon: '💎' },
{ value: 'go', label: 'Go', icon: '🐹' }
];
const frameworks = [
{ value: 'react', label: 'React', icon: '⚛️', year: 2013, subtitle: 'A JavaScript library for building user interfaces' },
{ value: 'vue', label: 'Vue.js', icon: '🖖', year: 2014, subtitle: 'The Progressive JavaScript Framework' },
{ value: 'angular', label: 'Angular', icon: '🅰️', year: 2016, subtitle: 'Platform for building web applications' },
{ value: 'svelte', label: 'Svelte', icon: '🔥', year: 2016, subtitle: 'Cybernetically enhanced web apps' },
{ value: 'solid', label: 'Solid', icon: '💎', year: 2018, subtitle: 'Simple and performant reactivity' }
];
// A deliberately MIXED list (some rows carry an icon, some don't) for the
// reserve-icon-slot demo — the effect is only visible when the two kinds mix.
const mixedIcons = [
{ value: 'apple', label: 'Apple', icon: '🍎' },
{ value: 'banana', label: 'Banana' },
{ value: 'cherry', label: 'Cherry', icon: '🍒' },
{ value: 'date', label: 'Date' },
{ value: 'grape', label: 'Grapes', icon: '🍇' },
{ value: 'kiwi', label: 'Kiwi' }
];
const groupedTechnologies = [
{ value: 'react', label: 'React', icon: '⚛️', group: 'frontend' },
{ value: 'vue', label: 'Vue.js', icon: '🖖', group: 'frontend' },
{ value: 'svelte', label: 'Svelte', icon: '🔥', group: 'frontend' },
{ value: 'angular', label: 'Angular', icon: '🅰️', group: 'frontend' },
{ value: 'nodejs', label: 'Node.js', icon: '🟢', group: 'backend' },
{ value: 'django', label: 'Django', icon: '🐍', group: 'backend' },
{ value: 'rails', label: 'Ruby on Rails', icon: '💎', group: 'backend' },
{ value: 'express', label: 'Express', icon: '🚂', group: 'backend' },
{ value: 'postgres', label: 'PostgreSQL', icon: '🐘', group: 'database' },
{ value: 'mongodb', label: 'MongoDB', icon: '🍃', group: 'database' },
{ value: 'redis', label: 'Redis', icon: '🔴', group: 'database' }
];
const allCountries = [
'Afghanistan|af','Albania|al','Algeria|dz','Argentina|ar','Armenia|am','Australia|au','Austria|at',
'Azerbaijan|az','Bahrain|bh','Bangladesh|bd','Belarus|by','Belgium|be','Belize|bz','Bolivia|bo',
'Brazil|br','Bulgaria|bg','Cambodia|kh','Cameroon|cm','Canada|ca','Chile|cl','China|cn','Colombia|co',
'Costa Rica|cr','Croatia|hr','Cuba|cu','Cyprus|cy','Czech Republic|cz','Denmark|dk','Ecuador|ec',
'Egypt|eg','El Salvador|sv','Estonia|ee','Ethiopia|et','Finland|fi','France|fr','Georgia|ge',
'Germany|de','Ghana|gh','Greece|gr','Guatemala|gt','Honduras|hn','Hong Kong|hk','Hungary|hu',
'Iceland|is','India|in','Indonesia|id','Iran|ir','Iraq|iq','Ireland|ie','Israel|il','Italy|it',
'Jamaica|jm','Japan|jp','Jordan|jo','Kazakhstan|kz','Kenya|ke','Kuwait|kw','Latvia|lv','Lebanon|lb',
'Lithuania|lt','Luxembourg|lu','Malaysia|my','Malta|mt','Mexico|mx','Morocco|ma','Nepal|np',
'Netherlands|nl','New Zealand|nz','Nigeria|ng','Norway|no','Oman|om','Pakistan|pk','Panama|pa',
'Paraguay|py','Peru|pe','Philippines|ph','Poland|pl','Portugal|pt','Qatar|qa','Romania|ro',
'Russia|ru','Saudi Arabia|sa','Serbia|rs','Singapore|sg','Slovakia|sk','Slovenia|si',
'South Africa|za','South Korea|kr','Spain|es','Sri Lanka|lk','Sweden|se','Switzerland|ch',
'Taiwan|tw','Thailand|th','Turkey|tr','Ukraine|ua','United Arab Emirates|ae','United Kingdom|uk',
'United States|us','Uruguay|uy','Venezuela|ve','Vietnam|vn'
].map(s => { const [label, value] = s.split('|'); return { value, label }; });
const countriesWithFlags = [
{ id: 1, name: 'Canada', flag: '🇨🇦' }, { id: 2, name: 'United States', flag: '🇺🇸' },
{ id: 3, name: 'Mexico', flag: '🇲🇽' }, { id: 4, name: 'United Kingdom', flag: '🇬🇧' },
{ id: 5, name: 'France', flag: '🇫🇷' }, { id: 6, name: 'Germany', flag: '🇩🇪' },
{ id: 7, name: 'Italy', flag: '🇮🇹' }, { id: 8, name: 'Spain', flag: '🇪🇸' },
{ id: 9, name: 'Japan', flag: '🇯🇵' }, { id: 10, name: 'China', flag: '🇨🇳' },
{ id: 11, name: 'India', flag: '🇮🇳' }, { id: 12, name: 'Brazil', flag: '🇧🇷' },
{ id: 13, name: 'Argentina', flag: '🇦🇷' }, { id: 14, name: 'Australia', flag: '🇦🇺' },
{ id: 15, name: 'New Zealand', flag: '🇳🇿' }, { id: 16, name: 'South Africa', flag: '🇿🇦' },
{ id: 17, name: 'Egypt', flag: '🇪🇬' }, { id: 18, name: 'Nigeria', flag: '🇳🇬' },
{ id: 19, name: 'Russia', flag: '🇷🇺' }, { id: 20, name: 'Poland', flag: '🇵🇱' },
{ id: 21, name: 'Sweden', flag: '🇸🇪' }, { id: 22, name: 'Norway', flag: '🇳🇴' },
{ id: 23, name: 'Denmark', flag: '🇩🇰' }, { id: 24, name: 'Finland', flag: '🇫🇮' },
{ id: 25, name: 'Netherlands', flag: '🇳🇱' }
];
// Emoji-prefixed group labels for the "custom labels" toggle on BU03. The 2nd arg is the
// GroupLabelRenderContext — under cascade it carries the per-group selection, so a custom
// header can write whatever it likes: here "N remaining" (= selectableCount − selectedCount).
// The suffix is left un-dimmed (same weight/color as the group name) so the whole label
// reads dark and legible — a faded suffix is harder to scan. The context also carries the
// presentation (isFullscreen) and is re-invoked when it changes: on the phone fullscreen
// overlay the base rem scales UP for touch, so we render a notch smaller (font-size:0.8em)
// to keep "NAME — N remaining" on one line.
const groupLabelRenderer = (name, ctx) => {
const emojis = { frontend: '🎨', backend: '🔧', database: '🗄️' };
const remaining = ctx ? ctx.selectableCount - ctx.selectedCount : 0;
const suffix = !ctx ? ''
: remaining > 0
? ` — ${remaining} remaining`
: ' — all selected';
const scale = ctx && ctx.isFullscreen ? ' style="font-size:0.8em"' : '';
return `<span class="js-custom-label"${scale}><strong>${emojis[name] || '📦'} ${name.toUpperCase()}</strong>${suffix}</span>`;
};
// Toggle a boolean (presence) attribute.
const boolAttr = (el, name, on) => on ? el.setAttribute(name, '') : el.removeAttribute(name);
document.addEventListener('DOMContentLoaded', () => {
// ── BU01 · Selection & input mode ────────────────────────────
const exSel = document.getElementById('ex-selection');
exSel.options = languages;
exSel.setSelected(['js', 'ts']);
document.getElementById('sel-multiple').addEventListener('change', (e) =>
e.target.checked ? exSel.removeAttribute('multiple') : exSel.setAttribute('multiple', 'false'));
document.getElementById('sel-clear').addEventListener('change', (e) =>
boolAttr(exSel, 'show-clear', e.target.checked));
document.getElementById('sel-counter').addEventListener('change', (e) =>
boolAttr(exSel, 'show-counter', e.target.checked));
document.getElementById('sel-close').addEventListener('change', (e) =>
e.target.checked ? exSel.setAttribute('close-on-select', 'true') : exSel.removeAttribute('close-on-select'));
document.querySelectorAll('input[name="sel-sim"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exSel.setAttribute('search-input-mode', e.target.value); }));
document.querySelectorAll('input[name="sel-dir"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exSel.setAttribute('dir', e.target.value); }));
// ── BU02 · Content & display ─────────────────────────────────
const exDisp = document.getElementById('ex-display');
exDisp.options = frameworks;
exDisp.setSelected(['react', 'vue', 'svelte']);
document.getElementById('disp-icons').addEventListener('change', (e) =>
e.target.checked ? exDisp.setAttribute('icon-member', 'icon') : exDisp.removeAttribute('icon-member'));
document.getElementById('disp-subtitles').addEventListener('change', (e) =>
e.target.checked ? exDisp.setAttribute('subtitle-member', 'subtitle') : exDisp.removeAttribute('subtitle-member'));
// Reserved icon gutter — a mixed icon / no-icon flat list; the toggle flips the
// fixed-width slot so icon-less rows' labels line up with the icon'd ones.
const exReserve = document.getElementById('ex-reserve');
exReserve.options = mixedIcons;
document.getElementById('disp-reserve').addEventListener('change', (e) =>
boolAttr(exReserve, 'reserve-icon-slot', e.target.checked));
const maxVisibleWrap = document.getElementById('disp-maxvisible-wrap');
document.querySelectorAll('input[name="disp-mode"]').forEach((r) =>
r.addEventListener('change', (e) => {
if (!e.target.checked) return;
exDisp.setAttribute('badges-display-mode', e.target.value);
// badges-max-visible only applies to partial mode — show it only then.
maxVisibleWrap.style.display = e.target.value === 'partial' ? '' : 'none';
}));
document.querySelectorAll('input[name="disp-pos"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exDisp.setAttribute('badges-position', e.target.value); }));
document.getElementById('disp-maxvisible').addEventListener('input', (e) => {
const n = parseInt(e.target.value, 10);
if (n >= 1) exDisp.setAttribute('badges-max-visible', String(n));
});
// 'custom' mode uses a comparator; install it once (ignored unless selected-order="custom").
exDisp.selectedOrderCompareCallback = (a, b) => b.year - a.year; // newest first
document.querySelectorAll('input[name="disp-order"]').forEach((r) =>
r.addEventListener('change', (e) => {
if (!e.target.checked) return;
exDisp.setAttribute('selected-order', e.target.value);
// 'member' mode needs a sort-key member; the frameworks carry `year`.
if (e.target.value === 'member') exDisp.setAttribute('selected-order-member', 'year');
}));
// ── BU03 · Groups ────────────────────────────────────────────
const exGroups = document.getElementById('ex-groups');
exGroups.options = groupedTechnologies;
exGroups.setSelected(['react', 'nodejs']);
document.querySelectorAll('input[name="grp-mode"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exGroups.setAttribute('group-select-mode', e.target.value); }));
document.getElementById('grp-custom').addEventListener('change', (e) =>
exGroups.renderGroupLabelContentCallback = e.target.checked ? groupLabelRenderer : null);
// One formatter for the in-input counter AND each group-header count.
document.querySelectorAll('input[name="grp-count"]').forEach((r) =>
r.addEventListener('change', (e) => {
if (!e.target.checked) return;
exGroups.getCountLabelCallback = e.target.value === 'ratio'
? (s, t) => `${s}/${t}`
: null; // null → default `[x]`
}));
// ── BU04 · Scroll-to API ─────────────────────────────────────
const scrollGrouped = document.getElementById('scroll-grouped');
const scrollVirtual = document.getElementById('scroll-virtual');
const scrollLogEl = document.getElementById('scroll-log');
const scrollLog = (msg) => {
if (!scrollLogEl) return;
if (scrollLogEl.querySelector('.muted')) scrollLogEl.innerHTML = '';
const row = document.createElement('div');
row.style.marginBottom = '0.4rem';
row.textContent = msg;
scrollLogEl.appendChild(row);
scrollLogEl.scrollTop = scrollLogEl.scrollHeight;
};
if (scrollGrouped) scrollGrouped.options = groupedTechnologies;
if (scrollVirtual) scrollVirtual.options = allCountries;
const describeViewport = (el, targetValue) => {
const container = el.shadowRoot && el.shadowRoot.querySelector('.ms__options');
if (!container) { scrollLog(' ↳ (dropdown not open / no list)'); return; }
const crect = container.getBoundingClientRect();
const rows = [...container.querySelectorAll('.ms__option')];
const visible = rows.filter((o) => {
const r = o.getBoundingClientRect();
return r.bottom > crect.top + 1 && r.top < crect.bottom - 1;
});
const label = (o) => (o.querySelector('.ms__option-title') || o).textContent.trim();
const first = visible[0] ? label(visible[0]) : '—';
const last = visible.length ? label(visible[visible.length - 1]) : '—';
let targetNote = '';
if (targetValue != null) {
const hit = visible.find((o) => o.dataset.value === String(targetValue));
targetNote = hit ? ` · target "${label(hit)}" VISIBLE` : ` · target "${targetValue}" NOT in view`;
}
scrollLog(` ↳ scrollTop=${Math.round(container.scrollTop)} · shows "${first}" … "${last}" (${visible.length} rows)${targetNote}`);
};
const runScroll = (el, spec) => {
el.open();
if (spec === 'clear') { el.clearSearch(); scrollLog('clearSearch() — search reset, full list restored'); return; }
const [kind, arg] = spec.split(':');
let ok, target = null;
if (kind === 'group') { ok = el.scrollToGroup(arg); }
else if (kind === 'value') { ok = el.scrollToValue(arg); target = arg; }
else if (kind === 'index') { ok = el.scrollToIndex(Number(arg)); }
const call = kind === 'index' ? `scrollToIndex(${arg})`
: kind === 'group' ? `scrollToGroup('${arg}')`
: `scrollToValue('${arg}')`;
scrollLog(`▶ ${call} → ${ok}`);
setTimeout(() => describeViewport(el, target), 90);
};
document.querySelectorAll('[data-scroll-grouped]').forEach((btn) =>
btn.addEventListener('click', () => runScroll(scrollGrouped, btn.dataset.scrollGrouped)));
document.querySelectorAll('[data-scroll-virtual]').forEach((btn) =>
btn.addEventListener('click', () => runScroll(scrollVirtual, btn.dataset.scrollVirtual)));
// ── BU05 · Search ────────────────────────────────────────────
const exSearch = document.getElementById('ex-search');
exSearch.options = countriesWithFlags;
document.querySelectorAll('input[name="srch-mode"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exSearch.setAttribute('search-mode', e.target.value); }));
document.getElementById('srch-hint').addEventListener('change', (e) =>
e.target.checked
? exSearch.setAttribute('search-hint', 'Ctrl/Cmd + ↓ / ↑ to jump · Enter to select · Esc to clear')
: exSearch.removeAttribute('search-hint'));
document.getElementById('srch-min').addEventListener('change', (e) =>
exSearch.setAttribute('min-search-length', e.target.value));
document.getElementById('srch-deb').addEventListener('change', (e) =>
exSearch.setAttribute('search-debounce', e.target.value));
// ── BU07 · Search matching ───────────────────────────────────
const exMatch = document.getElementById('ex-search-match');
exMatch.options = [
'Praha', 'Brno', 'Ostrava', 'Plzeň', 'Olomouc', 'České Budějovice', 'Český Krumlov',
'Český Těšín', 'Hradec Králové', 'Ústí nad Labem', 'Ústí nad Orlicí', 'Dvůr Králové nad Labem',
'Nové Město na Moravě', 'Nové Město nad Metují', 'Kraków', 'Łódź', 'Gdańsk', 'Zürich', 'München'
].map((label, i) => ({ value: String(i), label }));
document.querySelectorAll('input[name="sm-behavior"]').forEach((r) =>
r.addEventListener('change', (e) => { if (e.target.checked) exMatch.setAttribute('search-behavior', e.target.value); }));
document.getElementById('sm-match').addEventListener('change', (e) =>
exMatch.setAttribute('search-match', e.target.value));
// ── BU08 · Preparing the term ────────────────────────────────
const exParts = document.getElementById('ex-part-numbers');
exParts.options = [
['HA 324-56', 'Hydraulic adapter'], ['HA32456', 'Hydraulic adapter (old label)'],
['HA-324/56', 'Hydraulic adapter (supplier B)'], ['HA 324-57', 'Hydraulic adapter, long'],
['HB 324-56', 'Hose bracket'], ['XK 100/20', 'Coupling'], ['XK-100-21', 'Coupling, reinforced']
].map(([label, subtitle], i) => ({ value: String(i), label, subtitle }));
const separatorAgnostic = (term) =>
new RegExp(term.split(/[^a-z0-9]+/i).filter(Boolean).join('[^a-z0-9]*'), 'i');
const applyTermCb = (on) => { exParts.getSearchTermCallback = on ? separatorAgnostic : null; };
applyTermCb(true);
document.getElementById('pn-term-cb').addEventListener('change', (e) => applyTermCb(e.target.checked));
// Persist all control switches to localStorage and restore them on
// reload — re-applies to every picker via the listeners wired above.
persistControls('basic');
});
</script>
<!-- Development mode: Load from src -->
<script type="module" src="/src/index.ts"></script>
<script type="module" src="examples-copy-link.js"></script>
<script type="module" src="examples-chapter-nav.js"></script>
</body>
</html>