@oscarpalmer/oui 0.14.0 → 0.16.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 (46) hide show
  1. package/README.md +2 -0
  2. package/dist/css/button.css +0 -3
  3. package/dist/css/popover.css +9 -0
  4. package/dist/css/splitter.css +78 -0
  5. package/dist/css/tooltip.css +0 -3
  6. package/dist/js/floatable.cjs +22 -9
  7. package/dist/js/floatable.js +22 -9
  8. package/dist/js/focus-trap/embedded.cjs +150 -0
  9. package/dist/js/focus-trap/embedded.js +150 -0
  10. package/dist/js/focus-trap/standalone.cjs +8 -0
  11. package/dist/js/focus-trap/standalone.js +7 -0
  12. package/dist/js/index.cjs +5 -1
  13. package/dist/js/index.js +6 -1
  14. package/dist/js/popover.cjs +160 -0
  15. package/dist/js/popover.js +160 -0
  16. package/dist/js/splitter.cjs +211 -0
  17. package/dist/js/splitter.js +211 -0
  18. package/dist/js/tooltip.cjs +2 -3
  19. package/dist/js/tooltip.js +2 -3
  20. package/package.json +25 -5
  21. package/src/css/button.scss +0 -3
  22. package/src/css/popover.scss +9 -0
  23. package/src/css/splitter.scss +87 -0
  24. package/src/css/tooltip.scss +0 -3
  25. package/src/js/floatable.ts +53 -25
  26. package/src/js/focus-trap/embedded.ts +215 -0
  27. package/src/js/focus-trap/standalone.ts +9 -0
  28. package/src/js/index.ts +3 -1
  29. package/src/js/popover.ts +221 -0
  30. package/src/js/splitter.ts +317 -0
  31. package/src/js/tooltip.ts +2 -4
  32. package/types/floatable.d.cts +2 -2
  33. package/types/floatable.d.ts +2 -2
  34. package/types/focus-trap/embedded.d.cts +15 -0
  35. package/types/focus-trap/embedded.d.ts +11 -0
  36. package/types/index.d.cts +11 -0
  37. package/types/index.d.ts +3 -1
  38. package/types/popover.d.cts +14 -0
  39. package/types/popover.d.ts +15 -0
  40. package/types/splitter.d.cts +10 -0
  41. package/types/splitter.d.ts +11 -0
  42. package/dist/js/focus-trap.cjs +0 -104
  43. package/dist/js/focus-trap.js +0 -103
  44. package/src/js/focus-trap.ts +0 -133
  45. /package/types/{focus-trap.d.cts → focus-trap/standalone.d.cts} +0 -0
  46. /package/types/{focus-trap.d.ts → focus-trap/standalone.d.ts} +0 -0
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const is = require("@oscarpalmer/atoms/is");
4
+ const event = require("@oscarpalmer/toretto/event");
5
+ const find = require("@oscarpalmer/toretto/find");
6
+ const focusable = require("@oscarpalmer/toretto/focusable");
7
+ const floatable = require("./floatable.cjs");
8
+ const focusTrap_embedded = require("./focus-trap/embedded.cjs");
9
+ const mapped = /* @__PURE__ */ new Map();
10
+ let index = 0;
11
+ class OuiPopoverElement extends HTMLElement {
12
+ #floatable;
13
+ #focusTrap;
14
+ constructor() {
15
+ super();
16
+ const content = this.querySelector(":scope > [oui-popover-content]");
17
+ const toggle2 = this.querySelector(":scope > [oui-popover-toggle]");
18
+ if (!(content instanceof HTMLElement)) {
19
+ throw new Error(
20
+ "A oui-popover must have a direct child element with the [oui-popover-content]-attribute"
21
+ );
22
+ }
23
+ if (!(toggle2 instanceof HTMLButtonElement)) {
24
+ throw new Error(
25
+ "A oui-popover must have a direct child element with the [oui-popover-toggle]-attribute and be a button"
26
+ );
27
+ }
28
+ content.hidden = true;
29
+ content.setAttribute("role", "dialog");
30
+ content.setAttribute("oui-focus-trap", "");
31
+ content.setAttribute("oui-focus-trap-noescape", "");
32
+ if (is.isNullableOrWhitespace(content.id)) {
33
+ content.setAttribute("id", `oui-popover-content-${++index}`);
34
+ }
35
+ toggle2.setAttribute("aria-controls", content.id);
36
+ toggle2.setAttribute("aria-haspopup", "dialog");
37
+ this.#floatable = new floatable.Floatable(toggle2, content);
38
+ this.#focusTrap = focusTrap_embedded.createFocusTrap(content);
39
+ mapped.set(toggle2, this.#floatable);
40
+ mapped.set(content, this.#floatable);
41
+ }
42
+ close() {
43
+ this.hidePopover();
44
+ }
45
+ hidePopover() {
46
+ if (this.#floatable.active) {
47
+ closeAbove(this.#floatable.content);
48
+ floatable.deactivate(this.#floatable, active, order);
49
+ this.#floatable.anchor.focus();
50
+ }
51
+ }
52
+ open() {
53
+ this.showPopover();
54
+ }
55
+ showPopover() {
56
+ if (!this.#floatable.active) {
57
+ toggle(this.#floatable.anchor);
58
+ }
59
+ }
60
+ toggle() {
61
+ this.togglePopover();
62
+ }
63
+ togglePopover(options) {
64
+ if (typeof options === "boolean") {
65
+ if (options === true) {
66
+ this.showPopover();
67
+ } else {
68
+ this.hidePopover();
69
+ }
70
+ return options;
71
+ }
72
+ if (this.#floatable.active) {
73
+ this.hidePopover();
74
+ return false;
75
+ }
76
+ this.showPopover();
77
+ return true;
78
+ }
79
+ }
80
+ function closeAll() {
81
+ for (const floatable$1 of active) {
82
+ floatable.deactivate(floatable$1, active, order);
83
+ }
84
+ }
85
+ function closeAbove(element) {
86
+ const index2 = order.findIndex(
87
+ (floatable2) => floatable2.anchor === element || floatable2.content === element
88
+ );
89
+ if (index2 === -1 || index2 === order.length - 1) {
90
+ return;
91
+ }
92
+ while (index2 < order.length - 1) {
93
+ const floatable$1 = order.pop();
94
+ if (floatable$1 != null) {
95
+ floatable.deactivate(floatable$1, active, order);
96
+ }
97
+ }
98
+ focus(order[index2].content);
99
+ }
100
+ function focus(content) {
101
+ let target = focusable.getFocusable(content)[0];
102
+ if (!(target instanceof HTMLElement)) {
103
+ target = content;
104
+ }
105
+ requestAnimationFrame(() => {
106
+ target.focus();
107
+ });
108
+ }
109
+ function onClick(event2) {
110
+ const related = find.findAncestor(
111
+ event2.target,
112
+ "[oui-popover-toggle], [oui-popover-content]"
113
+ );
114
+ if (!(related instanceof HTMLElement)) {
115
+ closeAll();
116
+ } else if (related.hasAttribute("oui-popover-content")) {
117
+ closeAbove(related);
118
+ } else {
119
+ toggle(related);
120
+ }
121
+ }
122
+ function onKeydown(event2) {
123
+ if (event2.key !== "Escape" || order.length === 0) {
124
+ return;
125
+ }
126
+ const related = find.findAncestor(event2.target, "[oui-popover-content]");
127
+ if (!(related instanceof HTMLElement)) {
128
+ return;
129
+ }
130
+ closeAbove(related);
131
+ const floatable$1 = mapped.get(related);
132
+ if (floatable$1 != null) {
133
+ floatable.deactivate(floatable$1, active, order);
134
+ floatable$1.anchor.focus();
135
+ }
136
+ }
137
+ function toggle(anchor) {
138
+ const content = find.findAncestor(anchor, "[oui-popover-content]");
139
+ if (content instanceof HTMLElement) {
140
+ closeAbove(content);
141
+ } else {
142
+ closeAll();
143
+ }
144
+ const floatable$1 = mapped.get(anchor);
145
+ if (floatable$1 == null) {
146
+ return;
147
+ }
148
+ if (floatable$1.active) {
149
+ floatable.deactivate(floatable$1, active, order);
150
+ } else {
151
+ floatable.activate(floatable$1, active, order);
152
+ focus(floatable$1.content);
153
+ }
154
+ }
155
+ customElements.define("oui-popover", OuiPopoverElement);
156
+ event.on(document, "click", onClick);
157
+ event.on(document, "keydown", onKeydown);
158
+ const active = /* @__PURE__ */ new Set();
159
+ const order = [];
160
+ exports.OuiPopoverElement = OuiPopoverElement;
@@ -0,0 +1,160 @@
1
+ import { isNullableOrWhitespace } from "@oscarpalmer/atoms/is";
2
+ import { on } from "@oscarpalmer/toretto/event";
3
+ import { findAncestor } from "@oscarpalmer/toretto/find";
4
+ import { getFocusable } from "@oscarpalmer/toretto/focusable";
5
+ import { Floatable, deactivate, activate } from "./floatable.js";
6
+ import { createFocusTrap } from "./focus-trap/embedded.js";
7
+ const mapped = /* @__PURE__ */ new Map();
8
+ let index = 0;
9
+ class OuiPopoverElement extends HTMLElement {
10
+ #floatable;
11
+ #focusTrap;
12
+ constructor() {
13
+ super();
14
+ const content = this.querySelector(":scope > [oui-popover-content]");
15
+ const toggle2 = this.querySelector(":scope > [oui-popover-toggle]");
16
+ if (!(content instanceof HTMLElement)) {
17
+ throw new Error(
18
+ "A oui-popover must have a direct child element with the [oui-popover-content]-attribute"
19
+ );
20
+ }
21
+ if (!(toggle2 instanceof HTMLButtonElement)) {
22
+ throw new Error(
23
+ "A oui-popover must have a direct child element with the [oui-popover-toggle]-attribute and be a button"
24
+ );
25
+ }
26
+ content.hidden = true;
27
+ content.setAttribute("role", "dialog");
28
+ content.setAttribute("oui-focus-trap", "");
29
+ content.setAttribute("oui-focus-trap-noescape", "");
30
+ if (isNullableOrWhitespace(content.id)) {
31
+ content.setAttribute("id", `oui-popover-content-${++index}`);
32
+ }
33
+ toggle2.setAttribute("aria-controls", content.id);
34
+ toggle2.setAttribute("aria-haspopup", "dialog");
35
+ this.#floatable = new Floatable(toggle2, content);
36
+ this.#focusTrap = createFocusTrap(content);
37
+ mapped.set(toggle2, this.#floatable);
38
+ mapped.set(content, this.#floatable);
39
+ }
40
+ close() {
41
+ this.hidePopover();
42
+ }
43
+ hidePopover() {
44
+ if (this.#floatable.active) {
45
+ closeAbove(this.#floatable.content);
46
+ deactivate(this.#floatable, active, order);
47
+ this.#floatable.anchor.focus();
48
+ }
49
+ }
50
+ open() {
51
+ this.showPopover();
52
+ }
53
+ showPopover() {
54
+ if (!this.#floatable.active) {
55
+ toggle(this.#floatable.anchor);
56
+ }
57
+ }
58
+ toggle() {
59
+ this.togglePopover();
60
+ }
61
+ togglePopover(options) {
62
+ if (typeof options === "boolean") {
63
+ if (options === true) {
64
+ this.showPopover();
65
+ } else {
66
+ this.hidePopover();
67
+ }
68
+ return options;
69
+ }
70
+ if (this.#floatable.active) {
71
+ this.hidePopover();
72
+ return false;
73
+ }
74
+ this.showPopover();
75
+ return true;
76
+ }
77
+ }
78
+ function closeAll() {
79
+ for (const floatable of active) {
80
+ deactivate(floatable, active, order);
81
+ }
82
+ }
83
+ function closeAbove(element) {
84
+ const index2 = order.findIndex(
85
+ (floatable) => floatable.anchor === element || floatable.content === element
86
+ );
87
+ if (index2 === -1 || index2 === order.length - 1) {
88
+ return;
89
+ }
90
+ while (index2 < order.length - 1) {
91
+ const floatable = order.pop();
92
+ if (floatable != null) {
93
+ deactivate(floatable, active, order);
94
+ }
95
+ }
96
+ focus(order[index2].content);
97
+ }
98
+ function focus(content) {
99
+ let target = getFocusable(content)[0];
100
+ if (!(target instanceof HTMLElement)) {
101
+ target = content;
102
+ }
103
+ requestAnimationFrame(() => {
104
+ target.focus();
105
+ });
106
+ }
107
+ function onClick(event) {
108
+ const related = findAncestor(
109
+ event.target,
110
+ "[oui-popover-toggle], [oui-popover-content]"
111
+ );
112
+ if (!(related instanceof HTMLElement)) {
113
+ closeAll();
114
+ } else if (related.hasAttribute("oui-popover-content")) {
115
+ closeAbove(related);
116
+ } else {
117
+ toggle(related);
118
+ }
119
+ }
120
+ function onKeydown(event) {
121
+ if (event.key !== "Escape" || order.length === 0) {
122
+ return;
123
+ }
124
+ const related = findAncestor(event.target, "[oui-popover-content]");
125
+ if (!(related instanceof HTMLElement)) {
126
+ return;
127
+ }
128
+ closeAbove(related);
129
+ const floatable = mapped.get(related);
130
+ if (floatable != null) {
131
+ deactivate(floatable, active, order);
132
+ floatable.anchor.focus();
133
+ }
134
+ }
135
+ function toggle(anchor) {
136
+ const content = findAncestor(anchor, "[oui-popover-content]");
137
+ if (content instanceof HTMLElement) {
138
+ closeAbove(content);
139
+ } else {
140
+ closeAll();
141
+ }
142
+ const floatable = mapped.get(anchor);
143
+ if (floatable == null) {
144
+ return;
145
+ }
146
+ if (floatable.active) {
147
+ deactivate(floatable, active, order);
148
+ } else {
149
+ activate(floatable, active, order);
150
+ focus(floatable.content);
151
+ }
152
+ }
153
+ customElements.define("oui-popover", OuiPopoverElement);
154
+ on(document, "click", onClick);
155
+ on(document, "keydown", onKeydown);
156
+ const active = /* @__PURE__ */ new Set();
157
+ const order = [];
158
+ export {
159
+ OuiPopoverElement
160
+ };
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const number = require("@oscarpalmer/atoms/number");
4
+ const event = require("@oscarpalmer/toretto/event");
5
+ const find = require("@oscarpalmer/toretto/find");
6
+ class OuiSplitterElement extends HTMLElement {
7
+ #splitter;
8
+ constructor() {
9
+ super();
10
+ const panels = [...this.children].filter(
11
+ (child) => child instanceof HTMLElement
12
+ );
13
+ if (panels.length !== 2) {
14
+ throw new Error("oui-splitter must have exactly two panels.");
15
+ }
16
+ this.#splitter = new Splitter(this, panels);
17
+ }
18
+ connectedCallback() {
19
+ if (this.#splitter.types.auto) {
20
+ observer.observe(this);
21
+ }
22
+ }
23
+ disconnectedCallback() {
24
+ if (this.#splitter.types.auto) {
25
+ observer.unobserve(this);
26
+ }
27
+ }
28
+ }
29
+ class Splitter {
30
+ constructor(element, panels) {
31
+ this.element = element;
32
+ this.panels = panels;
33
+ this.handle = createHandle();
34
+ this.rectangle = element.getBoundingClientRect();
35
+ this.separator = createSeparator();
36
+ this.separator.append(this.handle);
37
+ panels[0].insertAdjacentElement("afterend", this.separator);
38
+ mapped.set(element, this);
39
+ mapped.set(this.handle, this);
40
+ setSize(this, this.flex.value);
41
+ this.types = getTypes(this);
42
+ if (this.types.horizontal) {
43
+ this.element.setAttribute("oui-splitter-horizontal", "");
44
+ }
45
+ }
46
+ flex = {
47
+ previous: 0.5,
48
+ value: 0.5
49
+ };
50
+ handle;
51
+ rectangle;
52
+ separator;
53
+ types;
54
+ }
55
+ function createHandle() {
56
+ const handle = document.createElement("span");
57
+ handle.tabIndex = 0;
58
+ handle.setAttribute("aria-label", "Resize panels");
59
+ handle.setAttribute("oui-splitter-handle", "");
60
+ return handle;
61
+ }
62
+ function createSeparator() {
63
+ const separator = document.createElement("div");
64
+ separator.setAttribute("oui-splitter-separator", "");
65
+ return separator;
66
+ }
67
+ function getFlex(splitter2, value) {
68
+ const { height, left, top, width } = splitter2.rectangle;
69
+ const offset = value - (splitter2.types.horizontal ? top : left);
70
+ const fraction = offset / (splitter2.types.horizontal ? height : width);
71
+ return number.clamp(fraction, 0.1, 0.9);
72
+ }
73
+ function getTypes(splitter2, width, height) {
74
+ const type = splitter2.element.getAttribute("type")?.trim() ?? "";
75
+ if (type === "horizontal") {
76
+ return {
77
+ auto: false,
78
+ horizontal: true
79
+ };
80
+ }
81
+ if (type === "auto") {
82
+ return {
83
+ auto: true,
84
+ horizontal: (height ?? splitter2.rectangle.height) > (width ?? splitter2.rectangle.width)
85
+ };
86
+ }
87
+ return {
88
+ auto: false,
89
+ horizontal: false
90
+ };
91
+ }
92
+ function onEnd(reset) {
93
+ if (splitter != null) {
94
+ splitter.handle.removeAttribute("oui-splitter-active");
95
+ splitter.separator.removeAttribute("oui-splitter-active");
96
+ if (reset) {
97
+ setSize(splitter, splitter.flex.previous);
98
+ } else {
99
+ splitter.flex.previous = splitter.flex.value;
100
+ }
101
+ }
102
+ splitter = void 0;
103
+ }
104
+ function onKeydown(event2) {
105
+ switch (true) {
106
+ case allKeys.has(event2.key):
107
+ onNavigate(event2);
108
+ break;
109
+ case event2.key === "Escape":
110
+ onEnd(true);
111
+ break;
112
+ }
113
+ }
114
+ function onMousedown(event2) {
115
+ splitter = mapped.get(
116
+ find.findAncestor(
117
+ event2.target,
118
+ "[oui-splitter-handle]"
119
+ )
120
+ );
121
+ if (splitter == null) {
122
+ return;
123
+ }
124
+ splitter.handle.setAttribute("oui-splitter-active", "");
125
+ splitter.separator.setAttribute("oui-splitter-active", "");
126
+ splitter.rectangle = splitter.element.getBoundingClientRect();
127
+ }
128
+ function onMousemove(event$1) {
129
+ if (splitter == null) {
130
+ return;
131
+ }
132
+ const position = event.getPosition(event$1);
133
+ if (position != null) {
134
+ splitter.flex.value = getFlex(
135
+ splitter,
136
+ splitter.types.horizontal ? position.y : position.x
137
+ );
138
+ setSize(splitter, splitter.flex.value);
139
+ }
140
+ }
141
+ function onMouseup() {
142
+ if (splitter != null) {
143
+ onEnd(false);
144
+ }
145
+ }
146
+ function onNavigate(event2) {
147
+ const splitter2 = mapped.get(
148
+ find.findAncestor(
149
+ event2.target,
150
+ "[oui-splitter-handle]"
151
+ )
152
+ );
153
+ if (splitter2 == null) {
154
+ return;
155
+ }
156
+ if (absoluteKeys.has(event2.key)) {
157
+ setSize(splitter2, event2.key === "End" ? 0.9 : 0.1, true);
158
+ return;
159
+ }
160
+ if (splitter2.types.horizontal && !horizontalKeys.has(event2.key) || !splitter2.types.horizontal && !verticalKeys.has(event2.key)) {
161
+ return;
162
+ }
163
+ const modifier = negativeKeys.has(event2.key) ? -1 : 1;
164
+ setSize(splitter2, splitter2.flex.value + modifier * 0.05, true);
165
+ }
166
+ function onObservation(entries) {
167
+ if (frame != null) {
168
+ cancelAnimationFrame(frame);
169
+ }
170
+ frame = requestAnimationFrame(() => {
171
+ for (const entry of entries) {
172
+ const splitter2 = mapped.get(entry.target);
173
+ if (splitter2 == null) {
174
+ return;
175
+ }
176
+ splitter2.types = getTypes(
177
+ splitter2,
178
+ entry.contentRect.width,
179
+ entry.contentRect.height
180
+ );
181
+ if (splitter2.types.horizontal) {
182
+ splitter2.element.setAttribute("oui-splitter-horizontal", "");
183
+ } else {
184
+ splitter2.element.removeAttribute("oui-splitter-horizontal");
185
+ }
186
+ }
187
+ frame = void 0;
188
+ });
189
+ }
190
+ function setSize(splitter2, size, previous) {
191
+ splitter2.flex.value = size;
192
+ if (previous) {
193
+ splitter2.flex.previous = size;
194
+ }
195
+ splitter2.element.style.setProperty("--oui-splitter-size", `${size}`);
196
+ }
197
+ const absoluteKeys = /* @__PURE__ */ new Set(["End", "Home"]);
198
+ const horizontalKeys = /* @__PURE__ */ new Set(["ArrowDown", "ArrowUp"]);
199
+ const mapped = /* @__PURE__ */ new WeakMap();
200
+ const observer = new ResizeObserver(onObservation);
201
+ const negativeKeys = /* @__PURE__ */ new Set(["ArrowLeft", "ArrowUp"]);
202
+ const verticalKeys = /* @__PURE__ */ new Set(["ArrowLeft", "ArrowRight"]);
203
+ const allKeys = /* @__PURE__ */ new Set([...absoluteKeys, ...horizontalKeys, ...verticalKeys]);
204
+ let frame;
205
+ let splitter;
206
+ customElements.define("oui-splitter", OuiSplitterElement);
207
+ event.on(document, "keydown", onKeydown);
208
+ event.on(document, "mousedown", onMousedown);
209
+ event.on(document, "mousemove", onMousemove);
210
+ event.on(document, "mouseup", onMouseup);
211
+ exports.OuiSplitterElement = OuiSplitterElement;