staffa 0.2.1 → 0.3.1

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.
Files changed (49) hide show
  1. package/README.md +169 -125
  2. package/dist/components/autocomplete.js +3 -2
  3. package/dist/components/box.js +2 -2
  4. package/dist/components/button.d.ts +11 -2
  5. package/dist/components/button.js +36 -8
  6. package/dist/components/buttonChooser.d.ts +4 -5
  7. package/dist/components/buttonChooser.js +2 -2
  8. package/dist/components/buttonGroup.js +0 -3
  9. package/dist/components/field.js +0 -3
  10. package/dist/components/main.d.ts +34 -9
  11. package/dist/components/main.js +187 -49
  12. package/dist/components/menu.d.ts +118 -0
  13. package/dist/components/menu.js +218 -0
  14. package/dist/components/tabs.d.ts +0 -2
  15. package/dist/components/tabs.js +6 -19
  16. package/dist/components/toast.d.ts +37 -0
  17. package/dist/components/toast.js +79 -0
  18. package/dist/components/tooltip.d.ts +32 -0
  19. package/dist/components/tooltip.js +130 -0
  20. package/dist/core.d.ts +1 -1
  21. package/dist/icons-helpers.d.ts +46 -0
  22. package/dist/icons-helpers.js +44 -0
  23. package/dist/icons.d.ts +1960 -0
  24. package/dist/icons.js +1972 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.js +8 -0
  27. package/dist/staffa.esm.js +1 -1
  28. package/dist/theme.d.ts +1 -1
  29. package/dist/theme.js +114 -13
  30. package/package.json +10 -4
  31. package/src/components/autocomplete.ts +3 -2
  32. package/src/components/box.ts +2 -2
  33. package/src/components/button.ts +42 -10
  34. package/src/components/buttonChooser.ts +6 -7
  35. package/src/components/buttonGroup.ts +0 -3
  36. package/src/components/field.ts +0 -3
  37. package/src/components/main.ts +201 -45
  38. package/src/components/menu.ts +288 -0
  39. package/src/components/tabs.ts +6 -20
  40. package/src/components/toast.ts +115 -0
  41. package/src/components/tooltip.ts +139 -0
  42. package/src/core.ts +1 -1
  43. package/src/icons-helpers.ts +90 -0
  44. package/src/icons.ts +1977 -0
  45. package/src/index.ts +11 -0
  46. package/src/theme.ts +128 -18
  47. package/dist/components/modal.d.ts +0 -2
  48. package/dist/components/modal.js +0 -2
  49. package/dist/skye.esm.js +0 -1
