@qingwu-ui/select 0.9.0-beta.1 → 0.9.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/style.css CHANGED
@@ -183,6 +183,30 @@
183
183
  max-height: 320px;
184
184
  overflow-y: auto;
185
185
  overscroll-behavior: contain;
186
+ /* 滚动条默认隐藏:面板开/关的 scale+opacity 动画期,滚动条在真实 GPU 合成
187
+ 会闪一下(headless 软件渲染测不到)。隐藏后动画期无滚动条渲染;hover /
188
+ 键盘聚焦时显示细滚动条,滚轮/触摸滚动不受影响。 */
189
+ scrollbar-width: none;
190
+ }
191
+ .qsel-list:hover,
192
+ .qsel-list:focus-within {
193
+ scrollbar-width: thin;
194
+ scrollbar-color: color-mix(in srgb, var(--qsel-ink-3) 35%, transparent) transparent;
195
+ }
196
+ .qsel-list::-webkit-scrollbar {
197
+ width: 6px;
198
+ display: none;
199
+ }
200
+ .qsel-list:hover::-webkit-scrollbar,
201
+ .qsel-list:focus-within::-webkit-scrollbar {
202
+ display: block;
203
+ }
204
+ .qsel-list::-webkit-scrollbar-track {
205
+ background: transparent;
206
+ }
207
+ .qsel-list::-webkit-scrollbar-thumb {
208
+ background: color-mix(in srgb, var(--qsel-ink-3) 35%, transparent);
209
+ border-radius: 3px;
186
210
  }
