vanilla-jui 1.3.11 → 1.4.1

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.
Files changed (106) hide show
  1. package/dist/index.d.ts +2921 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.umd.js +1 -1
  4. package/dist/style.css +1 -1
  5. package/package.json +4 -3
  6. package/src/components/accordion.ts +522 -0
  7. package/src/components/drop.ts +497 -0
  8. package/src/components/flow.ts +1579 -0
  9. package/src/components/form.ts +953 -0
  10. package/src/components/{icons.js → icons.ts} +24 -11
  11. package/src/components/menu.ts +615 -0
  12. package/src/components/{modal.js → modal.ts} +560 -237
  13. package/src/components/offcanvas.ts +457 -0
  14. package/src/components/{pagination.js → pagination.ts} +251 -76
  15. package/src/components/parabola.ts +335 -0
  16. package/src/components/{sticky.js → sticky.ts} +123 -98
  17. package/src/components/{swiper.js → swiper.ts} +510 -232
  18. package/src/components/tabs.ts +984 -0
  19. package/src/components/theme.ts +437 -0
  20. package/src/components/toast.ts +371 -0
  21. package/src/components/toc.ts +385 -0
  22. package/src/components/tooltip.ts +179 -0
  23. package/src/components/validator.ts +596 -0
  24. package/src/core/{Component.js → Component.ts} +134 -27
  25. package/src/css/accordion.css +0 -45
  26. package/src/css/animation.css +31 -1
  27. package/src/css/avatar.css +7 -33
  28. package/src/css/badge.css +11 -59
  29. package/src/css/breadcrumb.css +9 -32
  30. package/src/css/button.css +23 -224
  31. package/src/css/card.css +1 -1
  32. package/src/css/color.css +863 -1013
  33. package/src/css/color_p3.css +822 -975
  34. package/src/css/components.css +1 -0
  35. package/src/css/drop.css +7 -7
  36. package/src/css/flow.css +1 -1
  37. package/src/css/form.css +1 -1
  38. package/src/css/icon.css +7 -12
  39. package/src/css/index.css +5 -2
  40. package/src/css/loading.css +36 -20
  41. package/src/css/menu.css +1 -1
  42. package/src/css/modal.css +15 -44
  43. package/src/css/offcanvas.css +14 -40
  44. package/src/css/popup.css +4 -4
  45. package/src/css/status.css +0 -2
  46. package/src/css/swiper.css +35 -50
  47. package/src/css/tabs.css +9 -31
  48. package/src/css/tag.css +16 -77
  49. package/src/css/themes/default/accordion.css +62 -0
  50. package/src/css/themes/default/avatar.css +39 -0
  51. package/src/css/themes/default/badge.css +63 -0
  52. package/src/css/themes/default/breadcrumb.css +28 -0
  53. package/src/css/themes/default/button.css +204 -0
  54. package/src/css/themes/default/drop.css +11 -0
  55. package/src/css/themes/default/index.css +16 -0
  56. package/src/css/themes/default/loading.css +23 -0
  57. package/src/css/themes/default/modal.css +40 -0
  58. package/src/css/themes/default/offcanvas.css +13 -0
  59. package/src/css/themes/default/popup.css +7 -0
  60. package/src/css/themes/default/swiper.css +35 -0
  61. package/src/css/themes/default/tabs.css +37 -0
  62. package/src/css/themes/default/tag.css +85 -0
  63. package/src/css/themes/default/tip.css +49 -0
  64. package/src/css/themes/default/toast.css +70 -0
  65. package/src/css/themes/default/tooltip.css +14 -0
  66. package/src/css/themes/index.css +1 -0
  67. package/src/css/tip.css +20 -39
  68. package/src/css/toast.css +18 -80
  69. package/src/css/tokens.css +8 -3
  70. package/src/css/tooltip.css +10 -10
  71. package/src/index.ts +37 -0
  72. package/src/locales/{index.js → index.ts} +1 -2
  73. package/src/types.d.ts +1 -0
  74. package/src/utilities/browser.ts +131 -0
  75. package/src/utilities/cache.ts +57 -0
  76. package/src/utilities/core.ts +614 -0
  77. package/src/utilities/dom.ts +468 -0
  78. package/src/utilities/events.ts +106 -0
  79. package/src/utilities/http.ts +35 -0
  80. package/src/utilities/storage.ts +46 -0
  81. package/src/components/accordion.js +0 -373
  82. package/src/components/components.js +0 -0
  83. package/src/components/drop.js +0 -437
  84. package/src/components/flow.js +0 -1133
  85. package/src/components/form.js +0 -585
  86. package/src/components/menu.js +0 -422
  87. package/src/components/offcanvas.js +0 -332
  88. package/src/components/parabola.js +0 -295
  89. package/src/components/tabs.js +0 -746
  90. package/src/components/theme.js +0 -353
  91. package/src/components/toast.js +0 -329
  92. package/src/components/toc.js +0 -226
  93. package/src/components/tooltip.js +0 -86
  94. package/src/components/validator.js +0 -586
  95. package/src/index.js +0 -45
  96. package/src/locales/index.min.js +0 -1
  97. package/src/utilities/browser.js +0 -111
  98. package/src/utilities/cache.js +0 -159
  99. package/src/utilities/core.js +0 -502
  100. package/src/utilities/debug.js +0 -103
  101. package/src/utilities/dom.js +0 -313
  102. package/src/utilities/events.js +0 -69
  103. package/src/utilities/http.js +0 -30
  104. package/src/utilities/legacy-dom.js +0 -179
  105. package/src/utilities/storage.js +0 -46
  106. /package/src/icons/{index.js → index.ts} +0 -0