@@ -0,0 +1,218 @@
1
+ import A from "aberdeen";
2
+ import { matchCurrent } from "aberdeen/route";
3
+ import { drawSlot } from "../core.js";
4
+ import { button } from "./button.js";
5
+ // Styles shared by the floating dropdown and the sidebar nav, so both look
6
+ // identical. The item styles aren't scoped to a container, so `drawMenu` can
7
+ // render its items into either one.
8
+ A.insertGlobalCss({
9
+ ".s-menu-list": "position:fixed z-index:350 min-width:10rem display:flex flex-direction:column p:$1 " +
10
+ "border: 1px solid $s-border; r:$s-radius-lg box-shadow:$s-shadow " +
11
+ "overflow-y:auto max-height:min(80vh,28rem) " +
12
+ "transition: opacity 0.15s, transform 0.15s;",
13
+ ".s-menu-list.hidden": "opacity:0 pointer-events:none transform:translateY(-6px)",
14
+ ".s-menu-item, .s-menu-item-link": "display:flex align-items:center gap:$2 w:100% " +
15
+ "padding: 0.5em 0.65em; r:$s-radius cursor:pointer text-align:left " +
16
+ "font-size:0.9em border:0 background:transparent fg:$s-fg text-decoration:none " +
17
+ "transition: background 0.12s, color 0.12s, transform 0.12s, box-shadow 0.12s;",
18
+ // A translucent ink tint (rather than an *opaque* mix) so the hover fades in
19
+ // cleanly: transitioning background from `transparent` toward an opaque colour
20
+ // flashes through dark mid-tones in browsers that interpolate non-premultiplied.
21
+ // Staying ink-hued at low alpha keeps the fade the right colour throughout.
22
+ ".s-menu-item:hover:not([aria-disabled=true]):not([aria-current=page]), .s-menu-item-link:hover:not([aria-current=page])": "background: color-mix(in srgb, $s-fg 10%, transparent);",
23
+ // Active (current page): a filled brand-gradient pill with a soft glow — the
24
+ // one place the menu shows real colour, so the current page is unmistakable.
25
+ ".s-menu-item[aria-current=page], .s-menu-item-link[aria-current=page]": "color:$s-on-accent font-weight:600 background: $s-gradient; box-shadow: 0 3px 10px color-mix(in srgb, $s-primary 38%, transparent);",
26
+ ".s-menu-item[aria-current=page] .s-menu-icon, .s-menu-item-link[aria-current=page] .s-menu-icon": "color:$s-on-accent",
27
+ ".s-menu-item[aria-current=page]:hover, .s-menu-item-link[aria-current=page]:hover": "filter:brightness(1.06)",
28
+ ".s-menu-item:focus-visible, .s-menu-item-link:focus-visible": "outline:none background: color-mix(in srgb, $s-fg 10%, transparent); box-shadow: 0 0 0 2px inset $s-focus;",
29
+ ".s-menu-item[aria-disabled=true], .s-menu-item-link[aria-disabled=true]": "opacity:0.45 cursor:not-allowed pointer-events:none",
30
+ ".s-menu-icon": "fg:$s-fg-muted flex-shrink:0",
31
+ // A soft hairline that fades out at both ends, rather than a hard full-width
32
+ // rule — quieter, and it reads as a grouping cue instead of a divider bar.
33
+ // `hr.` (not just `.`) so this wins over the global hr flow-margin rule.
34
+ "hr.s-menu-sep": "border:0 height:1px margin: $1 0.6rem; " +
35
+ "background: linear-gradient(to right, transparent, $s-border-strong 18%, $s-border-strong 82%, transparent);",
36
+ });
37
+ /**
38
+ * Draw a list of {@link MenuEntry} items into the *current* element, with
39
+ * arrow-key / Home / End navigation between the focusable items. The single
40
+ * shared primitive behind both the floating dropdown ({@link showFloatingMenu})
41
+ * and the sidebar nav in `S.main()` — call it inside whatever container
42
+ * (`<nav>`, the floating panel, …) you've opened.
43
+ *
44
+ * Items are real `<a>`/`<button>` elements, so Enter/Space activate them and
45
+ * screen readers narrate them natively.
46
+ *
47
+ * @param items The entries to render.
48
+ * @param onActivate Optional — run when any item is activated (used by the
49
+ * floating menu to close itself on selection).
50
+ */
51
+ export function drawMenu(items, onActivate) {
52
+ // Roving focus via the DOM: query the live item elements on each keypress.
53
+ A("keydown=", (e) => {
54
+ if (e.key !== "ArrowDown" && e.key !== "ArrowUp" && e.key !== "Home" && e.key !== "End")
55
+ return;
56
+ e.preventDefault();
57
+ const container = e.currentTarget;
58
+ const els = [...container.querySelectorAll(".s-menu-item, .s-menu-item-link")]
59
+ .filter((el) => el.getAttribute("aria-disabled") !== "true");
60
+ if (!els.length)
61
+ return;
62
+ const cur = els.indexOf(document.activeElement);
63
+ const dir = e.key === "ArrowUp" ? -1 : 1;
64
+ const next = e.key === "Home" ? 0 :
65
+ e.key === "End" ? els.length - 1 :
66
+ cur < 0 ? (dir > 0 ? 0 : els.length - 1) :
67
+ (cur + dir + els.length) % els.length;
68
+ els[next].focus();
69
+ });
70
+ for (const entry of items) {
71
+ if (typeof entry === "function") {
72
+ entry();
73
+ continue;
74
+ }
75
+ if ("separator" in entry) {
76
+ A("hr.s-menu-sep");
77
+ continue;
78
+ }
79
+ A(entry.href ? "a.s-menu-item-link" : "button.s-menu-item type=button", entry.attrs, () => {
80
+ if (entry.href) {
81
+ A("href=", entry.href);
82
+ if (entry.target)
83
+ A("target=", entry.target);
84
+ A(() => { if (matchCurrent(entry.href))
85
+ A("aria-current=page"); });
86
+ }
87
+ if (entry.disabled)
88
+ A("aria-disabled=true");
89
+ A("click=", (e) => {
90
+ if (entry.disabled) {
91
+ e.preventDefault();
92
+ return;
93
+ }
94
+ onActivate?.();
95
+ entry.click?.(e);
96
+ });
97
+ if (entry.icon)
98
+ A("span.s-menu-icon", () => drawSlot(entry.icon));
99
+ drawSlot(entry.label);
100
+ });
101
+ }
102
+ }
103
+ // ─── Floating menu ───────────────────────────────────────────────────────────
104
+ // At most one floating menu is open at a time. The anchor lives in the options,
105
+ // so it's available for positioning and focus-return without extra state.
106
+ const $floating = A.proxy({ opts: null });
107
+ function closeFloating() {
108
+ const anchor = $floating.opts?.anchor;
109
+ $floating.opts = null;
110
+ anchor?.focus();
111
+ }
112
+ function positionMenu(menuEl, rect) {
113
+ const mw = menuEl.offsetWidth, mh = menuEl.offsetHeight;
114
+ const vw = window.innerWidth, vh = window.innerHeight;
115
+ const gap = 4;
116
+ let x = rect.left;
117
+ if (x + mw > vw - 8)
118
+ x = Math.max(8, rect.right - mw);
119
+ let y = rect.bottom + gap;
120
+ if (y + mh > vh - 8 && rect.top - mh - gap >= 8)
121
+ y = rect.top - mh - gap;
122
+ menuEl.style.left = Math.max(8, x) + "px";
123
+ menuEl.style.top = Math.max(8, y) + "px";
124
+ }
125
+ A.mount(document.body, () => {
126
+ const f = $floating.opts;
127
+ if (!f)
128
+ return;
129
+ const menuEl = A("div.s-menu-list.s-s.panel create=hidden destroy=hidden", f.dropdownAttrs, () => {
130
+ drawMenu(f.items, closeFloating);
131
+ });
132
+ // Capture-phase document handlers replace an invisible backdrop element:
133
+ // any click outside the panel + anchor closes; Escape/Tab close.
134
+ const onClick = (e) => {
135
+ const t = e.target;
136
+ if (!menuEl.contains(t) && !f.anchor.contains(t))
137
+ closeFloating();
138
+ };
139
+ const onKey = (e) => {
140
+ if (e.key === "Escape" || e.key === "Tab") {
141
+ e.preventDefault();
142
+ closeFloating();
143
+ }
144
+ };
145
+ document.addEventListener("click", onClick, true);
146
+ document.addEventListener("keydown", onKey, true);
147
+ A.clean(() => {
148
+ document.removeEventListener("click", onClick, true);
149
+ document.removeEventListener("keydown", onKey, true);
150
+ });
151
+ // Position after layout, then focus the first enabled item.
152
+ requestAnimationFrame(() => {
153
+ if (!document.body.contains(menuEl))
154
+ return;
155
+ positionMenu(menuEl, f.anchor.getBoundingClientRect());
156
+ menuEl.querySelector(".s-menu-item:not([aria-disabled=true]), .s-menu-item-link:not([aria-disabled=true])")?.focus();
157
+ });
158
+ });
159
+ // ─── Public API ──────────────────────────────────────────────────────────────
160
+ /**
161
+ * Open a floating dropdown menu anchored to an element. Portals to
162
+ * `document.body` (never clipped), positions itself (flipping up when there's
163
+ * no room below), and closes on Escape, Tab, item selection, or any click
164
+ * outside the panel and anchor. Returns a `close()` function.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * // Custom context menu:
169
+ * el.addEventListener("contextmenu", (e) => {
170
+ * e.preventDefault();
171
+ * S.showFloatingMenu({ items, anchor: el });
172
+ * });
173
+ * ```
174
+ */
175
+ export function showFloatingMenu(opts) {
176
+ $floating.opts = opts;
177
+ return closeFloating;
178
+ }
179
+ /**
180
+ * A button that opens a {@link showFloatingMenu | floating dropdown menu} on
181
+ * click. Keyboard navigation: Arrow Up/Down, Home, End; Escape/Tab to close;
182
+ * Enter/Space activate the focused item natively.
183
+ *
184
+ * **Tip:** set `href` on items and call `interceptLinks()` once at app startup
185
+ * for SPA navigation — active items are highlighted automatically.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * S.menuButton({
190
+ * button: { text: "Actions", attrs: ".neutral .outlined" },
191
+ * items: [
192
+ * { label: "Edit", icon: () => A("#✎"), click: () => edit() },
193
+ * { separator: true },
194
+ * { label: "Delete", attrs: "fg:$s-danger", click: () => del() },
195
+ * ],
196
+ * });
197
+ * ```
198
+ */
199
+ export function menuButton(opts) {
200
+ let myEl = null;
201
+ A.clean(() => { if ($floating.opts?.anchor === myEl)
202
+ closeFloating(); });
203
+ button({
204
+ icon: () => A("span aria-hidden=true #☰"),
205
+ ariaLabel: "Open menu",
206
+ attrs: ".neutral .outlined",
207
+ ...opts.button,
208
+ click: (e) => {
209
+ myEl = e.currentTarget;
210
+ // Toggle: a second click on the same trigger closes the menu.
211
+ if ($floating.opts?.anchor === myEl) {
212
+ closeFloating();
213
+ return;
214
+ }
215
+ showFloatingMenu({ items: opts.items, anchor: myEl, dropdownAttrs: opts.dropdownAttrs });
216
+ },
217
+ });
218
+ }
@@ -23,8 +23,6 @@ export interface TabsOptions {
23
23
  * its own internal selection, starting at the first tab.
24
24
  */
25
25
  bind?: Bindable<string>;
26
- /** Visual style of the tab strip. Defaults to `"underline"`. */
27
- variant?: "underline" | "pills";
28
26
  /** Aberdeen attr/style string applied to the active panel. */
29
27
  contentAttrs?: Attributes;
30
28
  }
