athena-ui-js 2.0.1 → 2.2.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/README.md +58 -4
- package/docs/migration-2.0.md +1 -1
- package/docs/styling.md +24 -7
- package/package.json +5 -1
- package/src/athena-ui.js +2 -1
- package/src/libs/config/config.js +1 -1
- package/src/libs/core/motion.js +358 -0
- package/src/libs/core/popover.js +96 -0
- package/src/libs/core/theme.js +12 -0
- package/src/libs/hephaestus/chartGenerator/chartGenerator.js +9 -5
- package/src/libs/hephaestus/componentGenerator/componentGenerator.js +43 -2
- package/src/libs/hephaestus/dialogGenerator/dialogGenerator.js +2 -1
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +3 -1
- package/src/libs/hephaestus/navigationGenerator/navigationGenerator.js +45 -9
- package/src/libs/hephaestus/router/router.js +18 -9
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +25 -0
- package/src/motion.js +6 -0
- package/src/res/themes/default/style.css +260 -10
- package/src/res/themes/tokens.css +25 -0
- package/types/athena-ui.d.ts +97 -96
- package/types/libs/config/config.d.ts +3 -3
- package/types/libs/core/motion.d.ts +123 -0
- package/types/libs/core/popover.d.ts +36 -0
- package/types/libs/core/theme.d.ts +41 -39
- package/types/libs/hephaestus/chartGenerator/chartGenerator.d.ts +2 -0
- package/types/libs/hephaestus/componentGenerator/componentGenerator.d.ts +238 -222
- package/types/libs/hephaestus/navigationGenerator/navigationGenerator.d.ts +4 -1
- package/types/libs/hephaestus/router/router.d.ts +71 -67
- package/types/libs/hephaestus/tableGenerator/tableGenerator.d.ts +123 -120
- package/types/motion.d.ts +1 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//popover helpers for Athena UI
|
|
2
|
+
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
// The native popover API (the `popover` attribute) gives a dropdown or a tooltip what it needs without a line of script: it is drawn in the top layer (above
|
|
7
|
+
// everything, never cut off by an overflow of a parent), a click outside or Esc closes it, and the focus goes back. What it does not do is place the popover
|
|
8
|
+
// next to its button: that is placePopover() below, which sets two CSS variables (allowed under a strict Content-Security-Policy: no inline style attribute)
|
|
9
|
+
// that the stylesheet reads. Browsers without the popover API (before 2023 to 2024) get the plain fallback: the element is shown and hidden with `hidden`.
|
|
10
|
+
|
|
11
|
+
/** True when the browser has the popover API. */
|
|
12
|
+
export const supportsPopover = () => typeof HTMLElement !== "undefined" && "popover" in HTMLElement.prototype && typeof HTMLElement.prototype.showPopover === "function";
|
|
13
|
+
|
|
14
|
+
/** Is the popover (or the fallback element) open? */
|
|
15
|
+
export const isOpen = element => (supportsPopover() && element.hasAttribute("popover") ? element.matches(":popover-open") : !element.hidden);
|
|
16
|
+
|
|
17
|
+
/** Opens it: showPopover(), or without the API: the element is no longer hidden. Does nothing when it is open already. */
|
|
18
|
+
export function openPopover(element) {
|
|
19
|
+
if (isOpen(element)) return;
|
|
20
|
+
if (supportsPopover() && element.hasAttribute("popover")) element.showPopover();
|
|
21
|
+
else element.hidden = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Closes it. Does nothing when it is closed already. */
|
|
25
|
+
export function closePopover(element) {
|
|
26
|
+
if (!isOpen(element)) return;
|
|
27
|
+
if (supportsPopover() && element.hasAttribute("popover")) element.hidePopover();
|
|
28
|
+
else element.hidden = true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const EDGE = 4; // px kept free between the popover and the edge of the window
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Where a popover goes, next to the element it belongs to, before it is shown (so there is no frame at the wrong place). Only the side and the alignment
|
|
35
|
+
* are chosen here, from the position of the anchor in the window: the popover is moved with CSS (translate) by its own size. Once it is shown,
|
|
36
|
+
* `settlePopover` measures it and keeps it inside the window.
|
|
37
|
+
*
|
|
38
|
+
* @param {Element} anchor the button or the target of the tooltip
|
|
39
|
+
* @param {HTMLElement} popover
|
|
40
|
+
* @param {object} [options]
|
|
41
|
+
* @param {"bottom"|"top"|"auto"} [options.side="auto"] "bottom": below. "top": above, unless there is less than `room` px above. "auto": below,
|
|
42
|
+
* unless there is less than `room` px below and more room above
|
|
43
|
+
* @param {"start"|"end"|"center"} [options.align="start"] which edge of the anchor the popover lines up with
|
|
44
|
+
* @param {number} [options.gap=4] px between the anchor and the popover
|
|
45
|
+
* @param {number} [options.room=240] how much room a side needs, in px (a tooltip needs about 40)
|
|
46
|
+
*/
|
|
47
|
+
export function placePopover(anchor, popover, { side = "auto", align = "start", gap = 4, room = 240 } = {}) {
|
|
48
|
+
const box = anchor.getBoundingClientRect();
|
|
49
|
+
const height = document.documentElement.clientHeight;
|
|
50
|
+
const above = side === "top" ? box.top >= room : side === "auto" && height - box.bottom < room && box.top > height - box.bottom;
|
|
51
|
+
const x = align === "end" ? box.right : align === "center" ? box.left + box.width / 2 : box.left;
|
|
52
|
+
const y = above ? box.top - gap : box.bottom + gap;
|
|
53
|
+
popover.style.setProperty("--athena-popover-left", `${Math.round(x)}px`);
|
|
54
|
+
popover.style.setProperty("--athena-popover-top", `${Math.round(y)}px`);
|
|
55
|
+
popover.classList.toggle("athena-popover-above", above);
|
|
56
|
+
popover.classList.toggle("athena-popover-end", align === "end");
|
|
57
|
+
popover.classList.toggle("athena-popover-center", align === "center");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* After the popover is shown: measures it and moves it back inside the window when it sticks out to the left, the right or the bottom (a popover that
|
|
62
|
+
* is taller than the room is left where it is: it scrolls).
|
|
63
|
+
*/
|
|
64
|
+
export function settlePopover(popover) {
|
|
65
|
+
const box = popover.getBoundingClientRect();
|
|
66
|
+
const width = document.documentElement.clientWidth;
|
|
67
|
+
const height = document.documentElement.clientHeight;
|
|
68
|
+
const dx = box.left < EDGE ? EDGE - box.left : box.right > width - EDGE ? width - EDGE - box.right : 0;
|
|
69
|
+
const dy = box.bottom > height - EDGE && box.top - (box.bottom - (height - EDGE)) >= EDGE ? height - EDGE - box.bottom : 0;
|
|
70
|
+
if (dx) popover.style.setProperty("--athena-popover-left", `${Math.round(parseFloat(popover.style.getPropertyValue("--athena-popover-left")) + dx)}px`);
|
|
71
|
+
if (dy) popover.style.setProperty("--athena-popover-top", `${Math.round(parseFloat(popover.style.getPropertyValue("--athena-popover-top")) + dy)}px`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Keeps an open popover next to its anchor while the page scrolls or the window changes size. Returns a function that stops it.
|
|
76
|
+
* (A popover in the top layer does not move with its anchor by itself.)
|
|
77
|
+
*/
|
|
78
|
+
export function followAnchor(anchor, popover, options) {
|
|
79
|
+
let queued = false;
|
|
80
|
+
const update = () => {
|
|
81
|
+
if (queued) return;
|
|
82
|
+
queued = true;
|
|
83
|
+
requestAnimationFrame(() => {
|
|
84
|
+
queued = false;
|
|
85
|
+
if (!isOpen(popover)) return;
|
|
86
|
+
placePopover(anchor, popover, options);
|
|
87
|
+
settlePopover(popover);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
window.addEventListener("scroll", update, { capture: true, passive: true });
|
|
91
|
+
window.addEventListener("resize", update, { passive: true });
|
|
92
|
+
return () => {
|
|
93
|
+
window.removeEventListener("scroll", update, { capture: true });
|
|
94
|
+
window.removeEventListener("resize", update);
|
|
95
|
+
};
|
|
96
|
+
}
|
package/src/libs/core/theme.js
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
// Nothing is sent to a server. theme-boot.js (a classic script of its own) sets the two attributes before the page is
|
|
16
16
|
// drawn, so the wrong colours never flash.
|
|
17
17
|
|
|
18
|
+
import { prefersReducedMotion, readDuration } from "./motion.js";
|
|
19
|
+
|
|
18
20
|
export const DEFAULT_MODES = [
|
|
19
21
|
{ id: "system", name: "Like the system" },
|
|
20
22
|
{ id: "light", name: "Light" },
|
|
@@ -29,6 +31,7 @@ export const DEFAULT_MODES = [
|
|
|
29
31
|
* @param {string} [options.themeColorVariable] CSS variable whose value becomes <meta name="theme-color"> (the bar of a phone browser)
|
|
30
32
|
* @param {HTMLElement} [options.root] element that gets the attributes (default: <html>)
|
|
31
33
|
* @param {Storage} [options.storage] default: localStorage
|
|
34
|
+
* @param {boolean} [options.transition=true] colours cross-fade when the theme or the mode changes (not under reduced motion)
|
|
32
35
|
* @returns {{themes, modes, appearance: () => object, setTheme: (id) => boolean, setMode: (id) => boolean,
|
|
33
36
|
* onChange: (listener) => Function, refresh: Function, destroy: Function}}
|
|
34
37
|
*/
|
|
@@ -58,7 +61,16 @@ export function createThemeManager(options = {}) {
|
|
|
58
61
|
let current = read();
|
|
59
62
|
const appearance = () => ({ ...current, dark: root.dataset.mode === "dark" });
|
|
60
63
|
|
|
64
|
+
let fade = 0;
|
|
65
|
+
let started = false;
|
|
61
66
|
function apply() {
|
|
67
|
+
// A change after the first one cross-fades: the class makes every colour transition for a moment
|
|
68
|
+
if (started && options.transition !== false && !prefersReducedMotion()) {
|
|
69
|
+
root.classList.add("athena-switching");
|
|
70
|
+
clearTimeout(fade);
|
|
71
|
+
fade = setTimeout(() => root.classList.remove("athena-switching"), readDuration("--athena-duration-slow", root) + 60);
|
|
72
|
+
}
|
|
73
|
+
started = true;
|
|
62
74
|
const dark = current.mode === "dark" || (current.mode === "system" && Boolean(systemDark()?.matches));
|
|
63
75
|
root.dataset.theme = current.theme;
|
|
64
76
|
root.dataset.mode = dark ? "dark" : "light";
|
|
@@ -274,7 +274,7 @@ function drawLine(svg, ctx) {
|
|
|
274
274
|
const outline = [`M${points[0][0]},${baseline}`, ...points.map(p => `L${p.join(",")}`), `L${points.at(-1)[0]},${baseline}`, "Z"];
|
|
275
275
|
clipped.append(svgEl("path", { d: outline.join(" "), class: "chart-area" }));
|
|
276
276
|
}
|
|
277
|
-
clipped.append(svgEl("polyline", { points: points.map(p => p.join(",")).join(" "), class: "chart-line", fill: "none" }));
|
|
277
|
+
clipped.append(svgEl("polyline", { points: points.map(p => p.join(",")).join(" "), class: "chart-line", fill: "none", pathLength: 1 }));
|
|
278
278
|
group.append(clipped);
|
|
279
279
|
if (dotted) {
|
|
280
280
|
for (const i of inside) {
|
|
@@ -524,6 +524,7 @@ function buildDataTable({ labels, series, times, title, type }) {
|
|
|
524
524
|
* @param {(range: ({from: number, to: number}|null)) => void} [options.onZoom] called when a person zooms or resets
|
|
525
525
|
* @param {number} [options.maxPoints] `push` keeps at most this many points per series (the oldest are dropped)
|
|
526
526
|
* @param {number[]} [options.hiddenSeries] the series (or slices) that are hidden at the start; a person hides and shows them with the legend, and a redraw keeps that
|
|
527
|
+
* @param {boolean} [options.animate=true] the first drawing animates: bars grow, a line is drawn, slices pop in (not when it is redrawn, and not under reduced motion)
|
|
527
528
|
* @param {boolean} [options.interactive=true] line and area charts: false leaves out the pointer layer (tooltip that follows the pointer, zoom)
|
|
528
529
|
* @returns {(SVGElement & {element: SVGElement, update: (options?: object) => SVGElement, destroy: () => void,
|
|
529
530
|
* zoom: (from: number, to: number) => void, resetZoom: () => void, getZoom: () => ({from: number, to: number}|null),
|
|
@@ -539,6 +540,8 @@ export function generateChart(options = {}) {
|
|
|
539
540
|
if (options.labels) state.used.labels = [...options.labels];
|
|
540
541
|
if (options.times) state.used.times = [...options.times];
|
|
541
542
|
const hidden = new Set(options.hiddenSeries ?? []);
|
|
543
|
+
// Only the first drawing animates (bars grow, the line is drawn, slices pop in): a redraw while streaming or zooming must not start it again
|
|
544
|
+
let firstDrawing = options.animate !== false;
|
|
542
545
|
const hooks = {
|
|
543
546
|
legend: (series, off) => {
|
|
544
547
|
if (off) hidden.add(series);
|
|
@@ -554,7 +557,7 @@ export function generateChart(options = {}) {
|
|
|
554
557
|
const handle = {
|
|
555
558
|
element: () => state.current.svg,
|
|
556
559
|
redraw() {
|
|
557
|
-
const next = buildChart(state.used, hooks);
|
|
560
|
+
const next = buildChart(state.used, hooks, { animate: false });
|
|
558
561
|
if (!next) return state.current?.svg;
|
|
559
562
|
// A redraw replaces the whole SVG (every second while streaming, and when someone zooms). The keyboard focus would be lost
|
|
560
563
|
// with it, so it goes to the same place in the new one: the same legend entry, the same point, or the plot.
|
|
@@ -623,7 +626,8 @@ export function generateChart(options = {}) {
|
|
|
623
626
|
dataTable: { get: () => state.current.table ?? null, configurable: true }
|
|
624
627
|
});
|
|
625
628
|
|
|
626
|
-
const built = buildChart(state.used, hooks);
|
|
629
|
+
const built = buildChart(state.used, hooks, { animate: firstDrawing });
|
|
630
|
+
firstDrawing = false;
|
|
627
631
|
if (!built) return null;
|
|
628
632
|
state.current = built;
|
|
629
633
|
const host = resolveElement(options.parent ?? "wrapper") ?? document.body;
|
|
@@ -632,7 +636,7 @@ export function generateChart(options = {}) {
|
|
|
632
636
|
return attach(built.svg);
|
|
633
637
|
}
|
|
634
638
|
|
|
635
|
-
function buildChart(options = {}, hooks) {
|
|
639
|
+
function buildChart(options = {}, hooks, { animate = false } = {}) {
|
|
636
640
|
const { type = "bar", id, title } = options;
|
|
637
641
|
const sparkline = type === "sparkline";
|
|
638
642
|
const width = options.width ?? (sparkline ? 120 : 600);
|
|
@@ -652,7 +656,7 @@ function buildChart(options = {}, hooks) {
|
|
|
652
656
|
return null;
|
|
653
657
|
}
|
|
654
658
|
|
|
655
|
-
const svg = svgEl("svg", { viewBox: `0 0 ${width} ${height}`, width, height, class: `athena-chart athena-chart-${type}`, role: "img" });
|
|
659
|
+
const svg = svgEl("svg", { viewBox: `0 0 ${width} ${height}`, width, height, class: `athena-chart athena-chart-${type}${animate && type !== "sparkline" ? " athena-chart-animated" : ""}`, role: "img" });
|
|
656
660
|
if (id) svg.id = id;
|
|
657
661
|
const summary = `${title ?? translate(`chart.${type}`)}: ${series.map(s => s.name ?? "").filter(Boolean).join(", ")}`.replace(/: $/, "");
|
|
658
662
|
svg.setAttribute("aria-label", options.ariaLabel ?? summary);
|
|
@@ -7,6 +7,7 @@ import { asHandle, cx, el, mount, resolveElement, setContent, uid } from "../../
|
|
|
7
7
|
import { generateBreadcrumbs } from "../navigationGenerator/navigationGenerator.js";
|
|
8
8
|
import { generateSpinner } from "../feedbackGenerator/feedbackGenerator.js";
|
|
9
9
|
import { translate } from "../../core/i18n.js";
|
|
10
|
+
import { closePopover, followAnchor, openPopover, placePopover, settlePopover, supportsPopover } from "../../core/popover.js";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Generates a link. External links open in a new tab and get `rel="noopener noreferrer"`.
|
|
@@ -199,7 +200,9 @@ export function generateGrid(options = {}) {
|
|
|
199
200
|
}
|
|
200
201
|
|
|
201
202
|
/**
|
|
202
|
-
* Adds a tooltip to an element. It shows on hover and on keyboard focus.
|
|
203
|
+
* Adds a tooltip to an element. It shows on hover and on keyboard focus, and Esc hides it.
|
|
204
|
+
* It is a native popover (`popover="manual"`), so it is drawn above everything and never cut off by a parent with `overflow: hidden`. A touch
|
|
205
|
+
* does not show it (a tap would hide the thing it describes). Browsers without the popover API show it with CSS, as before.
|
|
203
206
|
*
|
|
204
207
|
* @param {Element|string} target element or id
|
|
205
208
|
* @param {string} text
|
|
@@ -208,7 +211,8 @@ export function generateTooltip(target, text) {
|
|
|
208
211
|
const node = resolveElement(target);
|
|
209
212
|
if (!node) return null;
|
|
210
213
|
const id = uid("tooltip");
|
|
211
|
-
const
|
|
214
|
+
const native = supportsPopover();
|
|
215
|
+
const tip = el("span", { class: "tooltip-text athena-popover", id, text, attrs: { role: "tooltip", popover: native ? "manual" : null } });
|
|
212
216
|
// Inputs and images cannot hold the tooltip text, so they get a wrapper.
|
|
213
217
|
let host = node;
|
|
214
218
|
if (node.matches("input,img,select,textarea")) {
|
|
@@ -221,6 +225,26 @@ export function generateTooltip(target, text) {
|
|
|
221
225
|
host.classList.add("has-tooltip");
|
|
222
226
|
node.setAttribute("aria-describedby", id);
|
|
223
227
|
host.append(tip);
|
|
228
|
+
if (native) {
|
|
229
|
+
let stop = null;
|
|
230
|
+
const show = () => {
|
|
231
|
+
if (tip.matches(":popover-open")) return;
|
|
232
|
+
placePopover(node, tip, { side: "top", align: "center", gap: 6, room: 40 });
|
|
233
|
+
openPopover(tip);
|
|
234
|
+
settlePopover(tip);
|
|
235
|
+
stop = followAnchor(node, tip, { side: "top", align: "center", gap: 6, room: 40 });
|
|
236
|
+
};
|
|
237
|
+
const hide = () => {
|
|
238
|
+
stop?.();
|
|
239
|
+
stop = null;
|
|
240
|
+
closePopover(tip);
|
|
241
|
+
};
|
|
242
|
+
host.addEventListener("pointerenter", event => { if (event.pointerType !== "touch") show(); });
|
|
243
|
+
host.addEventListener("pointerleave", hide);
|
|
244
|
+
host.addEventListener("focusin", show);
|
|
245
|
+
host.addEventListener("focusout", hide);
|
|
246
|
+
host.addEventListener("keydown", event => { if (event.key === "Escape") hide(); });
|
|
247
|
+
}
|
|
224
248
|
return tip;
|
|
225
249
|
}
|
|
226
250
|
|
|
@@ -302,6 +326,23 @@ export function generateEmptyState(options = {}) {
|
|
|
302
326
|
return asHandle({ element });
|
|
303
327
|
}
|
|
304
328
|
|
|
329
|
+
/**
|
|
330
|
+
* A skeleton: grey lines with a shimmer where text or a card will be, so the page does not jump when it arrives. It is a live region that says
|
|
331
|
+
* "Loading..." to a screen reader; the shimmer stops under reduced motion.
|
|
332
|
+
*
|
|
333
|
+
* @param {object} [options]
|
|
334
|
+
* @param {string|Element} [options.parent]
|
|
335
|
+
* @param {number} [options.lines=3] the last of several lines is shorter
|
|
336
|
+
* @param {string} [options.class] e.g. "skeleton-card" in your own CSS gives the box a height
|
|
337
|
+
*/
|
|
338
|
+
export function generateSkeleton(options = {}) {
|
|
339
|
+
const lines = Array.from({ length: Math.max(1, options.lines ?? 3) }, () => el("span", { class: "athena-skeleton athena-skeleton-line", attrs: { "aria-hidden": "true" } }));
|
|
340
|
+
const element = el("div", { id: options.id, class: cx("athena-skeleton-box", options.class), attrs: { role: "status", "aria-live": "polite" } },
|
|
341
|
+
...lines, el("span", { class: "athena-visually-hidden", text: translate("loading") }));
|
|
342
|
+
mount(options.parent, element);
|
|
343
|
+
return asHandle({ element });
|
|
344
|
+
}
|
|
345
|
+
|
|
305
346
|
/**
|
|
306
347
|
* A placeholder while something loads: a spinner and a text (a live region, so a screen reader says it).
|
|
307
348
|
*
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
6
|
import { cx, el, setContent, uid } from "../../core/dom.js";
|
|
7
|
+
import { removeAfterTransition } from "../../core/motion.js";
|
|
7
8
|
import { translate } from "../../core/i18n.js";
|
|
8
9
|
import { generateButton, generateForm } from "../formGenerator/formBuilder.js";
|
|
9
10
|
|
|
@@ -48,8 +49,8 @@ export function showDialog(options = {}) {
|
|
|
48
49
|
return;
|
|
49
50
|
}
|
|
50
51
|
if (dialog.open) dialog.close();
|
|
51
|
-
dialog.remove();
|
|
52
52
|
resolve(value);
|
|
53
|
+
removeAfterTransition(dialog); // it fades out first (at once under reduced motion)
|
|
53
54
|
};
|
|
54
55
|
|
|
55
56
|
// Esc: the browser asks (cancel); a dialog that cannot be dismissed stays, and beforeClose can still say no
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
6
|
import { asHandle, cx, el, mount, setContent } from "../../core/dom.js";
|
|
7
|
+
import { removeAfterTransition } from "../../core/motion.js";
|
|
7
8
|
import { translate } from "../../core/i18n.js";
|
|
8
9
|
|
|
9
10
|
const TOAST_CONTAINER_ID = "athena-toasts";
|
|
@@ -38,7 +39,8 @@ export function showToast(options) {
|
|
|
38
39
|
let timer;
|
|
39
40
|
const close = () => {
|
|
40
41
|
clearTimeout(timer);
|
|
41
|
-
toast.
|
|
42
|
+
toast.classList.add("is-leaving");
|
|
43
|
+
removeAfterTransition(toast);
|
|
42
44
|
};
|
|
43
45
|
const toast = el("div", {
|
|
44
46
|
class: cx("toast", `alert-${type}`),
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { asHandle, cx, el, mount, setContent, uid } from "../../core/dom.js";
|
|
7
7
|
import { translate } from "../../core/i18n.js";
|
|
8
8
|
import { pageRange } from "../../core/pagination.js";
|
|
9
|
+
import { closePopover, followAnchor, isOpen, openPopover, placePopover, settlePopover, supportsPopover } from "../../core/popover.js";
|
|
9
10
|
|
|
10
11
|
/* ------------------TABS------------------- */
|
|
11
12
|
|
|
@@ -228,7 +229,10 @@ export function generateAccordion(options = {}) {
|
|
|
228
229
|
/* ------------------DROPDOWN------------------- */
|
|
229
230
|
|
|
230
231
|
/**
|
|
231
|
-
* Generates a button that opens a menu. Arrow keys move through the items, Esc closes it.
|
|
232
|
+
* Generates a button that opens a menu. Arrow keys move through the items, Esc closes it, and so does a click outside.
|
|
233
|
+
* The menu is a native popover (the `popover` attribute): it is drawn above everything, so no parent can cut it off, and the browser takes care of the
|
|
234
|
+
* light dismiss and of the focus. It is placed next to the button, below it, or above it when there is no room below. Browsers without the popover API
|
|
235
|
+
* get a plain menu that is shown and hidden with `hidden`.
|
|
232
236
|
*
|
|
233
237
|
* @param {object} options
|
|
234
238
|
* @param {string|Element} [options.parent]
|
|
@@ -243,12 +247,18 @@ export function generateAccordion(options = {}) {
|
|
|
243
247
|
*/
|
|
244
248
|
export function generateDropdown(options = {}) {
|
|
245
249
|
const menuId = uid("dropdown");
|
|
246
|
-
const
|
|
250
|
+
const native = supportsPopover();
|
|
251
|
+
const align = options.align === "right" ? "end" : "start";
|
|
252
|
+
const menu = el("div", {
|
|
253
|
+
class: cx("dropdown-menu athena-popover", options.align === "right" && "dropdown-right"), id: menuId,
|
|
254
|
+
attrs: { role: "menu", popover: native ? "auto" : null, hidden: native ? null : true }
|
|
255
|
+
});
|
|
247
256
|
const button = el("button", {
|
|
248
257
|
class: cx("athena-button button-secondary dropdown-toggle", options.icon && !options.label && "dropdown-icon-only", options.toggleClass),
|
|
249
258
|
attrs: {
|
|
250
259
|
type: "button", "aria-haspopup": "menu", "aria-expanded": "false", "aria-controls": menuId,
|
|
251
|
-
"aria-label": options.ariaLabel
|
|
260
|
+
"aria-label": options.ariaLabel,
|
|
261
|
+
popovertarget: native ? menuId : null // the browser opens and closes it, and a click on the button is not a click "outside"
|
|
252
262
|
}
|
|
253
263
|
});
|
|
254
264
|
if (options.icon) button.append(setContent(el("span", { class: "dropdown-icon", attrs: { "aria-hidden": "true" } }), options.icon));
|
|
@@ -257,18 +267,44 @@ export function generateDropdown(options = {}) {
|
|
|
257
267
|
const element = el("div", { id: options.id, class: cx("dropdown", options.class) }, button, menu);
|
|
258
268
|
|
|
259
269
|
const enabledItems = () => [...menu.querySelectorAll("[role=menuitem]:not([disabled])")];
|
|
270
|
+
// Without the popover API: the click outside is looked for here
|
|
260
271
|
const onOutside = event => { if (!element.contains(event.target)) close(); };
|
|
272
|
+
let stopFollowing = null;
|
|
261
273
|
function open() {
|
|
262
|
-
menu
|
|
274
|
+
if (isOpen(menu)) return;
|
|
275
|
+
if (native) {
|
|
276
|
+
openPopover(menu); // the handlers below place it and set aria-expanded
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
placePopover(button, menu, { align });
|
|
280
|
+
openPopover(menu);
|
|
281
|
+
settlePopover(menu);
|
|
263
282
|
button.setAttribute("aria-expanded", "true");
|
|
264
283
|
document.addEventListener("pointerdown", onOutside);
|
|
265
284
|
}
|
|
266
285
|
function close(restoreFocus = false) {
|
|
267
|
-
menu
|
|
268
|
-
|
|
269
|
-
|
|
286
|
+
closePopover(menu);
|
|
287
|
+
if (!native) {
|
|
288
|
+
button.setAttribute("aria-expanded", "false");
|
|
289
|
+
document.removeEventListener("pointerdown", onOutside);
|
|
290
|
+
}
|
|
270
291
|
if (restoreFocus === true) button.focus();
|
|
271
292
|
}
|
|
293
|
+
if (native) {
|
|
294
|
+
// beforetoggle comes before the menu is drawn: placed now, it never shows at the wrong place. It comes for a click, Esc, a click outside and open().
|
|
295
|
+
menu.addEventListener("beforetoggle", event => {
|
|
296
|
+
const opened = event.newState === "open";
|
|
297
|
+
button.setAttribute("aria-expanded", String(opened));
|
|
298
|
+
if (opened) placePopover(button, menu, { align });
|
|
299
|
+
});
|
|
300
|
+
menu.addEventListener("toggle", event => {
|
|
301
|
+
stopFollowing?.();
|
|
302
|
+
stopFollowing = null;
|
|
303
|
+
if (event.newState !== "open") return;
|
|
304
|
+
settlePopover(menu); // now it has a size: keep it inside the window
|
|
305
|
+
stopFollowing = followAnchor(button, menu, { align });
|
|
306
|
+
});
|
|
307
|
+
}
|
|
272
308
|
|
|
273
309
|
for (const item of options.items ?? []) {
|
|
274
310
|
if (item.divider) {
|
|
@@ -288,11 +324,11 @@ export function generateDropdown(options = {}) {
|
|
|
288
324
|
menu.append(node);
|
|
289
325
|
}
|
|
290
326
|
|
|
291
|
-
button.addEventListener("click", () => menu
|
|
327
|
+
if (!native) button.addEventListener("click", () => (isOpen(menu) ? close() : open()));
|
|
292
328
|
button.addEventListener("keydown", event => {
|
|
293
329
|
if (event.key === "ArrowDown") {
|
|
294
330
|
event.preventDefault();
|
|
295
|
-
|
|
331
|
+
open();
|
|
296
332
|
enabledItems()[0]?.focus();
|
|
297
333
|
}
|
|
298
334
|
});
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
6
|
import { resolveElement } from "../../core/dom.js";
|
|
7
|
+
import { viewTransition } from "../../core/motion.js";
|
|
7
8
|
import { matchRoute, normalizePath, parseQuery } from "./matchRoute.js";
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -25,6 +26,9 @@ import { matchRoute, normalizePath, parseQuery } from "./matchRoute.js";
|
|
|
25
26
|
* @param {boolean} [options.focusContent=false] after a page is shown the outlet gets the focus (it is made focusable for that), so a
|
|
26
27
|
* screen reader starts reading the new page and the keyboard continues there
|
|
27
28
|
* @param {boolean} [options.scrollTop=false] scroll to the top after a page is shown
|
|
29
|
+
* @param {boolean} [options.transition=false] a page change is a view transition (document.startViewTransition): the old page cross-fades into the new one,
|
|
30
|
+
* and elements with a `view-transition-name` move from one page to the next. Without the API, or under reduced motion, nothing changes. The old page
|
|
31
|
+
* stays on the screen until the page function of the new one is done, so pages that need long to build should draw something quickly.
|
|
28
32
|
* @param {object} [options.auth] sign-in rules for routes, see the route options below
|
|
29
33
|
* @param {() => boolean} [options.auth.isSignedIn]
|
|
30
34
|
* @param {(role: string) => boolean} [options.auth.hasRole]
|
|
@@ -91,15 +95,20 @@ export function createRouter(options = {}) {
|
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
const host = resolveElement(outlet);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
98
|
+
const show = async () => {
|
|
99
|
+
if (token !== renderToken) return; // a newer navigation took over while the transition was starting
|
|
100
|
+
host?.replaceChildren();
|
|
101
|
+
currentContext = context;
|
|
102
|
+
options.onRender?.(context, matched?.route);
|
|
103
|
+
if (!matched) {
|
|
104
|
+
notFound?.(context);
|
|
105
|
+
} else {
|
|
106
|
+
if (matched.route.title) document.title = titlePrefix + matched.route.title;
|
|
107
|
+
await matched.route.handler(context);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
if (options.transition) await viewTransition(show);
|
|
111
|
+
else await show();
|
|
103
112
|
if (token !== renderToken) return;
|
|
104
113
|
if (options.focusContent && host) {
|
|
105
114
|
if (!host.hasAttribute("tabindex")) host.setAttribute("tabindex", "-1");
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { downloadFile, toCsv } from "../../core/csv.js";
|
|
7
7
|
import { asHandle, resolveElement } from "../../core/dom.js";
|
|
8
8
|
import { translate } from "../../core/i18n.js";
|
|
9
|
+
import { animate, fadeIn, prefersReducedMotion } from "../../core/motion.js";
|
|
9
10
|
import { clampWidth, virtualWindow } from "../../core/tableMath.js";
|
|
10
11
|
|
|
11
12
|
const el = (tag, className, text) => {
|
|
@@ -65,6 +66,8 @@ const compare = (a, b) =>
|
|
|
65
66
|
* in chunks of `chunkSize` (`page` is the chunk number), rows that have not arrived yet show a placeholder, and sorting or the filter
|
|
66
67
|
* start again from the top.
|
|
67
68
|
* @param {number} [options.chunkSize=50] rows per request of a data source with `virtual`
|
|
69
|
+
* @param {boolean} [options.animateRows=false] rows slide to their new place when the table is sorted, filtered or changes page, and new rows fade in
|
|
70
|
+
* (at most 60 rows are animated, none under reduced motion, none in a virtual table)
|
|
68
71
|
* @param {number} [options.filterDelay=300] ms to wait after typing before the data source is asked
|
|
69
72
|
* @param {{label: string, onclick: (row: object) => void, danger?: boolean, hidden?: (row: object) => boolean, ariaLabel?: (row: object) => string}[]} [options.rowActions]
|
|
70
73
|
* a column of buttons at the end of every row
|
|
@@ -355,8 +358,12 @@ export function generateTable(options = {}) {
|
|
|
355
358
|
return tr;
|
|
356
359
|
}
|
|
357
360
|
|
|
361
|
+
const rowOf = new WeakMap(); // the element of a row -> its row object, to find the same row again after a redraw (animateRows)
|
|
362
|
+
let rendered = false;
|
|
363
|
+
|
|
358
364
|
function renderRow(row, index) {
|
|
359
365
|
const tr = el("tr");
|
|
366
|
+
if (row !== undefined) rowOf.set(tr, row);
|
|
360
367
|
if (virtual) tr.setAttribute("aria-rowindex", String(index + 2));
|
|
361
368
|
if (row === undefined) {
|
|
362
369
|
// A row of a data source that has not arrived yet
|
|
@@ -558,10 +565,28 @@ export function generateTable(options = {}) {
|
|
|
558
565
|
root.classList.toggle("athena-table-is-loading", remote && server.loading);
|
|
559
566
|
renderPager(current.length);
|
|
560
567
|
if (virtual) table.setAttribute("aria-rowcount", String(current.length + 1));
|
|
568
|
+
// Where the rows were before (FLIP): the same row object is the same row
|
|
569
|
+
const before = options.animateRows && rendered && !virtual && !prefersReducedMotion()
|
|
570
|
+
? new Map([...(table.tBodies[0]?.rows ?? [])].slice(0, 60).map(tr => [rowOf.get(tr), tr.getBoundingClientRect()]).filter(([row]) => row !== undefined))
|
|
571
|
+
: null;
|
|
561
572
|
const parts = [renderHead(current), renderBody()];
|
|
562
573
|
if (resizableColumns || Object.keys(widths).length) parts.unshift(renderColgroup());
|
|
563
574
|
if (options.caption) parts.unshift(el("caption", null, options.caption));
|
|
564
575
|
table.replaceChildren(...parts);
|
|
576
|
+
if (before?.size) {
|
|
577
|
+
for (const tr of [...table.tBodies[0].rows].slice(0, 60)) {
|
|
578
|
+
const row = rowOf.get(tr);
|
|
579
|
+
if (row === undefined) continue;
|
|
580
|
+
const old = before.get(row);
|
|
581
|
+
if (!old) {
|
|
582
|
+
fadeIn(tr);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const now = tr.getBoundingClientRect();
|
|
586
|
+
if (Math.abs(old.top - now.top) > 1) animate(tr, [{ translate: `0 ${old.top - now.top}px` }, { translate: "0 0" }], { easing: "cubic-bezier(0.2, 0, 0, 1)" });
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
rendered = true;
|
|
565
590
|
if (Object.keys(widths).length) applyWidths();
|
|
566
591
|
if (scroller) scroller.scrollTop = scrollTop;
|
|
567
592
|
}
|
package/src/motion.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//motion entry of Athena UI: animate, transition, flip, tween, spring, stagger, viewTransition, setMotion
|
|
2
|
+
//Import only what you use: `import { animate } from "athena-ui-js/motion"`.
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
export * from "./libs/core/motion.js";
|