Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
920 lines (852 loc) · 60.6 KB
/
Copy pathexamples-basic.html
File metadata and controls
920 lines (852 loc) · 60.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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
<meta name="kmwc:title" content="Basic Usage">
<meta name="kmwc:subtitle" content="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.">
<!-- Light-DOM pre-upgrade rule (= dist/style.css): reserves the input height before the JS defines the element -->
<link rel="stylesheet" href="/src/css/main.css">
<!-- ============================================================= -->
<!-- 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, turn search off (<code>enable-search="false"</code>
makes it a plain picker with keyboard-only navigation), toggle
<code>close-on-select</code> or <code>close-on-scroll</code> (on by default: like a native
select, scrolling the page closes the open dropdown; untick it and the dropdown stays open and
follows the field), and switch text direction (LTR / RTL). PHP is a disabled
row: <code>disabled: true</code> on the option object (the default
<code>is-disabled-member</code>) keeps it visible but not pickable. BU02 and BU03 have
disabled rows too.
</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>
<label title="Scrolling the page (or a scroll container around the picker) closes the open dropdown, like a native select. A scroll inside the dropdown never does."><input type="checkbox" id="sel-close-scroll" checked> <code>close-on-scroll</code></label>
<label><input type="checkbox" id="sel-search" checked> <code>enable-search</code></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" labels='{"searchPlaceholder": "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>max-visible-badge-count</code>:
<input type="number" id="disp-maxvisible" min="1" max="10" value="3" class="w-xs">
</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" labels='{"searchPlaceholder": "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" labels='{"searchPlaceholder": "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 <code>ctx</code> 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 labels='{"searchPlaceholder": "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> and <code>disabled</code> (the option
stays visible but can't be picked). 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>getCustomStylesCallback</code>).
</p>
<kmwc-demo>
<div class="grid">
<div class="form-group">
<label>Simple choice (single-select)</label>
<web-multiselect id="decl-single" multiple="false" labels='{"searchPlaceholder": "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"
custom-styles=".ms__badge { border-radius: 999px; font-weight: 600; }" labels='{"searchPlaceholder": "Select fruits…"}'>
<option value="apple" data-icon="🍎">Apple</option>
<option value="banana" data-icon="🍌" selected>Banana</option>
<option value="orange" data-icon="🍊" disabled>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. Orange is <code><option disabled></code>.</small>
</div>
</div>
<div class="grid mt-1">
<div class="form-group">
<label>Grouped options (<code><optgroup></code>)</label>
<web-multiselect id="decl-grouped" labels='{"searchPlaceholder": "Select languages…"}'>
<optgroup label="Frontend">
<option value="js" data-icon="🟨">JavaScript</option>
<option value="ts" data-icon="🔷">TypeScript</option>
<option value="html" data-icon="📄" disabled>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. HTML is disabled.</small>
</div>
<div class="form-group">
<label>With subtitles (<code>data-subtitle</code>)</label>
<web-multiselect id="decl-subtitles" labels='{"searchPlaceholder": "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>
</kmwc-demo>
</div>
<!-- ============================================================= -->
<!-- BU05 · Disabled / Readonly States -->
<!-- ============================================================= -->
<div class="card">
<h2>BU05 · Disabled / Readonly States</h2>
<kmwc-demo>
<div class="grid">
<div class="form-group">
<label>Disabled Multiselect</label>
<web-multiselect id="decl-disabled" disabled show-counter show-clear>
<option value="red" selected>Red</option>
<option value="green" selected>Green</option>
<option value="blue">Blue</option>
</web-multiselect>
<small class="form-text">No interaction, not focusable, not submitted with a form</small>
</div>
<div class="form-group">
<label>Readonly Multiselect</label>
<web-multiselect id="decl-readonly" readonly show-counter show-clear>
<option value="red" selected>Red</option>
<option value="green" selected>Green</option>
<option value="blue">Blue</option>
</web-multiselect>
<small class="form-text">The value can't change; still focusable and copyable, submitted with a form</small>
</div>
</div>
</kmwc-demo>
</div>
<!-- ============================================================= -->
<!-- BU06 · Scroll-to API -->
<!-- ============================================================= -->
<div class="card">
<h2>BU06 · 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="controls">
<span><code>block</code>:</span>
<label><input type="radio" name="scroll-block" value="" checked> default (<code>start</code>)</label>
<label><input type="radio" name="scroll-block" value="start"> start</label>
<label><input type="radio" name="scroll-block" value="center"> center</label>
<label><input type="radio" name="scroll-block" value="end"> end</label>
<label><input type="radio" name="scroll-block" value="nearest"> nearest</label>
<span class="muted" style="margin-inline-start:0.5rem">where the target lands; <code>nearest</code> moves only if it's out of view</span>
</div>
<div class="controls">
<span><code>--ms-scroll-behavior</code>:</span>
<label><input type="radio" name="scroll-behavior" value="" checked> default (<code>smooth</code>)</label>
<label><input type="radio" name="scroll-behavior" value="auto"> auto (instant)</label>
<span class="muted" style="margin-inline-start:0.5rem">the motion level / reduced motion also set it; virtual scroll always jumps</span>
</div>
<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> brings the group header into view.</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" data-empty="scrollTo* return values log here…"></div>
<details class="mt-1">
<summary>Show code</summary>
<div class="code-block" data-language="js"><pre><code>el.open();
el.scrollToGroup('backend'); // header at the top (or the group's first row in virtual mode)
el.scrollToValue('redis', { block: 'center' }); // → Promise<false> if filtered out by the current search
el.scrollToIndex(0, { block: 'nearest' }); // start (default) | center | end | nearest
// Smooth or instant: --ms-scroll-behavior (smooth by default; auto under reduced motion / motion="none")
el.style.setProperty('--ms-scroll-behavior', 'auto');
// Reveal a filtered-out option, then scroll to it:
el.clearSearch();
el.scrollToValue('jp');</code></pre></div>
</details>
</div>
<!-- ============================================================= -->
<!-- BU07 · Search -->
<!-- ============================================================= -->
<div class="card">
<h2>BU07 · 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 and require a minimum number of characters
before searching. (<code>search-debounce</code> applies to an async <code>searchCallback</code>
only. See <a href="examples-data-api.html">DA04 · Async Search</a>.)
</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>
</div>
<div class="demo-area">
<web-multiselect
id="ex-search"
search-mode="filter"
value-member="id"
display-value-member="name"
icon-member="flag" labels='{"searchPlaceholder": "Type to search countries…"}'>
</web-multiselect>
</div>
<p style="margin-top:1.5rem">
<strong>Drive it from code.</strong> <code>search(term)</code> applies a query exactly as if the
user typed it (the mode above, <code>min-search-length</code>, <code>beforeSearchCallback</code>,
an async <code>searchCallback</code>) but does <em>not</em> open the dropdown;
<code>searchTerm</code> reads the current query; <code>clearSearch()</code> resets it. There's no
separate "filter" method: <code>search-mode</code> decides what the query does. Handy for
deep-links ("open with a query pre-filled"), syncing an external field or restoring saved state.
</p>
<kmwc-demo markup-hidden>
<div class="controls">
<input type="text" id="srch-api-term" value="an" placeholder="query, e.g. an" style="padding:0.3rem 0.5rem; width:10rem">
<button type="button" id="srch-api-apply"><code>search(term)</code> + <code>open()</code></button>
<button type="button" id="srch-api-clear"><code>clearSearch()</code></button>
<span><code>searchTerm</code> = <code id="srch-api-out">""</code></span>
</div>
<script type="module" data-kmwc-show>
const el = document.getElementById('ex-search'); // search-mode decides what the query does
const termInput = document.getElementById('srch-api-term');
const out = document.getElementById('srch-api-out');
// searchTerm reads the current query (typing too — the search box's `input` event is composed)
const showSearchTerm = () => { out.textContent = JSON.stringify(el.searchTerm); };
el.addEventListener('input', showSearchTerm);
document.getElementById('srch-api-apply').addEventListener('click', () => {
el.search(termInput.value); // apply as if typed (does not open)
el.open(); // …open to see it
showSearchTerm(); // → "an"
});
document.getElementById('srch-api-clear').addEventListener('click', () => {
el.clearSearch(); // → ""
showSearchTerm();
});
</script>
</kmwc-demo>
<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>
<!-- ============================================================= -->
<!-- BU08 · Search matching -->
<!-- ============================================================= -->
<div class="card">
<h2>BU08 · 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>zurich</code> finds <em>Zürich</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>
<kmwc-demo>
<web-multiselect
id="ex-search-match"
value-member="value"
display-value-member="label" labels='{"searchPlaceholder": "Try zurich, PARIS, de, io, ^(San|Santa) …"}'>
</web-multiselect>
<script type="module" data-kmwc-show>
const el = document.getElementById('ex-search-match');
// Mostly plain Latin; the accented ones (Zürich, São Paulo, …) are there to be found by
// typing WITHOUT the accent — nothing in "Things to try" needs a special keyboard.
el.options = [
'Paris', 'Lyon', 'Bordeaux', 'Marseille', 'New York', 'New Orleans', 'New Delhi',
'San Francisco', 'San Diego', 'Santa Monica', 'Santa Cruz', 'Los Angeles', 'Las Vegas',
'Rio de Janeiro', 'Buenos Aires', 'Cape Town', 'São Paulo', 'Zürich', 'München', 'Kraków',
'Málaga', 'Montréal', 'Reykjavík'
].map((label, i) => ({ value: String(i), label }));
// Both are live — flip them from the switches above and the list re-filters in place
// (same as the search-behavior / search-match attributes).
document.querySelectorAll('input[name="sm-behavior"]').forEach((radio) =>
radio.addEventListener('change', (e) => { if (e.target.checked) el.searchBehavior = e.target.value; }));
document.getElementById('sm-match').addEventListener('change', (e) => { el.searchMatch = e.target.value; });
</script>
</kmwc-demo>
<div class="note">
<p><strong>Things to try</strong> (all typeable on any keyboard):</p>
<ul>
<li><code>zurich</code> / <code>sao</code> / <code>krakow</code>: match under <code>normalized</code> (accents ignored); switch to <code>case-insensitive</code> and they're gone.</li>
<li><code>PARIS</code>: found by <code>normalized</code> and <code>case-insensitive</code>; <code>exact</code> needs <code>Paris</code>.</li>
<li><code>new york</code> with <code>equals</code>: only the whole value counts (and case doesn't), so <code>york</code> finds nothing.</li>
<li><code>de</code>: <code>contains</code> finds <em>Bordeaux</em> too; <code>word-starts-with</code> only <em>Rio de Janeiro</em> / <em>New Delhi</em>; <code>starts-with</code> nothing.</li>
<li><code>io</code>: <code>ends-with</code> finds nothing; <code>word-ends-with</code> finds <em>Rio de Janeiro</em> (the word "Rio").</li>
<li><code>^(San|Santa) </code> with <code>regex</code>. Keep typing an unfinished pattern like <code>^(San</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>^Sao</code> finds nothing; <code>^S.o </code> finds <em>São Paulo</em> (the <code>.</code> matches the <em>ã</em>).</p>
</div>
<details class="mt-1">
<summary>The same normalization for your own code — <code>normalizeSearchText()</code></summary>
<div class="code-block" data-language="js"><pre><code>import { normalizeSearchText } from '@keenmate/web-multiselect';
normalizeSearchText('Český Krumlov'); // 'cesky krumlov'</code></pre></div>
</details>
</div>
<!-- ============================================================= -->
<!-- BU09 · Preparing the term -->
<!-- ============================================================= -->
<div class="card">
<h2>BU09 · 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>
<kmwc-demo>
<web-multiselect
id="ex-part-numbers"
value-member="value"
display-value-member="label"
subtitle-member="subtitle" labels='{"searchPlaceholder": "Try ha-324/56, xk 100 2, ha32…"}'>
</web-multiselect>
<script type="module" data-kmwc-show>
const el = document.getElementById('ex-part-numbers');
el.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 }));
// "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.
const separatorAgnostic = ({ term }) =>
new RegExp(term.split(/[^a-z0-9]+/i).filter(Boolean).join('[^a-z0-9]*'), 'i');
el.getSearchTermCallback = separatorAgnostic;
// the checkbox above compares with plain search
document.getElementById('pn-term-cb').addEventListener('change', (e) => {
el.getSearchTermCallback = e.target.checked ? separatorAgnostic : null;
});
</script>
</kmwc-demo>
</div>
<!-- ============================================================= -->
<!-- BU10 · Scrolling the list -->
<!-- ============================================================= -->
<div class="card">
<h2>BU10 · Scrolling the list — <code>beforeScrollCallback</code></h2>
<p>
The dropdown scrolls an option into view on its own: a keyboard move (<code>keyboard</code>),
a navigate-mode match (<code>search</code>), the selected option of a single-select on open
(<code>open</code>) and the <code>scrollTo*()</code> methods (<code>api</code>). Only the list
moves, never the page. <code>beforeScrollCallback</code> runs first: return <code>false</code>
to skip the scroll, <code>{ block, behavior }</code> to change it, or nothing for the defaults.
Open the picker, arrow down, try the buttons — the read-out shows each call.
</p>
<kmwc-demo>
<div class="controls">
<label><input type="checkbox" id="bs-skip-keyboard"> skip <code>keyboard</code> scrolls</label>
<label><input type="checkbox" id="bs-center-api" checked> centre <code>api</code> jumps</label>
<button type="button" id="bs-jump"><code>open()</code> + <code>scrollToValue('item-40')</code></button>
</div>
<web-multiselect id="ex-before-scroll" multiple="false" value='["item-25"]'
value-member="value" display-value-member="label"></web-multiselect>
<div class="output">
<div class="output-label">beforeScrollCallback:</div>
<pre id="bs-out">(open the picker)</pre>
</div>
<script type="module" data-kmwc-show>
const el = document.getElementById('ex-before-scroll');
el.options = Array.from({ length: 60 }, (_, i) => ({ value: `item-${i}`, label: `Item ${i}` }));
const out = document.getElementById('bs-out');
el.beforeScrollCallback = ({ source, option, options }) => {
out.textContent = `${source} → ${option?.label} (default block: ${options.block})`;
if (source === 'keyboard' && document.getElementById('bs-skip-keyboard').checked) return false;
if (source === 'api' && document.getElementById('bs-center-api').checked) return { block: 'center' };
};
document.getElementById('bs-jump').addEventListener('click', () => {
el.open();
el.scrollToValue('item-40');
});
</script>
</kmwc-demo>
</div>
<script type="module">
import { attachControlsPersistence } from '@keenmate/web-components-devkit/examples';
// ── 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: '🐘', disabled: true },
{ 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' },
{ value: 'preact', label: 'Preact', icon: '⚡', year: 2015, subtitle: 'Fast 3kB alternative to React' },
{ value: 'ember', label: 'Ember.js', icon: '🐹', year: 2011, subtitle: 'A framework for ambitious web developers', disabled: true },
{ value: 'backbone', label: 'Backbone.js', icon: '🦴', year: 2010, subtitle: 'Models, views and events for structure', disabled: true },
{ value: 'alpine', label: 'Alpine.js', icon: '🏔️', year: 2019, subtitle: 'Behaviour directly in your markup' },
{ value: 'lit', label: 'Lit', icon: '🔆', year: 2019, subtitle: 'Simple, fast web components' },
{ value: 'qwik', label: 'Qwik', icon: '🚀', year: 2021, subtitle: 'Resumable apps, instant loading' },
{ value: 'htmx', label: 'htmx', icon: '🔁', year: 2020, subtitle: 'High power tools for HTML' },
{ value: 'stencil', label: 'Stencil', icon: '✏️', year: 2017, subtitle: 'Compiler for web components' },
{ value: 'mithril', label: 'Mithril', icon: '🧵', year: 2014, subtitle: 'A tiny framework for single-page apps' },
{ value: 'inferno', label: 'Inferno', icon: '🌋', year: 2016, subtitle: 'An extremely fast React-like library' }
];
// 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', disabled: true },
{ 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' }
];
// BU06's grouped list: the same groups, long enough that every group overflows the
// dropdown on its own — so each scrollTo* jump actually has to scroll.
const scrollGroupedTechnologies = [
...groupedTechnologies.filter(t => t.group === 'frontend'),
{ value: 'solid', label: 'Solid', icon: '💠', group: 'frontend' },
{ value: 'preact', label: 'Preact', icon: '⚡', group: 'frontend' },
{ value: 'lit', label: 'Lit', icon: '🔆', group: 'frontend' },
{ value: 'qwik', label: 'Qwik', icon: '🚀', group: 'frontend' },
{ value: 'ember', label: 'Ember.js', icon: '🐹', group: 'frontend' },
{ value: 'alpine', label: 'Alpine.js', icon: '🏔️', group: 'frontend' },
...groupedTechnologies.filter(t => t.group === 'backend'),
{ value: 'fastapi', label: 'FastAPI', icon: '🐍', group: 'backend' },
{ value: 'spring', label: 'Spring Boot', icon: '🌱', group: 'backend' },
{ value: 'aspnet', label: 'ASP.NET Core', icon: '🟣', group: 'backend' },
{ value: 'laravel', label: 'Laravel', icon: '🧱', group: 'backend' },
{ value: 'phoenix', label: 'Phoenix', icon: '🔥', group: 'backend' },
{ value: 'gin', label: 'Gin', icon: '🍸', group: 'backend' },
...groupedTechnologies.filter(t => t.group === 'database'),
{ value: 'mysql', label: 'MySQL', icon: '🐬', group: 'database' },
{ value: 'sqlite', label: 'SQLite', icon: '🪶', group: 'database' },
{ value: 'mariadb', label: 'MariaDB', icon: '🦭', group: 'database' },
{ value: 'cassandra', label: 'Cassandra', icon: '👁️', group: 'database' },
{ value: 'neo4j', label: 'Neo4j', icon: '🕸️', group: 'database' },
{ value: 'elastic', label: 'Elasticsearch', icon: '🔎', group: 'database' },
{ value: 'clickhouse', label: 'ClickHouse', 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 single ctx is the
// GroupLabelRenderContext (ctx.groupName + the per-group selection) — 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 = (ctx) => {
const name = ctx.groupName;
const emojis = { frontend: '🎨', backend: '🔧', database: '🗄️' };
const remaining = ctx.selectableCount - ctx.selectedCount;
const suffix = remaining > 0
? ` — ${remaining} remaining`
: ' — all selected';
const scale = 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.getElementById('sel-close-scroll').addEventListener('change', (e) =>
e.target.checked ? exSel.removeAttribute('close-on-scroll') : exSel.setAttribute('close-on-scroll', 'false'));
document.getElementById('sel-search').addEventListener('change', (e) =>
e.target.checked ? exSel.removeAttribute('enable-search') : exSel.setAttribute('enable-search', 'false'));
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);
// max-visible-badge-count 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('max-visible-badge-count', 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'
? ({ selectedCount, totalCount }) => `${selectedCount}/${totalCount}`
: null; // null → default `[x]`
}));
// ── BU06 · 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;
const row = document.createElement('div');
row.className = 'log-line';
row.textContent = msg;
scrollLogEl.appendChild(row);
scrollLogEl.scrollTop = scrollLogEl.scrollHeight;
};
if (scrollGrouped) scrollGrouped.options = scrollGroupedTechnologies;
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}`);
};
// The block option for every BU06 jump ('' = the method's default, 'start').
const scrollBlock = () => document.querySelector('input[name="scroll-block"]:checked').value;
document.querySelectorAll('input[name="scroll-behavior"]').forEach((r) =>
r.addEventListener('change', (e) => {
if (!e.target.checked) return;
for (const el of [scrollGrouped, scrollVirtual]) {
if (e.target.value) el.style.setProperty('--ms-scroll-behavior', e.target.value);
else el.style.removeProperty('--ms-scroll-behavior');
}
}));
const runScroll = (el, spec) => {
el.open();
if (spec === 'clear') { el.clearSearch(); scrollLog('clearSearch() — search reset, full list restored'); return; }
const [kind, arg] = spec.split(':');
const block = scrollBlock();
const opts = block ? { block } : undefined;
let ok, target = null;
if (kind === 'group') { ok = el.scrollToGroup(arg, opts); }
else if (kind === 'value') { ok = el.scrollToValue(arg, opts); target = arg; }
else if (kind === 'index') { ok = el.scrollToIndex(Number(arg), opts); }
const optsText = opts ? `, { block: '${block}' }` : '';
const call = kind === 'index' ? `scrollToIndex(${arg}${optsText})`
: kind === 'group' ? `scrollToGroup('${arg}'${optsText})`
: `scrollToValue('${arg}'${optsText})`;
ok.then((r) => scrollLog(`▶ ${call} → ${r}`));
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)));
// ── BU07 · Search ────────────────────────────────────────────
const exSearch = document.getElementById('ex-search');
exSearch.options = countriesWithFlags;
// The hint names Ctrl/Cmd + ↓ / ↑ only in navigate mode — the only mode with matches to jump
// between (in filter mode the list is the matches; plain ↓ / ↑ walk it).
const srchHint = document.getElementById('srch-hint');
const applySearchHint = () => {
const isNavigate = exSearch.getAttribute('search-mode') === 'navigate';
const text = !srchHint.checked ? ''
: isNavigate ? 'Ctrl/Cmd + ↓ / ↑ to jump between matches · Enter to select · Esc to clear'
: '↓ / ↑ to move · Enter to select · Esc to clear';
exSearch.labels = { ...exSearch.labels, searchHint: text };
};
document.querySelectorAll('input[name="srch-mode"]').forEach((r) =>
r.addEventListener('change', (e) => {
if (!e.target.checked) return;
exSearch.setAttribute('search-mode', e.target.value);
applySearchHint();
}));
srchHint.addEventListener('change', applySearchHint);
document.getElementById('srch-min').addEventListener('change', (e) =>
exSearch.setAttribute('min-search-length', e.target.value));
// Persist all control switches to localStorage and restore them on
// reload — re-applies to every picker via the listeners wired above.
attachControlsPersistence('basic');
});
</script>