@@ -3,24 +3,16 @@ import { drawSlot, uniqueId } from "../core.js";
3
3
  A.insertGlobalCss({
4
4
  ".s-tabs": {
5
5
  "&": "display:flex flex-direction:column gap:$3",
6
- ".s-tablist": "display:flex gap:$1 align-items:stretch",
6
+ ".s-tablist": "display:flex gap:$1 align-items:stretch overflow-x:auto scrollbar-width:none border-bottom: 1px solid $s-border;",
7
+ ".s-tablist::-webkit-scrollbar": "display:none",
7
8
  ".s-tab": "display:inline-flex align-items:center gap:$2 cursor:pointer background:transparent " +
8
9
  "border:0 color: $s-fg-muted; font-weight:600 padding: 0.6em 0.9em; " +
10
+ "border-bottom: 3px solid transparent; margin-bottom:-1px " +
9
11
  "transition: color 0.15s, background 0.15s, border-color 0.15s;",
10
- ".s-tab:hover:not(:disabled)": "color: $s-fg;",
12
+ ".s-tab:hover:not(:disabled), .s-tab[aria-selected=true]": "color: $s-fg;",
11
13
  ".s-tab:disabled": "opacity:0.5 cursor:not-allowed",
12
14
  ".s-tab:focus-visible": "outline:none box-shadow: 0 0 0 3px $s-focus; r: $s-radius;",
13
- // Underline variant — the active marker uses the contextual brand accent.
14
- "&.s-underline .s-tablist": "border-bottom: 1px solid $s-border;",
15
- "&.s-underline .s-tab": "border-bottom: 2px solid transparent; margin-bottom:-1px",
16
- "&.s-underline .s-tab[aria-selected=true]": "color: $s-fg; border-bottom-color: $s-accent;",
17
- // Pills variant — the active pill gets the `.s-s.primary` surface (added in
18
- // tabs()), so its `--s-bg`/`--s-fg` are the brand surface's; we just paint
19
- // (the `.s-tab` rule's transparent background would otherwise win on it).
20
- "&.s-pills .s-tab": "r: $s-radius;",
21
- "&.s-pills .s-tab[aria-selected=true]": "background: $s-bg; color: $s-fg;",
22
- // The panel has no enclosing box, so no default padding — its content
23
- // aligns flush with the tab strip. Callers add padding/flex via `inner`.
15
+ ".s-tab[aria-selected=true]": "border-image: $s-gradient 1;",
24
16
  ".s-tabpanel": "display:block",
25
17
  },
26
18
  });