187
211
  .qsel-opt {
188
212
  position: relative;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qingwu-ui/select",
3
- "version": "0.9.0-beta.1",
3
+ "version": "0.9.0-beta.2",
4
4
  "description": "Framework-agnostic dropdown select for Qingwu UI — single-select, accordion stagger open animation, disabled options, zero dependencies",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -1,540 +0,0 @@
1
- (() => {
2
- // ../../icon/icons.ts
3
- var S = (d) => `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
4
- var S16 = (d) => `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
5
- var S18 = (d) => `<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
6
- var S36 = (d) => `<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
7
- var ICON_CALENDAR = S('<rect x="3" y="4" width="18" height="18" rx="2"/><path d="M8 2v4M16 2v4M3 10h18"/>');
8
- var ICON_CALENDAR_DOT = S('<rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="8" y1="14" x2="8.01" y2="14"/>');
9
- var ICON_SEARCH = S('<circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/>');
10
- var ICON_UPLOAD = S('<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/>');
11
- var ICON_UPLOAD_ARROW = S('<path d="M12 16V4"/><path d="m6 9 6-5 6 5"/><path d="M4 20h16"/>');
12
- var ICON_MENU = S('<path d="M3 7h18M3 12h18M3 17h18"/>');
13
- var ICON_BELL = S('<path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/><path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/>');
14
- var ICON_CLOSE = S('<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>');
15
- var ICON_CHECK = S('<polyline points="20 6 9 17 4 12"/>');
16
- var ICON_PLUS = S('<line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/>');
17
- var ICON_MINUS = S('<line x1="5" y1="12" x2="19" y2="12"/>');
18
- var ICON_BOX = S('<path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>');
19
- var ICON_EDIT = S('<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>');
20
- var ICON_FILE = S('<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/>');
21
- var ICON_FOLDER = S('<path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/>');
22
- var ICON_INFO = S('<circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/>');
23
- var ICON_WARNING = S('<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/>');
24
- var ICON_TRASH = S('<polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>');
25
- var ICON_DOWNLOAD = S('<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/>');
26
- var ICON_COPY = S('<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>');
27
- var ICON_EYE = S('<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/>');
28
- var ICON_STAR = S('<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>');
29
- var ICON_BOOKMARK = S('<path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/>');
30
- var ICON_EXTERNAL_LINK = S('<path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/>');
31
- var ICON_GITHUB = S('<path d="M15 22v-4a4.8 4.8 0 0 0-1-3.5c3 0 6-2 6-5.5.08-1.25-.27-2.48-1-3.5.28-1.15.28-2.35 0-3.5 0 0-1 0-3 1.5-2.64-.5-5.36-.5-8 0C6 2 5 2 5 2c-.3 1.15-.3 2.35 0 3.5A5.403 5.403 0 0 0 4 9c0 3.5 3 5.5 6 5.5-.39.49-.68 1.05-.85 1.65-.17.6-.22 1.23-.15 1.85v4"/><path d="M9 18c-4.51 2-5-2-7-2"/>');
32
- var ICON_CHEVRON_LEFT = S16('<path d="m15 18-6-6 6-6"/>');
33
- var ICON_CHEVRON_RIGHT = S16('<path d="m9 18 6-6-6-6"/>');
34
- var ICON_CHEVRON_UP = S('<polyline points="18 15 12 9 6 15"/>');
35
- var ICON_CHEVRON_DOWN = S('<polyline points="6 9 12 15 18 9"/>');
36
- var ICON_CODE = S('<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>');
37
- var ICON_FULLSCREEN = S('<polyline points="15 3 21 3 21 9"/><polyline points="9 21 3 21 3 15"/><line x1="21" y1="3" x2="14" y2="10"/><line x1="3" y1="21" x2="10" y2="14"/>');
38
- var ICON_FULLSCREEN_EXIT = S('<polyline points="4 8 4 3 9 3"/><polyline points="20 16 20 21 15 21"/><line x1="4" y1="3" x2="11" y2="10"/><line x1="20" y1="21" x2="13" y2="14"/>');
39
- var ICON_CLOCK = S('<circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/>');
40
- var ICON_TYPE = S('<polyline points="4 7 4 4 20 4 20 7"/><line x1="9" y1="20" x2="15" y2="20"/><line x1="12" y1="4" x2="12" y2="20"/>');
41
- var ICON_USERS = S('<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>');
42
- var ICON_MICROPHONE = S('<path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/><line x1="8" y1="23" x2="16" y2="23"/>');
43
- var ICON_MUSIC = S('<path d="M9 18V5l12-2v13"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/>');
44
- var ICON_VIDEO = S('<polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/>');
45
- var ICON_IMAGE = S('<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/>');
46
- var ICON_SUN = S('<circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>');
47
- var ICON_MOON = S('<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>');
48
- var ICON_SIDEBAR_TOGGLE = S('<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M9 3v18"/><path d="m14 9-3 3 3 3"/>');
49
- var ICON_LIST = S('<line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><line x1="3" y1="6" x2="3.01" y2="6"/><line x1="3" y1="12" x2="3.01" y2="12"/><line x1="3" y1="18" x2="3.01" y2="18"/>');
50
- var ICON_AI = S('<path d="M12 2l2.4 7.2h7.6l-6 4.8 2.4 7.2-6.4-4.8-6.4 4.8 2.4-7.2-6-4.8h7.6z"/>');
51
- var ICON_BOLT = S('<polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>');
52
- var ICON_REFRESH = S('<polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/>');
53
- var ICON_RETRY = S('<polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/>');
54
- var ICON_SETTINGS = S('<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/>');
55
- var ICON_HISTORY = S('<path d="M3 3v5h5"/><path d="M3.05 13A9 9 0 1 0 6.11 6.11L3 8"/><path d="M12 7v5l4 2"/>');
56
- var ICON_PUSH = S('<line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/>');
57
- var ICON_TRANSLATE = S('<path d="M5 8l6 6"/><path d="M4 14l6-6 2-3"/><path d="M2 5h12"/><path d="M7 2h1"/><path d="M22 22l-5-10-5 10"/><path d="M14 18h6"/>');
58
- var ICON_SUMMARY = S('<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/>');
59
- var ICON_RENAME = S('<path d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"/>');
60
- var ICON_SELECTION = S('<circle cx="12" cy="12" r="1"/><path d="M20 12a8 8 0 1 1-8-8"/><path d="m16 2 4 4-4 4"/>');
61
- var ICON_TAG = S('<path d="M20.59 13.41l-7.17 7.17a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"/><line x1="7" y1="7" x2="7.01" y2="7"/>');
62
- var ICON_GRIP = S('<circle cx="9" cy="12" r="1"/><circle cx="15" cy="12" r="1"/><circle cx="9" cy="6" r="1"/><circle cx="15" cy="6" r="1"/><circle cx="9" cy="18" r="1"/><circle cx="15" cy="18" r="1"/>');
63
- var ICON_DIAGRAM = S('<rect x="2" y="2" width="8" height="8" rx="1"/><rect x="14" y="2" width="8" height="8" rx="1"/><rect x="2" y="14" width="8" height="8" rx="1"/><rect x="14" y="14" width="8" height="8" rx="1"/>');
64
- var ICON_LANGUAGE = S('<circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>');
65
- var ICON_PAPERCLIP = S('<path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/>');
66
- var ICO_UPLOAD = S36('<path d="M12 16V4"/><path d="m6 9 6-5 6 5"/><path d="M4 20h16"/>');
67
- var S18c = (d) => `<svg class="qs-ico" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
68
- var S20c = (d) => `<svg class="qw-cal-svg" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
69
- var ICO_SEARCH = S18c('<circle cx="11" cy="11" r="7"/><path d="m20 20-3.2-3.2"/>');
70
- var ICO_MENU = S18('<path d="M4 7h16M4 12h16M4 17h16"/>');
71
- var ICO_CALENDAR = S20c('<rect x="3" y="4" width="18" height="18" rx="2"/><path d="M8 2v4M16 2v4M3 10h18"/>');
72
- var ICO_ARROW_LEFT = S16('<path d="m15 18-6-6 6-6"/>');
73
- var ICO_ARROW_RIGHT = S16('<path d="m9 18 6-6-6-6"/>');
74
- var SEARCH_ART = '<svg viewBox="0 0 160 140" role="img" aria-label="Search illustration">' + '<ellipse class="qs-art-shadow" cx="80" cy="124" rx="46" ry="6"/>' + '<g class="qs-art-float">' + '<rect class="qs-art-paper" x="40" y="20" width="80" height="82" rx="13"/>' + '<line class="qs-art-row-accent" x1="54" y1="42" x2="92" y2="42"/>' + '<line class="qs-art-row" x1="54" y1="56" x2="106" y2="56"/>' + '<line class="qs-art-row" x1="54" y1="70" x2="84" y2="70"/>' + '<circle class="qs-art-glass" cx="96" cy="86" r="23"/>' + '<line class="qs-art-handle" x1="113" y1="103" x2="129" y2="119"/>' + '<text class="qs-art-seal" x="96" y="94" text-anchor="middle" font-size="24">寻</text>' + "</g>" + '<circle class="qs-art-dot-a qs-art-twinkle" cx="34" cy="32" r="3"/>' + '<circle class="qs-art-dot-b qs-art-twinkle" cx="126" cy="40" r="2.6" style="animation-delay:.8s"/>' + '<circle class="qs-art-dot-c qs-art-twinkle" cx="30" cy="98" r="2.4" style="animation-delay:1.5s"/>' + "</svg>";
75
-
76
- // src/select.ts
77
- var PREFERS_REDUCED = typeof window !== "undefined" && typeof window.matchMedia === "function" ? window.matchMedia("(prefers-reduced-motion: reduce)").matches : false;
78
- var HTML_ESCAPES = {
79
- "&": "&amp;",
80
- "<": "&lt;",
81
- ">": "&gt;",
82
- '"': "&quot;"
83
- };
84
- function escapeHTML(s) {
85
- return s.replace(/[&<>"]/g, (c) => HTML_ESCAPES[c] ?? c);
86
- }
87
- function el(tag, cls, html) {
88
- const n = document.createElement(tag);
89
- if (cls)
90
- n.className = cls;
91
- if (html != null)
92
- n.innerHTML = html;
93
- return n;
94
- }
95
- var UID = 0;
96
-
97
- class Select {
98
- root;
99
- options;
100
- placeholder;
101
- customClass;
102
- width;
103
- duration;
104
- stagger;
105
- animate;
106
- maxStagger;
107
- frosted;
108
- onOpenChangeCb;
109
- onChangeCb;
110
- controlledValue;
111
- displayValue;
112
- isOpen = false;
113
- isDisabled = false;
114
- active = -1;
115
- dir = "down";
116
- closeTimer = null;
117
- trigger;
118
- valueEl;
119
- list;
120
- panel;
121
- onDocPointerDown = null;
122
- onWinScroll = null;
123
- constructor(root, opts = {}) {
124
- this.root = root;
125
- this.options = opts.options ?? [];
126
- this.placeholder = opts.placeholder ?? "";
127
- this.customClass = opts.className ?? "";
128
- this.width = opts.width ?? "trigger";
129
- this.duration = opts.duration ?? 380;
130
- this.stagger = opts.stagger ?? 28;
131
- this.animate = opts.animate !== false;
132
- this.maxStagger = opts.maxStagger ?? 12;
133
- this.frosted = opts.frosted !== false;
134
- this.onOpenChangeCb = opts.onOpenChange;
135
- this.onChangeCb = opts.onChange;
136
- this.controlledValue = "value" in opts;
137
- this.displayValue = opts.value ?? opts.defaultValue ?? null;
138
- this.isDisabled = opts.disabled === true;
139
- this.build(opts);
140
- if (opts.open === true || opts.defaultOpen === true && opts.open == null) {
141
- this.open();
142
- }
143
- }
144
- build(opts) {
145
- const ariaLabel = opts.ariaLabel ?? (this.placeholder || "请选择");
146
- this.root.classList.add("qsel");
147
- if (this.customClass)
148
- this.root.classList.add(this.customClass);
149
- this.root.classList.toggle("is-disabled", this.isDisabled);
150
- this.trigger = el("button", "qsel-trigger");
151
- this.trigger.type = "button";
152
- this.trigger.setAttribute("role", "combobox");
153
- this.trigger.setAttribute("aria-haspopup", "listbox");
154
- this.trigger.setAttribute("aria-expanded", "false");
155
- this.trigger.setAttribute("aria-label", ariaLabel);
156
- this.trigger.disabled = this.isDisabled;
157
- this.valueEl = el("span", "qsel-value");
158
- const chev = el("span", "qsel-chevron", ICON_CHEVRON_DOWN);
159
- chev.setAttribute("aria-hidden", "true");
160
- this.trigger.append(this.valueEl, chev);
161
- this.root.append(this.trigger);
162
- this.panel = el("div", "qsel-panel");
163
- this.panel.classList.toggle("is-frosted", this.frosted);
164
- this.panel.hidden = true;
165
- this.panel.id = `qsel-panel-${++UID}`;
166
- this.panel.setAttribute("role", "listbox");
167
- this.panel.setAttribute("aria-label", ariaLabel);
168
- this.list = el("ul", "qsel-list");
169
- this.panel.append(this.list);
170
- document.body.append(this.panel);
171
- this.syncValue();
172
- this.renderOptions();
173
- this.trigger.addEventListener("click", () => {
174
- if (this.isDisabled)
175
- return;
176
- this.isOpen ? this.close() : this.open();
177
- });
178
- this.trigger.addEventListener("keydown", (e) => this.onTriggerKey(e));
179
- this.list.addEventListener("pointerdown", (e) => e.preventDefault());
180
- this.list.addEventListener("click", (e) => {
181
- const opt = e.target.closest(".qsel-opt");
182
- if (opt)
183
- this.select(Number(opt.dataset.index));
184
- });
185
- }
186
- renderOptions() {
187
- this.list.textContent = "";
188
- const frag = document.createDocumentFragment();
189
- const shouldStagger = this.shouldStagger();
190
- this.options.forEach((opt, i) => {
191
- const li = el("li", "qsel-opt");
192
- li.dataset.index = String(i);
193
- li.id = `qsel-opt-${i}`;
194
- li.setAttribute("role", "option");
195
- li.setAttribute("aria-selected", String(opt.value === this.displayValue));
196
- if (opt.disabled) {
197
- li.classList.add("is-disabled");
198
- li.setAttribute("aria-disabled", "true");
199
- }
200
- if (shouldStagger)
201
- li.classList.add("is-enter");
202
- const glyph = opt.glyph ?? opt.label.slice(0, 1);
203
- li.innerHTML = `<span class="qsel-opt-ico">${escapeHTML(glyph)}</span>` + `<span class="qsel-opt-main"><span class="qsel-opt-label">${escapeHTML(opt.label)}</span>` + (opt.hint ? `<span class="qsel-opt-hint">${escapeHTML(opt.hint)}</span>` : "") + `</span>` + `<span class="qsel-check" aria-hidden="true">${ICON_CHECK}</span>`;
204
- frag.append(li);
205
- });
206
- this.list.append(frag);
207
- }
208
- shouldStagger() {
209
- return this.animate && !PREFERS_REDUCED && this.options.length <= this.maxStagger;
210
- }
211
- syncValue() {
212
- const opt = this.currentOption;
213
- this.valueEl.textContent = opt ? opt.label : this.placeholder;
214
- this.valueEl.classList.toggle("is-placeholder", !opt);
215
- }
216
- get currentOption() {
217
- return this.options.find((o) => o.value === this.displayValue) ?? null;
218
- }
219
- open() {
220
- if (this.isOpen || this.isDisabled)
221
- return;
222
- this.isOpen = true;
223
- this.renderOptions();
224
- this.panel.hidden = false;
225
- this.root.classList.add("is-open");
226
- this.trigger.setAttribute("aria-expanded", "true");
227
- this.position();
228
- this.setActive(this.startIndex(), false);
229
- this.applyStagger();
230
- requestAnimationFrame(() => this.panel.classList.add("is-open"));
231
- this.onDocPointerDown = (e) => {
232
- if (this.isOpen && !this.root.contains(e.target))
233
- this.close();
234
- };
235
- document.addEventListener("pointerdown", this.onDocPointerDown);
236
- this.onWinScroll = () => this.position();
237
- window.addEventListener("scroll", this.onWinScroll, true);
238
- window.addEventListener("resize", this.onWinScroll);
239
- this.onOpenChangeCb?.(true);
240
- }
241
- close() {
242
- if (!this.isOpen)
243
- return;
244
- this.isOpen = false;
245
- this.root.classList.remove("is-open");
246
- this.panel.classList.remove("is-open");
247
- this.trigger.setAttribute("aria-expanded", "false");
248
- this.trigger.removeAttribute("aria-activedescendant");
249
- this.active = -1;
250
- if (this.onDocPointerDown) {
251
- document.removeEventListener("pointerdown", this.onDocPointerDown);
252
- this.onDocPointerDown = null;
253
- }
254
- if (this.onWinScroll) {
255
- window.removeEventListener("scroll", this.onWinScroll, true);
256
- window.removeEventListener("resize", this.onWinScroll);
257
- this.onWinScroll = null;
258
- }
259
- if (this.closeTimer !== null)
260
- clearTimeout(this.closeTimer);
261
- this.closeTimer = setTimeout(() => {
262
- if (!this.isOpen)
263
- this.panel.hidden = true;
264
- }, PREFERS_REDUCED ? 0 : this.duration);
265
- this.onOpenChangeCb?.(false);
266
- }
267
- toggle() {
268
- this.isOpen ? this.close() : this.open();
269
- }
270
- position() {
271
- const tr = this.trigger.getBoundingClientRect();
272
- const gap = 8;
273
- this.panel.style.width = this.width === "auto" ? "max-content" : `${tr.width}px`;
274
- if (this.width === "auto") {
275
- this.panel.style.minWidth = `${tr.width}px`;
276
- }
277
- this.panel.style.visibility = "hidden";
278
- const panelH = this.panel.offsetHeight;
279
- const panelW = this.panel.offsetWidth;
280
- const spaceBelow = window.innerHeight - tr.bottom;
281
- const spaceAbove = tr.top;
282
- const flipUp = spaceBelow - gap < panelH && spaceAbove > spaceBelow;
283
- this.dir = flipUp ? "up" : "down";
284
- this.panel.classList.toggle("is-up", flipUp);
285
- let left = tr.left;
286
- if (this.width === "auto" && panelW > window.innerWidth - 16) {
287
- left = Math.max(8, window.innerWidth - panelW - 8);
288
- }
289
- this.panel.style.left = `${Math.max(8, left)}px`;
290
- if (flipUp) {
291
- this.panel.style.top = "auto";
292
- this.panel.style.bottom = `${Math.max(8, window.innerHeight - tr.top + gap)}px`;
293
- } else {
294
- this.panel.style.top = `${tr.bottom + gap}px`;
295
- this.panel.style.bottom = "auto";
296
- }
297
- this.panel.style.visibility = "";
298
- }
299
- applyStagger() {
300
- if (!this.shouldStagger())
301
- return;
302
- const opts = this.list.querySelectorAll(".qsel-opt.is-enter");
303
- const n = opts.length;
304
- opts.forEach((o, i) => {
305
- const delay = this.dir === "up" ? (n - 1 - i) * this.stagger : i * this.stagger;
306
- o.style.animationDelay = `${delay}ms`;
307
- o.style.animationDuration = `${this.duration}ms`;
308
- });
309
- }
310
- select(index) {
311
- const opt = this.options[index];
312
- if (!opt || opt.disabled)
313
- return;
314
- if (!this.controlledValue) {
315
- this.displayValue = opt.value;
316
- this.syncValue();
317
- }
318
- this.onChangeCb?.(opt.value, opt);
319
- this.close();
320
- }
321
- setActive(idx, scroll = true) {
322
- const opts = this.list.querySelectorAll(".qsel-opt");
323
- if (!opts.length)
324
- return;
325
- this.active = Math.max(0, Math.min(idx, opts.length - 1));
326
- opts.forEach((o, i) => {
327
- const on = i === this.active;
328
- o.classList.toggle("is-active", on);
329
- if (on) {
330
- this.trigger.setAttribute("aria-activedescendant", o.id);
331
- if (scroll)
332
- o.scrollIntoView({ block: "nearest" });
333
- }
334
- });
335
- }
336
- onTriggerKey(e) {
337
- if (this.isDisabled)
338
- return;
339
- switch (e.key) {
340
- case "ArrowDown":
341
- e.preventDefault();
342
- if (!this.isOpen)
343
- this.open();
344
- else
345
- this.moveActive(1);
346
- break;
347
- case "ArrowUp":
348
- e.preventDefault();
349
- if (!this.isOpen)
350
- this.open();
351
- else
352
- this.moveActive(-1);
353
- break;
354
- case "Home":
355
- if (this.isOpen) {
356
- e.preventDefault();
357
- this.moveActive(Number.MIN_SAFE_INTEGER);
358
- }
359
- break;
360
- case "End":
361
- if (this.isOpen) {
362
- e.preventDefault();
363
- this.moveActive(Number.MAX_SAFE_INTEGER);
364
- }
365
- break;
366
- case "Enter":
367
- case " ":
368
- e.preventDefault();
369
- if (this.isOpen) {
370
- if (this.active >= 0)
371
- this.select(this.active);
372
- } else {
373
- this.open();
374
- }
375
- break;
376
- case "Escape":
377
- if (this.isOpen) {
378
- e.preventDefault();
379
- this.close();
380
- }
381
- break;
382
- case "Tab":
383
- if (this.isOpen)
384
- this.close();
385
- break;
386
- }
387
- }
388
- nextEnabled(from, dir) {
389
- const n = this.options.length;
390
- if (!n)
391
- return -1;
392
- for (let step = 1;step <= n; step++) {
393
- const i = (from + dir * step + n) % n;
394
- const o = this.options[i];
395
- if (o && !o.disabled)
396
- return i;
397
- }
398
- return -1;
399
- }
400
- firstEnabled() {
401
- return this.options.findIndex((o) => !o.disabled);
402
- }
403
- lastEnabled() {
404
- for (let i = this.options.length - 1;i >= 0; i--) {
405
- const o = this.options[i];
406
- if (o && !o.disabled)
407
- return i;
408
- }
409
- return -1;
410
- }
411
- startIndex() {
412
- const idx = this.options.findIndex((o) => o.value === this.displayValue && !o.disabled);
413
- return idx >= 0 ? idx : this.firstEnabled();
414
- }
415
- moveActive(delta) {
416
- if (delta === Number.MIN_SAFE_INTEGER) {
417
- const i = this.firstEnabled();
418
- if (i >= 0)
419
- this.setActive(i);
420
- return;
421
- }
422
- if (delta === Number.MAX_SAFE_INTEGER) {
423
- const i = this.lastEnabled();
424
- if (i >= 0)
425
- this.setActive(i);
426
- return;
427
- }
428
- const from = this.active < 0 ? this.startIndex() : this.active;
429
- const next = this.nextEnabled(from, delta > 0 ? 1 : -1);
430
- if (next >= 0)
431
- this.setActive(next);
432
- }
433
- get value() {
434
- return this.displayValue;
435
- }
436
- get expanded() {
437
- return this.isOpen;
438
- }
439
- update(patch) {
440
- if ("options" in patch && patch.options) {
441
- this.options = patch.options;
442
- this.renderOptions();
443
- this.syncValue();
444
- }
445
- if ("value" in patch) {
446
- this.displayValue = patch.value ?? null;
447
- this.syncValue();
448
- if (this.isOpen)
449
- this.renderOptions();
450
- }
451
- if ("disabled" in patch && patch.disabled !== undefined) {
452
- this.setDisabled(patch.disabled);
453
- }
454
- if ("open" in patch && patch.open !== undefined) {
455
- patch.open ? this.open() : this.close();
456
- }
457
- if ("placeholder" in patch && patch.placeholder !== undefined) {
458
- this.placeholder = patch.placeholder;
459
- this.syncValue();
460
- }
461
- if ("ariaLabel" in patch && patch.ariaLabel !== undefined) {
462
- this.trigger.setAttribute("aria-label", patch.ariaLabel);
463
- }
464
- if ("frosted" in patch && patch.frosted !== undefined) {
465
- this.frosted = patch.frosted;
466
- this.panel.classList.toggle("is-frosted", this.frosted);
467
- }
468
- }
469
- setValue(value) {
470
- this.update({ value });
471
- }
472
- setDisabled(v) {
473
- this.isDisabled = v;
474
- this.root.classList.toggle("is-disabled", v);
475
- this.trigger.disabled = v;
476
- if (v)
477
- this.close();
478
- }
479
- destroy() {
480
- if (this.closeTimer !== null)
481
- clearTimeout(this.closeTimer);
482
- this.close();
483
- this.panel.remove();
484
- this.root.classList.remove("qsel", "is-open", "is-disabled");
485
- if (this.customClass)
486
- this.root.classList.remove(this.customClass);
487
- this.root.textContent = "";
488
- }
489
- }
490
-
491
- // src/demo-entry.ts
492
- window.Select = Select;
493
- function mountDemo() {
494
- const host = document.querySelector("#qsel-demo");
495
- if (!host)
496
- return;
497
- host.insertAdjacentHTML("beforeend", `<style>
498
- #qsel-demo { display: flex; gap: 28px; flex-wrap: wrap; align-items: flex-start; }
499
- #qsel-demo .col { width: 240px; }
500
- #qsel-demo h2 { margin: 0 0 4px; font-size: 15px; color: var(--qsel-ink, #1d2b2c); }
501
- #qsel-demo .hint { margin: 0 0 10px; font-size: 12px; color: var(--qsel-ink-3, #84928f); }
502
- #qsel-demo .toggle { margin-top: 18px; padding: 9px 16px; font-size: 13px; cursor: pointer;
503
- border: 1.5px solid var(--qsel-line, #dcdfd6); border-radius: 12px;
504
- background: var(--qsel-card, #fff); color: var(--qsel-ink, #1d2b2c); }
505
- #qsel-demo .toggle:hover { border-color: var(--qsel-teal, #1e605a); }
506
- </style>
507
- <div class="col">
508
- <h2>磨砂面板</h2>
509
- <p class="hint">frosted 默认开启 · 半透明底 + backdrop-filter 毛玻璃</p>
510
- <div id="qsel-frosted"></div>
511
- <button type="button" class="toggle" id="qsel-toggle">切换为不透明 →</button>
512
- </div>
513
- <div class="col">
514
- <h2>不透明面板</h2>
515
- <p class="hint">frosted: false · 实体面板</p>
516
- <div id="qsel-opaque"></div>
517
- </div>`);
518
- const options = [
519
- { value: "react", label: "React", hint: "框架无关 · 原生 DOM" },
520
- { value: "vue", label: "Vue", hint: "new Select(el) 即用" },
521
- { value: "svelte", label: "Svelte" },
522
- { value: "ember", label: "Ember", disabled: true }
523
- ];
524
- const frosted = new Select(host.querySelector("#qsel-frosted"), { placeholder: "磨砂面板(默认)", options, width: "trigger" });
525
- new Select(host.querySelector("#qsel-opaque"), {
526
- placeholder: "不透明面板",
527
- options,
528
- width: "trigger",
529
- frosted: false
530
- });
531
- let isFrosted = true;
532
- host.querySelector("#qsel-toggle").addEventListener("click", () => {
533
- isFrosted = !isFrosted;
534
- frosted.update({ frosted: isFrosted });
535
- const btn = host.querySelector("#qsel-toggle");
536
- btn.textContent = isFrosted ? "切换为不透明 →" : "切换为磨砂 →";
537
- });
538
- }
539
- mountDemo();
540
- })();