@codingsteve01/ember 0.0.0-stage → 1.6.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/LICENSE +21 -0
- package/PRINCIPLES.md +69 -0
- package/README.md +65 -2
- package/bin/check-tokens.d.mts +1 -0
- package/bin/check-tokens.mjs +69 -0
- package/css/base.css +77 -0
- package/css/components/back.css +39 -0
- package/css/components/badge.css +50 -0
- package/css/components/breadcrumbs.css +27 -0
- package/css/components/card.css +22 -0
- package/css/components/chart.css +9 -0
- package/css/components/controls.css +60 -0
- package/css/components/nav-flyout.css +133 -0
- package/css/components/page.css +20 -0
- package/css/components/stat.css +36 -0
- package/css/components/states.css +20 -0
- package/css/components/table.css +41 -0
- package/css/components/topbar.css +75 -0
- package/css/components/viz.css +56 -0
- package/css/components.css +14 -0
- package/css/ember.css +4 -0
- package/css/tokens.css +165 -0
- package/js/back-nav.d.ts +9 -0
- package/js/back-nav.js +52 -0
- package/js/chart-theme.d.ts +3 -0
- package/js/chart-theme.js +64 -0
- package/js/crumbs.d.ts +1 -0
- package/js/crumbs.js +12 -0
- package/js/nav-flyout.d.ts +44 -0
- package/js/nav-flyout.js +281 -0
- package/js/sticky-offsets.d.ts +10 -0
- package/js/sticky-offsets.js +29 -0
- package/js/theme-color.d.ts +1 -0
- package/js/theme-color.js +4 -0
- package/js/theme.d.ts +10 -0
- package/js/theme.js +40 -0
- package/js/viz.d.ts +29 -0
- package/js/viz.js +84 -0
- package/package.json +88 -4
package/js/nav-flyout.js
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// The side navigation folded to a rail of icons hides the sub views. Opening a main item there (pointer, tap or
|
|
2
|
+
// keyboard) floats its sub views out beside the rail, over the content: nothing in the layout moves. The product says
|
|
3
|
+
// when the rail is on and which sub views a main item has; this module places, opens and closes the flyout.
|
|
4
|
+
|
|
5
|
+
const esc = s =>
|
|
6
|
+
String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Where the flyout sits: just right of the rail (`edge`), its top level with the main item, moved up as far as needed
|
|
10
|
+
* to stay inside the viewport, never above its top margin.
|
|
11
|
+
*/
|
|
12
|
+
export function flyoutPlacement({ anchor, edge, height, viewport, gap = 6, margin = 8 }) {
|
|
13
|
+
const lowest = viewport - margin - height;
|
|
14
|
+
return { top: Math.max(margin, Math.min(anchor.top, lowest)), left: edge + gap };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const countHtml = (n, alert) =>
|
|
18
|
+
n > 0
|
|
19
|
+
? `<span class="nav-flyout-count${alert ? ' is-alert' : ''}" aria-hidden="true">${n > 99 ? '99+' : n}</span>`
|
|
20
|
+
: '';
|
|
21
|
+
// With a count, the link names it in words: the colour alone does not reach a screen reader.
|
|
22
|
+
const countLabel = (label, n, alert) =>
|
|
23
|
+
n > 0 ? ` aria-label="${esc(`${label}: ${n}${alert ? ', needs action' : ''}`)}"` : '';
|
|
24
|
+
|
|
25
|
+
// Every link is out of the tab order: Tab walks the main items, the arrow keys walk the sub views. The full label is
|
|
26
|
+
// the tooltip, since a long one ends in an ellipsis.
|
|
27
|
+
const linkHtml = (i, sub = false) => {
|
|
28
|
+
const n = Number(i.count) || 0;
|
|
29
|
+
const current = i.current ? ' aria-current="page"' : '';
|
|
30
|
+
return (
|
|
31
|
+
`<a class="nav-flyout-item${sub ? ' is-sub' : ''}" href="${esc(i.href)}" tabindex="-1" title="${esc(i.label)}"` +
|
|
32
|
+
`${current}${countLabel(i.label, n, i.alert)}><span class="nav-flyout-label">${esc(i.label)}</span>` +
|
|
33
|
+
`${countHtml(n, i.alert)}</a>`
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The parts of a sub view (third level), indented beneath it in named groups; a group name is no stop.
|
|
38
|
+
const partsHtml = groups =>
|
|
39
|
+
groups?.length
|
|
40
|
+
? `<div class="nav-flyout-sub">${groups
|
|
41
|
+
.map(
|
|
42
|
+
g =>
|
|
43
|
+
(g.label ? `<div class="nav-flyout-group">${esc(g.label)}</div>` : '') +
|
|
44
|
+
g.items.map(i => linkHtml(i, true)).join(''),
|
|
45
|
+
)
|
|
46
|
+
.join('')}</div>`
|
|
47
|
+
: '';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Inner HTML of the flyout: the main item's name, then one link per sub view, the shown one marked as current. A sub
|
|
51
|
+
* view may carry a count (grey, red with `alert` where it waits for action), as it does in the unfolded navigation,
|
|
52
|
+
* and its own parts (`children: [{ label, items }]`). With nothing to list, `empty` says why (loading, none yet).
|
|
53
|
+
*/
|
|
54
|
+
export function flyoutHtml({ title, items, empty }) {
|
|
55
|
+
const body = items.length
|
|
56
|
+
? items.map(i => linkHtml(i) + partsHtml(i.children)).join('')
|
|
57
|
+
: empty
|
|
58
|
+
? `<p class="nav-flyout-empty">${esc(empty)}</p>`
|
|
59
|
+
: '';
|
|
60
|
+
return `<div class="nav-flyout-title">${esc(title)}</div>${body}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Which flyout link a key moves to from `index` (-1: none yet), or null when the key does not move. Wraps around. */
|
|
64
|
+
export function nextIndex(key, index, count) {
|
|
65
|
+
if (!count) return null;
|
|
66
|
+
if (key === 'ArrowDown') return (index + 1) % count;
|
|
67
|
+
if (key === 'ArrowUp') return index <= 0 ? count - 1 : index - 1;
|
|
68
|
+
if (key === 'Home') return 0;
|
|
69
|
+
if (key === 'End') return count - 1;
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Wires the flyout to a navigation element. `menu(item)` returns `{ title, items: [{ label, href, current }] }` for a
|
|
75
|
+
* main item (the element matching `itemSelector`) or null when it has no sub views; `active()` says whether the rail
|
|
76
|
+
* is shown (otherwise everything stays closed). The flyout is placed right after its main item, so Tab walks from
|
|
77
|
+
* the main item through the sub views to the next main item.
|
|
78
|
+
*
|
|
79
|
+
* Opens on hover (mouse), on click or tap (the link still navigates) and on keyboard focus; ArrowRight or ArrowDown
|
|
80
|
+
* moves into it, Escape or ArrowLeft returns to the main item. Closes when the pointer or focus leaves both, on a
|
|
81
|
+
* click elsewhere, after choosing a sub view and when the rail is switched off.
|
|
82
|
+
*/
|
|
83
|
+
export function initNavFlyout({
|
|
84
|
+
nav,
|
|
85
|
+
menu,
|
|
86
|
+
active,
|
|
87
|
+
itemSelector = '.nav-item',
|
|
88
|
+
edge = () => nav.getBoundingClientRect().right,
|
|
89
|
+
doc = document,
|
|
90
|
+
win = window,
|
|
91
|
+
openDelay = 120,
|
|
92
|
+
closeDelay = 300,
|
|
93
|
+
} = {}) {
|
|
94
|
+
const fly = doc.createElement('div');
|
|
95
|
+
fly.className = 'nav-flyout';
|
|
96
|
+
fly.id = 'nav-flyout';
|
|
97
|
+
fly.setAttribute('role', 'group');
|
|
98
|
+
fly.hidden = true;
|
|
99
|
+
// A sticky or fixed navigation is a stacking context of its own; as a popover the flyout paints in the top layer
|
|
100
|
+
// above all content and still follows its main item in tab order. Without the Popover API, z-index has to do.
|
|
101
|
+
const popover = typeof fly.showPopover === 'function';
|
|
102
|
+
if (popover) fly.setAttribute('popover', 'manual');
|
|
103
|
+
let anchor = null;
|
|
104
|
+
let timer = null;
|
|
105
|
+
let returning = false; // focus handed back to the main item must not reopen the flyout
|
|
106
|
+
const bound = [];
|
|
107
|
+
const listen = (target, type, f, capture = false) => {
|
|
108
|
+
target.addEventListener(type, f, capture);
|
|
109
|
+
bound.push([target, type, f, capture]);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const wait = (f, ms) => {
|
|
113
|
+
win.clearTimeout(timer);
|
|
114
|
+
timer = ms > 0 ? win.setTimeout(f, ms) : (f(), null);
|
|
115
|
+
};
|
|
116
|
+
const links = () => [...fly.querySelectorAll('.nav-flyout-item')];
|
|
117
|
+
const itemOf = target => target?.closest?.(itemSelector) ?? null;
|
|
118
|
+
const inside = node => !!node && (fly.contains(node) || (anchor?.contains(node) ?? false));
|
|
119
|
+
|
|
120
|
+
function place() {
|
|
121
|
+
if (!anchor) return;
|
|
122
|
+
const r = anchor.getBoundingClientRect();
|
|
123
|
+
const { top, left } = flyoutPlacement({
|
|
124
|
+
anchor: r,
|
|
125
|
+
edge: edge(),
|
|
126
|
+
height: fly.offsetHeight,
|
|
127
|
+
viewport: win.innerHeight,
|
|
128
|
+
});
|
|
129
|
+
fly.style.top = `${top}px`;
|
|
130
|
+
fly.style.left = `${left}px`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function close({ focus = false } = {}) {
|
|
134
|
+
win.clearTimeout(timer);
|
|
135
|
+
if (!anchor) return;
|
|
136
|
+
const was = anchor;
|
|
137
|
+
was.removeAttribute('aria-expanded');
|
|
138
|
+
was.removeAttribute('aria-controls');
|
|
139
|
+
anchor = null;
|
|
140
|
+
if (popover && fly.matches(':popover-open')) fly.hidePopover();
|
|
141
|
+
fly.hidden = true;
|
|
142
|
+
fly.remove();
|
|
143
|
+
if (focus) handBack(was);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function open(item) {
|
|
147
|
+
win.clearTimeout(timer);
|
|
148
|
+
const m = active() ? menu(item) : null;
|
|
149
|
+
if (!m || (!m.items?.length && !m.empty)) return close();
|
|
150
|
+
if (anchor && anchor !== item) close();
|
|
151
|
+
anchor = item;
|
|
152
|
+
fly.innerHTML = flyoutHtml(m);
|
|
153
|
+
fly.setAttribute('aria-label', m.title);
|
|
154
|
+
if (fly.previousElementSibling !== item) item.after(fly);
|
|
155
|
+
item.setAttribute('aria-expanded', 'true');
|
|
156
|
+
item.setAttribute('aria-controls', fly.id);
|
|
157
|
+
fly.hidden = false;
|
|
158
|
+
if (popover && !fly.matches(':popover-open')) fly.showPopover();
|
|
159
|
+
place();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Draws the open flyout again (new current sub view, counts, parts). The focus stays on the sub view it was on,
|
|
163
|
+
// found by its target, since the list may have grown or shrunk; is that one gone, the first sub view takes it.
|
|
164
|
+
function redraw() {
|
|
165
|
+
const item = anchor;
|
|
166
|
+
const had = links().includes(doc.activeElement) ? doc.activeElement.getAttribute('href') : null;
|
|
167
|
+
open(item);
|
|
168
|
+
if (had == null) return;
|
|
169
|
+
const all = links();
|
|
170
|
+
if (!all.length) return handBack(item); // nothing left to stand on ("Loading …", or closed)
|
|
171
|
+
const i = all.findIndex(a => a.getAttribute('href') === had);
|
|
172
|
+
focusLink(i >= 0 ? i : 0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Gives the focus back to a main item without that focus opening the flyout again.
|
|
176
|
+
function handBack(item) {
|
|
177
|
+
returning = true;
|
|
178
|
+
try {
|
|
179
|
+
item.focus();
|
|
180
|
+
} finally {
|
|
181
|
+
returning = false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function focusLink(i) {
|
|
186
|
+
const all = links();
|
|
187
|
+
if (i != null && all[i]) all[i].focus();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const mouse = e => e.pointerType === 'mouse';
|
|
191
|
+
listen(nav, 'pointerover', e => {
|
|
192
|
+
const item = itemOf(e.target);
|
|
193
|
+
if (!mouse(e) || !item || !active()) return;
|
|
194
|
+
if (item === anchor) return win.clearTimeout(timer);
|
|
195
|
+
wait(() => open(item), anchor ? 0 : openDelay);
|
|
196
|
+
});
|
|
197
|
+
const leave = e => {
|
|
198
|
+
if (mouse(e) && anchor && !inside(e.relatedTarget)) wait(() => close(), closeDelay);
|
|
199
|
+
};
|
|
200
|
+
listen(nav, 'pointerout', leave);
|
|
201
|
+
listen(fly, 'pointerover', e => mouse(e) && win.clearTimeout(timer));
|
|
202
|
+
|
|
203
|
+
listen(nav, 'click', e => {
|
|
204
|
+
if (fly.contains(e.target)) return e.target.closest?.('.nav-flyout-item') && close();
|
|
205
|
+
const item = itemOf(e.target);
|
|
206
|
+
if (item && active()) open(item);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
listen(nav, 'focusin', e => {
|
|
210
|
+
const item = itemOf(e.target);
|
|
211
|
+
if (!item || item === anchor || returning || !active()) return;
|
|
212
|
+
let keyboard = true;
|
|
213
|
+
try {
|
|
214
|
+
keyboard = item.matches(':focus-visible');
|
|
215
|
+
} catch {
|
|
216
|
+
/* older engines: treat focus as keyboard focus */
|
|
217
|
+
}
|
|
218
|
+
if (keyboard) open(item);
|
|
219
|
+
});
|
|
220
|
+
// Only a move of focus to a known element elsewhere closes: a click on a sub view in Safari blurs without a
|
|
221
|
+
// relatedTarget, and closing then would drop the click. A click elsewhere closes through pointerdown.
|
|
222
|
+
listen(nav, 'focusout', e => {
|
|
223
|
+
if (anchor && e.relatedTarget && !inside(e.relatedTarget)) close();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
listen(nav, 'keydown', e => {
|
|
227
|
+
if (fly.contains(e.target)) {
|
|
228
|
+
const i = links().indexOf(e.target);
|
|
229
|
+
if (e.key === 'Escape' || e.key === 'ArrowLeft') {
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
return close({ focus: true });
|
|
232
|
+
}
|
|
233
|
+
const next = nextIndex(e.key, i, links().length);
|
|
234
|
+
if (next != null) {
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
focusLink(next);
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const item = itemOf(e.target);
|
|
241
|
+
if (!item || !active()) return;
|
|
242
|
+
if (e.key === 'Escape' && anchor) {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
return close({ focus: true });
|
|
245
|
+
}
|
|
246
|
+
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
|
247
|
+
if (item !== anchor) open(item);
|
|
248
|
+
if (anchor !== item) return;
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
focusLink(0);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
listen(doc, 'pointerdown', e => {
|
|
255
|
+
if (anchor && !inside(e.target) && !itemOf(e.target)) close();
|
|
256
|
+
});
|
|
257
|
+
listen(doc, 'keydown', e => {
|
|
258
|
+
if (e.key === 'Escape' && anchor && !nav.contains(e.target)) close();
|
|
259
|
+
});
|
|
260
|
+
listen(win, 'resize', () => (active() ? place() : close()));
|
|
261
|
+
// Scrolling the page or the navigation moves the main item; scrolling inside the flyout does not.
|
|
262
|
+
listen(win, 'scroll', e => fly.contains(e.target) || place(), true);
|
|
263
|
+
// A new address marks another sub view as current; a sub view with the focus keeps it.
|
|
264
|
+
listen(win, 'hashchange', () => anchor && redraw());
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
open,
|
|
268
|
+
close,
|
|
269
|
+
/** Call after the rail was switched on or off. */
|
|
270
|
+
refresh: () => (anchor && active() ? redraw() : close()),
|
|
271
|
+
/** Closes and removes every listener it added: on the navigation, the flyout, document and window. */
|
|
272
|
+
destroy() {
|
|
273
|
+
close();
|
|
274
|
+
for (const [target, type, f, capture] of bound.splice(0)) target.removeEventListener(type, f, capture);
|
|
275
|
+
},
|
|
276
|
+
get anchor() {
|
|
277
|
+
return anchor;
|
|
278
|
+
},
|
|
279
|
+
element: fly,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function stickyOffsets(layout?: { above?: number; topbar?: number }): {
|
|
2
|
+
'--topbar-top': string;
|
|
3
|
+
'--sticky-top': string;
|
|
4
|
+
};
|
|
5
|
+
export function initStickyOffsets(options?: {
|
|
6
|
+
topbar?: HTMLElement | null;
|
|
7
|
+
above?: HTMLElement | null;
|
|
8
|
+
aboveWhen?: string;
|
|
9
|
+
win?: Window;
|
|
10
|
+
}): () => void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The top bar stays in view; everything else that sticks (table heads, day headings, side columns) stops just below
|
|
2
|
+
// it. Two custom properties carry the offsets: --topbar-top (where the bar sticks: 0, or below a navigation strip
|
|
3
|
+
// that sits above it on narrow screens) and --sticky-top (that plus the bar's own height). CSS uses them with a
|
|
4
|
+
// fallback of 0px, so a page without this script still works, only without the offsets.
|
|
5
|
+
|
|
6
|
+
/** The offsets for a given layout: `above` is the height of whatever sticks above the bar (0 when nothing does). */
|
|
7
|
+
export function stickyOffsets({ above = 0, topbar = 0 } = {}) {
|
|
8
|
+
return { '--topbar-top': `${above}px`, '--sticky-top': `${above + topbar}px` };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Keeps both properties current with a ResizeObserver on the bar and on the element above it. `aboveWhen` is a media
|
|
13
|
+
* query under which `above` sits above the bar (e.g. a side navigation that becomes a top strip below 900 px).
|
|
14
|
+
*/
|
|
15
|
+
export function initStickyOffsets({ topbar, above = null, aboveWhen = '(max-width: 900px)', win = window } = {}) {
|
|
16
|
+
const root = win.document.documentElement;
|
|
17
|
+
const narrow = win.matchMedia ? win.matchMedia(aboveWhen) : { matches: false };
|
|
18
|
+
const sync = () => {
|
|
19
|
+
const values = stickyOffsets({
|
|
20
|
+
above: narrow.matches && above ? above.offsetHeight : 0,
|
|
21
|
+
topbar: topbar ? topbar.offsetHeight : 0,
|
|
22
|
+
});
|
|
23
|
+
for (const [name, value] of Object.entries(values)) root.style.setProperty(name, value);
|
|
24
|
+
};
|
|
25
|
+
if (win.ResizeObserver) for (const el of [topbar, above]) if (el) new win.ResizeObserver(sync).observe(el);
|
|
26
|
+
narrow.addEventListener?.('change', sync);
|
|
27
|
+
sync();
|
|
28
|
+
return sync;
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function themeColor(token: string, root?: Element): string;
|
package/js/theme.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type Theme = 'light' | 'dark';
|
|
2
|
+
export function systemTheme(win?: Window): Theme;
|
|
3
|
+
export function effectiveTheme(doc?: Document, win?: Window): Theme;
|
|
4
|
+
export function applyTheme(theme: Theme | null, doc?: Document): void;
|
|
5
|
+
export function initThemeToggle(
|
|
6
|
+
button: HTMLElement | null | undefined,
|
|
7
|
+
onChange?: (theme: Theme) => void,
|
|
8
|
+
doc?: Document,
|
|
9
|
+
win?: Window,
|
|
10
|
+
): void;
|
package/js/theme.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Light and dark: the OS preference decides (CSS alone does that, see tokens.css). The toggle sets a temporary
|
|
2
|
+
// override as [data-theme] on <html>; the next OS change or reload follows the system again.
|
|
3
|
+
|
|
4
|
+
export const systemTheme = (win = window) =>
|
|
5
|
+
win.matchMedia && win.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
6
|
+
|
|
7
|
+
/** The theme actually showing: an override wins, else the OS preference. */
|
|
8
|
+
export const effectiveTheme = (doc = document, win = window) => doc.documentElement.dataset.theme || systemTheme(win);
|
|
9
|
+
|
|
10
|
+
/** Sets the override, or clears it with null so the OS preference applies again. */
|
|
11
|
+
export function applyTheme(theme, doc = document) {
|
|
12
|
+
if (theme === null) delete doc.documentElement.dataset.theme;
|
|
13
|
+
else doc.documentElement.dataset.theme = theme;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Wires a toggle button. `onChange(theme)` runs after every switch, for whatever paints resolved colours
|
|
18
|
+
* (charts: call applyChartTheme and redraw). The button gets aria-pressed and a label for the next state.
|
|
19
|
+
*/
|
|
20
|
+
export function initThemeToggle(button, onChange = () => {}, doc = document, win = window) {
|
|
21
|
+
const sync = () => {
|
|
22
|
+
const theme = effectiveTheme(doc, win);
|
|
23
|
+
if (button) {
|
|
24
|
+
button.setAttribute('aria-pressed', String(theme === 'dark'));
|
|
25
|
+
button.title = theme === 'light' ? 'Dunkel anzeigen' : 'Hell anzeigen';
|
|
26
|
+
button.dataset.theme = theme;
|
|
27
|
+
}
|
|
28
|
+
onChange(theme);
|
|
29
|
+
};
|
|
30
|
+
applyTheme(null, doc);
|
|
31
|
+
win.matchMedia?.('(prefers-color-scheme: dark)').addEventListener?.('change', () => {
|
|
32
|
+
applyTheme(null, doc);
|
|
33
|
+
sync();
|
|
34
|
+
});
|
|
35
|
+
button?.addEventListener('click', () => {
|
|
36
|
+
applyTheme(effectiveTheme(doc, win) === 'light' ? 'dark' : 'light', doc);
|
|
37
|
+
sync();
|
|
38
|
+
});
|
|
39
|
+
sync();
|
|
40
|
+
}
|
package/js/viz.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function esc(s: unknown): string;
|
|
2
|
+
export function dataTone(i: number): string;
|
|
3
|
+
export function sparkPoints(
|
|
4
|
+
values: Array<number | null | undefined>,
|
|
5
|
+
w: number,
|
|
6
|
+
h: number,
|
|
7
|
+
pad?: number,
|
|
8
|
+
): Array<[number, number]>;
|
|
9
|
+
export function sparkline(
|
|
10
|
+
values: Array<number | null | undefined>,
|
|
11
|
+
options?: { w?: number; h?: number; tone?: string; label?: string; fluid?: boolean },
|
|
12
|
+
): string;
|
|
13
|
+
export interface SplitPart {
|
|
14
|
+
label: string;
|
|
15
|
+
value: number;
|
|
16
|
+
tone: string;
|
|
17
|
+
href?: string;
|
|
18
|
+
}
|
|
19
|
+
export function splitShares<T extends { value: number }>(parts: T[]): Array<T & { share: number }>;
|
|
20
|
+
export function splitBar(parts: SplitPart[], options?: { legend?: boolean }): string;
|
|
21
|
+
export function usual(values: number[]): number | null;
|
|
22
|
+
export function statTile(tile: {
|
|
23
|
+
label: string;
|
|
24
|
+
values: number[];
|
|
25
|
+
format?: (n: number) => string;
|
|
26
|
+
href?: string;
|
|
27
|
+
tone?: string;
|
|
28
|
+
alert?: boolean;
|
|
29
|
+
}): string;
|
package/js/viz.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Small figures next to a number: a sparkline for its course, a split bar for its parts, a stat tile that carries
|
|
2
|
+
// a value, its usual level and its course. Plain SVG/HTML strings without the DOM, so they render anywhere and test
|
|
3
|
+
// in node. Styles: css/components.css (.sparkline, .split, .stat).
|
|
4
|
+
|
|
5
|
+
export const esc = s =>
|
|
6
|
+
String(s ?? '').replace(
|
|
7
|
+
/[&<>"']/g,
|
|
8
|
+
c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c],
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
/** The data palette (--d1 … --d6): categories, never meaning. Red stays reserved for action. */
|
|
12
|
+
export const dataTone = i => `var(--d${(((i % 6) + 6) % 6) + 1})`;
|
|
13
|
+
|
|
14
|
+
const round = n => Math.round(n * 10) / 10;
|
|
15
|
+
|
|
16
|
+
/** The points of a line through `values`, scaled into w×h; a null keeps its place on the axis without a point. */
|
|
17
|
+
export function sparkPoints(values, w, h, pad = 2) {
|
|
18
|
+
const v = (values || []).map(x => (x == null || !Number.isFinite(Number(x)) ? null : Number(x)));
|
|
19
|
+
const known = v.filter(x => x != null);
|
|
20
|
+
if (!known.length) return [];
|
|
21
|
+
const max = Math.max(...known);
|
|
22
|
+
const min = Math.min(...known, 0);
|
|
23
|
+
const span = max - min || 1;
|
|
24
|
+
const step = v.length > 1 ? (w - 2 * pad) / (v.length - 1) : 0;
|
|
25
|
+
return v
|
|
26
|
+
.map((x, i) => (x == null ? null : [round(pad + i * step), round(h - pad - ((x - min) / span) * (h - 2 * pad))]))
|
|
27
|
+
.filter(Boolean);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A sparkline: the course as a line over a faint area, the last value marked. Fewer than two points draw nothing. */
|
|
31
|
+
export function sparkline(values, { w = 96, h = 28, tone = 'var(--d1)', label = '', fluid = false } = {}) {
|
|
32
|
+
const pts = sparkPoints(values, w, h);
|
|
33
|
+
if (pts.length < 2) return '';
|
|
34
|
+
const line = pts.map(([x, y]) => `${x},${y}`).join(' ');
|
|
35
|
+
const [lx, ly] = pts[pts.length - 1];
|
|
36
|
+
const area = `M${pts[0][0]},${h} L${line.replace(/ /g, ' L')} L${lx},${h} Z`;
|
|
37
|
+
const size = fluid ? `width="100%" height="${h}" preserveAspectRatio="none"` : `width="${w}" height="${h}"`;
|
|
38
|
+
const dot = fluid ? '' : `<circle class="sparkline-dot" cx="${lx}" cy="${ly}" r="2.2"/>`;
|
|
39
|
+
return `<svg class="sparkline" viewBox="0 0 ${w} ${h}" ${size} role="img" aria-label="${esc(label)}" style="--spark:${tone}"><path class="sparkline-area" d="${area}"/><polyline class="sparkline-line" points="${line}"/>${dot}</svg>`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The parts of a whole as shares in percent; parts without a value are left out. */
|
|
43
|
+
export function splitShares(parts) {
|
|
44
|
+
const list = (parts || []).filter(p => p.value > 0);
|
|
45
|
+
const total = list.reduce((n, p) => n + p.value, 0);
|
|
46
|
+
return list.map(p => ({ ...p, share: total ? (p.value / total) * 100 : 0 }));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A split bar with its legend; a part with `href` links to its detail. */
|
|
50
|
+
export function splitBar(parts, { legend = true } = {}) {
|
|
51
|
+
const shares = splitShares(parts);
|
|
52
|
+
if (!shares.length) return '';
|
|
53
|
+
const seg = p =>
|
|
54
|
+
`<span class="split-seg" style="width:${round(p.share)}%;background:${p.tone}" title="${esc(p.label)}: ${p.value}"></span>`;
|
|
55
|
+
const key = p => {
|
|
56
|
+
const inner = `<i style="background:${p.tone}"></i>${esc(p.label)} <b>${p.value}</b>`;
|
|
57
|
+
return p.href
|
|
58
|
+
? `<a class="split-key" href="${esc(p.href)}">${inner}</a>`
|
|
59
|
+
: `<span class="split-key">${inner}</span>`;
|
|
60
|
+
};
|
|
61
|
+
const keys = legend ? `<div class="split-legend">${shares.map(key).join('')}</div>` : '';
|
|
62
|
+
return `<div class="split"><div class="split-bar">${shares.map(seg).join('')}</div>${keys}</div>`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The median of all values but the last: the usual level the latest one is read against. */
|
|
66
|
+
export function usual(values) {
|
|
67
|
+
const prior = (values || [])
|
|
68
|
+
.slice(0, -1)
|
|
69
|
+
.filter(x => Number.isFinite(x))
|
|
70
|
+
.sort((a, b) => a - b);
|
|
71
|
+
if (!prior.length) return null;
|
|
72
|
+
const mid = Math.floor(prior.length / 2);
|
|
73
|
+
return prior.length % 2 ? prior[mid] : (prior[mid - 1] + prior[mid]) / 2;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A stat tile: label, the latest value, "sonst …" and the course. With `href` the whole tile is a link. */
|
|
77
|
+
export function statTile({ label, values, format = String, href = '', tone = 'var(--d1)', alert = false }) {
|
|
78
|
+
const last = values?.length ? values[values.length - 1] : null;
|
|
79
|
+
const base = usual(values);
|
|
80
|
+
const tag = href ? 'a' : 'div';
|
|
81
|
+
const link = href ? ` href="${esc(href)}"` : '';
|
|
82
|
+
const usualLine = base == null ? '' : `<span class="stat-usual">sonst ${esc(format(base))}</span>`;
|
|
83
|
+
return `<${tag} class="stat${alert ? ' is-alert' : ''}"${link}><span class="stat-label">${esc(label)}</span><span class="stat-value">${last == null ? '–' : esc(format(last))}</span>${usualLine}${sparkline(values, { tone, label, fluid: true })}</${tag}>`;
|
|
84
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,90 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingsteve01/ember",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"description": "Ember: design tokens, base CSS, components, theme toggle and chart theme for calm dashboards",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/CodingSteve01/design-ember.git"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"registry": "https://registry.npmjs.org",
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"css",
|
|
17
|
+
"js",
|
|
18
|
+
"bin",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"PRINCIPLES.md"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
"./ember.css": "./css/ember.css",
|
|
24
|
+
"./css/*": "./css/*",
|
|
25
|
+
"./theme": {
|
|
26
|
+
"types": "./js/theme.d.ts",
|
|
27
|
+
"default": "./js/theme.js"
|
|
28
|
+
},
|
|
29
|
+
"./theme-color": {
|
|
30
|
+
"types": "./js/theme-color.d.ts",
|
|
31
|
+
"default": "./js/theme-color.js"
|
|
32
|
+
},
|
|
33
|
+
"./chart-theme": {
|
|
34
|
+
"types": "./js/chart-theme.d.ts",
|
|
35
|
+
"default": "./js/chart-theme.js"
|
|
36
|
+
},
|
|
37
|
+
"./viz": {
|
|
38
|
+
"types": "./js/viz.d.ts",
|
|
39
|
+
"default": "./js/viz.js"
|
|
40
|
+
},
|
|
41
|
+
"./back-nav": {
|
|
42
|
+
"types": "./js/back-nav.d.ts",
|
|
43
|
+
"default": "./js/back-nav.js"
|
|
44
|
+
},
|
|
45
|
+
"./crumbs": {
|
|
46
|
+
"types": "./js/crumbs.d.ts",
|
|
47
|
+
"default": "./js/crumbs.js"
|
|
48
|
+
},
|
|
49
|
+
"./nav-flyout": {
|
|
50
|
+
"types": "./js/nav-flyout.d.ts",
|
|
51
|
+
"default": "./js/nav-flyout.js"
|
|
52
|
+
},
|
|
53
|
+
"./sticky-offsets": {
|
|
54
|
+
"types": "./js/sticky-offsets.d.ts",
|
|
55
|
+
"default": "./js/sticky-offsets.js"
|
|
56
|
+
},
|
|
57
|
+
"./check-tokens": {
|
|
58
|
+
"types": "./bin/check-tokens.d.mts",
|
|
59
|
+
"default": "./bin/check-tokens.mjs"
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"bin": {
|
|
64
|
+
"ember-check-tokens": "bin/check-tokens.mjs"
|
|
65
|
+
},
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": ">=20"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"test": "node --test 'test/*.test.mjs'",
|
|
71
|
+
"format": "prettier --write .",
|
|
72
|
+
"format:check": "prettier --check .",
|
|
73
|
+
"private": "node scripts/check-private.mjs",
|
|
74
|
+
"prepare": "lefthook install || true"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@commitlint/cli": "^21.3.0",
|
|
78
|
+
"@commitlint/config-conventional": "^21.3.0",
|
|
79
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
80
|
+
"@semantic-release/github": "^12.0.11",
|
|
81
|
+
"@semantic-release/npm": "^13.2.0",
|
|
82
|
+
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
83
|
+
"lefthook": "^2.2.4",
|
|
84
|
+
"prettier": "^3.10.1",
|
|
85
|
+
"semantic-release": "^25.0.9"
|
|
86
|
+
},
|
|
87
|
+
"allowScripts": {
|
|
88
|
+
"lefthook": true
|
|
89
|
+
}
|
|
90
|
+
}
|