@qingwu-ui/select 0.9.0-beta

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.
@@ -0,0 +1,118 @@
1
+ //#region src/types.d.ts
2
+ /** 单个下拉选项 */
3
+ interface SelectOption {
4
+ /** 唯一值(必填,选中后作为 value 返回) */
5
+ value: string;
6
+ /** 显示文本(必填) */
7
+ label: string;
8
+ /** 禁用:置灰、不可选中、键盘导航跳过 */
9
+ disabled?: boolean;
10
+ /** 辅助说明(可选,渲染在 label 下方小字) */
11
+ hint?: string;
12
+ /** 左侧图标字符,默认取 label 首字 */
13
+ glyph?: string;
14
+ }
15
+ /** 下拉选择器构造配置 */
16
+ interface SelectOptions {
17
+ /** 选项列表 */
18
+ options?: SelectOption[];
19
+ /** 受控值(传入后进入受控模式:用户选择仅回调 onChange,显示值由外部经 update({ value }) 同步) */
20
+ value?: string | null;
21
+ /** 非受控初始值 */
22
+ defaultValue?: string | null;
23
+ /** 未选中时的占位文本 */
24
+ placeholder?: string;
25
+ /** 整体禁用 */
26
+ disabled?: boolean;
27
+ /** 受控展开 */
28
+ open?: boolean;
29
+ /** 非受控初始展开 */
30
+ defaultOpen?: boolean;
31
+ /** 附加到根容器的自定义类名 */
32
+ className?: string;
33
+ /** 面板宽度:trigger 跟随触发器宽度 / auto 内容自适应(min-width 至少等于触发器) */
34
+ width?: "trigger" | "auto";
35
+ /** 单个选项错峰动画时长 ms,默认 380 */
36
+ duration?: number;
37
+ /** 选项错峰间隔 ms,默认 28 */
38
+ stagger?: number;
39
+ /** 是否启用手风琴错峰动画,默认 true(自动尊重 prefers-reduced-motion) */
40
+ animate?: boolean;
41
+ /** 错峰动画最大选项数:超过即降级为面板整体淡入(性能/体验保护),0 表示不降级,默认 12 */
42
+ maxStagger?: number;
43
+ /** 无障碍标签(trigger aria-label),缺省取 placeholder */
44
+ ariaLabel?: string;
45
+ /** 展开状态变化回调 */
46
+ onOpenChange?: (open: boolean) => void;
47
+ /** 选中值变化回调(取消为 null) */
48
+ onChange?: (value: string | null, option: SelectOption | null) => void;
49
+ }
50
+ //#endregion
51
+ //#region src/select.d.ts
52
+ declare class Select {
53
+ private root;
54
+ private options;
55
+ private placeholder;
56
+ private customClass;
57
+ private readonly width;
58
+ private readonly duration;
59
+ private readonly stagger;
60
+ private readonly animate;
61
+ private readonly maxStagger;
62
+ private readonly onOpenChangeCb?;
63
+ private readonly onChangeCb?;
64
+ private readonly controlledValue;
65
+ private displayValue;
66
+ private isOpen;
67
+ private isDisabled;
68
+ private active;
69
+ private dir;
70
+ private closeTimer;
71
+ private trigger;
72
+ private valueEl;
73
+ private list;
74
+ private panel;
75
+ private onDocPointerDown;
76
+ private onWinScroll;
77
+ constructor(root: HTMLElement, opts?: SelectOptions);
78
+ private build;
79
+ private renderOptions;
80
+ /** 是否启用逐项错峰动画(尊重 reduced-motion 与超大列表降级) */
81
+ private shouldStagger;
82
+ private syncValue;
83
+ private get currentOption();
84
+ /** 打开下拉面板 */
85
+ open(): void;
86
+ /** 关闭下拉面板 */
87
+ close(): void;
88
+ /** 切换展开状态 */
89
+ toggle(): void;
90
+ /** 计算面板位置:宽度跟随触发器、向上/向下翻转、错峰方向随之反向 */
91
+ private position;
92
+ /** 错峰动画:按展开方向给每个选项设置递增 / 递减延迟 */
93
+ private applyStagger;
94
+ private select;
95
+ private setActive;
96
+ private onTriggerKey;
97
+ /** 在可用选项中移动(跳过禁用项,支持环绕) */
98
+ private nextEnabled;
99
+ private firstEnabled;
100
+ private lastEnabled;
101
+ private startIndex;
102
+ private moveActive;
103
+ /** 当前选中值(无选中为 null) */
104
+ get value(): string | null;
105
+ /** 是否展开 */
106
+ get expanded(): boolean;
107
+ /** 外部同步 / 更新配置。value 同步显示但不触发 onChange(UI 选择才会回调) */
108
+ update(patch: Partial<SelectOptions>): void;
109
+ /** 程序化选中(等价 update({ value })) */
110
+ setValue(value: string | null): void;
111
+ /** 动态切换整体禁用 */
112
+ setDisabled(v: boolean): void;
113
+ /** 销毁组件,清空宿主容器并移除 body 上的面板 */
114
+ destroy(): void;
115
+ }
116
+ //#endregion
117
+ export { Select, type SelectOption, type SelectOptions };
118
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/select.ts"],"mappings":";;UAMiB;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA;;;UAIe;;EAEf,UAAU;;EAEV;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA,gBAAgB;;EAEhB,YAAY,sBAAsB,QAAQ;;;;cChB/B;UAEH;UACA;UACA;UACA;mBACS;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBAGA;UACT;UAGA;UACA;UACA;UACA;UACA;UAGA;UACA;UACA;UACA;UAGA;UACA;EAER,YAAY,MAAM,aAAa,OAAM;UA2B7B;UAoDA;;UA8BA;UAIA;cAMI;;EAQZ;;EA2BA;;EA+BA;;UAKQ;;UAqCA;UAcA;UAYA;UAiBA;;UA+CA;UAWA;UAIA;UAQA;UAKA;;MAqBJ;;MAKA;;EAKJ,OAAO,OAAO,QAAQ;;EA2BtB,SAAS;;EAKT,YAAY;;EAQZ"}
package/dist/index.mjs ADDED
@@ -0,0 +1,449 @@
1
+ //#region ../../icon/icons.ts
2
+ const 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>`;
3
+ const 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>`;
4
+ const 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>`;
5
+ const 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>`;
6
+ S("<rect x=\"3\" y=\"4\" width=\"18\" height=\"18\" rx=\"2\"/><path d=\"M8 2v4M16 2v4M3 10h18\"/>");
7
+ 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\"/>");
8
+ S("<circle cx=\"11\" cy=\"11\" r=\"8\"/><path d=\"m21 21-4.35-4.35\"/>");
9
+ 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\"/>");
10
+ S("<path d=\"M12 16V4\"/><path d=\"m6 9 6-5 6 5\"/><path d=\"M4 20h16\"/>");
11
+ S("<path d=\"M3 7h18M3 12h18M3 17h18\"/>");
12
+ 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\"/>");
13
+ S("<line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"/><line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"/>");
14
+ const ICON_CHECK = S("<polyline points=\"20 6 9 17 4 12\"/>");
15
+ S("<line x1=\"12\" y1=\"5\" x2=\"12\" y2=\"19\"/><line x1=\"5\" y1=\"12\" x2=\"19\" y2=\"12\"/>");
16
+ S("<line x1=\"5\" y1=\"12\" x2=\"19\" y2=\"12\"/>");
17
+ 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\"/>");
18
+ 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\"/>");
19
+ 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\"/>");
20
+ 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\"/>");
21
+ 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\"/>");
22
+ 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\"/>");
23
+ 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\"/>");
24
+ 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\"/>");
25
+ 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\"/>");
26
+ 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\"/>");
27
+ 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\"/>");
28
+ S("<path d=\"M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z\"/>");
29
+ 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\"/>");
30
+ 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\"/>");
31
+ S16("<path d=\"m15 18-6-6 6-6\"/>");
32
+ S16("<path d=\"m9 18 6-6-6-6\"/>");
33
+ S("<polyline points=\"18 15 12 9 6 15\"/>");
34
+ const ICON_CHEVRON_DOWN = S("<polyline points=\"6 9 12 15 18 9\"/>");
35
+ S("<polyline points=\"16 18 22 12 16 6\"/><polyline points=\"8 6 2 12 8 18\"/>");
36
+ 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\"/>");
37
+ 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\"/>");
38
+ S("<circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"M12 6v6l4 2\"/>");
39
+ 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\"/>");
40
+ 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\"/>");
41
+ 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\"/>");
42
+ S("<path d=\"M9 18V5l12-2v13\"/><circle cx=\"6\" cy=\"18\" r=\"3\"/><circle cx=\"18\" cy=\"16\" r=\"3\"/>");
43
+ S("<polygon points=\"23 7 16 12 23 17 23 7\"/><rect x=\"1\" y=\"5\" width=\"15\" height=\"14\" rx=\"2\" ry=\"2\"/>");
44
+ 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\"/>");
45
+ 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\"/>");
46
+ S("<path d=\"M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z\"/>");
47
+ S("<rect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\"/><path d=\"M9 3v18\"/><path d=\"m14 9-3 3 3 3\"/>");
48
+ 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\"/>");
49
+ 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\"/>");
50
+ S("<polygon points=\"13 2 3 14 12 14 11 22 21 10 12 10 13 2\"/>");
51
+ 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\"/>");
52
+ S("<polyline points=\"1 4 1 10 7 10\"/><path d=\"M3.51 15a9 9 0 1 0 2.13-9.36L1 10\"/>");
53
+ 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\"/>");
54
+ S("<path d=\"M3 3v5h5\"/><path d=\"M3.05 13A9 9 0 1 0 6.11 6.11L3 8\"/><path d=\"M12 7v5l4 2\"/>");
55
+ S("<line x1=\"22\" y1=\"2\" x2=\"11\" y2=\"13\"/><polygon points=\"22 2 15 22 11 13 2 9 22 2\"/>");
56
+ 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\"/>");
57
+ 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\"/>");
58
+ S("<path d=\"M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z\"/>");
59
+ 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\"/>");
60
+ 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\"/>");
61
+ 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\"/>");
62
+ 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\"/>");
63
+ 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\"/>");
64
+ 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\"/>");
65
+ S36("<path d=\"M12 16V4\"/><path d=\"m6 9 6-5 6 5\"/><path d=\"M4 20h16\"/>");
66
+ const 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>`;
67
+ const 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>`;
68
+ S18c("<circle cx=\"11\" cy=\"11\" r=\"7\"/><path d=\"m20 20-3.2-3.2\"/>");
69
+ S18("<path d=\"M4 7h16M4 12h16M4 17h16\"/>");
70
+ S20c("<rect x=\"3\" y=\"4\" width=\"18\" height=\"18\" rx=\"2\"/><path d=\"M8 2v4M16 2v4M3 10h18\"/>");
71
+ S16("<path d=\"m15 18-6-6 6-6\"/>");
72
+ S16("<path d=\"m9 18 6-6-6-6\"/>");
73
+ //#endregion
74
+ //#region src/select.ts
75
+ const PREFERS_REDUCED = typeof window !== "undefined" && typeof window.matchMedia === "function" ? window.matchMedia("(prefers-reduced-motion: reduce)").matches : false;
76
+ const HTML_ESCAPES = {
77
+ "&": "&amp;",
78
+ "<": "&lt;",
79
+ ">": "&gt;",
80
+ "\"": "&quot;"
81
+ };
82
+ function escapeHTML(s) {
83
+ return s.replace(/[&<>"]/g, (c) => HTML_ESCAPES[c] ?? c);
84
+ }
85
+ function el(tag, cls, html) {
86
+ const n = document.createElement(tag);
87
+ if (cls) n.className = cls;
88
+ if (html != null) n.innerHTML = html;
89
+ return n;
90
+ }
91
+ let UID = 0;
92
+ var Select = class {
93
+ root;
94
+ options;
95
+ placeholder;
96
+ customClass;
97
+ width;
98
+ duration;
99
+ stagger;
100
+ animate;
101
+ maxStagger;
102
+ onOpenChangeCb;
103
+ onChangeCb;
104
+ controlledValue;
105
+ displayValue;
106
+ isOpen = false;
107
+ isDisabled = false;
108
+ active = -1;
109
+ dir = "down";
110
+ closeTimer = null;
111
+ trigger;
112
+ valueEl;
113
+ list;
114
+ panel;
115
+ onDocPointerDown = null;
116
+ onWinScroll = null;
117
+ constructor(root, opts = {}) {
118
+ this.root = root;
119
+ this.options = opts.options ?? [];
120
+ this.placeholder = opts.placeholder ?? "";
121
+ this.customClass = opts.className ?? "";
122
+ this.width = opts.width ?? "trigger";
123
+ this.duration = opts.duration ?? 380;
124
+ this.stagger = opts.stagger ?? 28;
125
+ this.animate = opts.animate !== false;
126
+ this.maxStagger = opts.maxStagger ?? 12;
127
+ this.onOpenChangeCb = opts.onOpenChange;
128
+ this.onChangeCb = opts.onChange;
129
+ this.controlledValue = "value" in opts;
130
+ this.displayValue = opts.value ?? opts.defaultValue ?? null;
131
+ this.isDisabled = opts.disabled === true;
132
+ this.build(opts);
133
+ if (opts.open === true || opts.defaultOpen === true && opts.open == null) this.open();
134
+ }
135
+ build(opts) {
136
+ const ariaLabel = opts.ariaLabel ?? (this.placeholder || "请选择");
137
+ this.root.classList.add("qsel");
138
+ if (this.customClass) this.root.classList.add(this.customClass);
139
+ this.root.classList.toggle("is-disabled", this.isDisabled);
140
+ this.trigger = el("button", "qsel-trigger");
141
+ this.trigger.type = "button";
142
+ this.trigger.setAttribute("role", "combobox");
143
+ this.trigger.setAttribute("aria-haspopup", "listbox");
144
+ this.trigger.setAttribute("aria-expanded", "false");
145
+ this.trigger.setAttribute("aria-label", ariaLabel);
146
+ this.trigger.disabled = this.isDisabled;
147
+ this.valueEl = el("span", "qsel-value");
148
+ const chev = el("span", "qsel-chevron", ICON_CHEVRON_DOWN);
149
+ chev.setAttribute("aria-hidden", "true");
150
+ this.trigger.append(this.valueEl, chev);
151
+ this.root.append(this.trigger);
152
+ this.panel = el("div", "qsel-panel");
153
+ this.panel.hidden = true;
154
+ this.panel.id = `qsel-panel-${++UID}`;
155
+ this.panel.setAttribute("role", "listbox");
156
+ this.panel.setAttribute("aria-label", ariaLabel);
157
+ this.list = el("ul", "qsel-list");
158
+ this.panel.append(this.list);
159
+ document.body.append(this.panel);
160
+ this.syncValue();
161
+ this.renderOptions();
162
+ this.trigger.addEventListener("click", () => {
163
+ if (this.isDisabled) return;
164
+ this.isOpen ? this.close() : this.open();
165
+ });
166
+ this.trigger.addEventListener("keydown", (e) => this.onTriggerKey(e));
167
+ this.list.addEventListener("pointerdown", (e) => e.preventDefault());
168
+ this.list.addEventListener("click", (e) => {
169
+ const opt = e.target.closest(".qsel-opt");
170
+ if (opt) this.select(Number(opt.dataset.index));
171
+ });
172
+ }
173
+ renderOptions() {
174
+ this.list.textContent = "";
175
+ const frag = document.createDocumentFragment();
176
+ const shouldStagger = this.shouldStagger();
177
+ this.options.forEach((opt, i) => {
178
+ const li = el("li", "qsel-opt");
179
+ li.dataset.index = String(i);
180
+ li.id = `qsel-opt-${i}`;
181
+ li.setAttribute("role", "option");
182
+ li.setAttribute("aria-selected", String(opt.value === this.displayValue));
183
+ if (opt.disabled) {
184
+ li.classList.add("is-disabled");
185
+ li.setAttribute("aria-disabled", "true");
186
+ }
187
+ if (shouldStagger) li.classList.add("is-enter");
188
+ li.innerHTML = `<span class="qsel-opt-ico">${escapeHTML(opt.glyph ?? opt.label.slice(0, 1))}</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>`;
189
+ frag.append(li);
190
+ });
191
+ this.list.append(frag);
192
+ }
193
+ /** 是否启用逐项错峰动画(尊重 reduced-motion 与超大列表降级) */
194
+ shouldStagger() {
195
+ return this.animate && !PREFERS_REDUCED && this.options.length <= this.maxStagger;
196
+ }
197
+ syncValue() {
198
+ const opt = this.currentOption;
199
+ this.valueEl.textContent = opt ? opt.label : this.placeholder;
200
+ this.valueEl.classList.toggle("is-placeholder", !opt);
201
+ }
202
+ get currentOption() {
203
+ return this.options.find((o) => o.value === this.displayValue) ?? null;
204
+ }
205
+ /** 打开下拉面板 */
206
+ open() {
207
+ if (this.isOpen || this.isDisabled) return;
208
+ this.isOpen = true;
209
+ this.renderOptions();
210
+ this.panel.hidden = false;
211
+ this.root.classList.add("is-open");
212
+ this.trigger.setAttribute("aria-expanded", "true");
213
+ this.position();
214
+ this.setActive(this.startIndex(), false);
215
+ this.applyStagger();
216
+ requestAnimationFrame(() => this.panel.classList.add("is-open"));
217
+ this.onDocPointerDown = (e) => {
218
+ if (this.isOpen && !this.root.contains(e.target)) this.close();
219
+ };
220
+ document.addEventListener("pointerdown", this.onDocPointerDown);
221
+ this.onWinScroll = () => this.position();
222
+ window.addEventListener("scroll", this.onWinScroll, true);
223
+ window.addEventListener("resize", this.onWinScroll);
224
+ this.onOpenChangeCb?.(true);
225
+ }
226
+ /** 关闭下拉面板 */
227
+ close() {
228
+ if (!this.isOpen) return;
229
+ this.isOpen = false;
230
+ this.root.classList.remove("is-open");
231
+ this.panel.classList.remove("is-open");
232
+ this.trigger.setAttribute("aria-expanded", "false");
233
+ this.trigger.removeAttribute("aria-activedescendant");
234
+ this.active = -1;
235
+ if (this.onDocPointerDown) {
236
+ document.removeEventListener("pointerdown", this.onDocPointerDown);
237
+ this.onDocPointerDown = null;
238
+ }
239
+ if (this.onWinScroll) {
240
+ window.removeEventListener("scroll", this.onWinScroll, true);
241
+ window.removeEventListener("resize", this.onWinScroll);
242
+ this.onWinScroll = null;
243
+ }
244
+ if (this.closeTimer !== null) clearTimeout(this.closeTimer);
245
+ this.closeTimer = setTimeout(() => {
246
+ if (!this.isOpen) this.panel.hidden = true;
247
+ }, PREFERS_REDUCED ? 0 : this.duration);
248
+ this.onOpenChangeCb?.(false);
249
+ }
250
+ /** 切换展开状态 */
251
+ toggle() {
252
+ this.isOpen ? this.close() : this.open();
253
+ }
254
+ /** 计算面板位置:宽度跟随触发器、向上/向下翻转、错峰方向随之反向 */
255
+ position() {
256
+ const tr = this.trigger.getBoundingClientRect();
257
+ const gap = 8;
258
+ this.panel.style.width = this.width === "auto" ? "max-content" : `${tr.width}px`;
259
+ if (this.width === "auto") this.panel.style.minWidth = `${tr.width}px`;
260
+ this.panel.style.visibility = "hidden";
261
+ const panelH = this.panel.offsetHeight;
262
+ const panelW = this.panel.offsetWidth;
263
+ const spaceBelow = window.innerHeight - tr.bottom;
264
+ const spaceAbove = tr.top;
265
+ const flipUp = spaceBelow - gap < panelH && spaceAbove > spaceBelow;
266
+ this.dir = flipUp ? "up" : "down";
267
+ this.panel.classList.toggle("is-up", flipUp);
268
+ let left = tr.left;
269
+ if (this.width === "auto" && panelW > window.innerWidth - 16) left = Math.max(8, window.innerWidth - panelW - 8);
270
+ this.panel.style.left = `${Math.max(8, left)}px`;
271
+ if (flipUp) {
272
+ this.panel.style.top = "auto";
273
+ this.panel.style.bottom = `${Math.max(8, window.innerHeight - tr.top + gap)}px`;
274
+ } else {
275
+ this.panel.style.top = `${tr.bottom + gap}px`;
276
+ this.panel.style.bottom = "auto";
277
+ }
278
+ this.panel.style.visibility = "";
279
+ }
280
+ /** 错峰动画:按展开方向给每个选项设置递增 / 递减延迟 */
281
+ applyStagger() {
282
+ if (!this.shouldStagger()) return;
283
+ const opts = this.list.querySelectorAll(".qsel-opt.is-enter");
284
+ const n = opts.length;
285
+ opts.forEach((o, i) => {
286
+ const delay = this.dir === "up" ? (n - 1 - i) * this.stagger : i * this.stagger;
287
+ o.style.animationDelay = `${delay}ms`;
288
+ o.style.animationDuration = `${this.duration}ms`;
289
+ });
290
+ }
291
+ select(index) {
292
+ const opt = this.options[index];
293
+ if (!opt || opt.disabled) return;
294
+ if (!this.controlledValue) {
295
+ this.displayValue = opt.value;
296
+ this.syncValue();
297
+ }
298
+ this.onChangeCb?.(opt.value, opt);
299
+ this.close();
300
+ }
301
+ setActive(idx, scroll = true) {
302
+ const opts = this.list.querySelectorAll(".qsel-opt");
303
+ if (!opts.length) return;
304
+ this.active = Math.max(0, Math.min(idx, opts.length - 1));
305
+ opts.forEach((o, i) => {
306
+ const on = i === this.active;
307
+ o.classList.toggle("is-active", on);
308
+ if (on) {
309
+ this.trigger.setAttribute("aria-activedescendant", o.id);
310
+ if (scroll) o.scrollIntoView({ block: "nearest" });
311
+ }
312
+ });
313
+ }
314
+ onTriggerKey(e) {
315
+ if (this.isDisabled) return;
316
+ switch (e.key) {
317
+ case "ArrowDown":
318
+ e.preventDefault();
319
+ if (!this.isOpen) this.open();
320
+ else this.moveActive(1);
321
+ break;
322
+ case "ArrowUp":
323
+ e.preventDefault();
324
+ if (!this.isOpen) this.open();
325
+ else this.moveActive(-1);
326
+ break;
327
+ case "Home":
328
+ if (this.isOpen) {
329
+ e.preventDefault();
330
+ this.moveActive(Number.MIN_SAFE_INTEGER);
331
+ }
332
+ break;
333
+ case "End":
334
+ if (this.isOpen) {
335
+ e.preventDefault();
336
+ this.moveActive(Number.MAX_SAFE_INTEGER);
337
+ }
338
+ break;
339
+ case "Enter":
340
+ case " ":
341
+ e.preventDefault();
342
+ if (this.isOpen) {
343
+ if (this.active >= 0) this.select(this.active);
344
+ } else this.open();
345
+ break;
346
+ case "Escape":
347
+ if (this.isOpen) {
348
+ e.preventDefault();
349
+ this.close();
350
+ }
351
+ break;
352
+ case "Tab":
353
+ if (this.isOpen) this.close();
354
+ break;
355
+ }
356
+ }
357
+ /** 在可用选项中移动(跳过禁用项,支持环绕) */
358
+ nextEnabled(from, dir) {
359
+ const n = this.options.length;
360
+ if (!n) return -1;
361
+ for (let step = 1; step <= n; step++) {
362
+ const i = (from + dir * step + n) % n;
363
+ const o = this.options[i];
364
+ if (o && !o.disabled) return i;
365
+ }
366
+ return -1;
367
+ }
368
+ firstEnabled() {
369
+ return this.options.findIndex((o) => !o.disabled);
370
+ }
371
+ lastEnabled() {
372
+ for (let i = this.options.length - 1; i >= 0; i--) {
373
+ const o = this.options[i];
374
+ if (o && !o.disabled) return i;
375
+ }
376
+ return -1;
377
+ }
378
+ startIndex() {
379
+ const idx = this.options.findIndex((o) => o.value === this.displayValue && !o.disabled);
380
+ return idx >= 0 ? idx : this.firstEnabled();
381
+ }
382
+ moveActive(delta) {
383
+ if (delta === Number.MIN_SAFE_INTEGER) {
384
+ const i = this.firstEnabled();
385
+ if (i >= 0) this.setActive(i);
386
+ return;
387
+ }
388
+ if (delta === Number.MAX_SAFE_INTEGER) {
389
+ const i = this.lastEnabled();
390
+ if (i >= 0) this.setActive(i);
391
+ return;
392
+ }
393
+ const from = this.active < 0 ? this.startIndex() : this.active;
394
+ const next = this.nextEnabled(from, delta > 0 ? 1 : -1);
395
+ if (next >= 0) this.setActive(next);
396
+ }
397
+ /** 当前选中值(无选中为 null) */
398
+ get value() {
399
+ return this.displayValue;
400
+ }
401
+ /** 是否展开 */
402
+ get expanded() {
403
+ return this.isOpen;
404
+ }
405
+ /** 外部同步 / 更新配置。value 同步显示但不触发 onChange(UI 选择才会回调) */
406
+ update(patch) {
407
+ if ("options" in patch && patch.options) {
408
+ this.options = patch.options;
409
+ this.renderOptions();
410
+ this.syncValue();
411
+ }
412
+ if ("value" in patch) {
413
+ this.displayValue = patch.value ?? null;
414
+ this.syncValue();
415
+ if (this.isOpen) this.renderOptions();
416
+ }
417
+ if ("disabled" in patch && patch.disabled !== void 0) this.setDisabled(patch.disabled);
418
+ if ("open" in patch && patch.open !== void 0) patch.open ? this.open() : this.close();
419
+ if ("placeholder" in patch && patch.placeholder !== void 0) {
420
+ this.placeholder = patch.placeholder;
421
+ this.syncValue();
422
+ }
423
+ if ("ariaLabel" in patch && patch.ariaLabel !== void 0) this.trigger.setAttribute("aria-label", patch.ariaLabel);
424
+ }
425
+ /** 程序化选中(等价 update({ value })) */
426
+ setValue(value) {
427
+ this.update({ value });
428
+ }
429
+ /** 动态切换整体禁用 */
430
+ setDisabled(v) {
431
+ this.isDisabled = v;
432
+ this.root.classList.toggle("is-disabled", v);
433
+ this.trigger.disabled = v;
434
+ if (v) this.close();
435
+ }
436
+ /** 销毁组件,清空宿主容器并移除 body 上的面板 */
437
+ destroy() {
438
+ if (this.closeTimer !== null) clearTimeout(this.closeTimer);
439
+ this.close();
440
+ this.panel.remove();
441
+ this.root.classList.remove("qsel", "is-open", "is-disabled");
442
+ if (this.customClass) this.root.classList.remove(this.customClass);
443
+ this.root.textContent = "";
444
+ }
445
+ };
446
+ //#endregion
447
+ export { Select };
448
+
449
+ //# sourceMappingURL=index.mjs.map