@oscarpalmer/oui 0.14.0 → 0.15.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.
@@ -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
+ order[index2].content.focus();
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
+ };
@@ -112,7 +112,7 @@ function initialize(tooltip) {
112
112
  element,
113
113
  event$1,
114
114
  () => {
115
- floatable.activate(floatable$1, active, 250);
115
+ floatable.activate(floatable$1, active, void 0, 250);
116
116
  },
117
117
  {
118
118
  signal: abortController.signal
@@ -124,7 +124,7 @@ function initialize(tooltip) {
124
124
  element,
125
125
  event$1,
126
126
  () => {
127
- floatable.deactivate(floatable$1, active, 250);
127
+ floatable.deactivate(floatable$1, active, void 0, 250);
128
128
  },
129
129
  {
130
130
  signal: abortController.signal
@@ -138,7 +138,6 @@ function removeTooltip(element) {
138
138
  tooltips.delete(element);
139
139
  }
140
140
  function reset(anchor, attributes) {
141
- console.log("reset", anchor, attributes);
142
141
  if (attributes.ariaDescribedby == null) {
143
142
  anchor.removeAttribute("aria-describedby");
144
143
  } else {
@@ -111,7 +111,7 @@ function initialize(tooltip) {
111
111
  element,
112
112
  event,
113
113
  () => {
114
- activate(floatable, active, 250);
114
+ activate(floatable, active, void 0, 250);
115
115
  },
116
116
  {
117
117
  signal: abortController.signal
@@ -123,7 +123,7 @@ function initialize(tooltip) {
123
123
  element,
124
124
  event,
125
125
  () => {
126
- deactivate(floatable, active, 250);
126
+ deactivate(floatable, active, void 0, 250);
127
127
  },
128
128
  {
129
129
  signal: abortController.signal
@@ -137,7 +137,6 @@ function removeTooltip(element) {
137
137
  tooltips.delete(element);
138
138
  }
139
139
  function reset(anchor, attributes) {
140
- console.log("reset", anchor, attributes);
141
140
  if (attributes.ariaDescribedby == null) {
142
141
  anchor.removeAttribute("aria-describedby");
143
142
  } else {
package/package.json CHANGED
@@ -41,12 +41,12 @@
41
41
  },
42
42
  "./focus-trap": {
43
43
  "import": {
44
- "types": "./types/focus-trap.d.ts",
45
- "default": "./dist/js/focus-trap.js"
44
+ "types": "./types/focus-trap/standalone.d.ts",
45
+ "default": "./dist/js/focus-trap/standalone.js"
46
46
  },
47
47
  "require": {
48
- "types": "./types/focus-trap.d.cts",
49
- "default": "./dist/js/focus-trap.cjs"
48
+ "types": "./types/focus-trap/standalone.d.cts",
49
+ "default": "./dist/js/focus-trap/standalone.cjs"
50
50
  }
51
51
  },
52
52
  "./tooltip": {
@@ -94,5 +94,5 @@
94
94
  },
95
95
  "type": "module",
96
96
  "types": "types/index.d.cts",
97
- "version": "0.14.0"
97
+ "version": "0.15.0"
98
98
  }
@@ -0,0 +1,9 @@
1
+ @use 'mixins.scss' as mixins;
2
+
3
+ :where([oui-popover-content]) {
4
+ @include mixins.shadowOverlay;
5
+
6
+ padding: 1rem;
7
+ background-color: var(--oui-absolute);
8
+ border-radius: 0.5rem;
9
+ }
@@ -15,7 +15,4 @@
15
15
  background-color: var(--oui-absolute);
16
16
  border-radius: 0.25rem;
17
17
  font-size: 0.875rem;
18
- position: fixed;
19
- inset: 0 auto auto 0;
20
- z-index: 10;
21
18
  }
@@ -1,27 +1,30 @@
1
1
  export class Floatable {
2
- activate: number | undefined;
3
- active = false;
4
- deactivate: number | undefined;
5
- frame: DOMHighResTimeStamp | undefined;
6
-
7
- constructor(
8
- public anchor: HTMLElement,
9
- public content: HTMLElement,
10
- ) {}
11
-
12
- destroy(): void {
13
- this.activate = undefined;
14
- this.deactivate = undefined;
15
- this.frame = undefined;
16
-
17
- this.anchor = undefined as never;
18
- this.content = undefined as never;
2
+ activate: number | undefined;
3
+ active = false;
4
+ deactivate: number | undefined;
5
+ frame: DOMHighResTimeStamp | undefined;
6
+
7
+ constructor(
8
+ public anchor: HTMLElement,
9
+ public content: HTMLElement,
10
+ ) {
11
+ setStyles(content);
12
+ }
13
+
14
+ destroy(): void {
15
+ this.activate = undefined;
16
+ this.deactivate = undefined;
17
+ this.frame = undefined;
18
+
19
+ this.anchor = undefined as never;
20
+ this.content = undefined as never;
21
+ }
19
22
  }
20
- }
21
23
 
22
24
  export function activate(
23
25
  floatable: Floatable,
24
26
  active: Set<Floatable>,
27
+ order?: Floatable[],
25
28
  time?: number,
26
29
  ): void {
27
30
  stop(floatable);
@@ -31,10 +34,10 @@ export function activate(
31
34
  }
32
35
 
33
36
  if (time == null || time === 0) {
34
- onActivate(floatable, active);
37
+ onActivate(floatable, active, order);
35
38
  } else {
36
39
  floatable.activate = +setTimeout(() => {
37
- onActivate(floatable, active);
40
+ onActivate(floatable, active, order);
38
41
  }, time);
39
42
  }
40
43
  }
@@ -42,6 +45,7 @@ export function activate(
42
45
  export function deactivate(
43
46
  floatable: Floatable,
44
47
  active: Set<Floatable>,
48
+ order?: Floatable[],
45
49
  time?: number,
46
50
  ): void {
47
51
  stop(floatable);
@@ -51,10 +55,10 @@ export function deactivate(
51
55
  }
52
56
 
53
57
  if (time == null || time === 0) {
54
- onDeactivate(floatable, active);
58
+ onDeactivate(floatable, active, order);
55
59
  } else {
56
60
  floatable.deactivate = +setTimeout(() => {
57
- onDeactivate(floatable, active);
61
+ onDeactivate(floatable, active, order);
58
62
  }, time);
59
63
  }
60
64
  }
@@ -106,11 +110,19 @@ function getY(anchor: DOMRect, content: DOMRect): number {
106
110
  return anchor.top + (anchor.height - content.height) / 2;
107
111
  }
108
112
 
109
- function onActivate(floatable: Floatable, active: Set<Floatable>): void {
113
+ function onActivate(
114
+ floatable: Floatable,
115
+ active: Set<Floatable>,
116
+ order?: Floatable[],
117
+ ): void {
110
118
  floatable.active = true;
111
119
 
112
120
  active.add(floatable);
113
121
 
122
+ if (order != null) {
123
+ order.push(floatable);
124
+ }
125
+
114
126
  document.body.append(floatable.content);
115
127
 
116
128
  floatable.content.hidden = false;
@@ -118,11 +130,21 @@ function onActivate(floatable: Floatable, active: Set<Floatable>): void {
118
130
  update(floatable);
119
131
  }
120
132
 
121
- function onDeactivate(floatable: Floatable, active: Set<Floatable>): void {
133
+ function onDeactivate(
134
+ floatable: Floatable,
135
+ active: Set<Floatable>,
136
+ order?: Floatable[],
137
+ ): void {
122
138
  floatable.active = false;
123
139
 
124
140
  active.delete(floatable);
125
141
 
142
+ const index = order?.indexOf(floatable) ?? -1;
143
+
144
+ if (index > -1) {
145
+ order?.splice(index, 1);
146
+ }
147
+
126
148
  floatable.content.hidden = true;
127
149
 
128
150
  floatable.anchor.insertAdjacentElement('afterend', floatable.content);
@@ -144,6 +166,12 @@ function update(floatable: Floatable): void {
144
166
  floatable.frame = requestAnimationFrame(run);
145
167
  }
146
168
 
169
+ function setStyles(content: HTMLElement): void {
170
+ content.style.position = 'fixed';
171
+ content.style.inset = '0 auto auto 0';
172
+ content.style.zIndex = '10';
173
+ }
174
+
147
175
  function stop(floatable: Floatable): void {
148
176
  if (floatable.frame != null) {
149
177
  cancelAnimationFrame(floatable.frame);
@@ -0,0 +1,215 @@
1
+ import {clamp} from '@oscarpalmer/atoms/number';
2
+ import {on} from '@oscarpalmer/toretto/event';
3
+ import {findAncestor} from '@oscarpalmer/toretto/find';
4
+ import {getFocusable} from '@oscarpalmer/toretto/focusable';
5
+
6
+ export class FocusTrap {
7
+ active = true;
8
+
9
+ constructor(public element: HTMLElement) {
10
+ element.setAttribute('aria-modal', 'true');
11
+ element.setAttribute('tabindex', '-1');
12
+ }
13
+
14
+ destroy() {
15
+ this.element = undefined as never;
16
+ }
17
+
18
+ disable() {
19
+ if (this.active && !this.element.hasAttribute(`${selector}-noescape`)) {
20
+ setDisabled(this, true);
21
+ }
22
+ }
23
+
24
+ enable() {
25
+ if (!this.active) {
26
+ setDisabled(this, false);
27
+ }
28
+ }
29
+ }
30
+
31
+ //
32
+
33
+ export function createFocusTrap(element: HTMLElement): FocusTrap {
34
+ let focusTrap = focusTraps.get(element);
35
+
36
+ if (focusTrap == null) {
37
+ focusTrap = new FocusTrap(element);
38
+
39
+ focusTraps.set(element, focusTrap);
40
+ }
41
+
42
+ return focusTrap;
43
+ }
44
+
45
+ function onEscape(event: KeyboardEvent): void {
46
+ focusTraps
47
+ .get(
48
+ findAncestor(event.target as HTMLElement, `[${selector}]`) as HTMLElement,
49
+ )
50
+ ?.disable();
51
+ }
52
+
53
+ function onFocusIn(event: Event): void {
54
+ lastTarget =
55
+ findAncestor(event.target as HTMLElement, `[${selector}]`) == null
56
+ ? undefined
57
+ : (event.target as HTMLElement);
58
+ }
59
+
60
+ function onKeydown(event: KeyboardEvent): void {
61
+ switch (event.key) {
62
+ case 'Escape':
63
+ onEscape(event);
64
+ break;
65
+
66
+ case 'Tab':
67
+ onTab(event);
68
+ break;
69
+
70
+ default:
71
+ return;
72
+ }
73
+ }
74
+
75
+ function onPointerdown(event: MouseEvent | TouchEvent): void {
76
+ const lastFocusTrap = findAncestor(
77
+ lastTarget as HTMLElement,
78
+ `[${selector}]`,
79
+ ) as HTMLElement;
80
+
81
+ const nextFocusTrap = findAncestor(
82
+ event.target as HTMLElement,
83
+ `[${selector}]`,
84
+ ) as HTMLElement;
85
+
86
+ if (lastFocusTrap != null && nextFocusTrap !== lastFocusTrap) {
87
+ event.preventDefault();
88
+
89
+ lastTarget?.focus();
90
+ }
91
+
92
+ if (disabled.size === 0) {
93
+ return;
94
+ }
95
+
96
+ for (const focusTrap of disabled) {
97
+ if (focusTrap.element !== nextFocusTrap) {
98
+ focusTrap.enable();
99
+ return;
100
+ }
101
+ }
102
+ }
103
+
104
+ function onTab(event: KeyboardEvent): void {
105
+ const element = findAncestor(
106
+ event.target as HTMLElement,
107
+ `[${selector}]`,
108
+ ) as HTMLElement;
109
+
110
+ const focusTrap = focusTraps.get(element);
111
+
112
+ if (focusTrap == null) {
113
+ return;
114
+ }
115
+
116
+ if (focusTrap.active) {
117
+ event.preventDefault();
118
+ }
119
+
120
+ const elements = getFocusable(element);
121
+
122
+ const index = elements.indexOf(event.target as HTMLElement);
123
+
124
+ if (index === -1 || elements.length === 0) {
125
+ tabToTrap(focusTrap, elements, element);
126
+ } else {
127
+ tabToElement(event, focusTrap, elements, index);
128
+ }
129
+ }
130
+
131
+ function setDisabled(focusTrap: FocusTrap, value: boolean): void {
132
+ focusTrap.element.setAttribute('aria-modal', String(!value));
133
+
134
+ focusTrap.active = !value;
135
+
136
+ if (value) {
137
+ disabled.add(focusTrap);
138
+ } else {
139
+ disabled.delete(focusTrap);
140
+ }
141
+ }
142
+
143
+ function tabToElement(
144
+ event: KeyboardEvent,
145
+ focusTrap: FocusTrap,
146
+ elements: Element[],
147
+ index: number,
148
+ ): void {
149
+ const next = clamp(
150
+ index + (event.shiftKey ? -1 : 1),
151
+ 0,
152
+ elements.length - 1,
153
+ true,
154
+ );
155
+
156
+ if (!focusTrap.active) {
157
+ if (
158
+ (index === length - 1 && next === 0) ||
159
+ (index === 0 && next === length - 1)
160
+ ) {
161
+ lastTarget = undefined;
162
+
163
+ focusTrap.enable();
164
+ }
165
+
166
+ return;
167
+ }
168
+
169
+ (elements[next] as HTMLElement).focus();
170
+
171
+ lastTarget = elements[next] as HTMLElement;
172
+ }
173
+
174
+ function tabToTrap(
175
+ focusTrap: FocusTrap,
176
+ elements: Element[],
177
+ element: HTMLElement,
178
+ ): void {
179
+ if (focusTrap.active) {
180
+ const next = length === 0 ? element : elements[0];
181
+
182
+ (next as HTMLElement).focus();
183
+
184
+ lastTarget = next as HTMLElement;
185
+ } else {
186
+ lastTarget = undefined;
187
+
188
+ focusTrap.enable();
189
+ }
190
+ }
191
+
192
+ //
193
+
194
+ const disabled = new Set<FocusTrap>();
195
+
196
+ const eventOptions = {
197
+ capture: true,
198
+ passive: false,
199
+ };
200
+
201
+ export const focusTraps = new WeakMap<HTMLElement, FocusTrap>();
202
+
203
+ let lastTarget: HTMLElement | undefined;
204
+
205
+ export const selector = 'oui-focus-trap';
206
+
207
+ //
208
+
209
+ on(document, 'focusin', onFocusIn, eventOptions);
210
+
211
+ on(document, 'keydown', onKeydown, eventOptions);
212
+
213
+ on(document, 'mousedown', onPointerdown, eventOptions);
214
+
215
+ on(document, 'touchstart', onPointerdown, eventOptions);
@@ -0,0 +1,9 @@
1
+ import {attributable} from '../attributable';
2
+ import {createFocusTrap, focusTraps, selector} from './embedded';
3
+
4
+ function removeFocusTraps(element: HTMLElement): void {
5
+ focusTraps.get(element)?.destroy();
6
+ focusTraps.delete(element);
7
+ }
8
+
9
+ attributable(selector, createFocusTrap, removeFocusTraps);
package/src/js/index.ts CHANGED
@@ -1 +1,3 @@
1
- export * from './focus-trap';
1
+ export * from './focus-trap/standalone';
2
+ export * from './popover';
3
+ export * from './tooltip';