@@ -37,7 +29,6 @@ A.insertGlobalCss({
37
29
  * ```
38
30
  */
39
31
  export function tabs(opts) {
40
- const variant = opts.variant ?? "underline";
41
32
  const groupId = uniqueId("tabs");
42
33
  // Resolve a tab's selection key (its id, or its index as a string).
43
34
  const keyOf = (tab, index) => tab.id ?? String(index);
@@ -48,7 +39,7 @@ export function tabs(opts) {
48
39
  return;
49
40
  $sel.value = keyOf(tab, index);
50
41
  };
51
- A(`div.s-tabs.s-${variant}`, opts.attrs, () => {
42
+ A("div.s-tabs", opts.attrs, () => {
52
43
  A("div.s-tablist role=tablist", () => {
53
44
  opts.tabs.forEach((tab, index) => {
54
45
  const key = keyOf(tab, index);
@@ -58,10 +49,6 @@ export function tabs(opts) {
58
49
  const selected = $sel.value === key;
59
50
  A("aria-selected=", selected ? "true" : "false");
60
51
  A("tabindex=", selected ? "0" : "-1");
61
- // The active pill is a filled brand surface; the classes flip
62
- // with selection (Aberdeen removes them when this scope re-runs).
63
- if (selected && variant === "pills")
64
- A(".s-s.primary");
65
52
  });
66
53
  if (tab.disabled)
67
54
  A("disabled=true");
@@ -0,0 +1,37 @@
1
+ import { type Slot, type Attributes } from "../core.js";
2
+ import type { SurfaceRole } from "../theme.js";
3
+ /** Options for {@link toast}. */
4
+ export interface ToastOptions {
5
+ /** Primary message. A string is rendered as rich text. */
6
+ message: Slot;
7
+ /** Optional bold title above the message. */
8
+ title?: Slot;
9
+ /**
10
+ * Colour role. Defaults to `"neutral"`.
11
+ * Use `"success"` / `"danger"` / `"warning"` for semantic feedback.
12
+ */
13
+ type?: SurfaceRole;
14
+ /**
15
+ * Auto-dismiss delay in milliseconds. Defaults to `4000`.
16
+ * Pass `0` to make the toast persistent until dismissed manually.
17
+ */
18
+ duration?: number;
19
+ /** Show a close button. Defaults to `true`. */
20
+ dismissible?: boolean;
21
+ /** Aberdeen attr/style string applied to the toast element. */
22
+ attrs?: Attributes;
23
+ }
24
+ /**
25
+ * Show a toast notification. Returns a `dismiss()` function to remove it
26
+ * programmatically. Auto-dismisses after `duration` ms (default 4 000).
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * S.toast({ message: "Saved!", type: "success" });
31
+ * S.toast({ title: "Error", message: "Upload failed.", type: "danger", duration: 0 });
32
+ * const off = S.toast({ message: "Uploading…", duration: 0, dismissible: false });
33
+ * // later:
34
+ * off();
35
+ * ```
36
+ */
37
+ export declare function toast(opts: ToastOptions): () => void;
@@ -0,0 +1,79 @@
1
+ import A from "aberdeen";
2
+ import { grow, shrink } from "aberdeen/transitions";
3
+ import { drawSlot } from "../core.js";
4
+ A.insertGlobalCss({
5
+ ".s-toasts": "position:fixed bottom:$3 right:$3 z-index:400 " +
6
+ "display:flex flex-direction:column gap:$2 " +
7
+ "pointer-events:none max-width:min(90vw,24rem) w:24rem",
8
+ ".s-toast": {
9
+ "&": "display:flex align-items:flex-start gap:$2 " +
10
+ "padding: $3; border: 1px solid $s-border; r:$s-radius box-shadow:$s-shadow " +
11
+ "pointer-events:auto",
12
+ ".s-toast-body": "display:flex flex-direction:column gap:$1 flex:1 min-width:0",
13
+ ".s-toast-title": "font-weight:700 line-height:1.3",
14
+ ".s-toast-msg": "font-size:0.9em fg:$s-fg-muted line-height:1.4",
15
+ ".s-toast-close": "cursor:pointer border:0 background:transparent fg:$s-fg-muted font-size:1.1em line-height:1 " +
16
+ "padding: 0 0.15em; r:4px flex-shrink:0 align-self:flex-start",
17
+ ".s-toast-close:hover": "fg:$s-fg",
18
+ ".s-toast-close:focus-visible": "outline:none box-shadow: 0 0 0 3px $s-focus; fg:$s-fg",
19
+ },
20
+ });
21
+ let toastCount = 0;
22
+ // Keyed by stable ID so A.onEach scopes are per-toast — removing one never re-renders others.
23
+ const toasts = A.proxy({});
24
+ A.mount(document.body, () => {
25
+ A("div.s-toasts aria-live=polite aria-atomic=false", () => {
26
+ A.onEach(toasts, (entry) => {
27
+ const { opts } = entry;
28
+ const role = opts.type === "danger" || opts.type === "warning" ? "alert" : "status";
29
+ const surface = opts.type ?? "neutral";
30
+ A(`div.s-toast.s-s.${surface} role=${role}`, "create=", grow, "destroy=", shrink, opts.attrs, () => {
31
+ A("div.s-toast-body", () => {
32
+ A(() => {
33
+ if (opts.title != null)
34
+ A("div.s-toast-title", () => drawSlot(opts.title));
35
+ });
36
+ A("div.s-toast-msg", () => drawSlot(opts.message));
37
+ });
38
+ A(() => {
39
+ if (opts.dismissible === false)
40
+ return;
41
+ A("button.s-toast-close type=button aria-label=Dismiss", () => {
42
+ A("#×");
43
+ A("click=", () => dismiss(entry.id));
44
+ });
45
+ });
46
+ });
47
+ });
48
+ });
49
+ });
50
+ function dismiss(id) {
51
+ delete toasts[id];
52
+ }
53
+ /**
54
+ * Show a toast notification. Returns a `dismiss()` function to remove it
55
+ * programmatically. Auto-dismisses after `duration` ms (default 4 000).
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * S.toast({ message: "Saved!", type: "success" });
60
+ * S.toast({ title: "Error", message: "Upload failed.", type: "danger", duration: 0 });
61
+ * const off = S.toast({ message: "Uploading…", duration: 0, dismissible: false });
62
+ * // later:
63
+ * off();
64
+ * ```
65
+ */
66
+ export function toast(opts) {
67
+ const id = ++toastCount;
68
+ toasts[id] = { id, opts };
69
+ const duration = opts.duration ?? 4000;
70
+ let timer;
71
+ if (duration > 0) {
72
+ timer = setTimeout(() => dismiss(id), duration);
73
+ }
74
+ return () => {
75
+ if (timer != null)
76
+ clearTimeout(timer);
77
+ dismiss(id);
78
+ };
79
+ }
@@ -0,0 +1,32 @@
1
+ import { type Slot, type Attributes } from "../core.js";
2
+ /** Options for {@link addTooltip}. */
3
+ export interface TooltipOptions {
4
+ /** The tooltip text or draw function. A string is rendered as rich text. */
5
+ tip: Slot;
6
+ /**
7
+ * Which side the tooltip appears on. Defaults to `"top"`.
8
+ * Automatically flips to the opposite side when there isn't enough room.
9
+ */
10
+ placement?: "top" | "bottom" | "left" | "right";
11
+ /** Aberdeen attr/style string applied to the tip panel. */
12
+ attrs?: Attributes;
13
+ }
14
+ /**
15
+ * Attaches a tooltip to the current element: adds hover/focus handlers via
16
+ * {@link A} so the tip appears when the element is hovered or keyboard-focused.
17
+ * The tip panel is rendered into `document.body` via a portal, so it is never
18
+ * clipped by `overflow:hidden` ancestors. Position is computed from the
19
+ * element's bounding rect and automatically flips when near the viewport edge.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * A("button #Save", () => {
24
+ * S.addTooltip({ tip: "Saves your work to the cloud" });
25
+ * });
26
+ *
27
+ * A("button #Delete", () => {
28
+ * S.addTooltip({ tip: "Dangerous — cannot be undone", placement: "bottom" });
29
+ * });
30
+ * ```
31
+ */
32
+ export declare function addTooltip(opts: TooltipOptions): void;
@@ -0,0 +1,130 @@
1
+ import A from "aberdeen";
2
+ import { drawSlot } from "../core.js";
3
+ A.insertGlobalCss({
4
+ ".s-tt-tip": {
5
+ "&": "position:fixed z-index:500 " +
6
+ "max-width:20rem w:max-content " +
7
+ "bg:$s-raised fg:$s-fg border: 1px solid $s-border-strong; " +
8
+ "r:$s-radius box-shadow:$s-shadow " +
9
+ "padding: 0.3em 0.65em; font-size:0.85em line-height:1.4 " +
10
+ "pointer-events:none",
11
+ },
12
+ });
13
+ // ─── Global portal state ────────────────────────────────────────────────────
14
+ // At most one tooltip is visible at a time. The anchor is the element the
15
+ // handlers were attached to (its bounding rect drives positioning).
16
+ const $ttActive = A.proxy(undefined);
17
+ let hideTimer = null;
18
+ // Hide tooltip when the page scrolls (anchor has moved).
19
+ if (typeof window !== "undefined") {
20
+ window.addEventListener("scroll", () => { $ttActive.value = undefined; }, { capture: true, passive: true });
21
+ }
22
+ function computePos(rect, tipW, tipH, placement) {
23
+ const gap = 7;
24
+ const vw = window.innerWidth;
25
+ const vh = window.innerHeight;
26
+ let x = 0, y = 0;
27
+ if (placement === "bottom") {
28
+ x = rect.left + (rect.width - tipW) / 2;
29
+ y = rect.bottom + gap;
30
+ if (y + tipH > vh - 8) {
31
+ y = rect.top - tipH - gap;
32
+ }
33
+ }
34
+ else if (placement === "left") {
35
+ x = rect.left - tipW - gap;
36
+ y = rect.top + (rect.height - tipH) / 2;
37
+ if (x < 8) {
38
+ x = rect.right + gap;
39
+ }
40
+ }
41
+ else if (placement === "right") {
42
+ x = rect.right + gap;
43
+ y = rect.top + (rect.height - tipH) / 2;
44
+ if (x + tipW > vw - 8) {
45
+ x = rect.left - tipW - gap;
46
+ }
47
+ }
48
+ else {
49
+ // top (default)
50
+ x = rect.left + (rect.width - tipW) / 2;
51
+ y = rect.top - tipH - gap;
52
+ if (y < 8) {
53
+ y = rect.bottom + gap;
54
+ }
55
+ }
56
+ return {
57
+ x: Math.max(8, Math.min(x, vw - tipW - 8)),
58
+ y: Math.max(8, Math.min(y, vh - tipH - 8)),
59
+ };
60
+ }
61
+ function scheduleHide() {
62
+ if (hideTimer)
63
+ clearTimeout(hideTimer);
64
+ hideTimer = setTimeout(() => {
65
+ $ttActive.value = undefined;
66
+ hideTimer = null;
67
+ }, 100);
68
+ }
69
+ // ─── Portal ──────────────────────────────────────────────────────────────────
70
+ A.mount(document.body, () => {
71
+ const active = $ttActive.value;
72
+ if (!active)
73
+ return;
74
+ const { opts, anchor } = active;
75
+ const placement = opts.placement ?? "top";
76
+ const tipEl = A("div.s-tt-tip role=tooltip visibility:hidden", opts.attrs, () => {
77
+ A("mouseenter=", () => {
78
+ if (hideTimer) {
79
+ clearTimeout(hideTimer);
80
+ hideTimer = null;
81
+ }
82
+ });
83
+ A("mouseleave=", scheduleHide);
84
+ drawSlot(opts.tip);
85
+ });
86
+ requestAnimationFrame(() => {
87
+ if (!document.body.contains(tipEl))
88
+ return;
89
+ const { x, y } = computePos(anchor.getBoundingClientRect(), tipEl.offsetWidth, tipEl.offsetHeight, placement);
90
+ tipEl.style.left = x + "px";
91
+ tipEl.style.top = y + "px";
92
+ tipEl.style.visibility = "";
93
+ });
94
+ });
95
+ // ─── Public component ────────────────────────────────────────────────────────
96
+ /**
97
+ * Attaches a tooltip to the current element: adds hover/focus handlers via
98
+ * {@link A} so the tip appears when the element is hovered or keyboard-focused.
99
+ * The tip panel is rendered into `document.body` via a portal, so it is never
100
+ * clipped by `overflow:hidden` ancestors. Position is computed from the
101
+ * element's bounding rect and automatically flips when near the viewport edge.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * A("button #Save", () => {
106
+ * S.addTooltip({ tip: "Saves your work to the cloud" });
107
+ * });
108
+ *
109
+ * A("button #Delete", () => {
110
+ * S.addTooltip({ tip: "Dangerous — cannot be undone", placement: "bottom" });
111
+ * });
112
+ * ```
113
+ */
114
+ export function addTooltip(opts) {
115
+ const show = (e) => {
116
+ if (hideTimer) {
117
+ clearTimeout(hideTimer);
118
+ hideTimer = null;
119
+ }
120
+ $ttActive.value = { opts, anchor: e.currentTarget };
121
+ };
122
+ A("mouseenter=", show);
123
+ A("mouseleave=", scheduleHide);
124
+ A("focusin=", show);
125
+ A("focusout=", scheduleHide);
126
+ A.clean(() => {
127
+ if ($ttActive.value?.opts === opts)
128
+ $ttActive.value = undefined;
129
+ });
130
+ }
package/dist/core.d.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  * An Aberdeen attribute/style/class string, e.g. `"display:flex gap:$3 .my-class"`.
11
11
  *
12
12
  * Common values are our surface modifier classes:
13
- * - for colors: `.panel` `.raised` `.neutral` `.primary` `.danger` `.success` and `.warning`
13
+ * - for colors: `.panel` `.raised` `.neutral` `.primary` `.secondary` `.gradient` `.danger` `.success` and `.warning`
14
14
  * - for variant: `.filled` `.tonal` and `.outlined`
15
15
  *
16
16
  * These strings are passed straight through to {@link A} as positional
@@ -0,0 +1,46 @@
1
+ /** Stroke line-cap, as accepted by SVG's `stroke-linecap`. */
2
+ export type IconCap = "butt" | "round" | "square";
3
+ /** Stroke line-join, as accepted by SVG's `stroke-linejoin`. */
4
+ export type IconJoin = "arcs" | "bevel" | "miter" | "miter-clip" | "round";
5
+ /** Per-call overrides for a drawn icon. Anything omitted falls back to the
6
+ * module defaults (see {@link setDefaults}). */
7
+ export interface IconOptions {
8
+ /** Width & height, as a CSS length. A bare number is treated as pixels by
9
+ * SVG. Pass e.g. `"1em"` to scale the icon with the surrounding font. */
10
+ size?: number | string;
11
+ /** Stroke colour. Defaults to `"currentColor"`, so the icon inherits the
12
+ * current text colour. */
13
+ color?: string;
14
+ /** Stroke width in viewBox units (the viewBox is 24×24). */
15
+ strokeWidth?: number;
16
+ /** Stroke line-cap. */
17
+ cap?: IconCap;
18
+ /** Stroke line-join. */
19
+ join?: IconJoin;
20
+ /** Aberdeen attr/style string applied to the `<svg>` element. */
21
+ attrs?: string;
22
+ }
23
+ /** The resolved, always-present defaults backing {@link IconOptions}. */
24
+ export interface IconDefaults {
25
+ size: number | string;
26
+ color: string;
27
+ strokeWidth: number;
28
+ cap: IconCap;
29
+ join: IconJoin;
30
+ }
31
+ /**
32
+ * Override the module-wide icon defaults. Affects every icon drawn afterwards.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { setDefaults } from "staffa/icons";
37
+ * setDefaults({ size: "1.25em", strokeWidth: 1.5 });
38
+ * ```
39
+ */
40
+ export declare function setDefaults(opts: Partial<IconDefaults>): void;
41
+ /**
42
+ * Turn a piece of inner-SVG markup into an icon draw-function. The returned
43
+ * function emits a freshly-built `<svg>` into the current Aberdeen scope,
44
+ * applying the {@link IconOptions} (or the module defaults).
45
+ */
46
+ export declare function mk(inner: string): (opts?: IconOptions) => void;
@@ -0,0 +1,44 @@
1
+ import A from "aberdeen";
2
+ const defaults = {
3
+ size: 24,
4
+ color: "currentColor",
5
+ strokeWidth: 2,
6
+ cap: "round",
7
+ join: "round",
8
+ };
9
+ /**
10
+ * Override the module-wide icon defaults. Affects every icon drawn afterwards.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { setDefaults } from "staffa/icons";
15
+ * setDefaults({ size: "1.25em", strokeWidth: 1.5 });
16
+ * ```
17
+ */
18
+ export function setDefaults(opts) {
19
+ Object.assign(defaults, opts);
20
+ }
21
+ /**
22
+ * Draw a single icon: build one `<svg>` through Aberdeen (applying the
23
+ * {@link IconOptions} or the module defaults) and fill in its inner markup.
24
+ *
25
+ * This is the shared body behind every icon. {@link mk} hands it the icon's
26
+ * `inner` markup, so the per-icon closures stay tiny instead of each carrying
27
+ * a copy of this logic.
28
+ */
29
+ function drawIcon(inner, opts) {
30
+ const size = opts.size ?? defaults.size;
31
+ const el = A('svg.s-icon aria-hidden=true viewBox="0 0 24 24" fill=none', "width=", size, "height=", size, "stroke=", opts.color ?? defaults.color, "stroke-width=", opts.strokeWidth ?? defaults.strokeWidth, "stroke-linecap=", opts.cap ?? defaults.cap, "stroke-linejoin=", opts.join ?? defaults.join, opts.attrs);
32
+ // Drop the primitives in via innerHTML: setting it on the `<svg>` itself
33
+ // makes the parser put the children in the SVG namespace. (Aberdeen's
34
+ // `html=` builds them in the HTML namespace, leaving them non-rendering.)
35
+ el.innerHTML = inner;
36
+ }
37
+ /**
38
+ * Turn a piece of inner-SVG markup into an icon draw-function. The returned
39
+ * function emits a freshly-built `<svg>` into the current Aberdeen scope,
40
+ * applying the {@link IconOptions} (or the module defaults).
41
+ */
42
+ export function mk(inner) {
43
+ return (opts = {}) => drawIcon(inner, opts);
44
+ }