@haiyue/ui 0.3.0 → 0.4.0

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 (73) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +84 -2
  3. package/dist/adapters/web/controls.js +48 -8
  4. package/dist/adapters/web/renderer.js +8 -3
  5. package/dist/anchor.d.ts +2 -0
  6. package/dist/anchor.js +23 -4
  7. package/dist/border-beam.js +2 -1
  8. package/dist/cascader.js +7 -1
  9. package/dist/context-menu.js +0 -1
  10. package/dist/core/actions.js +7 -8
  11. package/dist/core/data.d.ts +1 -1
  12. package/dist/core/data.js +44 -10
  13. package/dist/core/platform-view.js +8 -2
  14. package/dist/core/selection.js +4 -4
  15. package/dist/core/semantics.d.ts +26 -0
  16. package/dist/core/semantics.js +8 -0
  17. package/dist/core/text.js +3 -3
  18. package/dist/core/validation.d.ts +3 -1
  19. package/dist/core/validation.js +23 -5
  20. package/dist/date-picker.js +6 -0
  21. package/dist/dialog.d.ts +4 -0
  22. package/dist/dialog.js +37 -16
  23. package/dist/drag-source.d.ts +27 -0
  24. package/dist/drag-source.js +114 -0
  25. package/dist/draggable.d.ts +1 -2
  26. package/dist/draggable.js +25 -31
  27. package/dist/drawer.js +4 -12
  28. package/dist/dropdown.js +0 -1
  29. package/dist/dropzone.d.ts +29 -0
  30. package/dist/dropzone.js +89 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +9 -0
  33. package/dist/input.js +3 -3
  34. package/dist/internal/drag-drop.d.ts +29 -0
  35. package/dist/internal/drag-drop.js +171 -0
  36. package/dist/internal/focus.d.ts +2 -0
  37. package/dist/internal/focus.js +31 -0
  38. package/dist/internal/keyboard.d.ts +2 -0
  39. package/dist/internal/keyboard.js +12 -0
  40. package/dist/internal/native-drop.d.ts +17 -0
  41. package/dist/internal/native-drop.js +62 -0
  42. package/dist/internal/numbers.d.ts +2 -0
  43. package/dist/internal/numbers.js +8 -0
  44. package/dist/internal/pointer-drag.d.ts +15 -0
  45. package/dist/internal/pointer-drag.js +100 -0
  46. package/dist/modal.js +1 -30
  47. package/dist/notification.js +2 -1
  48. package/dist/portable/page.js +25 -14
  49. package/dist/radio.js +1 -1
  50. package/dist/resizable.d.ts +1 -1
  51. package/dist/resizable.js +26 -27
  52. package/dist/sortable.d.ts +62 -0
  53. package/dist/sortable.js +363 -0
  54. package/dist/table.d.ts +3 -1
  55. package/dist/table.js +65 -42
  56. package/dist/time-picker.js +6 -0
  57. package/dist/toast.js +3 -2
  58. package/dist/tooltip.d.ts +6 -0
  59. package/dist/tooltip.js +103 -2
  60. package/dist/tour.js +15 -6
  61. package/dist/tree-model.d.ts +2 -0
  62. package/dist/tree-model.js +32 -11
  63. package/dist/tree.d.ts +0 -2
  64. package/dist/tree.js +8 -24
  65. package/dist/upload.d.ts +2 -5
  66. package/dist/upload.js +19 -25
  67. package/dist/virtual-keyboard.d.ts +1 -0
  68. package/dist/virtual-keyboard.js +67 -36
  69. package/dist/virtual-list.d.ts +1 -0
  70. package/dist/virtual-list.js +21 -4
  71. package/dist/virtual-mouse.js +2 -1
  72. package/dist/watermark.js +2 -1
  73. package/package.json +33 -6
@@ -97,16 +97,26 @@ const schemas = {
97
97
  PlatformView: shape({ ...common, slot: name, resourceId: name }, ['slot', 'resourceId']),
98
98
  };
99
99
  export const HY_COMPONENTS = Object.freeze(Object.keys(schemas));
100
+ const validatedCapabilities = new WeakSet();
101
+ /** Internal descriptor-safe handoff: validation and consumers see the same immutable values. */
102
+ export function snapshotProtocolData(value, context = {}) {
103
+ return snapshot(value, (message, property) => fail(message, property ? field(context, property) : context));
104
+ }
100
105
  export function hyValidateCapabilities(capabilities) {
101
106
  const context = { property: 'capabilities' };
107
+ capabilities = snapshotProtocolData(capabilities, context);
108
+ if (validatedCapabilities.has(capabilities))
109
+ return;
102
110
  shape({ contract: choice(1), backend: choice('web', 'nativescript', 'desktop'), components: list(choice(...HY_COMPONENTS)),
103
111
  features: list((v, c) => { if (!HY_FEATURES.includes(v))
104
112
  throw new HYUIError('capability', 'Unknown capability', c); }) }, ['contract', 'backend', 'components', 'features'])(capabilities, context);
105
113
  if (new Set(capabilities.components).size !== capabilities.components.length || new Set(capabilities.features).size !== capabilities.features.length) {
106
114
  throw new HYUIError('capability', 'Duplicate capability declaration', context);
107
115
  }
116
+ validatedCapabilities.add(capabilities);
108
117
  }
