@mdbase-dev/ui 0.1.0 → 0.1.3

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/Tooltip.js CHANGED
@@ -2,8 +2,42 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
2
2
  import { cloneElement, useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
3
3
  import { useLocaleRevision } from "./i18n.js";
4
4
  import { isApplePlatform, shortcutLabel } from "./shortcuts.js";
5
- let activeTooltip;
5
+ /** Before a tooltip shows on hover, and before it goes once the pointer has left. */
6
+ const SHOW_DELAY_MS = 400;
7
+ const HIDE_DELAY_MS = 120;
8
+ /** After one tooltip closes, the next opens at once: moving along a toolbar reads each name. */
9
+ const WARM_MS = 800;
10
+ const GAP = 6;
11
+ const MARGIN = 8;
12
+ // One tooltip shows at a time, and the warm window spans tooltips, so both are page-wide.
13
+ let closeActive;
6
14
  let warmUntil = 0;
15
+ /** A trigger no one can use or see has no tooltip. */
16
+ function available(trigger) {
17
+ return trigger.isConnected && !trigger.matches(":disabled, [aria-disabled=true]") && !trigger.closest("[hidden], [inert], [aria-hidden=true]") && trigger.checkVisibility({
18
+ checkVisibilityCSS: true,
19
+ checkOpacity: true
20
+ });
21
+ }
22
+ /** Below the trigger and centred on it; above when there is no room below; inside the window. */
23
+ function place(bubble, trigger) {
24
+ const anchor = trigger.getBoundingClientRect();
25
+ const size = bubble.getBoundingClientRect();
26
+ const centred = anchor.left + (anchor.width - size.width) / 2;
27
+ const fitsBelow = anchor.bottom + GAP + size.height <= innerHeight - MARGIN;
28
+ const top = fitsBelow ? anchor.bottom + GAP : anchor.top - size.height - GAP;
29
+ const within = (value, room) => Math.max(MARGIN, Math.min(value, room - MARGIN));
30
+ bubble.style.left = `${String(within(centred, innerWidth - size.width))}px`;
31
+ bubble.style.top = `${String(within(top, innerHeight - size.height))}px`;
32
+ }
33
+ function keyShortcuts(shortcut) {
34
+ return shortcut.replace(/\bmod\b/giu, isApplePlatform() ? "Meta" : "Control").replace(/\bctrl\b/giu, "Control");
35
+ }
36
+ /**
37
+ * Names an icon-only control on hover and keyboard focus, with its shortcut when it has one.
38
+ * The label also becomes the control's accessible name unless it already has one. Wrap a
39
+ * single focusable element: a button or a link.
40
+ */
7
41
  export function Tooltip({
8
42
  label,
9
43
  shortcut,
@@ -14,38 +48,34 @@ export function Tooltip({
14
48
  const trigger = useRef(null);
15
49
  const tooltip = useRef(null);
16
50
  const timer = useRef(undefined);
17
- const dispose = useRef(undefined);
51
+ const stopWatching = useRef(undefined);
18
52
  const touch = useRef(false);
19
53
  const [open, setOpen] = useState(false);
20
54
  const close = useCallback(() => {
21
55
  clearTimeout(timer.current);
22
- dispose.current?.();
23
- if (tooltip.current) warmUntil = performance.now() + 800;
24
- if (activeTooltip === close) activeTooltip = undefined;
56
+ stopWatching.current?.();
57
+ if (tooltip.current) warmUntil = performance.now() + WARM_MS;
58
+ if (closeActive === close) closeActive = undefined;
25
59
  setOpen(false);
26
60
  }, []);
27
61
  useEffect(() => close, [close]);
28
62
  useLayoutEffect(() => {
29
63
  if (!open) return;
30
- const bubble = tooltip.current;
31
- bubble.showPopover();
32
- const button = trigger.current.getBoundingClientRect();
33
- const bounds = bubble.getBoundingClientRect();
34
- bubble.style.left = `${Math.max(8, Math.min(button.left + (button.width - bounds.width) / 2, innerWidth - bounds.width - 8))}px`;
35
- bubble.style.top = `${Math.max(8, Math.min(button.bottom + 6 + bounds.height <= innerHeight - 8 ? button.bottom + 6 : button.top - bounds.height - 6, innerHeight - bounds.height - 8))}px`;
64
+ tooltip.current.showPopover();
65
+ place(tooltip.current, trigger.current);
36
66
  }, [open, label, shortcut]);
37
- const show = (button, delay) => {
38
- const available = () => button.isConnected && !button.matches(":disabled, [aria-disabled=true]") && !button.closest("[hidden], [inert], [aria-hidden=true]") && button.checkVisibility({
39
- checkVisibilityCSS: true,
40
- checkOpacity: true
41
- });
42
- if (!available()) return;
43
- activeTooltip?.();
44
- activeTooltip = close;
45
- trigger.current = button;
67
+ const show = (element, delay) => {
68
+ if (!available(element)) return;
69
+ closeActive?.();
70
+ closeActive = close;
71
+ trigger.current = element;
72
+ // Anything that takes the trigger away, or the person's attention elsewhere, closes it.
46
73
  const controller = new AbortController();
74
+ const {
75
+ signal
76
+ } = controller;
47
77
  const observer = new MutationObserver(() => {
48
- if (!available()) close();
78
+ if (!available(element)) close();
49
79
  });
50
80
  observer.observe(document.documentElement, {
51
81
  subtree: true,
@@ -53,12 +83,13 @@ export function Tooltip({
53
83
  attributes: true,
54
84
  attributeFilter: ["disabled", "aria-disabled", "hidden", "aria-hidden", "inert", "style", "class", "open"]
55
85
  });
56
- dispose.current = () => {
86
+ stopWatching.current = () => {
57
87
  controller.abort();
58
88
  observer.disconnect();
59
89
  };
60
90
  document.addEventListener("keydown", event => {
61
91
  if (event.key !== "Escape") return;
92
+ // Escape closes a showing tooltip and nothing else; the dialog beneath stays open.
62
93
  if (tooltip.current) {
63
94
  event.preventDefault();
64
95
  event.stopImmediatePropagation();
@@ -66,66 +97,79 @@ export function Tooltip({
66
97
  close();
67
98
  }, {
68
99
  capture: true,
69
- signal: controller.signal
100
+ signal
70
101
  });
71
102
  document.addEventListener("pointerdown", close, {
72
103
  capture: true,
73
- signal: controller.signal
104
+ signal
74
105
  });
75
106
  document.addEventListener("scroll", close, {
76
107
  capture: true,
77
- signal: controller.signal
108
+ signal
78
109
  });
79
110
  window.addEventListener("resize", close, {
80
- signal: controller.signal
111
+ signal
81
112
  });
82
113
  window.addEventListener("blur", close, {
83
- signal: controller.signal
114
+ signal
84
115
  });
85
- if (delay && performance.now() >= warmUntil) timer.current = setTimeout(() => {
86
- if (available()) setOpen(true);else close();
87
- }, 400);else setOpen(true);
116
+ if (delay && performance.now() >= warmUntil) {
117
+ timer.current = setTimeout(() => {
118
+ if (available(element)) setOpen(true);else close();
119
+ }, SHOW_DELAY_MS);
120
+ } else {
121
+ setOpen(true);
122
+ }
88
123
  };
124
+ // The pointer may cross the gap to the tooltip, so leaving waits a moment.
89
125
  const leave = () => {
90
126
  clearTimeout(timer.current);
91
- if (tooltip.current) timer.current = setTimeout(close, 120);else close();
127
+ if (tooltip.current) timer.current = setTimeout(close, HIDE_DELAY_MS);else close();
92
128
  };
129
+ const {
130
+ props
131
+ } = children;
132
+ const describedBy = [props["aria-describedby"], open && shortcut ? id : ""].filter(Boolean).join(" ");
93
133
  return _jsxs(_Fragment, {
94
134
  children: [cloneElement(children, {
95
- "aria-label": children.props["aria-label"] ?? label,
96
- "aria-keyshortcuts": shortcut ? shortcut.replace(/\bmod\b/giu, isApplePlatform() ? "Meta" : "Control").replace(/\bctrl\b/giu, "Control") : children.props["aria-keyshortcuts"],
97
- "aria-describedby": [children.props["aria-describedby"], open && shortcut ? id : ""].filter(Boolean).join(" ") || undefined,
135
+ "aria-label": props["aria-label"] ?? label,
136
+ "aria-keyshortcuts": shortcut ? keyShortcuts(shortcut) : props["aria-keyshortcuts"],
137
+ "aria-describedby": describedBy || undefined,
98
138
  title: undefined,
99
139
  onPointerEnter: event => {
100
- if (event.pointerType !== "touch" && matchMedia("(hover: hover)").matches) show(event.currentTarget, true);
101
- children.props.onPointerEnter?.(event);
140
+ if (event.pointerType !== "touch" && matchMedia("(hover: hover)").matches) {
141
+ show(event.currentTarget, true);
142
+ }
143
+ props.onPointerEnter?.(event);
102
144
  },
103
145
  onPointerLeave: event => {
104
146
  leave();
105
- children.props.onPointerLeave?.(event);
147
+ props.onPointerLeave?.(event);
106
148
  },
107
149
  onPointerDown: event => {
108
150
  touch.current = event.pointerType === "touch";
109
151
  close();
110
- children.props.onPointerDown?.(event);
152
+ props.onPointerDown?.(event);
111
153
  },
112
154
  onFocus: event => {
113
- if (!touch.current && event.currentTarget.matches(":focus-visible")) show(event.currentTarget, false);
114
- children.props.onFocus?.(event);
155
+ if (!touch.current && event.currentTarget.matches(":focus-visible")) {
156
+ show(event.currentTarget, false);
157
+ }
158
+ props.onFocus?.(event);
115
159
  },
116
160
  onBlur: event => {
117
161
  touch.current = false;
118
162
  close();
119
- children.props.onBlur?.(event);
163
+ props.onBlur?.(event);
120
164
  },
121
165
  onKeyDown: event => {
122
166
  touch.current = false;
123
167
  if (event.key === "Enter" || event.key === " ") close();
124
- children.props.onKeyDown?.(event);
168
+ props.onKeyDown?.(event);
125
169
  },
126
170
  onClick: event => {
127
171
  close();
128
- children.props.onClick?.(event);
172
+ props.onClick?.(event);
129
173
  }
130
174
  }), open && _jsxs("span", {
131
175
  ref: tooltip,
package/dist/popover.js CHANGED
@@ -35,6 +35,9 @@ function place(trigger, popover, options) {
35
35
  popover.style.top = `${placement.top}px`;
36
36
  popover.style.left = `${placement.left}px`;
37
37
  popover.style.maxHeight = `${placement.maxHeight}px`;
38
+ // The popover grows from the corner it shares with its trigger (.mdbase-overlay).
39
+ const vertical = placement.top < box.top ? "bottom" : "top";
40
+ popover.style.setProperty("--overlay-origin", `${vertical} ${options.align === "end" ? "right" : "left"}`);
38
41
  }
39
42
  // Browsers without the Popover API (and jsdom) keep the element in place instead.
40
43
  function showInTopLayer(element) {
@@ -113,6 +116,7 @@ export function useMenuPopover(menuRef, triggerRef, onClose, {
113
116
  const bounds = menu.getBoundingClientRect();
114
117
  menu.style.left = `${Math.max(8, Math.min(point.left, window.innerWidth - bounds.width - 8))}px`;
115
118
  menu.style.top = `${Math.max(8, Math.min(point.top, window.innerHeight - bounds.height - 8))}px`;
119
+ menu.style.setProperty("--overlay-origin", "top left");
116
120
  } else place(trigger, menu, {
117
121
  gap: 6,
118
122
  align,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdbase-dev/ui",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -124,10 +124,18 @@
124
124
  "types": "./dist/opening-page.d.ts",
125
125
  "import": "./dist/opening-page.js"
126
126
  },
127
+ "./segmented": {
128
+ "types": "./dist/Segmented.d.ts",
129
+ "import": "./dist/Segmented.js"
130
+ },
127
131
  "./select": {
128
132
  "types": "./dist/Select.d.ts",
129
133
  "import": "./dist/Select.js"
130
134
  },
135
+ "./switch": {
136
+ "types": "./dist/Switch.d.ts",
137
+ "import": "./dist/Switch.js"
138
+ },
131
139
  "./text-diff": {
132
140
  "types": "./dist/text-diff.d.ts",
133
141
  "import": "./dist/text-diff.js"
@@ -242,6 +250,6 @@
242
250
  "typecheck": "node scripts/compile-catalogues.mjs && tsc -p tsconfig.json --noEmit"
243
251
  },
244
252
  "mdbaseRelease": {
245
- "commit": "f1c417117b2cefd5bad52f1f5aaa60260c752c11"
253
+ "commit": "25e1422ce7be6721603125681044cd1784d25618"
246
254
  }
247
255
  }