@oscarpalmer/oui 0.17.0 → 0.18.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.
- package/dist/css/badge.css +3 -3
- package/dist/css/content.css +9 -11
- package/dist/css/form.css +36 -22
- package/dist/css/message.css +4 -0
- package/dist/css/reset.css +176 -56
- package/dist/css/tooltip.css +8 -0
- package/dist/js/attributable.full.js +48 -0
- package/dist/js/attributable.js +30 -45
- package/dist/js/floatable.full.js +413 -0
- package/dist/js/floatable.js +254 -169
- package/dist/js/focus-trap/embedded.full.js +351 -0
- package/dist/js/focus-trap/embedded.js +131 -131
- package/dist/js/focus-trap/standalone.full.js +402 -0
- package/dist/js/focus-trap/standalone.js +4 -4
- package/dist/js/index.js +4 -5
- package/dist/js/oui.full.js +1596 -0
- package/dist/js/popover.full.js +822 -0
- package/dist/js/popover.js +83 -162
- package/dist/js/splitter.full.js +605 -0
- package/dist/js/splitter.js +263 -245
- package/dist/js/tooltip.full.js +679 -0
- package/dist/js/tooltip.js +159 -150
- package/package.json +31 -59
- package/src/css/content.scss +1 -2
- package/src/css/form.scss +12 -4
- package/src/css/message.scss +3 -0
- package/src/css/mixins.scss +7 -0
- package/src/css/reset.scss +180 -54
- package/src/css/tooltip.scss +8 -0
- package/src/js/floatable.ts +380 -133
- package/src/js/focus-trap/embedded.ts +149 -64
- package/src/js/focus-trap/standalone.ts +4 -4
- package/src/js/index.ts +1 -0
- package/src/js/popover.ts +50 -120
- package/src/js/splitter.ts +250 -103
- package/src/js/tooltip.ts +155 -68
- package/types/floatable.d.ts +17 -10
- package/types/focus-trap/embedded.d.ts +18 -5
- package/types/index.d.ts +1 -0
- package/types/popover.d.ts +2 -0
- package/types/splitter.d.ts +13 -1
- package/types/tooltip.d.ts +2 -1
- package/dist/js/attributable.cjs +0 -51
- package/dist/js/floatable.cjs +0 -176
- package/dist/js/focus-trap/embedded.cjs +0 -150
- package/dist/js/focus-trap/standalone.cjs +0 -8
- package/dist/js/index.cjs +0 -6
- package/dist/js/popover.cjs +0 -166
- package/dist/js/splitter.cjs +0 -272
- package/dist/js/tooltip.cjs +0 -166
- package/types/attributable.d.cts +0 -5
- package/types/floatable.d.cts +0 -20
- package/types/focus-trap/embedded.d.cts +0 -15
- package/types/focus-trap/standalone.d.cts +0 -3
- package/types/index.d.cts +0 -14
- package/types/popover.d.cts +0 -14
- package/types/splitter.d.cts +0 -10
- package/types/tooltip.d.cts +0 -3
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
function isEventTarget(value) {
|
|
2
|
+
return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function getBoolean(value, defaultValue) {
|
|
6
|
+
return defaultValue ?? false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function calculate() {
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
const values = [];
|
|
12
|
+
let last;
|
|
13
|
+
function step(now) {
|
|
14
|
+
if (last != null) values.push(now - last);
|
|
15
|
+
last = now;
|
|
16
|
+
if (values.length >= CALCULATION_TOTAL) {
|
|
17
|
+
const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - CALCULATION_TRIM);
|
|
18
|
+
resolve(median);
|
|
19
|
+
} else requestAnimationFrame(step);
|
|
20
|
+
}
|
|
21
|
+
requestAnimationFrame(step);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function noop() {}
|
|
25
|
+
var CALCULATION_TOTAL = 10;
|
|
26
|
+
var CALCULATION_TRIM = 4;
|
|
27
|
+
calculate().then((value) => {
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
function createEventOptions(options) {
|
|
31
|
+
return {
|
|
32
|
+
capture: getBoolean(),
|
|
33
|
+
once: getBoolean(),
|
|
34
|
+
passive: getBoolean(options?.passive, true),
|
|
35
|
+
signal: options?.signal
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function on(target, type, listener, options) {
|
|
39
|
+
if (!isEventTarget(target) || typeof type !== "string" || typeof listener !== "function") return noop;
|
|
40
|
+
const extended = createEventOptions(options);
|
|
41
|
+
target.addEventListener(type, listener, extended);
|
|
42
|
+
return () => {
|
|
43
|
+
target.removeEventListener(type, listener, extended);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function findAncestor(origin, selector) {
|
|
48
|
+
if (!(origin instanceof Element) || selector == null) return null;
|
|
49
|
+
if (typeof selector === "string") {
|
|
50
|
+
if (origin.matches?.(selector)) return origin;
|
|
51
|
+
return origin.closest(selector);
|
|
52
|
+
}
|
|
53
|
+
if (typeof selector !== "function") return null;
|
|
54
|
+
if (selector(origin)) return origin;
|
|
55
|
+
let parent = origin.parentElement;
|
|
56
|
+
while (parent != null && !selector(parent)) {
|
|
57
|
+
if (parent === document.body) return null;
|
|
58
|
+
parent = parent.parentElement;
|
|
59
|
+
}
|
|
60
|
+
return parent;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class Floatable {
|
|
64
|
+
options;
|
|
65
|
+
active = false;
|
|
66
|
+
disabled = false;
|
|
67
|
+
frame;
|
|
68
|
+
ignoreFocus = false;
|
|
69
|
+
constructor(options) {
|
|
70
|
+
this.options = options;
|
|
71
|
+
initialize(this);
|
|
72
|
+
}
|
|
73
|
+
destroy() {
|
|
74
|
+
stop(this);
|
|
75
|
+
const state = getState();
|
|
76
|
+
state?.active.delete(this);
|
|
77
|
+
state?.mapped.delete(this.options.anchor);
|
|
78
|
+
state?.mapped.delete(this.options.content);
|
|
79
|
+
this.options.anchor = undefined;
|
|
80
|
+
this.options.content = undefined;
|
|
81
|
+
}
|
|
82
|
+
disable() {
|
|
83
|
+
if (this.disabled) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.disabled = true;
|
|
87
|
+
stop(this);
|
|
88
|
+
getState()?.active.delete(this);
|
|
89
|
+
}
|
|
90
|
+
enable() {
|
|
91
|
+
if (!this.disabled) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.disabled = false;
|
|
95
|
+
initialize(this);
|
|
96
|
+
}
|
|
97
|
+
toggle(active, ignoreFocus) {
|
|
98
|
+
if (this.disabled || this.active === active) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const state = getState();
|
|
102
|
+
if (state == null) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.ignoreFocus = !active && (ignoreFocus ?? false);
|
|
106
|
+
stop(this);
|
|
107
|
+
if (active) {
|
|
108
|
+
state.active.add(this);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
state.active.delete(this);
|
|
112
|
+
}
|
|
113
|
+
if (this.options.interactive) {
|
|
114
|
+
if (active) {
|
|
115
|
+
state.order.push(this);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const index = state.order.indexOf(this);
|
|
119
|
+
if (index > -1) {
|
|
120
|
+
state.order.splice(index, 1);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (active) {
|
|
125
|
+
activate(this);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
deactivate(this);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//
|
|
133
|
+
function activate(floatable) {
|
|
134
|
+
floatable.active = true;
|
|
135
|
+
document.body.append(floatable.options.content);
|
|
136
|
+
floatable.options.content.hidden = false;
|
|
137
|
+
updatePosition(floatable);
|
|
138
|
+
}
|
|
139
|
+
function closeAbove(element) {
|
|
140
|
+
const state = getState();
|
|
141
|
+
if (state == null) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const index = state.order.findIndex(floatable => floatable.options.anchor === element ||
|
|
145
|
+
floatable.options.content === element);
|
|
146
|
+
if (index === -1 || index === state.order.length - 1) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
while (index < state.order.length - 1) {
|
|
150
|
+
void state.order.pop()?.toggle(false, true);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function closeAll() {
|
|
154
|
+
const state = getState();
|
|
155
|
+
const floatables = [...(state?.active ?? [])];
|
|
156
|
+
state?.active.clear();
|
|
157
|
+
for (const floatable of floatables) {
|
|
158
|
+
void floatable.toggle(false, true);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function closeNonInteractive(state) {
|
|
162
|
+
for (const floatable of state.active) {
|
|
163
|
+
if (!floatable.options.interactive) {
|
|
164
|
+
void floatable.toggle(false);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function deactivate(floatable) {
|
|
169
|
+
floatable.active = false;
|
|
170
|
+
floatable.options.content.hidden = true;
|
|
171
|
+
floatable.options.anchor.insertAdjacentElement(INSERT_AFTEREND, floatable.options.content);
|
|
172
|
+
floatable.options.onAfter?.(false);
|
|
173
|
+
}
|
|
174
|
+
function getState() {
|
|
175
|
+
return window._oscarpalmer_oui_floatable;
|
|
176
|
+
}
|
|
177
|
+
function getX(anchor, content, position) {
|
|
178
|
+
if (position.endsWith(POSITION_SUFFIX_END)) {
|
|
179
|
+
const end = anchor.right - content.width;
|
|
180
|
+
return end < 0 ? 0 : end;
|
|
181
|
+
}
|
|
182
|
+
if (position.endsWith(POSITION_SUFFIX_START)) {
|
|
183
|
+
const start = anchor.left;
|
|
184
|
+
return start + content.width > window.innerWidth
|
|
185
|
+
? window.innerWidth - content.width
|
|
186
|
+
: start;
|
|
187
|
+
}
|
|
188
|
+
if (position.startsWith(POSITION_END)) {
|
|
189
|
+
return anchor.right;
|
|
190
|
+
}
|
|
191
|
+
if (position.startsWith(POSITION_START)) {
|
|
192
|
+
return anchor.left - content.width;
|
|
193
|
+
}
|
|
194
|
+
if (position.startsWith(POSITION_HORIZONTAL)) {
|
|
195
|
+
if (anchor.right + content.width <= window.innerWidth) {
|
|
196
|
+
return anchor.right;
|
|
197
|
+
}
|
|
198
|
+
return anchor.left - content.width;
|
|
199
|
+
}
|
|
200
|
+
const left = anchor.left + (anchor.width - content.width) / 2;
|
|
201
|
+
return left < 0 ? 0 : left;
|
|
202
|
+
}
|
|
203
|
+
function getY(anchor, content, position, preferAbove) {
|
|
204
|
+
if (position.startsWith(POSITION_ABOVE)) {
|
|
205
|
+
return anchor.top - content.height;
|
|
206
|
+
}
|
|
207
|
+
if (position.startsWith(POSITION_BELOW)) {
|
|
208
|
+
return anchor.bottom;
|
|
209
|
+
}
|
|
210
|
+
if (position.endsWith(POSITION_BOTTOM)) {
|
|
211
|
+
const bottom = anchor.bottom - content.height;
|
|
212
|
+
return bottom < 0 ? 0 : bottom;
|
|
213
|
+
}
|
|
214
|
+
if (position.endsWith(POSITION_TOP)) {
|
|
215
|
+
const top = anchor.top;
|
|
216
|
+
return top + content.height > window.innerHeight
|
|
217
|
+
? window.innerHeight - content.height
|
|
218
|
+
: top;
|
|
219
|
+
}
|
|
220
|
+
if (position.startsWith(POSITION_VERTICAL)) {
|
|
221
|
+
if (preferAbove && anchor.top - content.height >= 0) {
|
|
222
|
+
return anchor.top - content.height;
|
|
223
|
+
}
|
|
224
|
+
if (anchor.bottom + content.height <= document.body.clientHeight) {
|
|
225
|
+
return anchor.bottom;
|
|
226
|
+
}
|
|
227
|
+
return anchor.top - content.height;
|
|
228
|
+
}
|
|
229
|
+
return anchor.top + (anchor.height - content.height) / 2;
|
|
230
|
+
}
|
|
231
|
+
function initialize(floatable) {
|
|
232
|
+
const { anchor, content, interactive } = floatable.options;
|
|
233
|
+
if (interactive) {
|
|
234
|
+
anchor.setAttribute(ATTRIBUTE_ANCHOR, '');
|
|
235
|
+
content.setAttribute(ATTRIBUTE_CONTENT, '');
|
|
236
|
+
}
|
|
237
|
+
content.style.position = 'fixed';
|
|
238
|
+
content.style.inset = '0 auto auto 0';
|
|
239
|
+
content.style.zIndex = '10';
|
|
240
|
+
const state = getState();
|
|
241
|
+
state?.mapped.set(anchor, floatable);
|
|
242
|
+
state?.mapped.set(content, floatable);
|
|
243
|
+
}
|
|
244
|
+
function isDisabled(element) {
|
|
245
|
+
return (element.disabled ||
|
|
246
|
+
element.readOnly ||
|
|
247
|
+
element.getAttribute('aria-disabled') === 'true');
|
|
248
|
+
}
|
|
249
|
+
function onClick(event) {
|
|
250
|
+
const related = findAncestor(event.target, SELECTOR_ALL);
|
|
251
|
+
if (!(related instanceof HTMLElement)) {
|
|
252
|
+
closeAll();
|
|
253
|
+
}
|
|
254
|
+
else if (related.hasAttribute(ATTRIBUTE_CONTENT)) {
|
|
255
|
+
closeAbove(related);
|
|
256
|
+
}
|
|
257
|
+
else if (!isDisabled(related)) {
|
|
258
|
+
toggle(related);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function onKeyDown(event) {
|
|
262
|
+
if (event.key !== 'Escape') {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const state = getState();
|
|
266
|
+
if (state == null || state.active.size === 0) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
closeNonInteractive(state);
|
|
270
|
+
const related = findAncestor(event.target, SELECTOR_CONTENT);
|
|
271
|
+
if (!(related instanceof HTMLElement)) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
closeAbove(related);
|
|
275
|
+
const floatable = state.mapped.get(related);
|
|
276
|
+
if (floatable != null) {
|
|
277
|
+
void floatable.toggle(false);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function stop(floatable) {
|
|
281
|
+
if (floatable.frame != null) {
|
|
282
|
+
cancelAnimationFrame(floatable.frame);
|
|
283
|
+
}
|
|
284
|
+
floatable.frame = undefined;
|
|
285
|
+
}
|
|
286
|
+
function toggle(anchor) {
|
|
287
|
+
const floatable = getState()?.mapped.get(anchor);
|
|
288
|
+
if (floatable == null) {
|
|
289
|
+
closeAll();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const active = Boolean(floatable.active);
|
|
293
|
+
const content = findAncestor(floatable.options.anchor, SELECTOR_CONTENT);
|
|
294
|
+
if (content == null) {
|
|
295
|
+
closeAll();
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
closeAbove(content);
|
|
299
|
+
}
|
|
300
|
+
void floatable.toggle(!active);
|
|
301
|
+
}
|
|
302
|
+
function updatePosition(floatable) {
|
|
303
|
+
let position = floatable.options.anchor
|
|
304
|
+
.getAttribute(floatable.options.positionAttribute)
|
|
305
|
+
?.trim() ?? floatable.options.defaultPosition;
|
|
306
|
+
if (!POSITIONS.has(position)) {
|
|
307
|
+
position = floatable.options.defaultPosition;
|
|
308
|
+
}
|
|
309
|
+
let previousAnchor;
|
|
310
|
+
let previousContent;
|
|
311
|
+
let calculated = false;
|
|
312
|
+
let runOnAfter = false;
|
|
313
|
+
function run() {
|
|
314
|
+
const anchor = floatable.options.anchor.getBoundingClientRect();
|
|
315
|
+
const content = floatable.options.content.getBoundingClientRect();
|
|
316
|
+
if (calculated) {
|
|
317
|
+
if (previousAnchor != null && previousContent != null) {
|
|
318
|
+
if (PROPERTIES.every(property => anchor[property] === previousAnchor[property]) &&
|
|
319
|
+
PROPERTIES.every(property => content[property] === previousContent[property])) {
|
|
320
|
+
floatable.frame = requestAnimationFrame(run);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
calculated = true;
|
|
327
|
+
}
|
|
328
|
+
update(anchor, content);
|
|
329
|
+
}
|
|
330
|
+
function update(anchor, content) {
|
|
331
|
+
previousAnchor = anchor;
|
|
332
|
+
previousContent = content;
|
|
333
|
+
const left = getX(anchor, content, position);
|
|
334
|
+
const top = getY(anchor, content, position, floatable.options.preferAbove);
|
|
335
|
+
if (top + content.height > window.innerHeight) {
|
|
336
|
+
floatable.options.content.style.height = `${window.innerHeight - top}px`;
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
floatable.options.content.style.height = '';
|
|
340
|
+
}
|
|
341
|
+
if (left + content.width > window.innerWidth) {
|
|
342
|
+
floatable.options.content.style.width = `${window.innerWidth - left}px`;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
floatable.options.content.style.width = '';
|
|
346
|
+
}
|
|
347
|
+
// floatable.options.content.style.inset = `${top}px auto auto ${left}px`;
|
|
348
|
+
floatable.options.content.style.transform = `translate3d(${left}px, ${top}px, 0)`;
|
|
349
|
+
floatable.frame = requestAnimationFrame(run);
|
|
350
|
+
if (!runOnAfter) {
|
|
351
|
+
runOnAfter = true;
|
|
352
|
+
floatable.options.onAfter?.(true);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
floatable.frame = requestAnimationFrame(run);
|
|
356
|
+
}
|
|
357
|
+
//
|
|
358
|
+
const ATTRIBUTE_ANCHOR = 'oui-floatable-anchor';
|
|
359
|
+
const ATTRIBUTE_CONTENT = 'oui-floatable-content';
|
|
360
|
+
const INSERT_AFTEREND = 'afterend';
|
|
361
|
+
const POSITION_ABOVE = 'above';
|
|
362
|
+
const POSITION_BELOW = 'below';
|
|
363
|
+
const POSITION_BOTTOM = 'bottom';
|
|
364
|
+
const POSITION_END = 'end';
|
|
365
|
+
const POSITION_HORIZONTAL = 'horizontal';
|
|
366
|
+
const POSITION_START = 'start';
|
|
367
|
+
const POSITION_SUFFIX_END = '-end';
|
|
368
|
+
const POSITION_SUFFIX_START = '-start';
|
|
369
|
+
const POSITION_TOP = 'top';
|
|
370
|
+
const POSITION_VERTICAL = 'vertical';
|
|
371
|
+
const POSITIONS = new Set([
|
|
372
|
+
'above',
|
|
373
|
+
'above-end',
|
|
374
|
+
'above-start',
|
|
375
|
+
'below',
|
|
376
|
+
'below-end',
|
|
377
|
+
'below-start',
|
|
378
|
+
'end',
|
|
379
|
+
'end-bottom',
|
|
380
|
+
'end-top',
|
|
381
|
+
'horizontal',
|
|
382
|
+
'horizontal-bottom',
|
|
383
|
+
'horizontal-top',
|
|
384
|
+
'start',
|
|
385
|
+
'start-bottom',
|
|
386
|
+
'start-top',
|
|
387
|
+
'vertical',
|
|
388
|
+
'vertical-end',
|
|
389
|
+
'vertical-start',
|
|
390
|
+
]);
|
|
391
|
+
const PROPERTIES = ['bottom', 'left', 'right', 'top'];
|
|
392
|
+
const SELECTOR_ALL = `[${ATTRIBUTE_ANCHOR}], [${ATTRIBUTE_CONTENT}]`;
|
|
393
|
+
const SELECTOR_CONTENT = `[${ATTRIBUTE_CONTENT}]`;
|
|
394
|
+
//
|
|
395
|
+
if (window._oscarpalmer_oui_floatable == null) {
|
|
396
|
+
Object.defineProperty(window, '_oscarpalmer_oui_floatable', {
|
|
397
|
+
enumerable: false,
|
|
398
|
+
value: {
|
|
399
|
+
active: new Set(),
|
|
400
|
+
mapped: new Map(),
|
|
401
|
+
order: [],
|
|
402
|
+
},
|
|
403
|
+
writable: false,
|
|
404
|
+
});
|
|
405
|
+
on(document, 'keydown', event => {
|
|
406
|
+
onKeyDown(event);
|
|
407
|
+
});
|
|
408
|
+
on(document, 'click', event => {
|
|
409
|
+
onClick(event);
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export { Floatable, deactivate };
|