109
118
  export function hySupports(capabilities, feature) {
119
+ capabilities = snapshotProtocolData(capabilities, { property: 'capabilities' });
110
120
  hyValidateCapabilities(capabilities);
111
121
  if (!HY_FEATURES.includes(feature))
112
122
  throw new HYUIError('capability', 'Unknown capability', { property: feature });
@@ -123,12 +133,17 @@ function remember(props, kind) {
123
133
  }
124
134
  /** Internal immutable hand-off. Mutable and merely shallow-frozen inputs are always validated. */
125
135
  export function snapshotValidatedProps(kind, props, context, capabilities) {
136
+ props = snapshotProtocolData(props, context);
126
137
  hyValidateProps(kind, props, context, capabilities);
127
- const result = snapshot(props);
128
- remember(result, kind);
129
- return result;
138
+ remember(props, kind);
139
+ return props;
130
140
  }
131
141
  export function hyValidateProps(kind, props, context = {}, capabilities) {
142
+ props = snapshotProtocolData(props, context);
143
+ if (capabilities) {
144
+ capabilities = snapshotProtocolData(capabilities, { property: 'capabilities' });
145
+ hyValidateCapabilities(capabilities);
146
+ }
132
147
  if (!Object.hasOwn(schemas, kind))
133
148
  fail('Unknown component kind', context);
134
149
  if (!validatedSnapshots.get(props)?.has(kind)) {
@@ -181,8 +196,11 @@ export function hyKey(...parts) {
181
196
  return '/' + parts.map(key => key.replace(/~/g, '~0').replace(/\//g, '~1')).join('/');
182
197
  }
183
198
  export function hyValidateNode(node, capabilities) {
184
- if (capabilities)
199
+ if (capabilities) {
200
+ capabilities = snapshotProtocolData(capabilities, { property: 'capabilities' });
185
201
  hyValidateCapabilities(capabilities);
202
+ }
203
+ node = snapshotProtocolData(node, { property: 'node' });
186
204
  const ancestors = new Set();
187
205
  const walk = (value, parent) => {
188
206
  const raw = record(value, { property: 'node' });
@@ -238,6 +256,6 @@ export function hyValidateNode(node, capabilities) {
238
256
  return snapshot(node);
239
257
  }
240
258
  export function hyNode(kind, key, props, content = {}) {
241
- return hyValidateNode({ kind, key, props, ...content });
259
+ return hyValidateNode({ kind, key, props, ...snapshotProtocolData(content, { key }) });
242
260
  }
243
261
  //# sourceMappingURL=validation.js.map
@@ -191,6 +191,10 @@ export class HYDatePicker extends HTMLElement {
191
191
  return new Intl.DateTimeFormat(this.locale, { year: 'numeric', month: 'short', day: 'numeric' }).format(date);
192
192
  }
193
193
  _render() {
194
+ if (this.disabled && this.open) {
195
+ this.open = false;
196
+ return;
197
+ }
194
198
  const text = this._formatDate(this._value);
195
199
  this._label.textContent = text || this.placeholder;
196
200
  this._label.classList.toggle('placeholder', !text);
@@ -327,6 +331,8 @@ export class HYDatePicker extends HTMLElement {
327
331
  else
328
332
  this._panel.querySelector('.day:not(:disabled)')?.focus(); }
329
333
  _commit(value) {
334
+ if (this.disabled)
335
+ return;
330
336
  this._value = this._normalizeValue(value);
331
337
  this._reflectValue();
332
338
  if (this.open)
package/dist/dialog.d.ts CHANGED
@@ -9,9 +9,12 @@ export declare class HYDialog extends HTMLElement {
9
9
  private readonly _closeButton;
10
10
  private _returnFocus;
11
11
  private _closeReason;
12
+ private _shown;
13
+ private _session;
12
14
  static get observedAttributes(): string[];
13
15
  constructor();
14
16
  connectedCallback(): void;
17
+ disconnectedCallback(): void;
15
18
  attributeChangedCallback(name: string): void;
16
19
  get open(): boolean;
17
20
  set open(value: boolean);
@@ -26,6 +29,7 @@ export declare class HYDialog extends HTMLElement {
26
29
  close(reason?: HYDialogCloseDetail['reason']): void;
27
30
  private _syncHeading;
28
31
  private _syncOpen;
32
+ private _finishClose;
29
33
  private _onCancel;
30
34
  private _onDialogClick;
31
35
  private _requestClose;
package/dist/dialog.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { focusedElement } from './internal/focus.js';
1
2
  /** Accessible modal surface with slotted body/footer content and reflected open state. */
2
3
  export class HYDialog extends HTMLElement {
3
4
  _dialog = document.createElement('dialog');
@@ -5,6 +6,8 @@ export class HYDialog extends HTMLElement {
5
6
  _closeButton = document.createElement('button');
6
7
  _returnFocus = null;
7
8
  _closeReason = 'programmatic';
9
+ _shown = false;
10
+ _session = 0;
8
11
  static get observedAttributes() {
9
12
  return ['open', 'heading', 'dismissible', 'controlled'];
10
13
  }
@@ -109,6 +112,14 @@ export class HYDialog extends HTMLElement {
109
112
  this._syncHeading();
110
113
  this._syncOpen();
111
114
  }
115
+ disconnectedCallback() {
116
+ this._shown = false;
117
+ this._session++;
118
+ this._returnFocus = null;
119
+ this._closeReason = 'programmatic';
120
+ if (this._dialog.open)
121
+ this._dialog.close();
122
+ }
112
123
  attributeChangedCallback(name) {
113
124
  if (name === 'heading')
114
125
  this._syncHeading();
@@ -127,7 +138,7 @@ export class HYDialog extends HTMLElement {
127
138
  showModal() {
128
139
  if (this.open && this._dialog.open)
129
140
  return;
130
- this._returnFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
141
+ this._returnFocus = focusedElement(this.ownerDocument);
131
142
  this._closeReason = 'programmatic';
132
143
  this.open = true;
133
144
  }
@@ -142,12 +153,32 @@ export class HYDialog extends HTMLElement {
142
153
  }
143
154
  _syncOpen() {
144
155
  if (this.open && !this._dialog.open) {
145
- this._returnFocus ??= document.activeElement instanceof HTMLElement ? document.activeElement : null;
146
- this._dialog.showModal();
156
+ this._returnFocus ??= focusedElement(this.ownerDocument);
157
+ this._shown = true;
158
+ this._session++;
159
+ try {
160
+ this._dialog.showModal();
161
+ }
162
+ catch (error) {
163
+ this._shown = false;
164
+ throw error;
165
+ }
147
166
  return;
148
167
  }
149
- if (!this.open && this._dialog.open)
168
+ if (!this.open && this._shown)
169
+ this._finishClose();
170
+ }
171
+ _finishClose() {
172
+ const session = this._session, reason = this._closeReason, returnFocus = this._returnFocus, controlled = this.controlled;
173
+ this._shown = false;
174
+ this._returnFocus = null;
175
+ this._closeReason = 'programmatic';
176
+ this.removeAttribute('open');
177
+ if (this._dialog.open)
150
178
  this._dialog.close();
179
+ this.dispatchEvent(new CustomEvent('dialog-close', { detail: { reason }, bubbles: true, composed: true }));
180
+ if (session === this._session && !this.open && this.isConnected && !controlled && returnFocus?.isConnected)
181
+ returnFocus.focus();
151
182
  }
152
183
  _onCancel = (event) => {
153
184
  event.preventDefault();
@@ -179,19 +210,9 @@ export class HYDialog extends HTMLElement {
179
210
  }
180
211
  _onNativeClose = () => {
181
212
  // Native close is queued. A subsequent showModal must not be undone by that old event.
182
- if (this._dialog.open)
213
+ if (!this.isConnected || this._dialog.open || !this._shown)
183
214
  return;
184
- this.removeAttribute('open');
185
- const reason = this._closeReason;
186
- this._closeReason = 'programmatic';
187
- this.dispatchEvent(new CustomEvent('dialog-close', {
188
- detail: { reason },
189
- bubbles: true,
190
- composed: true,
191
- }));
192
- if (!this.controlled)
193
- this._returnFocus?.focus();
194
- this._returnFocus = null;
215
+ this._finishClose();
195
216
  };
196
217
  }
197
218
  export function defineDialogComponents() {
@@ -0,0 +1,27 @@
1
+ export type { HYDragPayload, HYDragEndDetail } from './internal/drag-drop.js';
2
+ /** Application data source for pointer, touch and keyboard drag-and-drop. */
3
+ export declare class HYDragSource extends HTMLElement {
4
+ private readonly _surface;
5
+ private readonly _pointer;
6
+ private readonly _instructions;
7
+ private _data;
8
+ static get observedAttributes(): string[];
9
+ constructor();
10
+ connectedCallback(): void;
11
+ disconnectedCallback(): void;
12
+ attributeChangedCallback(): void;
13
+ get type(): string;
14
+ set type(value: string);
15
+ get data(): unknown;
16
+ set data(value: unknown);
17
+ get effect(): 'copy' | 'move';
18
+ set effect(value: 'copy' | 'move');
19
+ get disabled(): boolean;
20
+ set disabled(value: boolean);
21
+ get handle(): string;
22
+ set handle(value: string);
23
+ focus(options?: FocusOptions): void;
24
+ private _sync;
25
+ private _begin;
26
+ }
27
+ export declare function defineDragSourceComponents(): void;
@@ -0,0 +1,114 @@
1
+ import { HYPointerDrag, dragTargetAllowed } from './internal/pointer-drag.js';
2
+ import { activeDrag, beginDrag, cancelDragSource, finishDrag, moveDrag } from './internal/drag-drop.js';
3
+ import { isKeyboardControlEvent } from './internal/keyboard.js';
4
+ let instructionSequence = 0;
5
+ /** Application data source for pointer, touch and keyboard drag-and-drop. */
6
+ export class HYDragSource extends HTMLElement {
7
+ _surface = document.createElement('div');
8
+ _pointer = new HYPointerDrag();
9
+ _instructions = document.createElement('div');
10
+ _data;
11
+ static get observedAttributes() { return ['type', 'value', 'effect', 'disabled', 'handle', 'aria-label']; }
12
+ constructor() {
13
+ super();
14
+ const root = this.attachShadow({ mode: 'open' });
15
+ const style = document.createElement('style');
16
+ style.textContent = `
17
+ :host { display: inline-block; min-width: 0; max-width: 100%; touch-action: none; }
18
+ :host([hidden]) { display: none !important; }
19
+ .surface { cursor: grab; touch-action: none; border-radius: var(--hy-radius-md, 6px); }
20
+ .surface:focus-visible { outline: 2px solid var(--hy-focus-border-color, #8177f2); outline-offset: 2px; }
21
+ :host([dragging]) .surface { opacity: .55; cursor: grabbing; }
22
+ :host([disabled]) .surface { cursor: default; }
23
+ .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
24
+ `;
25
+ this._surface.className = 'surface';
26
+ this._surface.part.add('surface');
27
+ this._surface.setAttribute('role', 'group');
28
+ this._surface.append(document.createElement('slot'));
29
+ this._instructions.id = `hy-drag-instructions-${++instructionSequence}`;
30
+ this._instructions.className = 'sr';
31
+ this._instructions.setAttribute('role', 'status');
32
+ this._instructions.setAttribute('aria-live', 'polite');
33
+ this._instructions.textContent = 'Press Space to pick up, Tab to a drop zone, Enter to drop, and Escape to cancel.';
34
+ this._surface.setAttribute('aria-describedby', this._instructions.id);
35
+ root.append(style, this._surface, this._instructions);
36
+ this.addEventListener('pointerdown', event => {
37
+ if (this.disabled || !dragTargetAllowed(event, this, this.handle))
38
+ return;
39
+ if (this._pointer.start(event, this._surface, {
40
+ start: () => this._begin('pointer'),
41
+ move: move => moveDrag(this.ownerDocument, move.clientX, move.clientY),
42
+ end: (up, cancelled) => {
43
+ if (!cancelled)
44
+ moveDrag(this.ownerDocument, up.clientX, up.clientY);
45
+ finishDrag(this.ownerDocument, cancelled);
46
+ },
47
+ }, 6))
48
+ event.stopPropagation();
49
+ });
50
+ this._surface.addEventListener('dragstart', event => event.preventDefault());
51
+ this._surface.addEventListener('keydown', event => {
52
+ if (this.disabled || event.defaultPrevented || (isKeyboardControlEvent(event, this._surface) && !(this.handle && dragTargetAllowed(event, this, this.handle))))
53
+ return;
54
+ if (event.key !== ' ' && event.key !== 'Enter')
55
+ return;
56
+ event.preventDefault();
57
+ if (activeDrag(this.ownerDocument)?.source === this)
58
+ finishDrag(this.ownerDocument);
59
+ else
60
+ this._begin('keyboard');
61
+ });
62
+ }
63
+ connectedCallback() { this._sync(); }
64
+ disconnectedCallback() { this._pointer.cancel(); cancelDragSource(this); this.removeAttribute('dragging'); }
65
+ attributeChangedCallback() {
66
+ if (this.disabled) {
67
+ this._pointer.cancel();
68
+ cancelDragSource(this);
69
+ }
70
+ this._sync();
71
+ }
72
+ get type() { return this.getAttribute('type')?.trim() || 'item'; }
73
+ set type(value) { this.setAttribute('type', value); }
74
+ get data() { return this._data === undefined ? this.getAttribute('value') : this._data; }
75
+ set data(value) { this._data = value; }
76
+ get effect() { return this.getAttribute('effect') === 'copy' ? 'copy' : 'move'; }
77
+ set effect(value) { this.setAttribute('effect', value); }
78
+ get disabled() { return this.hasAttribute('disabled'); }
79
+ set disabled(value) { this.toggleAttribute('disabled', value); }
80
+ get handle() { return this.getAttribute('handle') ?? ''; }
81
+ set handle(value) { this.setAttribute('handle', value); }
82
+ focus(options) { this._surface.focus(options); }
83
+ _sync() {
84
+ this._surface.tabIndex = this.disabled ? -1 : 0;
85
+ this._surface.setAttribute('aria-disabled', String(this.disabled));
86
+ this._surface.setAttribute('aria-label', this.getAttribute('aria-label') || 'Drag source. Press Space to pick up, Tab to a drop zone, and Enter to drop.');
87
+ }
88
+ _begin(input) {
89
+ if (this.disabled || !this.isConnected || activeDrag(this.ownerDocument))
90
+ return false;
91
+ const payload = { type: this.type, data: this.data, source: this, effect: this.effect, input };
92
+ if (!this.dispatchEvent(new CustomEvent('drag-start', { detail: payload, bubbles: true, composed: true, cancelable: true })) || this.disabled || !this.isConnected)
93
+ return false;
94
+ const started = beginDrag(payload, detail => {
95
+ this._pointer.cancel();
96
+ this.removeAttribute('dragging');
97
+ this._instructions.textContent = detail.accepted ? 'Item dropped.' : 'Drag ended without a drop. Press Space to pick up again.';
98
+ if (detail.cancelled && detail.input === 'keyboard' && this.isConnected)
99
+ this.focus({ preventScroll: true });
100
+ this.dispatchEvent(new CustomEvent('drag-end', { detail, bubbles: true, composed: true }));
101
+ });
102
+ if (started) {
103
+ this.setAttribute('dragging', '');
104
+ this._instructions.textContent = 'Picked up. Tab to a drop zone, Enter to drop, or Escape to cancel.';
105
+ this.focus({ preventScroll: true });
106
+ }
107
+ return started;
108
+ }
109
+ }
110
+ export function defineDragSourceComponents() {
111
+ if (!customElements.get('hy-drag-source'))
112
+ customElements.define('hy-drag-source', HYDragSource);
113
+ }
114
+ //# sourceMappingURL=drag-source.js.map
@@ -24,7 +24,7 @@ export declare class HYDraggable extends HTMLElement {
24
24
  private _y;
25
25
  private _reflecting;
26
26
  private _boundsTarget;
27
- private _dragAbort;
27
+ private readonly _pointer;
28
28
  private _pointerId;
29
29
  private _originX;
30
30
  private _originY;
@@ -63,7 +63,6 @@ export declare class HYDraggable extends HTMLElement {
63
63
  refresh(): HYDraggablePosition;
64
64
  private _startPointerDrag;
65
65
  private _handlePointerMove;
66
- private _finishPointerDrag;
67
66
  private _completePointerDrag;
68
67
  private _cancelDrag;
69
68
  private _handleKeydown;
package/dist/draggable.js CHANGED
@@ -1,6 +1,9 @@
1
+ import { finiteNumber as readFiniteNumber } from './internal/numbers.js';
2
+ import { isKeyboardControlEvent } from './internal/keyboard.js';
3
+ import { HYPointerDrag } from './internal/pointer-drag.js';
1
4
  const DEFAULT_CANCEL = 'input, textarea, select, button, a, [contenteditable="true"], [data-no-drag]';
2
5
  function finiteNumber(value, fallback = 0) {
3
- const number = Number(value);
6
+ const number = readFiniteNumber(value, fallback);
4
7
  return Number.isFinite(number) ? number : fallback;
5
8
  }
6
9
  function safeMatches(element, selector) {
@@ -18,7 +21,7 @@ export class HYDraggable extends HTMLElement {
18
21
  _y = 0;
19
22
  _reflecting = false;
20
23
  _boundsTarget = null;
21
- _dragAbort = null;
24
+ _pointer = new HYPointerDrag();
22
25
  _pointerId = null;
23
26
  _originX = 0;
24
27
  _originY = 0;
@@ -115,24 +118,23 @@ export class HYDraggable extends HTMLElement {
115
118
  _startPointerDrag(event) {
116
119
  if (this.disabled || event.button !== 0 || this._pointerId != null || !this._allowsTarget(event))
117
120
  return;
118
- const start = new CustomEvent('drag-start', { bubbles: true, composed: true, cancelable: true, detail: { x: this._x, y: this._y, pointerId: event.pointerId } });
119
- if (!this.dispatchEvent(start))
120
- return;
121
- event.preventDefault();
122
- this._surface.focus({ preventScroll: true });
123
- this._surface.setPointerCapture(event.pointerId);
124
- this._pointerId = event.pointerId;
125
- this._originX = this._x;
126
- this._originY = this._y;
127
- this._pointerX = event.clientX;
128
- this._pointerY = event.clientY;
129
- this.toggleAttribute('dragging', true);
130
- const abort = new AbortController();
131
- this._dragAbort = abort;
132
- this._surface.addEventListener('pointermove', move => this._handlePointerMove(move), { signal: abort.signal });
133
- this._surface.addEventListener('pointerup', up => this._finishPointerDrag(up, false), { signal: abort.signal });
134
- this._surface.addEventListener('pointercancel', cancel => this._finishPointerDrag(cancel, true), { signal: abort.signal });
135
- this._surface.addEventListener('lostpointercapture', () => this._cancelDrag(true), { signal: abort.signal });
121
+ this._pointer.start(event, this._surface, {
122
+ start: () => {
123
+ const start = new CustomEvent('drag-start', { bubbles: true, composed: true, cancelable: true, detail: { x: this._x, y: this._y, pointerId: event.pointerId } });
124
+ if (!this.dispatchEvent(start) || this.disabled || !this.isConnected)
125
+ return false;
126
+ this._surface.focus({ preventScroll: true });
127
+ this._pointerId = event.pointerId;
128
+ this._originX = this._x;
129
+ this._originY = this._y;
130
+ this._pointerX = event.clientX;
131
+ this._pointerY = event.clientY;
132
+ this.toggleAttribute('dragging', true);
133
+ return true;
134
+ },
135
+ move: move => this._handlePointerMove(move),
136
+ end: (up, cancelled) => this._completePointerDrag(up.pointerId, cancelled),
137
+ });
136
138
  }
137
139
  _handlePointerMove(event) {
138
140
  if (event.pointerId !== this._pointerId)
@@ -142,29 +144,21 @@ export class HYDraggable extends HTMLElement {
142
144
  const y = this.axis === 'x' ? this._originY : this._originY + event.clientY - this._pointerY;
143
145
  this._moveTo(x, y, 'pointer');
144
146
  }
145
- _finishPointerDrag(event, cancelled) {
146
- this._completePointerDrag(event.pointerId, cancelled);
147
- }
148
147
  _completePointerDrag(pointerId, cancelled) {
149
148
  if (pointerId !== this._pointerId)
150
149
  return;
151
150
  const originX = this._originX;
152
151
  const originY = this._originY;
153
- this._dragAbort?.abort();
154
- this._dragAbort = null;
155
152
  this._pointerId = null;
156
153
  this.toggleAttribute('dragging', false);
157
- if (this._surface.hasPointerCapture(pointerId))
158
- this._surface.releasePointerCapture(pointerId);
159
154
  this.dispatchEvent(new CustomEvent('drag-end', { bubbles: true, composed: true, detail: { x: this._x, y: this._y, deltaX: this._x - originX, deltaY: this._y - originY, source: 'pointer', cancelled } }));
160
155
  }
161
156
  _cancelDrag(cancelled) {
162
- if (this._pointerId == null)
163
- return;
164
- this._completePointerDrag(this._pointerId, cancelled);
157
+ if (cancelled)
158
+ this._pointer.cancel();
165
159
  }
166
160
  _handleKeydown(event) {
167
- if (this.disabled || !this.keyboard)
161
+ if (this.disabled || !this.keyboard || event.defaultPrevented || isKeyboardControlEvent(event, this._surface))
168
162
  return;
169
163
  const multiplier = event.shiftKey ? 10 : 1;
170
164
  const step = this.grid * multiplier;
package/dist/drawer.js CHANGED
@@ -1,12 +1,5 @@
1
+ import { focusedElement, focusableElements } from './internal/focus.js';
1
2
  const CONTENT_DESTROY_DELAY = 220;
2
- const FOCUSABLE_SELECTOR = [
3
- 'a[href]',
4
- 'button:not([disabled])',
5
- 'input:not([disabled])',
6
- 'select:not([disabled])',
7
- 'textarea:not([disabled])',
8
- '[tabindex]:not([tabindex="-1"])',
9
- ].join(',');
10
3
  const DRAWER_STYLES = `
11
4
  :host {
12
5
  position: fixed;
@@ -278,7 +271,7 @@ export class HYDrawer extends HTMLElement {
278
271
  if (this.open) {
279
272
  this._cancelContentDestroy();
280
273
  this._restoreContent();
281
- this._returnFocus ??= document.activeElement instanceof HTMLElement ? document.activeElement : null;
274
+ this._returnFocus ??= focusedElement(this.ownerDocument);
282
275
  this._panel.setAttribute('aria-hidden', 'false');
283
276
  this._syncPanelSemantics();
284
277
  this._attachDocumentListener();
@@ -329,14 +322,13 @@ export class HYDrawer extends HTMLElement {
329
322
  }
330
323
  if (event.key !== 'Tab' || !this.mask)
331
324
  return;
332
- const focusable = [this._closeButton, ...this.querySelectorAll(FOCUSABLE_SELECTOR)]
333
- .filter(element => !element.hidden && element.getAttribute('aria-hidden') !== 'true');
325
+ const focusable = focusableElements(this._panel);
334
326
  if (focusable.length === 0) {
335
327
  event.preventDefault();
336
328
  this._panel.focus();
337
329
  return;
338
330
  }
339
- const active = this.shadowRoot?.activeElement ?? document.activeElement;
331
+ const active = focusedElement(this.ownerDocument);
340
332
  const index = focusable.indexOf(active);
341
333
  if (event.shiftKey && index <= 0) {
342
334
  event.preventDefault();
package/dist/dropdown.js CHANGED
@@ -49,7 +49,6 @@ ${GE_MENU_ITEM_STYLES}
49
49
  this._root.append(this._style, this._triggerSlot, this._menu);
50
50
  }
51
51
  connectedCallback() {
52
- this._readItemsAttribute();
53
52
  this._render();
54
53
  this.addEventListener('click', this._onHostClick);
55
54
  this._syncDocumentListeners();
@@ -0,0 +1,29 @@
1
+ export interface HYDropzoneDropDetail {
2
+ readonly source: HTMLElement | null;
3
+ readonly type: string;
4
+ readonly data: unknown;
5
+ readonly files: readonly File[];
6
+ readonly effect: 'copy' | 'move';
7
+ readonly input: 'pointer' | 'keyboard' | 'native';
8
+ }
9
+ /** Drop target for typed application data and native files or text. */
10
+ export declare class HYDropzone extends HTMLElement {
11
+ private readonly _surface;
12
+ private readonly _native;
13
+ private _unregister;
14
+ private _listeners;
15
+ static get observedAttributes(): string[];
16
+ constructor();
17
+ connectedCallback(): void;
18
+ disconnectedCallback(): void;
19
+ attributeChangedCallback(name: string): void;
20
+ get accept(): string;
21
+ set accept(value: string);
22
+ get disabled(): boolean;
23
+ set disabled(value: boolean);
24
+ focus(options?: FocusOptions): void;
25
+ private _sync;
26
+ private _acceptsTypes;
27
+ private _drop;
28
+ }
29
+ export declare function defineDropzoneComponents(): void;
@@ -0,0 +1,89 @@
1
+ import { registerDropTarget } from './internal/drag-drop.js';
2
+ import { HYNativeDrop, hasFiles } from './internal/native-drop.js';
3
+ /** Drop target for typed application data and native files or text. */
4
+ export class HYDropzone extends HTMLElement {
5
+ _surface = document.createElement('div');
6
+ _native = new HYNativeDrop(this._surface, {
7
+ enabled: () => true,
8
+ accepts: transfer => !this.disabled && this._acceptsTypes(hasFiles(transfer) ? ['Files'] : [...transfer.types]),
9
+ active: active => this.toggleAttribute('dragover', active),
10
+ drop: event => {
11
+ const transfer = event.dataTransfer;
12
+ const files = [...transfer.files];
13
+ const type = files.length ? 'Files' : [...transfer.types].find(value => this._acceptsTypes([value])) || 'text/plain';
14
+ this._drop({ source: null, type, data: files.length ? null : transfer.getData(type), files, effect: 'copy', input: 'native' });
15
+ },
16
+ });
17
+ _unregister = null;
18
+ _listeners = null;
19
+ static get observedAttributes() { return ['accept', 'disabled', 'aria-label']; }
20
+ constructor() {
21
+ super();
22
+ const root = this.attachShadow({ mode: 'open' });
23
+ const style = document.createElement('style');
24
+ style.textContent = `
25
+ :host { display: block; min-width: 0; color: var(--hy-text-color, #eef0ff); font: var(--hy-font-size-sm, 12px)/var(--hy-line-height-relaxed, 1.5) var(--hy-font-family, system-ui, sans-serif); }
26
+ :host([hidden]) { display: none !important; }
27
+ .surface { box-sizing: border-box; min-height: var(--hy-dropzone-min-height, 120px); padding: var(--hy-dropzone-padding, var(--hy-space-8, 16px)); border: 1px dashed var(--hy-border-color, #343765); border-radius: var(--hy-dropzone-radius, var(--hy-radius-lg, 8px)); background: var(--hy-dropzone-bg-color, var(--hy-surface-color, #10132f)); }
28
+ :host([dragover]) .surface { border-color: var(--hy-drop-indicator-color, var(--hy-accent-color, #8177f2)); background: var(--hy-drop-target-bg-color, var(--hy-hover-bg-color, #242752)); }
29
+ .surface:focus-visible { outline: 2px solid var(--hy-focus-border-color, #8177f2); outline-offset: 2px; }
30
+ :host([disabled]) .surface { opacity: .5; }
31
+ `;
32
+ this._surface.className = 'surface';
33
+ this._surface.part.add('surface');
34
+ this._surface.setAttribute('role', 'group');
35
+ this._surface.append(document.createElement('slot'));
36
+ root.append(style, this._surface);
37
+ }
38
+ connectedCallback() {
39
+ this._listeners?.abort();
40
+ this._unregister?.();
41
+ this._listeners = new AbortController();
42
+ this._native.connect(this._listeners.signal);
43
+ this._unregister = registerDropTarget({
44
+ element: this,
45
+ accepts: payload => !this.disabled && this._acceptsTypes([payload.type]),
46
+ over: payload => {
47
+ if (this.hasAttribute('dragover'))
48
+ return;
49
+ this.setAttribute('dragover', '');
50
+ this.dispatchEvent(new CustomEvent('drag-enter', { detail: payload, bubbles: true, composed: true }));
51
+ },
52
+ leave: payload => {
53
+ this.removeAttribute('dragover');
54
+ this.dispatchEvent(new CustomEvent('drag-leave', { detail: payload, bubbles: true, composed: true }));
55
+ },
56
+ drop: payload => this._drop({ ...payload, files: [] }),
57
+ });
58
+ this._sync();
59
+ }
60
+ disconnectedCallback() { this._listeners?.abort(); this._listeners = null; this._unregister?.(); this._unregister = null; this.removeAttribute('dragover'); }
61
+ attributeChangedCallback(name) { if (name === 'accept' || name === 'disabled') {
62
+ this._native.reset();
63
+ this.removeAttribute('dragover');
64
+ } this._sync(); }
65
+ get accept() { return this.getAttribute('accept') ?? '*'; }
66
+ set accept(value) { this.setAttribute('accept', value); }
67
+ get disabled() { return this.hasAttribute('disabled'); }
68
+ set disabled(value) { this.toggleAttribute('disabled', value); }
69
+ focus(options) { this._surface.focus(options); }
70
+ _sync() {
71
+ this._surface.tabIndex = this.disabled ? -1 : 0;
72
+ this._surface.setAttribute('aria-disabled', String(this.disabled));
73
+ this._surface.setAttribute('aria-label', this.getAttribute('aria-label') || 'Drop zone. Press Enter to drop a picked-up item.');
74
+ }
75
+ _acceptsTypes(types) {
76
+ const accepted = this.accept.split(',').map(value => value.trim().toLowerCase()).filter(Boolean);
77
+ return accepted.includes('*') || types.some(type => accepted.includes(type.toLowerCase()));
78
+ }
79
+ _drop(detail) {
80
+ if (this.disabled || !this.isConnected || !this._acceptsTypes([detail.type]))
81
+ return false;
82
+ return this.dispatchEvent(new CustomEvent('data-drop', { detail, bubbles: true, composed: true, cancelable: true }));
83
+ }
84
+ }
85
+ export function defineDropzoneComponents() {
86
+ if (!customElements.get('hy-dropzone'))
87
+ customElements.define('hy-dropzone', HYDropzone);
88
+ }
89
+ //# sourceMappingURL=dropzone.js.map
package/dist/index.d.ts CHANGED
@@ -132,6 +132,12 @@ export { HYCard, defineCardComponents } from './card.js';
132
132
  export type { HYCardSize } from './card.js';
133
133
  export { HYRibbon, defineRibbonComponents } from './ribbon.js';
134
134
  export type { HYRibbonPlacement } from './ribbon.js';
135
+ export { HYDragSource, defineDragSourceComponents } from './drag-source.js';
136
+ export type { HYDragPayload, HYDragEndDetail } from './drag-source.js';
137
+ export { HYDropzone, defineDropzoneComponents } from './dropzone.js';
138
+ export type { HYDropzoneDropDetail } from './dropzone.js';
139
+ export { HYSortable, defineSortableComponents } from './sortable.js';
140
+ export type { HYSortableDirection, HYSortableChangeDetail, HYSortableEndDetail } from './sortable.js';
135
141
  export { HYDescriptions, defineDescriptionsComponents } from './descriptions.js';
136
142
  export type { HYDescriptionItem, HYDescriptionsLayout, HYDescriptionsSize } from './descriptions.js';
137
143
  export declare function defineHaiyueUI(): void;