@@ -0,0 +1,457 @@
1
+ import { createDeepStore, flushSync, jsx } from 'vanilla-signal';
2
+
3
+ import Component, {
4
+ type ComponentDOM,
5
+ type ComponentRuntime,
6
+ } from '../core/Component.ts';
7
+ import {
8
+ type ResolveSchema,
9
+ randomId,
10
+ resolveProps,
11
+ timer,
12
+ } from '../utilities/core.ts';
13
+ import {
14
+ type RenderableContent,
15
+ createLoading,
16
+ isRenderableContent,
17
+ normalizeContentNodes,
18
+ } from '../utilities/dom.ts';
19
+
20
+ export type OffcanvasDirection = 'top' | 'right' | 'bottom' | 'left';
21
+ export type OffcanvasAnimation = 'slide' | 'none';
22
+
23
+ export interface OffcanvasClassNames {
24
+ root: string;
25
+ overlay: string;
26
+ content: string;
27
+ active: string;
28
+ top: string;
29
+ right: string;
30
+ bottom: string;
31
+ left: string;
32
+ slide: string;
33
+ none: string;
34
+ }
35
+
36
+ export type OffcanvasClassNameConfig = Partial<OffcanvasClassNames>;
37
+
38
+ export type OffcanvasContent =
39
+ | RenderableContent<Offcanvas>
40
+ | ((offcanvas: Offcanvas) => Promise<RenderableContent<Offcanvas>>);
41
+
42
+ export interface OffcanvasProps extends Record<string, unknown> {
43
+ content?: OffcanvasContent;
44
+ overlay?: boolean;
45
+ filter?: boolean;
46
+ cache?: boolean;
47
+ ttl?: number;
48
+ direction?: OffcanvasDirection;
49
+ animation?: OffcanvasAnimation;
50
+ bgClose?: boolean;
51
+ escClose?: boolean;
52
+ id?: string | null;
53
+ className?: OffcanvasClassNameConfig;
54
+ onShow?: ((offcanvas: Offcanvas) => void | Promise<void>) | null;
55
+ onShown?: ((offcanvas: Offcanvas) => void | Promise<void>) | null;
56
+ onHide?: ((offcanvas: Offcanvas) => void | Promise<void>) | null;
57
+ onHidden?: ((offcanvas: Offcanvas) => void | Promise<void>) | null;
58
+ }
59
+
60
+ interface ResolvedOffcanvasProps extends Record<string, unknown> {
61
+ content: OffcanvasContent;
62
+ overlay: boolean;
63
+ filter: boolean;
64
+ cache: boolean;
65
+ ttl: number;
66
+ direction: OffcanvasDirection;
67
+ animation: OffcanvasAnimation;
68
+ bgClose: boolean;
69
+ escClose: boolean;
70
+ id: string;
71
+ className: OffcanvasClassNames;
72
+ onShow: NonNullable<OffcanvasProps['onShow']> | null;
73
+ onShown: NonNullable<OffcanvasProps['onShown']> | null;
74
+ onHide: NonNullable<OffcanvasProps['onHide']> | null;
75
+ onHidden: NonNullable<OffcanvasProps['onHidden']> | null;
76
+ }
77
+
78
+ interface OffcanvasState extends Record<string, unknown> {
79
+ visible: boolean;
80
+ loading: boolean;
81
+ }
82
+
83
+ interface OffcanvasDOM extends ComponentDOM {
84
+ root: HTMLElement | null;
85
+ content: HTMLElement | null;
86
+ }
87
+
88
+ interface OffcanvasCache {
89
+ content: RenderableContent<Offcanvas>;
90
+ hasContent: boolean;
91
+ updatedAt: number;
92
+ }
93
+
94
+ interface OffcanvasRuntime extends ComponentRuntime {
95
+ cache: OffcanvasCache;
96
+ contentLoadId: number;
97
+ }
98
+
99
+ const DEFAULT_CLASS_NAMES: OffcanvasClassNames = {
100
+ root: 'j-offcanvas',
101
+ overlay: 'j-offcanvas-overlay',
102
+ content: 'offcanvas-content',
103
+ active: 'is-active',
104
+ top: 'is-top',
105
+ right: 'is-right',
106
+ bottom: 'is-bottom',
107
+ left: 'is-left',
108
+ slide: 'is-slide',
109
+ none: 'is-none',
110
+ };
111
+
112
+ const OFFCANVAS_PROPS_SCHEMA = {
113
+ content: {
114
+ default: '',
115
+ validate: isRenderableContent,
116
+ message: 'expects string, Node, array, function or null.',
117
+ },
118
+ overlay: { default: true, type: 'boolean' },
119
+ filter: { default: true, type: 'boolean' },
120
+ cache: { default: false, type: 'boolean' },
121
+ ttl: { default: 0, type: 'number' },
122
+ direction: {
123
+ default: 'left',
124
+ type: 'string',
125
+ enum: ['top', 'right', 'bottom', 'left'],
126
+ },
127
+ animation: {
128
+ default: 'slide',
129
+ type: 'string',
130
+ enum: ['slide', 'none'],
131
+ },
132
+ bgClose: { default: true, type: 'boolean' },
133
+ escClose: { default: true, type: 'boolean' },
134
+ id: {
135
+ default: null,
136
+ types: ['string', 'null'],
137
+ normalize: (value: unknown) => {
138
+ if (typeof value === 'string') {
139
+ const id = value.trim();
140
+ return id || randomId();
141
+ }
142
+ if (value == null) return randomId();
143
+ return value;
144
+ },
145
+ },
146
+ className: {
147
+ default: DEFAULT_CLASS_NAMES,
148
+ type: 'object',
149
+ normalize: (value: unknown) => ({
150
+ ...DEFAULT_CLASS_NAMES,
151
+ ...(value && typeof value === 'object' ? value : {}),
152
+ }),
153
+ },
154
+ onShow: { default: null, types: ['function', 'null'] },
155
+ onShown: { default: null, types: ['function', 'null'] },
156
+ onHide: { default: null, types: ['function', 'null'] },
157
+ onHidden: { default: null, types: ['function', 'null'] },
158
+ } satisfies ResolveSchema<OffcanvasProps>;
159
+
160
+ function normalizeTtl(ttl: number): number {
161
+ return ttl > 0 ? ttl : 0;
162
+ }
163
+
164
+ function normalizeProps(input: OffcanvasProps): ResolvedOffcanvasProps {
165
+ const props = resolveProps(input, OFFCANVAS_PROPS_SCHEMA, 'Offcanvas');
166
+ return {
167
+ content: props.content as OffcanvasContent,
168
+ overlay: props.overlay as boolean,
169
+ filter: props.filter as boolean,
170
+ cache: props.cache as boolean,
171
+ ttl: props.ttl as number,
172
+ direction: props.direction as OffcanvasDirection,
173
+ animation: props.animation as OffcanvasAnimation,
174
+ bgClose: props.bgClose as boolean,
175
+ escClose: props.escClose as boolean,
176
+ id: props.id as string,
177
+ className: props.className as OffcanvasClassNames,
178
+ onShow: props.onShow as ResolvedOffcanvasProps['onShow'],
179
+ onShown: props.onShown as ResolvedOffcanvasProps['onShown'],
180
+ onHide: props.onHide as ResolvedOffcanvasProps['onHide'],
181
+ onHidden: props.onHidden as ResolvedOffcanvasProps['onHidden'],
182
+ };
183
+ }
184
+
185
+ function joinClasses(
186
+ ...classes: Array<string | null | undefined | false>
187
+ ): string {
188
+ return classes.filter(Boolean).join(' ');
189
+ }
190
+
191
+ /**
192
+ * 侧滑面板组件,继承 Component。
193
+ *
194
+ * 适用于侧边菜单、筛选面板、移动端抽屉等场景。
195
+ */
196
+ export class Offcanvas extends Component<
197
+ ResolvedOffcanvasProps,
198
+ OffcanvasState,
199
+ OffcanvasDOM
200
+ > {
201
+ declare runtime: OffcanvasRuntime;
202
+ declare state: OffcanvasState;
203
+ _overlay: HTMLElement | null;
204
+
205
+ constructor(input: OffcanvasProps = {}) {
206
+ const props = normalizeProps(input);
207
+ super(props);
208
+
209
+ this.dom.content = null;
210
+ this._overlay = null;
211
+ this.runtime.cache = {
212
+ content: null,
213
+ hasContent: false,
214
+ updatedAt: 0,
215
+ };
216
+ this.runtime.contentLoadId = 0;
217
+
218
+ this.state = createDeepStore({
219
+ visible: false,
220
+ loading: false,
221
+ }) as OffcanvasState;
222
+
223
+ this.onInit(props);
224
+ }
225
+
226
+ protected onInit(props: ResolvedOffcanvasProps): void {
227
+ if (props.overlay) this._overlay = this.buildOverlay(props);
228
+ this.root = this.buildRoot(props);
229
+ }
230
+
231
+ private buildOverlay(props: ResolvedOffcanvasProps): HTMLElement {
232
+ return jsx('div', {
233
+ className: props.className.overlay,
234
+ 'data-offcanvas-overlay': props.id,
235
+ style: props.filter ? { backdropFilter: 'blur(2px)' } : {},
236
+ }) as HTMLElement;
237
+ }
238
+
239
+ private buildRoot(props: ResolvedOffcanvasProps): HTMLElement {
240
+ const content = jsx('div', {
241
+ className: props.className.content,
242
+ 'data-offcanvas-content': props.id,
243
+ }) as HTMLElement;
244
+ this.dom.content = content;
245
+
246
+ if (typeof props.content !== 'function') {
247
+ content.append(...normalizeContentNodes(props.content, this));
248
+ }
249
+
250
+ return jsx('div', {
251
+ className: joinClasses(
252
+ props.className.root,
253
+ props.className[props.direction],
254
+ props.className[props.animation]
255
+ ),
256
+ id: props.id,
257
+ 'data-offcanvas': 'root',
258
+ 'data-offcanvas-direction': props.direction,
259
+ 'data-offcanvas-animation': props.animation,
260
+ children: content,
261
+ }) as HTMLElement;
262
+ }
263
+
264
+ private isCacheValid(): boolean {
265
+ if (!this.props.cache || !this.runtime.cache.hasContent) return false;
266
+
267
+ const ttl = normalizeTtl(this.props.ttl);
268
+ return !ttl || Date.now() - this.runtime.cache.updatedAt <= ttl;
269
+ }
270
+
271
+ private renderContent(content: RenderableContent<Offcanvas>): void {
272
+ if (!this.dom.content) return;
273
+
274
+ this.dom.content.textContent = '';
275
+ this.dom.content.append(...normalizeContentNodes(content, this));
276
+ }
277
+
278
+ private async loadContent(): Promise<void> {
279
+ const { content, cache } = this.props;
280
+ if (typeof content !== 'function') {
281
+ this.runtime.contentLoadId += 1;
282
+ flushSync(() => {
283
+ this.state.loading = false;
284
+ });
285
+ return;
286
+ }
287
+
288
+ if (this.isCacheValid()) {
289
+ this.runtime.contentLoadId += 1;
290
+ this.renderContent(this.runtime.cache.content);
291
+ flushSync(() => {
292
+ this.state.loading = false;
293
+ });
294
+ return;
295
+ }
296
+
297
+ const loadId = ++this.runtime.contentLoadId;
298
+ flushSync(() => {
299
+ this.state.loading = true;
300
+ });
301
+
302
+ if (this.dom.content) {
303
+ this.dom.content.textContent = '';
304
+ this.dom.content.appendChild(createLoading());
305
+ }
306
+
307
+ try {
308
+ const result = await Promise.resolve(content(this));
309
+
310
+ if (this.runtime.destroyed || loadId !== this.runtime.contentLoadId) {
311
+ return;
312
+ }
313
+
314
+ if (cache) {
315
+ this.runtime.cache.content = result;
316
+ this.runtime.cache.hasContent = true;
317
+ this.runtime.cache.updatedAt = Date.now();
318
+ }
319
+ this.renderContent(result);
320
+ } finally {
321
+ if (!this.runtime.destroyed && loadId === this.runtime.contentLoadId) {
322
+ flushSync(() => {
323
+ this.state.loading = false;
324
+ });
325
+ }
326
+ }
327
+ }
328
+
329
+ private bindEvents(): void {
330
+ this.unbindEvents();
331
+ const { bgClose, escClose } = this.props;
332
+
333
+ if (this._overlay && bgClose) {
334
+ this.cleanup.events.on('overlay', this._overlay, 'click', () => {
335
+ void this.hide();
336
+ });
337
+ }
338
+ if (escClose) {
339
+ this.cleanup.events.on('esc', document, 'keydown', (event) => {
340
+ if (
341
+ event instanceof KeyboardEvent &&
342
+ event.key === 'Escape' &&
343
+ this.state.visible
344
+ ) {
345
+ void this.hide();
346
+ }
347
+ });
348
+ }
349
+ if (this.dom.root) {
350
+ this.cleanup.events.on('close', this.dom.root, 'click', (event) => {
351
+ if (!(event.target instanceof Element)) return;
352
+ const action = event.target.closest<HTMLElement>('[data-action]');
353
+ const name = action?.dataset.action;
354
+ if (name === 'close' || name === 'cancel') void this.hide();
355
+ });
356
+ }
357
+ }
358
+
359
+ unbindEvents(): void {
360
+ this.cleanup?.events.clear();
361
+ }
362
+
363
+ private renderPanel(): void {
364
+ const { overlay, id, className } = this.props;
365
+ const body = document.body;
366
+
367
+ if (overlay && this._overlay) body.appendChild(this._overlay);
368
+ if (this.dom.root) body.appendChild(this.dom.root);
369
+
370
+ body.style.overflow = 'hidden';
371
+
372
+ timer.start(`oc-show-${id}`, 10, () => {
373
+ if (!this.dom.root) return;
374
+ this._overlay?.classList.add(className.active);
375
+ this.dom.root.classList.add(className.active);
376
+ });
377
+ }
378
+
379
+ private removePanel(): void {
380
+ const { id, className } = this.props;
381
+ const body = document.body;
382
+
383
+ this._overlay?.classList.remove(className.active);
384
+ this.dom.root?.classList.remove(className.active);
385
+
386
+ timer.start(`oc-remove-${id}`, 100, () => {
387
+ this._overlay?.remove();
388
+ this.dom.root?.remove();
389
+ body.style.overflow = '';
390
+ });
391
+ }
392
+
393
+ async show(): Promise<void> {
394
+ if (this.runtime.destroyed || this.state.visible) return;
395
+
396
+ const { onShow, onShown, id } = this.props;
397
+ if (onShow) await Promise.resolve(onShow(this));
398
+
399
+ this.renderPanel();
400
+ this.bindEvents();
401
+
402
+ flushSync(() => {
403
+ this.state.visible = true;
404
+ });
405
+
406
+ await this.loadContent();
407
+
408
+ timer.start(`oc-shown-${id}`, 300, () => {
409
+ if (this.runtime.destroyed) return;
410
+ void onShown?.(this);
411
+ });
412
+ }
413
+
414
+ async hide(): Promise<void> {
415
+ if (this.runtime.destroyed || !this.state.visible) return;
416
+
417
+ const { onHide, onHidden } = this.props;
418
+ if (onHide) await Promise.resolve(onHide(this));
419
+
420
+ this.runtime.contentLoadId += 1;
421
+ flushSync(() => {
422
+ this.state.loading = false;
423
+ });
424
+
425
+ this.unbindEvents();
426
+ this.removePanel();
427
+
428
+ flushSync(() => {
429
+ this.state.visible = false;
430
+ });
431
+
432
+ void onHidden?.(this);
433
+ }
434
+
435
+ onDestroy(): void {
436
+ const { id, className } = this.props;
437
+
438
+ timer.cancel(`oc-show-${id}`);
439
+ timer.cancel(`oc-remove-${id}`);
440
+ timer.cancel(`oc-shown-${id}`);
441
+
442
+ if (this.state.visible) this.unbindEvents();
443
+
444
+ this.dom.root?.classList.remove(className.active);
445
+ this.dom.root?.remove();
446
+ this._overlay?.remove();
447
+
448
+ const body = document.body;
449
+ body.style.overflow = '';
450
+
451
+ this._overlay = null;
452
+ }
453
+ }
454
+
455
+ export function createOffcanvas(options: OffcanvasProps = {}): Offcanvas {
456
+ return new Offcanvas(options);
457
+ }