athena-ui-js 2.0.1 → 2.1.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 CHANGED
@@ -5,7 +5,7 @@ layout (header, menu, content wrapper, footer) from a single `init()` call, load
5
5
  and theme, and ships the building blocks of a multipurpose application: forms, tables, charts,
6
6
  feedback, navigation, a router and more.
7
7
 
8
- Current version: **2.0.1**
8
+ Current version: **2.1.0**
9
9
 
10
10
  [Quick start](#usage) · [API](#api) · [Components](#charts) · [Themes](#themes) · [Tests](#tests) · [Release notes](#releases)
11
11
 
@@ -22,7 +22,7 @@ Known limits are listed below, so you can judge whether it fits your project:
22
22
  - Athena UI has **no dependencies**: no jQuery, no bundled library, nothing is loaded at run time, and nothing is put on `window` unless you opt in with
23
23
  `athena-ui-js/classic`. The older generators (`generateLoginForm`, the layout and the header / footer menu built from `menuItems`) are plain DOM too.
24
24
  - The default theme follows the system's dark mode. Put `data-mode="light"` on `<html>` (or use `createThemeManager`) to choose.
25
- - Dialogs and the accordion use the native `<dialog>` and `<details>` elements, so they need a browser that supports cascade layers and `<dialog>` (2022 or later). The dropdown still has its own menu.
25
+ - Dialogs, the accordion, the dropdown menu and tooltips use the native `<dialog>`, `<details>` and popover elements, so they need a browser that supports cascade layers and `<dialog>` (2022 or later). The dropdown menu and the tooltips are native popovers (the `popover` attribute) in browsers that have them (Chrome / Edge 114, Safari 17, Firefox 125), and a plain fallback in older ones.
26
26
 
27
27
  ## Features
28
28
 
@@ -74,6 +74,8 @@ Without npm, serve the `src` folder with any static web server and open `index.h
74
74
 
75
75
  ### The demo application
76
76
 
77
+ **Try it live: https://athena-ui-js-demo-duc7dncrfhg5fxfs.northeurope-01.azurewebsites.net** (sign in with `demo` / `demo-pass`, or `admin` / `admin-pass`; the app sleeps when nobody visits, so the first request can take a few seconds).
78
+
77
79
  `demo/` is a demo and test application: tables with 100,000 rows from a server, a streaming chart, every field type, native dialogs, a menu with groups inside groups, themes
78
80
  and modes, and a real sign in, served by a small Node server (no dependencies) under a strict Content-Security-Policy.
79
81
 
@@ -147,7 +149,7 @@ It takes the options of `init()`. The header and footer follow the settings `gen
147
149
  | `header_title_text` | `"Athena UI"` | Title in the header |
148
150
  | `header_img_url` | | Header logo URL |
149
151
  | `copyright` | placeholder text | Footer text, the version is appended |
150
- | `version` | `"2.0.1"` | Version shown in the footer |
152
+ | `version` | `"2.1.0"` | Version shown in the footer |
151
153
  | `bordered` | `true` | Draw a border around the layout blocks |
152
154
  | `generateHeader`, `generateFooter` | `true` | Generate the header / footer block |
153
155
  | `generateHeadercontent`, `generateFooterContent`, `generateWrappercontent` | `true` | Generate the content of the block |
@@ -355,6 +357,11 @@ never parsed as HTML. `openModal` / `closeModal` got the same focus handling.
355
357
 
356
358
  ### Navigation
357
359
 
360
+ The **dropdown** menu and the **tooltip** are native popovers: they are drawn in the top layer, so no parent with `overflow: hidden` can cut them off and no `z-index` fight is possible;
361
+ a click outside and Esc close the dropdown, and the focus goes back to its button. They are placed next to their button by a small helper (it sets two CSS variables, which a strict
362
+ Content-Security-Policy allows): below, or above when there is no room below, and always kept inside the window. A tooltip shows on hover and on focus and goes on Esc; a touch does not show it.
363
+ Without the popover API the dropdown is a plain menu that is shown and hidden with `hidden`, and the tooltip a CSS one.
364
+
358
365
  ```js
359
366
  generateTabs({ parent: "wrapper", tabs: [{ id: "a", label: "A", content: "First" }, { id: "b", label: "B", content: () => lazyNode() }] });
360
367
  generateAccordion({ parent: "wrapper", items: [{ title: "Q", content: "A", open: true }], multiple: false });
@@ -741,6 +748,17 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
741
748
 
742
749
  ## Releases
743
750
 
751
+ ### 2.1.0
752
+ #### Changed:
753
+ 1. The dropdown menu is a native popover (`popover="auto"`, opened by its button through `popovertarget`): it is drawn in the top layer, so no parent with `overflow: hidden` cuts it off; a click outside and Esc close it and the focus goes back to the button; `aria-expanded` follows every way of opening and closing it. Markup: the menu has the attribute `popover` and no `hidden` any more (use `:popover-open`)
754
+ 2. The tooltip is a native popover (`popover="manual"`): shown on hover and on keyboard focus, hidden on leave, blur and Esc, never shown by a touch, and never cut off by a parent
755
+ 3. Both are placed next to their button by a small helper that sets two CSS variables (`--athena-popover-left`, `--athena-popover-top`; allowed by a strict Content-Security-Policy): below, or above when there is no room below, centred or aligned to an edge, and always kept inside the window; they follow the button when the page scrolls or resizes
756
+ 4. Browsers without the popover API keep the previous behaviour (a plain menu shown with `hidden` and closed by a click outside, a CSS tooltip)
757
+ #### Added:
758
+ 1. `src/libs/core/popover.js` (placement helpers) and the CSS class `.athena-popover`
759
+ 2. The address of the live demo in the READMEs
760
+ 3. Section 3 ("Make it lighter") of the to-do list is complete: the native elements are `<dialog>`, `<details>` and the popover
761
+
744
762
  ### 2.0.1
745
763
  #### Added:
746
764
  1. `demo/`: a demo and test application of Athena UI (tables with 100,000 rows from a server, streaming charts, forms, dialogs, menus, themes, sign in) with a dependency free Node server, and end to end tests that drive a real browser (DevTools protocol) under a strict Content-Security-Policy
@@ -53,7 +53,7 @@ They are part of Athena UI (`athena-ui-js/controls`) and no longer git submodule
53
53
  `athena-ui-js/testing`, which gives jsdom `showModal()`; a synthetic Esc key does not close a native dialog, send the `cancel` event instead.
54
54
  - `generateAccordion` is `<details>` and `<summary>`: `.accordion-header` is a `<summary>`, there is no `aria-expanded` on a button, `.accordion-open` is the
55
55
  `[open]` attribute and `.accordion-title` is gone. `accordion.toggle(index, open)` works as before.
56
- - The dropdown keeps its own menu (a top-layer `popover` would need anchor positioning, which not every supported browser has yet).
56
+ - The dropdown and the tooltip became native popovers in 2.1 (see the release notes): the menu is `<div class="dropdown-menu" popover>` (no `hidden` attribute any more: use `:popover-open`), and the tooltip text is a `popover="manual"` element.
57
57
 
58
58
  ## 6. CSS: a layer, tokens and dark mode
59
59
 
package/docs/styling.md CHANGED
@@ -61,11 +61,13 @@ Dark colours: set `data-mode="dark"` on `<html>` (or let `createThemeManager` do
61
61
  | Tabs | `.tab` `.tab-active` `.tab-list` `.tab-panel` | `--athena-accent` `--athena-border-width` `--athena-fg` `--athena-line` |
62
62
  | Breadcrumbs, pagination | `.breadcrumb-item` `.breadcrumb-list` `.page-button` `.page-current` `.pagination` | `--athena-accent` `--athena-accent-fg` `--athena-bg` `--athena-border-width` `--athena-fg` `--athena-line` `--athena-muted` `--athena-space-1` |
63
63
  | Accordion: <details> and <summary> | `.accordion-header` `.accordion-icon` `.accordion-item` `.accordion-panel` `.sidebar-parent` | `--athena-accent` `--athena-border-width` `--athena-fg` `--athena-focus` `--athena-focus-width` `--athena-line` `--athena-surface` |
64
- | Dropdown | `.dropdown` `.dropdown-divider` `.dropdown-item` `.dropdown-menu` `.dropdown-right` | `--athena-bg` `--athena-border-width` `--athena-fg` `--athena-hover` `--athena-line` `--athena-radius` `--athena-shadow` |
64
+ | Dropdown | `.dropdown` `.dropdown-menu` `.dropdown-right` | `--athena-bg` `--athena-border-width` `--athena-line` `--athena-radius` `--athena-shadow` |
65
+ | A popover (the dropdown menu, the tooltip) is in the top layer, placed next to its anchor by script through two variables | `.athena-popover` `.athena-popover-above` `.athena-popover-center` `.athena-popover-end` `.dropdown-divider` `.dropdown-item` `.dropdown-menu` | `--athena-fg` `--athena-hover` `--athena-line` `--athena-popover-left` `--athena-popover-top` |
65
66
  | Sidebar | `.accordion-icon` `.sidebar` `.sidebar-link` `.sidebar-list` `.sidebar-parent` `.sidebar-sublist` `.sidebar-toggle` | `--athena-accent` `--athena-accent-fg` `--athena-fg` `--athena-hover` `--athena-space-2` |
66
67
  | Cards, sections, grid | `.athena-grid` `.card` `.card-body` `.card-footer` `.card-image` `.card-title` | `--athena-bg` `--athena-border-width` `--athena-line` `--athena-radius` `--athena-surface-alt` `--grid-columns` `--grid-gap` |
67
68
  | Badge, avatar, stat | `.avatar` `.avatar-large` `.avatar-small` `.badge` `.badge-error` `.badge-info` `.badge-pill` `.badge-success` `.badge-warning` `.stat` `.stat-bad` `.stat-good` `.stat-label` `.stat-neutral` `.stat-value` | `--athena-accent` `--athena-accent-fg` `--athena-bg` `--athena-border-width` `--athena-danger` `--athena-fg` `--athena-info` `--athena-line` `--athena-muted` `--athena-radius` `--athena-success` `--athena-surface` `--athena-warning` |
68
69
  | Tooltip | `.has-tooltip` `.tooltip-text` `.tooltip-wrap` | - |
70
+ | As a popover it is only drawn while it is open, so it is always visible; the position comes from placePopover() | `.tooltip-text` | - |
69
71
  | Combobox (autocomplete, multi select) and chips | `.chip` `.chip-remove` `.combobox` `.combobox-active` `.combobox-chips` `.combobox-empty` `.combobox-input` `.combobox-list` `.combobox-multiple` `.combobox-option` `.combobox-selected` | `--athena-accent` `--athena-accent-fg` `--athena-bg` `--athena-border-width` `--athena-hover` `--athena-line` `--athena-muted` `--athena-radius` `--athena-shadow` `--athena-space-1` |
70
72
  | File upload | `.dropzone` `.dropzone-disabled` `.dropzone-input` `.dropzone-over` `.file-item` `.file-list` `.file-status` | `--athena-accent` `--athena-hover` `--athena-line` `--athena-muted` `--athena-radius` |
71
73
  | Stepper | `.form-actions` `.stepper` `.stepper-current` `.stepper-done` `.stepper-number` `.stepper-step` `.stepper-steps` | `--athena-accent` `--athena-accent-fg` `--athena-fg` `--athena-line` `--athena-muted` `--athena-space-2` `--athena-success` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "athena-ui-js",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "A small, dependency-free UI framework: forms, tables, charts, menus, dialogs, themes and a router, written as ES modules.",
5
5
  "type": "module",
6
6
  "main": "./src/athena-ui.js",
package/src/athena-ui.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //Author: Csaba Tóth https://github.com/csabatibortoth/
3
3
  //LICENCE: MIT
4
4
  //Repository: https://github.com/csabatibortoth/athena-ui
5
- //Version: 2.0.1
5
+ //Version: 2.1.0
6
6
 
7
7
  // The whole API as ES module exports. There are no side effects: importing this file does not touch the page, does not load a script and does not
8
8
  // put anything on globalThis. For pages that need globals use `athena-ui-js/classic`, and to download only what you use import from the entry points
@@ -6,7 +6,7 @@
6
6
  export const BORDERED_CLASS = "default-cointainer-bordered";
7
7
 
8
8
  export const defaults = Object.freeze({
9
- version: "2.0.1",
9
+ version: "2.1.0",
10
10
  container_class: "container",
11
11
  header_class: "header-container",
12
12
  header_menu_class: "header-menu menu",
@@ -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
+ }
@@ -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 tip = el("span", { class: "tooltip-text", id, text, attrs: { role: "tooltip" } });
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
 
@@ -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 menu = el("div", { class: cx("dropdown-menu", options.align === "right" && "dropdown-right"), id: menuId, attrs: { role: "menu", hidden: true } });
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.hidden = false;
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.hidden = true;
268
- button.setAttribute("aria-expanded", "false");
269
- document.removeEventListener("pointerdown", onOutside);
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.hidden ? open() : close());
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
- if (menu.hidden) open();
331
+ open();
296
332
  enabledItems()[0]?.focus();
297
333
  }
298
334
  });
@@ -632,6 +632,29 @@ dialog.dialog::backdrop {
632
632
  right: 0;
633
633
  }
634
634
 
635
+ /* A popover (the dropdown menu, the tooltip) is in the top layer, placed next to its anchor by script through two variables */
636
+ .athena-popover[popover] {
637
+ position: fixed;
638
+ inset: auto;
639
+ left: var(--athena-popover-left, 0);
640
+ top: var(--athena-popover-top, 0);
641
+ margin: 0;
642
+ width: max-content;
643
+ max-width: min(92vw, 36em);
644
+ overflow: visible;
645
+ }
646
+
647
+ .athena-popover-center { translate: -50% 0; }
648
+ .athena-popover-end { translate: -100% 0; }
649
+ .athena-popover-above { translate: 0 -100%; }
650
+ .athena-popover-center.athena-popover-above { translate: -50% -100%; }
651
+ .athena-popover-end.athena-popover-above { translate: -100% -100%; }
652
+
653
+ .dropdown-menu[popover] {
654
+ max-height: min(70vh, 28em);
655
+ overflow-y: auto;
656
+ }
657
+
635
658
  .dropdown-item {
636
659
  display: block;
637
660
  width: 100%;
@@ -884,6 +907,15 @@ dialog.dialog::backdrop {
884
907
  opacity: 1;
885
908
  }
886
909
 
910
+ /* As a popover it is only drawn while it is open, so it is always visible; the position comes from placePopover() */
911
+ .tooltip-text[popover] {
912
+ border: 0;
913
+ bottom: auto;
914
+ transform: none;
915
+ visibility: visible;
916
+ opacity: 1;
917
+ }
918
+
887
919
  /* Combobox (autocomplete, multi select) and chips */
888
920
 
889
921
  .combobox {
@@ -1,5 +1,5 @@
1
1
  export function applyConfig(...sources: any[]): {
2
- version: "2.0.1";
2
+ version: "2.1.0";
3
3
  container_class: "container";
4
4
  header_class: "header-container";
5
5
  header_menu_class: "header-menu menu";
@@ -32,7 +32,7 @@ export function applyConfig(...sources: any[]): {
32
32
  export function withBorder(className: any): any;
33
33
  export const BORDERED_CLASS: "default-cointainer-bordered";
34
34
  export const defaults: Readonly<{
35
- version: "2.0.1";
35
+ version: "2.1.0";
36
36
  container_class: "container";
37
37
  header_class: "header-container";
38
38
  header_menu_class: "header-menu menu";
@@ -64,7 +64,7 @@ export const defaults: Readonly<{
64
64
  }>;
65
65
  export default config;
66
66
  declare namespace config {
67
- let version: "2.0.1";
67
+ let version: "2.1.0";
68
68
  let container_class: "container";
69
69
  let header_class: "header-container";
70
70
  let header_menu_class: "header-menu menu";
@@ -0,0 +1,36 @@
1
+ /** Opens it: showPopover(), or without the API: the element is no longer hidden. Does nothing when it is open already. */
2
+ export function openPopover(element: any): void;
3
+ /** Closes it. Does nothing when it is closed already. */
4
+ export function closePopover(element: any): void;
5
+ /**
6
+ * 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
7
+ * 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,
8
+ * `settlePopover` measures it and keeps it inside the window.
9
+ *
10
+ * @param {Element} anchor the button or the target of the tooltip
11
+ * @param {HTMLElement} popover
12
+ * @param {object} [options]
13
+ * @param {"bottom"|"top"|"auto"} [options.side="auto"] "bottom": below. "top": above, unless there is less than `room` px above. "auto": below,
14
+ * unless there is less than `room` px below and more room above
15
+ * @param {"start"|"end"|"center"} [options.align="start"] which edge of the anchor the popover lines up with
16
+ * @param {number} [options.gap=4] px between the anchor and the popover
17
+ * @param {number} [options.room=240] how much room a side needs, in px (a tooltip needs about 40)
18
+ */
19
+ export function placePopover(anchor: Element, popover: HTMLElement, { side, align, gap, room }?: {
20
+ side?: "bottom" | "top" | "auto";
21
+ align?: "start" | "end" | "center";
22
+ gap?: number;
23
+ room?: number;
24
+ }): void;
25
+ /**
26
+ * 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
27
+ * is taller than the room is left where it is: it scrolls).
28
+ */
29
+ export function settlePopover(popover: any): void;
30
+ /**
31
+ * Keeps an open popover next to its anchor while the page scrolls or the window changes size. Returns a function that stops it.
32
+ * (A popover in the top layer does not move with its anchor by itself.)
33
+ */
34
+ export function followAnchor(anchor: any, popover: any, options: any): () => void;
35
+ export function supportsPopover(): boolean;
36
+ export function isOpen(element: any): any;