@oscarpalmer/oui 0.20.0 → 0.22.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 (37) hide show
  1. package/dist/css/popover.css +9 -9
  2. package/dist/css/tooltip.css +20 -20
  3. package/dist/js/focus-trap/embedded.mjs +1 -1
  4. package/dist/js/focus-trap/standalone.mjs +1 -1
  5. package/dist/js/index.d.mts +47 -4
  6. package/dist/js/index.mjs +2537 -5
  7. package/dist/js/{attributable.d.mts → internal/attributable.d.mts} +1 -1
  8. package/dist/js/{attributable.mjs → internal/attributable.mjs} +1 -1
  9. package/dist/js/internal/draggable.d.mts +2 -0
  10. package/dist/js/internal/draggable.mjs +215 -0
  11. package/dist/js/{floatable.d.mts → internal/floatable.d.mts} +3 -3
  12. package/dist/js/internal/floatable.mjs +187 -0
  13. package/dist/js/movable-BKFgoaOl.d.mts +101 -0
  14. package/dist/js/movable.d.mts +2 -0
  15. package/dist/js/movable.mjs +152 -0
  16. package/dist/js/popover.mjs +2 -2
  17. package/dist/js/sortable.d.mts +2 -0
  18. package/dist/js/sortable.mjs +243 -0
  19. package/dist/js/splitter.mjs +4 -4
  20. package/dist/js/tooltip.d.mts +2 -2
  21. package/dist/js/tooltip.mjs +98 -100
  22. package/package.json +28 -18
  23. package/src/css/mixins.scss +8 -12
  24. package/src/css/popover.scss +1 -1
  25. package/src/css/tooltip.scss +22 -22
  26. package/src/js/focus-trap/standalone.ts +1 -1
  27. package/src/js/index.ts +3 -1
  28. package/src/js/internal/draggable.ts +515 -0
  29. package/src/js/internal/floatable.ts +373 -0
  30. package/src/js/movable.ts +260 -0
  31. package/src/js/popover.ts +2 -2
  32. package/src/js/sortable.ts +467 -0
  33. package/src/js/splitter.ts +1 -4
  34. package/src/js/tooltip.ts +132 -132
  35. package/dist/js/floatable.mjs +0 -284
  36. package/src/js/floatable.ts +0 -590
  37. /package/src/js/{attributable.ts → internal/attributable.ts} +0 -0
