@oscarpalmer/oui 0.20.0 → 0.21.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.
@@ -1,6 +1,6 @@
1
+ import { clamp } from "@oscarpalmer/atoms/number";
1
2
  import { on } from "@oscarpalmer/toretto/event";
2
3
  import { findAncestor } from "@oscarpalmer/toretto/find";
3
- import { clamp } from "@oscarpalmer/atoms/number";
4
4
  import { getFocusable } from "@oscarpalmer/toretto/focusable";
5
5
  //#region src/js/focus-trap/embedded.ts
6
6
  var FocusTrap = class {
@@ -1,4 +1,4 @@
1
- import { attributable } from "../attributable.mjs";
1
+ import { attributable } from "../internal/attributable.mjs";
2
2
  import { FOCUSTRAPS_ALL, FOCUS_TRAP_SELECTOR, createFocusTrap } from "./embedded.mjs";
3
3
  //#region src/js/focus-trap/standalone.ts
4
4
  function removeFocusTraps(element) {
@@ -1,4 +1,4 @@
1
1
  import { OuiPopoverElement } from "./popover.mjs";
2
2
  import { OuiSplitterElement } from "./splitter.mjs";
3
- import { setDelay } from "./tooltip.mjs";
4
- export { OuiPopoverElement, OuiSplitterElement, setDelay };
3
+ import { setTooltipDelay } from "./tooltip.mjs";
4
+ export { OuiPopoverElement, OuiSplitterElement, setTooltipDelay };
package/dist/js/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  import "./focus-trap/standalone.mjs";
2
+ import "./movable.mjs";
2
3
  import { OuiPopoverElement } from "./popover.mjs";
4
+ import "./sortable.mjs";
3
5
  import { OuiSplitterElement } from "./splitter.mjs";
4
- import { setDelay } from "./tooltip.mjs";
5
- export { OuiPopoverElement, OuiSplitterElement, setDelay };
6
+ import { setTooltipDelay } from "./tooltip.mjs";
7
+ export { OuiPopoverElement, OuiSplitterElement, setTooltipDelay };
@@ -1,4 +1,4 @@
1
- //#region src/js/attributable.d.ts
1
+ //#region src/js/internal/attributable.d.ts
2
2
  declare function attributable(attribute: string, add: (element: HTMLElement) => void, remove: (element: HTMLElement) => void): void;
3
3
  //#endregion
4
4
  export { attributable };
@@ -1,4 +1,4 @@
1
- //#region src/js/attributable.ts
1
+ //#region src/js/internal/attributable.ts
2
2
  function attributable(attribute, add, remove) {
3
3
  new MutationObserver((entries) => {
4
4
  const { length } = entries;
@@ -0,0 +1,2 @@
1
+ import { a as DraggableState, i as DraggableBound, n as DragMovePosition, o as addDraggable, r as Draggable, s as removeDraggable } from "../movable-BEo2LbUI.mjs";
2
+ export { DragMovePosition, Draggable, DraggableBound, DraggableState, addDraggable, removeDraggable };
@@ -0,0 +1,212 @@
1
+ import { clamp } from "@oscarpalmer/atoms/number";
2
+ import { getPosition, on } from "@oscarpalmer/toretto/event";
3
+ import { findAncestor } from "@oscarpalmer/toretto/find";
4
+ import { toggleStyles } from "@oscarpalmer/toretto/style";
5
+ import supportsTouch from "@oscarpalmer/toretto/touch";
6
+ //#region src/js/internal/draggable.ts
7
+ var Draggable = class {
8
+ container;
9
+ hasHandles;
10
+ horizontal = true;
11
+ vertical = true;
12
+ constructor(prefix, element, options) {
13
+ this.prefix = prefix;
14
+ this.element = element;
15
+ this.options = options;
16
+ setContainer(this, options);
17
+ setDirection(this, options);
18
+ }
19
+ destroy() {
20
+ unsetContainer(this, this.options);
21
+ this.container = void 0;
22
+ this.element = void 0;
23
+ this.options = void 0;
24
+ }
25
+ };
26
+ function addDraggable(element) {
27
+ let instances = state.instances.get(element);
28
+ if (instances == null) {
29
+ instances = {};
30
+ state.instances.set(element, instances);
31
+ }
32
+ instances[this.property] ??= new this.constructor(element);
33
+ }
34
+ function getHandle(event, isMovable) {
35
+ return findAncestor(event, isMovable ? SELECTOR_MOVABLE_HANDLE : SELECTOR_SORTABLE_HANDLE);
36
+ }
37
+ function getNextPosition(draggable, position) {
38
+ let x = state.original?.x ?? 0;
39
+ let y = state.original?.y ?? 0;
40
+ if (draggable.horizontal) {
41
+ x = position.x - (state.offset?.x ?? 0);
42
+ if (state.container != null && state.element != null) x = clamp(x, state.container.left, state.container.right - state.element.rectangle.width);
43
+ }
44
+ if (draggable.vertical) {
45
+ y = position.y - (state.offset?.y ?? 0);
46
+ if (state.container != null && state.element != null) y = clamp(y, state.container.top, state.container.bottom - state.element.rectangle.height);
47
+ }
48
+ return {
49
+ x,
50
+ y
51
+ };
52
+ }
53
+ function onKeydown(event) {
54
+ if (current == null || event.key !== KEY_ESCAPE) return;
55
+ const { origin } = current;
56
+ current.options.drag.onCancel?.(state, current);
57
+ current.element.dispatchEvent(new CustomEvent(`${current.prefix}:cancel`, { detail: { element: origin } }));
58
+ reset();
59
+ }
60
+ function onPointerdown(event) {
61
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || (supportsTouch.value ? event.type === "mousedown" : event.type === "touchstart" || event.button !== 0)) return;
62
+ const container = findAncestor(event, SELECTOR);
63
+ const instances = state.instances.get(container);
64
+ if (instances == null) return;
65
+ const isMovable = container.hasAttribute(ATTRIBUTE_MOVABLE);
66
+ const instance = isMovable ? instances.movable : instances.sortable;
67
+ if (instance == null) return;
68
+ if (supportsTouch.value) event.preventDefault();
69
+ let element = container;
70
+ if (!isMovable) {
71
+ element = findAncestor(event, SELECTOR_SORTABLE_ITEM);
72
+ if (element == null || element.parentElement !== container) return;
73
+ }
74
+ let handle = element;
75
+ instance.hasHandles ??= element.querySelector(SELECTOR_SORTABLE_HANDLE) != null;
76
+ if (instance.hasHandles) handle = getHandle(event, isMovable);
77
+ if (handle == null) return;
78
+ const { dragged } = instance;
79
+ const rectangle = dragged.getBoundingClientRect();
80
+ const position = getPosition(event) ?? {
81
+ x: 0,
82
+ y: 0
83
+ };
84
+ current = instance;
85
+ state.container = instance.container?.getBoundingClientRect();
86
+ state.element = {
87
+ rectangle,
88
+ node: dragged
89
+ };
90
+ state.offset = instance.options.position.getOffset(state, instance, element, position);
91
+ state.original = instance.options.position.getOriginal(state, instance, position);
92
+ const begin = new CustomEvent(`${instance.prefix}:begin`, {
93
+ detail: {
94
+ element,
95
+ position: { ...state.original }
96
+ },
97
+ cancelable: true
98
+ });
99
+ instance.element.dispatchEvent(begin);
100
+ setTimeout(() => {
101
+ if (begin.defaultPrevented) {
102
+ current?.options.drag.onCancel(state, instance);
103
+ reset();
104
+ return;
105
+ }
106
+ const next = getNextPosition(instance, position);
107
+ dragged.style.position = "fixed";
108
+ dragged.style.inset = `${next.y}px auto auto ${next.x}px`;
109
+ instance.options.drag.onBegin(event, state, instance, element, handle, next);
110
+ styleToggler.set();
111
+ });
112
+ }
113
+ function onPointermove(event) {
114
+ if (current == null) return;
115
+ const position = getPosition(event);
116
+ if (position == null) return;
117
+ const next = getNextPosition(current, position);
118
+ if (state.element != null) state.element.node.style.inset = `${next.y}px auto auto ${next.x}px`;
119
+ const detail = current.options.drag.onMove(event, state, current, {
120
+ calculated: next,
121
+ original: position
122
+ });
123
+ current.element.dispatchEvent(new CustomEvent(`${current.prefix}:move`, { detail }));
124
+ }
125
+ function onPointerup(event) {
126
+ current?.options.drag.onEnd?.(event, state, current, reset);
127
+ }
128
+ function removeDraggable(element) {
129
+ const { property } = this;
130
+ const instances = state.instances.get(element);
131
+ if (instances == null || instances[property] == null) return;
132
+ instances[property]?.destroy();
133
+ instances[property] = void 0;
134
+ if (instances[properties[property]] == null) state.instances.delete(element);
135
+ }
136
+ function reset() {
137
+ current = void 0;
138
+ state.container = void 0;
139
+ state.element = void 0;
140
+ state.offset = void 0;
141
+ state.original = void 0;
142
+ styleToggler.remove();
143
+ }
144
+ function setContainer(draggable, options) {
145
+ const selector = draggable.element.getAttribute(options.container.attribute);
146
+ if (typeof selector !== "string") return;
147
+ draggable.container = globals.has(selector) ? draggable.element.ownerDocument.body : document.querySelector(selector) ?? void 0;
148
+ if (draggable.container == null) return;
149
+ const set = options.container.map.get(draggable.container);
150
+ if (set == null) options.container.map.set(draggable.container, new Set([draggable]));
151
+ else set.add(draggable);
152
+ options.container.onInitialize?.(draggable);
153
+ }
154
+ function setDirection(draggable, options) {
155
+ const direction = draggable.element.getAttribute(options.direction.attribute);
156
+ if (horizontals.has(direction)) draggable.vertical = false;
157
+ else if (verticals.has(direction)) draggable.horizontal = false;
158
+ options.direction.onAfter?.(draggable);
159
+ }
160
+ function unsetContainer(draggable, options) {
161
+ const { container } = draggable;
162
+ if (container == null) return;
163
+ const set = options.container.map.get(container);
164
+ let empty = false;
165
+ if (set != null) {
166
+ set.delete(draggable);
167
+ if (set.size === 0) {
168
+ empty = true;
169
+ options.container.map.delete(container);
170
+ }
171
+ }
172
+ options.container.onDestroy?.(draggable, container, empty);
173
+ }
174
+ const ATTRIBUTE_MOVABLE = "oui-movable";
175
+ const ATTRIBUTE_SORTABLE = "oui-sortable";
176
+ const KEY_ESCAPE = "Escape";
177
+ const SELECTOR = `[${ATTRIBUTE_MOVABLE}], [${ATTRIBUTE_SORTABLE}]`;
178
+ const SELECTOR_MOVABLE_HANDLE = `[${ATTRIBUTE_MOVABLE}-handle]`;
179
+ const SELECTOR_SORTABLE_HANDLE = `[${ATTRIBUTE_SORTABLE}-handle]`;
180
+ const SELECTOR_SORTABLE_ITEM = `[${ATTRIBUTE_SORTABLE}] > *`;
181
+ const globals = new Set([
182
+ "body",
183
+ "document",
184
+ "window"
185
+ ]);
186
+ const horizontals = new Set(["horizontal", "x"]);
187
+ const properties = {
188
+ movable: "sortable",
189
+ sortable: "movable"
190
+ };
191
+ const state = {
192
+ container: void 0,
193
+ element: void 0,
194
+ instances: /* @__PURE__ */ new WeakMap(),
195
+ offset: void 0,
196
+ original: void 0
197
+ };
198
+ const styleToggler = toggleStyles(document.body, {
199
+ touchAction: "none",
200
+ userSelect: "none",
201
+ webkitUserSelect: "none"
202
+ });
203
+ const verticals = new Set(["vertical", "y"]);
204
+ let current;
205
+ on(document, "keydown", onKeydown);
206
+ on(document, "mousedown", onPointerdown);
207
+ on(document, "pointermove", onPointermove);
208
+ on(document, "pointerup", onPointerup);
209
+ on(document, "touchcancel", onPointerup, { passive: false });
210
+ on(document, "touchstart", onPointerdown, { passive: false });
211
+ //#endregion
212
+ export { Draggable, addDraggable, removeDraggable };
@@ -1,4 +1,4 @@
1
- //#region src/js/floatable.d.ts
1
+ //#region src/js/internal/floatable.d.ts
2
2
  declare class Floatable {
3
3
  readonly options: Options;
4
4
  active: boolean;
@@ -1,6 +1,6 @@
1
1
  import { on } from "@oscarpalmer/toretto/event";
2
2
  import { findAncestor } from "@oscarpalmer/toretto/find";
3
- //#region src/js/floatable.ts
3
+ //#region src/js/internal/floatable.ts
4
4
  var Floatable = class {
5
5
  active = false;
6
6
  disabled = false;
@@ -0,0 +1,101 @@
1
+ import { EventPosition, PlainObject } from "@oscarpalmer/atoms/models";
2
+
3
+ //#region src/js/sortable.d.ts
4
+ declare class Sortable extends Draggable {
5
+ connections: string | null;
6
+ origin: HTMLElement | undefined;
7
+ get dragged(): HTMLElement;
8
+ constructor(element: HTMLElement);
9
+ }
10
+ //#endregion
11
+ //#region src/js/internal/draggable.d.ts
12
+ declare abstract class Draggable {
13
+ prefix: string;
14
+ element: HTMLElement;
15
+ options: DraggableOptions;
16
+ container: HTMLElement | undefined;
17
+ hasHandles: boolean | undefined;
18
+ horizontal: boolean;
19
+ vertical: boolean;
20
+ abstract readonly dragged: HTMLElement;
21
+ abstract origin: HTMLElement | undefined;
22
+ constructor(prefix: string, element: HTMLElement, options: DraggableOptions);
23
+ destroy(): void;
24
+ }
25
+ type DragBegin = (event: MouseEvent | TouchEvent, state: DraggableState, draggable: Draggable, element: HTMLElement, handle: HTMLElement, position: EventPosition) => void;
26
+ type DragCancel = (state: DraggableState, draggable: Draggable) => void;
27
+ type DragEnd = (event: MouseEvent | TouchEvent, state: DraggableState, draggable: Draggable, reset: () => void) => void;
28
+ type DragMove = (event: MouseEvent | TouchEvent, state: DraggableState, draggable: Draggable, position: DragMovePosition) => PlainObject | undefined;
29
+ type DragMovePosition = {
30
+ calculated: EventPosition;
31
+ original: EventPosition;
32
+ };
33
+ type DraggableBound<Property extends keyof DraggableInstances = keyof DraggableInstances, Instance extends DraggableInstances[Property] = DraggableInstances[Property]> = {
34
+ constructor: new (element: HTMLElement) => Instance;
35
+ property: Property;
36
+ };
37
+ type DraggableInstances = {
38
+ movable?: Movable;
39
+ sortable?: Sortable;
40
+ };
41
+ type DraggableOptions = {
42
+ container: DraggableOptionsContainer;
43
+ direction: DraggableOptionsDirection;
44
+ drag: DraggableOptionsDrag;
45
+ position: DraggableOptionsPosition;
46
+ };
47
+ type DraggableOptionsContainer = {
48
+ attribute: string;
49
+ map: Map<HTMLElement, Set<Draggable>>;
50
+ onDestroy?: DraggableOptionsContainerOnDestroy;
51
+ onInitialize?: DraggableOptionsContainerOnInitialize;
52
+ };
53
+ type DraggableOptionsContainerOnDestroy = (draggable: Draggable, container: HTMLElement, empty: boolean) => void;
54
+ type DraggableOptionsContainerOnInitialize = (draggable: Draggable) => void;
55
+ type DraggableOptionsDirection = {
56
+ attribute: string;
57
+ onAfter?: DraggableOptionsDirectionOnAfter;
58
+ };
59
+ type DraggableOptionsDirectionOnAfter = (draggable: Draggable) => void;
60
+ type DraggableOptionsDrag = {
61
+ onBegin: DragBegin;
62
+ onCancel: DragCancel;
63
+ onEnd: DragEnd;
64
+ onMove: DragMove;
65
+ };
66
+ type DraggableOptionsPosition = {
67
+ getOffset: DraggableOptionsPositionGetOffset;
68
+ getOriginal: DraggableOptionsPositionGetOriginal;
69
+ };
70
+ type DraggableOptionsPositionGetOffset = (state: DraggableState, draggable: Draggable, element: HTMLElement, position: EventPosition) => EventPosition;
71
+ type DraggableOptionsPositionGetOriginal = (state: DraggableState, draggable: Draggable, position: EventPosition) => EventPosition;
72
+ type DraggableState = {
73
+ container: DOMRect | undefined;
74
+ element: DraggableStateElement | undefined;
75
+ instances: WeakMap<HTMLElement, DraggableInstances>;
76
+ offset: EventPosition | undefined;
77
+ original: EventPosition | undefined;
78
+ };
79
+ type DraggableStateElement = {
80
+ node: HTMLElement;
81
+ rectangle: DOMRect;
82
+ };
83
+ declare function addDraggable<Property extends keyof DraggableInstances, Instance extends DraggableInstances[Property]>(this: DraggableBound<Property, Instance>, element: HTMLElement): void;
84
+ declare function removeDraggable<Property extends keyof DraggableInstances, Instance extends DraggableInstances[Property]>(this: {
85
+ constructor: new (element: HTMLElement) => Instance;
86
+ property: Property;
87
+ }, element: HTMLElement): void;
88
+ //#endregion
89
+ //#region src/js/movable.d.ts
90
+ declare class Movable extends Draggable {
91
+ inset: string;
92
+ moved: boolean;
93
+ offset: Partial<EventPosition> | undefined;
94
+ position: string;
95
+ get dragged(): HTMLElement;
96
+ get origin(): HTMLElement | undefined;
97
+ constructor(element: HTMLElement);
98
+ destroy(): void;
99
+ }
100
+ //#endregion
101
+ export { DraggableState as a, Sortable as c, DraggableBound as i, DragMovePosition as n, addDraggable as o, Draggable as r, removeDraggable as s, Movable as t };
@@ -0,0 +1,2 @@
1
+ import { t as Movable } from "./movable-BEo2LbUI.mjs";
2
+ export { Movable };
@@ -0,0 +1,152 @@
1
+ import { attributable } from "./internal/attributable.mjs";
2
+ import { Draggable, addDraggable, removeDraggable } from "./internal/draggable.mjs";
3
+ import { getPosition, on } from "@oscarpalmer/toretto/event";
4
+ //#region src/js/movable.ts
5
+ var Movable = class extends Draggable {
6
+ inset;
7
+ moved = false;
8
+ offset;
9
+ position;
10
+ get dragged() {
11
+ return this.element;
12
+ }
13
+ get origin() {
14
+ return this.element;
15
+ }
16
+ constructor(element) {
17
+ super("move", element, {
18
+ container: {
19
+ attribute: ATTRIBUTE_CONTAINER,
20
+ map: CONTAINERS,
21
+ onDestroy: destroyContainer,
22
+ onInitialize: initializeContainer
23
+ },
24
+ direction: {
25
+ attribute: ATTRIBUTE_DIRECTION,
26
+ onAfter: afterDirection
27
+ },
28
+ drag: {
29
+ onBegin,
30
+ onCancel,
31
+ onEnd,
32
+ onMove
33
+ },
34
+ position: {
35
+ getOffset: getOffsetPosition,
36
+ getOriginal: getOriginalPosition
37
+ }
38
+ });
39
+ this.hasHandles = element.querySelector(SELECTOR_HANDLE) != null;
40
+ this.inset = element.style.inset;
41
+ this.position = element.style.position;
42
+ }
43
+ destroy() {
44
+ super.destroy();
45
+ MOVED.delete(this);
46
+ this.element.style.inset = this.inset;
47
+ this.element.style.position = this.position;
48
+ }
49
+ };
50
+ function afterDirection(draggable) {
51
+ const container = draggable.container?.getBoundingClientRect();
52
+ if (container != null) setOffset(draggable, container);
53
+ }
54
+ function destroyContainer(_, container, empty) {
55
+ if (empty) resizer.unobserve(container);
56
+ }
57
+ function getOffsetPosition(state, _, __, position) {
58
+ return {
59
+ x: position.x - (state.element?.rectangle.left ?? 0),
60
+ y: position.y - (state.element?.rectangle.top ?? 0)
61
+ };
62
+ }
63
+ function getOriginalPosition(state) {
64
+ return {
65
+ x: state.element?.rectangle.left ?? 0,
66
+ y: state.element?.rectangle.top ?? 0
67
+ };
68
+ }
69
+ function initializeContainer(draggable) {
70
+ resizer.observe(draggable.container);
71
+ }
72
+ function onBegin() {}
73
+ function onCancel(state, draggable) {
74
+ const { element, inset, moved, position } = draggable;
75
+ const { x, y } = state.original ?? {
76
+ x: 0,
77
+ y: 0
78
+ };
79
+ if (moved) element.style.inset = `${y}px auto auto ${x}px`;
80
+ else {
81
+ element.style.position = position;
82
+ element.style.inset = inset;
83
+ }
84
+ }
85
+ function onEnd(event, state, draggable, reset) {
86
+ const movable = draggable;
87
+ if (!movable.moved) {
88
+ movable.moved = true;
89
+ if (movable.container != null) MOVED.add(movable);
90
+ }
91
+ const container = movable.container?.getBoundingClientRect();
92
+ if (container != null) setOffset(movable, container);
93
+ const end = new CustomEvent(EVENT_END, {
94
+ detail: {
95
+ from: { ...state.original },
96
+ to: getPosition(event)
97
+ },
98
+ cancelable: true
99
+ });
100
+ movable.element.dispatchEvent(end);
101
+ setTimeout(() => {
102
+ if (end.defaultPrevented) onCancel(state, draggable);
103
+ reset();
104
+ });
105
+ }
106
+ function onMove(_, state, __, position) {
107
+ return {
108
+ from: { ...state.original },
109
+ to: { ...position.calculated }
110
+ };
111
+ }
112
+ function onResize(entries) {
113
+ const { length } = entries;
114
+ for (let index = 0; index < length; index += 1) {
115
+ const entry = entries[index];
116
+ const set = CONTAINERS.get(entry.target);
117
+ if (set == null) continue;
118
+ for (const movable of set) reposition(movable);
119
+ }
120
+ }
121
+ function onScroll() {
122
+ for (const moved of MOVED) reposition(moved);
123
+ }
124
+ function reposition(movable) {
125
+ if (!movable.moved || movable.container == null) return;
126
+ const container = movable.container.getBoundingClientRect();
127
+ const x = container.left + (movable.offset?.x ?? 0);
128
+ const y = container.top + (movable.offset?.y ?? 0);
129
+ movable.element.style.inset = `${y}px auto auto ${x}px`;
130
+ }
131
+ function setOffset(movable, container) {
132
+ movable.offset = {};
133
+ const element = movable.element.getBoundingClientRect();
134
+ movable.offset.y = element.top - container.top;
135
+ movable.offset.x = element.left - container.left;
136
+ }
137
+ const ATTRIBUTE = "oui-movable";
138
+ const ATTRIBUTE_CONTAINER = `${ATTRIBUTE}-container`;
139
+ const ATTRIBUTE_DIRECTION = `${ATTRIBUTE}-direction`;
140
+ const CONTAINERS = /* @__PURE__ */ new Map();
141
+ const EVENT_END = "move:end";
142
+ const MOVED = /* @__PURE__ */ new Set();
143
+ const SELECTOR_HANDLE = `[${ATTRIBUTE}-handle]`;
144
+ const bound = {
145
+ constructor: Movable,
146
+ property: "movable"
147
+ };
148
+ const resizer = new ResizeObserver(onResize);
149
+ attributable(ATTRIBUTE, addDraggable.bind(bound), removeDraggable.bind(bound));
150
+ on(document, "scroll", onScroll);
151
+ //#endregion
152
+ export { Movable };
@@ -1,5 +1,5 @@
1
- import { Floatable } from "./floatable.mjs";
2
1
  import { createFocusTrap } from "./focus-trap/embedded.mjs";
2
+ import { Floatable } from "./internal/floatable.mjs";
3
3
  import { getFocusable } from "@oscarpalmer/toretto/focusable";
4
4
  import { isNullableOrWhitespace } from "@oscarpalmer/atoms/is";
5
5
  //#region src/js/popover.ts
@@ -0,0 +1,2 @@
1
+ import { c as Sortable } from "./movable-BEo2LbUI.mjs";
2
+ export { Sortable };