@@ -0,0 +1,467 @@
1
+ import type {EventPosition, PlainObject} from '@oscarpalmer/atoms/models';
2
+ import {createElement} from '@oscarpalmer/toretto/create';
3
+ import {getPosition} from '@oscarpalmer/toretto/event';
4
+ import {findAncestor, getElementFromPosition} from '@oscarpalmer/toretto/find';
5
+ import {attributable} from './internal/attributable';
6
+ import {
7
+ addDraggable,
8
+ Draggable,
9
+ removeDraggable,
10
+ type DraggableBound,
11
+ type DraggableState,
12
+ type DragMovePosition,
13
+ } from './internal/draggable';
14
+
15
+ // #region Types
16
+
17
+ export class Sortable extends Draggable {
18
+ connections: string | null;
19
+
20
+ origin: HTMLElement | undefined;
21
+
22
+ get dragged(): HTMLElement {
23
+ return getDragPlaceholder();
24
+ }
25
+
26
+ constructor(element: HTMLElement) {
27
+ super('sort', element, {
28
+ container: {
29
+ attribute: ATTRIBUTE_CONTAINER,
30
+ map: CONTAINERS,
31
+ },
32
+ direction: {
33
+ attribute: ATTRIBUTE_DIRECTION,
34
+ },
35
+ drag: {
36
+ onBegin,
37
+ onCancel,
38
+ onEnd,
39
+ onMove,
40
+ },
41
+ position: {
42
+ getOffset: getOffsetPosition,
43
+ getOriginal: getOriginalPosition,
44
+ },
45
+ });
46
+
47
+ this.connections = this.element.getAttribute(ATTRIBUTE_CONNECTIONS);
48
+ }
49
+ }
50
+
51
+ type SortablePlaceholder = {
52
+ drag: SortablePlaceholderDrag;
53
+ position: HTMLElement | undefined;
54
+ };
55
+
56
+ type SortablePlaceholderDrag = {
57
+ custom: HTMLElement | undefined;
58
+ default: HTMLElement | undefined;
59
+ };
60
+
61
+ // #endregion
62
+
63
+ // #region Functions
64
+
65
+ function afterEnd(
66
+ insert: 'append' | 'insert' | 'prepend' | undefined,
67
+ from: Sortable,
68
+ events: CustomEvent[],
69
+ elements: {origin?: HTMLElement; target: HTMLElement},
70
+ position: EventPosition | undefined,
71
+ valid: boolean,
72
+ ): void {
73
+ elements.origin?.removeAttribute(ATTRIBUTE_ORIGIN);
74
+
75
+ from.origin = undefined;
76
+
77
+ resetPlaceholders();
78
+
79
+ if (!valid || events.some(event => event.defaultPrevented)) {
80
+ return;
81
+ }
82
+
83
+ if (insert !== 'insert') {
84
+ elements.target.append(elements.origin!);
85
+
86
+ return;
87
+ }
88
+
89
+ if (position == null) {
90
+ return;
91
+ }
92
+
93
+ const {height, top} = elements.target.getBoundingClientRect();
94
+
95
+ let insertion: 'afterend' | 'beforebegin';
96
+
97
+ if (position.y < top + height / 2) {
98
+ insertion = INSERT_BEFORE;
99
+ } else {
100
+ insertion = INSERT_AFTER;
101
+ }
102
+
103
+ elements.target?.insertAdjacentElement(insertion, elements.origin!);
104
+
105
+ document.body.removeAttribute(ATTRIBUTE_ACTIVE);
106
+ }
107
+
108
+ function getDragPlaceholder(): HTMLElement {
109
+ placeholder.drag.default ??= createElement('div');
110
+
111
+ return placeholder.drag.custom ?? placeholder.drag.default;
112
+ }
113
+
114
+ function getOffsetPosition(
115
+ _: DraggableState,
116
+ draggable: Draggable,
117
+ element: HTMLElement,
118
+ position: EventPosition,
119
+ ): EventPosition {
120
+ const rectangle = element.getBoundingClientRect();
121
+
122
+ return {
123
+ x: draggable.horizontal ? 0 : position.x - rectangle.left,
124
+ y: draggable.vertical ? 0 : position.y - rectangle.top,
125
+ };
126
+ }
127
+
128
+ function getOriginalPosition(
129
+ _: DraggableState,
130
+ __: Draggable,
131
+ position: EventPosition,
132
+ ): EventPosition {
133
+ return position;
134
+ }
135
+
136
+ function getPositionPlaceholder(sortable: Sortable): HTMLElement {
137
+ placeholder.position ??= createElement('div');
138
+
139
+ placeholder.position.setAttribute(ATTRIBUTE_PLACEHOLDER_POSITION, '');
140
+
141
+ if (sortable.origin != null) {
142
+ placeholder.position.style.setProperty(
143
+ '--placeholder-height',
144
+ `${sortable.origin.getBoundingClientRect().height}px`,
145
+ );
146
+ }
147
+
148
+ return placeholder.position;
149
+ }
150
+
151
+ function onBegin(
152
+ _: MouseEvent | TouchEvent,
153
+ state: DraggableState,
154
+ draggable: Draggable,
155
+ element: HTMLElement,
156
+ ): void {
157
+ const sortable = draggable as Sortable;
158
+
159
+ sortable.origin = element;
160
+
161
+ setPlaceholder(state, sortable, element);
162
+
163
+ element.setAttribute(ATTRIBUTE_ORIGIN, '');
164
+
165
+ document.body.setAttribute(ATTRIBUTE_ACTIVE, '');
166
+ }
167
+
168
+ function onCancel(_: DraggableState, draggable: Draggable): void {
169
+ const sortable = draggable as Sortable;
170
+
171
+ const {origin} = sortable;
172
+
173
+ origin?.removeAttribute(ATTRIBUTE_ORIGIN);
174
+
175
+ document.body.removeAttribute(ATTRIBUTE_ACTIVE);
176
+
177
+ resetPlaceholders();
178
+
179
+ sortable.origin = undefined;
180
+ }
181
+
182
+ function onEnd(
183
+ event: MouseEvent | TouchEvent,
184
+ state: DraggableState,
185
+ draggable: Draggable,
186
+ reset: () => void,
187
+ ): void {
188
+ const from = draggable as Sortable;
189
+ const {origin} = from;
190
+
191
+ const position = getPosition(event) ?? {x: 0, y: 0};
192
+
193
+ const target = getElementFromPosition(position)[0];
194
+
195
+ let element = findAncestor(target, SELECTOR_ITEM) as HTMLElement;
196
+
197
+ if (element?.hasAttribute(ATTRIBUTE_PLACEHOLDER_POSITION)) {
198
+ const position = element.getAttribute(ATTRIBUTE_PLACEHOLDER_POSITION);
199
+
200
+ if (position === 'after') {
201
+ element = element.previousElementSibling as HTMLElement;
202
+ } else {
203
+ element = element.nextElementSibling as HTMLElement;
204
+ }
205
+ }
206
+
207
+ let insert: 'append' | 'insert' | 'prepend' | undefined = 'insert';
208
+ let to: Sortable | undefined;
209
+
210
+ if (element == null) {
211
+ element = findAncestor(target, SELECTOR) as HTMLElement;
212
+
213
+ to = state.instances.get(element)?.sortable;
214
+
215
+ insert = element == null ? undefined : element.children.length === 0 ? 'append' : 'prepend';
216
+ } else {
217
+ to = state.instances.get(element.parentElement!)?.sortable;
218
+ }
219
+
220
+ const valid = insert != null && validatePosition(state, from, to, position);
221
+
222
+ const detail = valid
223
+ ? {
224
+ from: {
225
+ element: origin,
226
+ position: {...state.original},
227
+ },
228
+ to: {
229
+ element: element,
230
+ position: {...position},
231
+ },
232
+ }
233
+ : undefined;
234
+
235
+ const options = {
236
+ detail,
237
+ bubbles: false,
238
+ cancelable: true,
239
+ };
240
+
241
+ const fromEvent = new CustomEvent(EVENT_END, options);
242
+ const toEvent = new CustomEvent(EVENT_END, options);
243
+
244
+ from.element.dispatchEvent(fromEvent);
245
+
246
+ if (from !== to) {
247
+ to?.element.dispatchEvent(toEvent);
248
+ }
249
+
250
+ setTimeout(() => {
251
+ afterEnd(insert, from, [fromEvent, toEvent], {origin, target: element}, position, valid);
252
+
253
+ reset();
254
+ });
255
+ }
256
+
257
+ function onMove(
258
+ event: MouseEvent | TouchEvent,
259
+ state: DraggableState,
260
+ draggable: Draggable,
261
+ position: DragMovePosition,
262
+ ): PlainObject | undefined {
263
+ const from = draggable as Sortable;
264
+
265
+ const fromElement = from.origin;
266
+ const toElement = findAncestor(event, SELECTOR_ITEM) as HTMLElement;
267
+
268
+ let to: Sortable | undefined;
269
+
270
+ if (toElement == null) {
271
+ to = state.instances.get(findAncestor(event, SELECTOR) as HTMLElement)?.sortable;
272
+ } else {
273
+ to = state.instances.get(toElement.parentElement!)?.sortable;
274
+ }
275
+
276
+ if (!validatePosition(state, from, to, position.original)) {
277
+ return;
278
+ }
279
+
280
+ const placeholder = getPositionPlaceholder(from);
281
+
282
+ if (toElement != null && toElement !== fromElement) {
283
+ const {height, top} = toElement.getBoundingClientRect();
284
+
285
+ let place: 'after' | 'before';
286
+
287
+ if (position.calculated.y < top + height / 2) {
288
+ place = 'before';
289
+
290
+ toElement.insertAdjacentElement(INSERT_BEFORE, placeholder);
291
+ } else {
292
+ place = 'after';
293
+
294
+ toElement.insertAdjacentElement(INSERT_AFTER, placeholder);
295
+ }
296
+
297
+ placeholder.setAttribute(ATTRIBUTE_PLACEHOLDER_POSITION, place);
298
+ } else {
299
+ placeholder.remove();
300
+ }
301
+
302
+ return {
303
+ from: {
304
+ element: fromElement,
305
+ position: {...state.original},
306
+ },
307
+ to: {
308
+ element: toElement,
309
+ position: {...position.calculated},
310
+ },
311
+ };
312
+ }
313
+
314
+ function resetPlaceholders(): void {
315
+ placeholder.drag.custom?.remove();
316
+ placeholder.drag.default?.remove();
317
+ placeholder.position?.remove();
318
+
319
+ placeholder.drag.custom = undefined;
320
+ }
321
+
322
+ function setPlaceholder(state: DraggableState, sortable: Sortable, origin: HTMLElement): void {
323
+ const element = getDragPlaceholder();
324
+
325
+ placeholder.drag.default!.innerHTML = '';
326
+
327
+ updatePlaceholder(sortable, element);
328
+
329
+ const event = new CustomEvent(EVENT_PLACEHOLDER, {
330
+ cancelable: true,
331
+ detail: {
332
+ origin,
333
+ parent: sortable.element,
334
+ placeholder: element,
335
+ create: (value: unknown) => {
336
+ if (!(value instanceof HTMLElement)) {
337
+ return;
338
+ }
339
+
340
+ placeholder.drag.custom = value;
341
+
342
+ updatePlaceholder(sortable, value);
343
+
344
+ state.element = {
345
+ rectangle: value.getBoundingClientRect(),
346
+ node: value,
347
+ };
348
+ },
349
+ },
350
+ });
351
+
352
+ sortable.element.dispatchEvent(event);
353
+
354
+ setTimeout(() => {
355
+ if (!event.defaultPrevented && placeholder.drag.custom == null) {
356
+ placeholder.drag.default?.append(element.cloneNode(true));
357
+ }
358
+
359
+ document.body.append(placeholder.drag.custom ?? placeholder.drag.default ?? '');
360
+ });
361
+ }
362
+
363
+ function updatePlaceholder(sortable: Sortable, element: HTMLElement): void {
364
+ element.style.position = 'fixed';
365
+
366
+ element.setAttribute(ATTRIBUTE_PLACEHOLDER_DRAG, '');
367
+
368
+ if (sortable.horizontal) {
369
+ element.setAttribute(ATTRIBUTE_HORIZONTAL, '');
370
+ } else {
371
+ element.removeAttribute(ATTRIBUTE_HORIZONTAL);
372
+ }
373
+
374
+ if (sortable.vertical) {
375
+ element.setAttribute(ATTRIBUTE_VERTICAL, '');
376
+ } else {
377
+ element.removeAttribute(ATTRIBUTE_VERTICAL);
378
+ }
379
+ }
380
+
381
+ function validatePosition(
382
+ _: DraggableState,
383
+ from: Sortable,
384
+ to: Sortable | undefined,
385
+ position: EventPosition,
386
+ ): boolean {
387
+ let valid = true;
388
+
389
+ const rectangle = from.origin?.getBoundingClientRect();
390
+
391
+ if (rectangle != null && !from.horizontal) {
392
+ valid = position.x >= rectangle.left && position.x <= rectangle.right;
393
+ }
394
+
395
+ if (valid && rectangle != null && !from.vertical) {
396
+ valid = position.y >= rectangle.top && position.y <= rectangle.bottom;
397
+ }
398
+
399
+ if (valid && from.connections != null) {
400
+ valid = to?.element.matches(from.connections) ?? true;
401
+ }
402
+
403
+ if (valid && to?.connections != null) {
404
+ valid = from.element.matches(to.connections);
405
+ }
406
+
407
+ return valid;
408
+ }
409
+
410
+ // #endregion
411
+
412
+ // #region Variables
413
+
414
+ const ATTRIBUTE = 'oui-sortable';
415
+
416
+ const ATTRIBUTE_ACTIVE = `${ATTRIBUTE}-active`;
417
+
418
+ const ATTRIBUTE_CONNECTIONS = `${ATTRIBUTE}-connections`;
419
+
420
+ const ATTRIBUTE_CONTAINER = `${ATTRIBUTE}-container`;
421
+
422
+ const ATTRIBUTE_DIRECTION = `${ATTRIBUTE}-direction`;
423
+
424
+ const ATTRIBUTE_HORIZONTAL = `${ATTRIBUTE}-horizontal`;
425
+
426
+ const ATTRIBUTE_ORIGIN = `${ATTRIBUTE}-origin`;
427
+
428
+ const ATTRIBUTE_PLACEHOLDER_DRAG = `${ATTRIBUTE}-placeholder-drag`;
429
+
430
+ const ATTRIBUTE_PLACEHOLDER_POSITION = `${ATTRIBUTE}-placeholder-position`;
431
+
432
+ const ATTRIBUTE_VERTICAL = `${ATTRIBUTE}-vertical`;
433
+
434
+ const CONTAINERS = new Map<HTMLElement, Set<Sortable>>();
435
+
436
+ const EVENT_END = 'sort:end';
437
+
438
+ const EVENT_PLACEHOLDER = 'sort:placeholder';
439
+
440
+ const INSERT_AFTER = 'afterend';
441
+
442
+ const INSERT_BEFORE = 'beforebegin';
443
+
444
+ const SELECTOR = `[${ATTRIBUTE}]`;
445
+
446
+ const SELECTOR_ITEM = `[${ATTRIBUTE}] > *`;
447
+
448
+ const bound: DraggableBound = {
449
+ constructor: Sortable,
450
+ property: 'sortable',
451
+ };
452
+
453
+ const placeholder: SortablePlaceholder = {
454
+ drag: {
455
+ custom: undefined,
456
+ default: undefined,
457
+ },
458
+ position: undefined,
459
+ };
460
+
461
+ // #endregion
462
+
463
+ // #region Initialization
464
+
465
+ attributable(ATTRIBUTE, addDraggable.bind(bound), removeDraggable.bind(bound));
466
+
467
+ // #endregion
@@ -383,10 +383,7 @@ function onObservation(entries: ResizeObserverEntry[]): void {
383
383
  }
384
384
 
385
385
  function onPointerdown(event: Event): void {
386
- if (
387
- (event.type === 'mousedown' && supportsTouch.value) ||
388
- (event.type === 'touchstart' && !supportsTouch.value)
389
- ) {
386
+ if (supportsTouch.value ? event.type === 'mousedown' : event.type === 'touchstart') {
390
387
  return;
391
388
  }
392
389