@n1kname/components 0.30.0 → 0.31.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.
- package/package.json +4 -2
- package/src/CardList.astro +64 -0
- package/src/Tag.astro +7 -1
- package/src/cardlist.css +289 -0
- package/src/cardlist.js +119 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"./loadfailed.js": "./src/loadfailed.js",
|
|
29
29
|
"./hexmap.js": "./src/hexmap.js",
|
|
30
30
|
"./controls.css": "./src/controls.css",
|
|
31
|
-
"./controls.js": "./src/controls.js"
|
|
31
|
+
"./controls.js": "./src/controls.js",
|
|
32
|
+
"./cardlist.css": "./src/cardlist.css",
|
|
33
|
+
"./cardlist.js": "./src/cardlist.js"
|
|
32
34
|
},
|
|
33
35
|
"dependencies": {
|
|
34
36
|
"@n1kname/charts": "0.12.0"
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A list of long cards, one per item: a score dial (or any lead), a title and a secondary line,
|
|
4
|
+
* fact chips with a tone, and actions. The whole card opens the item; the actions stay controls of
|
|
5
|
+
* their own. For a list someone works through one item at a time, where a table row is too thin.
|
|
6
|
+
*
|
|
7
|
+
* Fill it at build time with `items`, or at runtime with setCards() from cardlist.js, which also
|
|
8
|
+
* has setCardState() for loading, empty and failed, and removeCard(), which slides a card out and
|
|
9
|
+
* closes the gap. Fires `card-open` and `card-action` with the item's id. The page sorts.
|
|
10
|
+
*/
|
|
11
|
+
import './controls.css';
|
|
12
|
+
import './cardlist.css';
|
|
13
|
+
import { cardHTML, placeholdersHTML } from './cardlist.js';
|
|
14
|
+
|
|
15
|
+
interface Chip {
|
|
16
|
+
icon?: string;
|
|
17
|
+
label: string;
|
|
18
|
+
/** good: something in the item's favour, in the status-good tint. The page decides what. */
|
|
19
|
+
tone?: 'neutral' | 'good';
|
|
20
|
+
}
|
|
21
|
+
interface Item {
|
|
22
|
+
id: string;
|
|
23
|
+
title: string;
|
|
24
|
+
subtitle?: string;
|
|
25
|
+
href?: string;
|
|
26
|
+
score?: number | null;
|
|
27
|
+
scoreText?: string;
|
|
28
|
+
chips?: Chip[];
|
|
29
|
+
actions?: { action: string; icon: string; label: string; pressed?: boolean }[];
|
|
30
|
+
}
|
|
31
|
+
interface Props {
|
|
32
|
+
items?: Item[];
|
|
33
|
+
/** Names the list for a screen reader. */
|
|
34
|
+
label: string;
|
|
35
|
+
/** The dial is good from this score, warning from `warning`, neutral below. */
|
|
36
|
+
good?: number;
|
|
37
|
+
warning?: number;
|
|
38
|
+
/** The top of the scale. */
|
|
39
|
+
max?: number;
|
|
40
|
+
/** The heading level of each card's title, to follow the page's own headings. */
|
|
41
|
+
level?: 2 | 3 | 4 | 5;
|
|
42
|
+
/** Actions show on hover or focus where a pointer hovers; always on touch. Off: always shown. */
|
|
43
|
+
reveal?: boolean;
|
|
44
|
+
/** Starts with placeholder cards; setCards() or setCardState() ends it. */
|
|
45
|
+
loading?: boolean;
|
|
46
|
+
loadingCards?: number;
|
|
47
|
+
id?: string;
|
|
48
|
+
}
|
|
49
|
+
const { items = [], label, good = 7, warning = 5, max = 10, level = 3, reveal = false, loading = false, loadingCards = 4, id } = Astro.props;
|
|
50
|
+
const hasEmpty = Astro.slots.has('empty');
|
|
51
|
+
const html = loading ? placeholdersHTML(loadingCards) : items.map((i) => cardHTML(i, { good, warning, max, level })).join('');
|
|
52
|
+
const state = loading ? 'loading' : items.length === 0 && hasEmpty ? 'empty' : 'ready';
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
<div class="card-list-wrap" data-state={state}>
|
|
56
|
+
<ul class:list={['card-list', { reveal }]} id={id} aria-label={label} data-card-list data-good={good} data-warning={warning} data-max={max} data-level={level} aria-busy={loading ? 'true' : undefined} data-filled={items.length ? '' : undefined} set:html={html} />
|
|
57
|
+
{hasEmpty && <div class="state empty" data-state-for="empty"><slot name="empty" /></div>}
|
|
58
|
+
{Astro.slots.has('failed') && <div class="state failed" data-state-for="failed"><slot name="failed" /></div>}
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<script>
|
|
62
|
+
import { wireCards } from './cardlist.js';
|
|
63
|
+
document.querySelectorAll<HTMLElement>('[data-card-list]').forEach(wireCards);
|
|
64
|
+
</script>
|
package/src/Tag.astro
CHANGED
|
@@ -18,7 +18,7 @@ import type { IconName } from './icons';
|
|
|
18
18
|
interface Props {
|
|
19
19
|
icon?: IconName;
|
|
20
20
|
href?: string;
|
|
21
|
-
tone?: 'accent' | 'quiet' | 'warm' | 'cool';
|
|
21
|
+
tone?: 'accent' | 'quiet' | 'warm' | 'cool' | 'good';
|
|
22
22
|
small?: boolean;
|
|
23
23
|
/** No lift or press, for a linked tag inside something that moves as a whole. */
|
|
24
24
|
still?: boolean;
|
|
@@ -69,6 +69,12 @@ const Tag = href ? 'a' : 'span';
|
|
|
69
69
|
color: var(--c-tag-cool-text);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/* Something in its favour: the page decides what (nearby, remote), the tag only says good. */
|
|
73
|
+
.good {
|
|
74
|
+
background: color-mix(in oklab, var(--c-status-good) 16%, var(--c-surface));
|
|
75
|
+
color: var(--c-text);
|
|
76
|
+
}
|
|
77
|
+
|
|
72
78
|
.tag.small {
|
|
73
79
|
padding-block: 2px;
|
|
74
80
|
font-size: var(--text-caption);
|
package/src/cardlist.css
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CardList: one long card per item. Plain classes, not scoped, because setCards() writes the cards
|
|
3
|
+
* at runtime. CardList imports this file with controls.css (the actions are IconButtons).
|
|
4
|
+
*/
|
|
5
|
+
.card-list {
|
|
6
|
+
display: grid;
|
|
7
|
+
gap: var(--sp-2);
|
|
8
|
+
margin: 0;
|
|
9
|
+
padding: 0;
|
|
10
|
+
list-style: none;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.card {
|
|
14
|
+
position: relative;
|
|
15
|
+
display: grid;
|
|
16
|
+
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
17
|
+
grid-template-areas:
|
|
18
|
+
'lead head actions'
|
|
19
|
+
'lead chips actions';
|
|
20
|
+
align-items: center;
|
|
21
|
+
column-gap: var(--sp-4);
|
|
22
|
+
row-gap: var(--sp-2);
|
|
23
|
+
padding: var(--sp-4) var(--sp-5);
|
|
24
|
+
border: 1px solid var(--c-border);
|
|
25
|
+
border-radius: var(--radius-lg);
|
|
26
|
+
background: var(--c-surface);
|
|
27
|
+
transition:
|
|
28
|
+
border-color var(--motion-hover) var(--ease-hover),
|
|
29
|
+
transform var(--motion-hover) var(--ease-hover),
|
|
30
|
+
box-shadow var(--motion-hover) var(--ease-hover);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.card-item:not(.placeholder) .card:hover {
|
|
34
|
+
border-color: var(--c-border-strong);
|
|
35
|
+
transform: translateY(-1px);
|
|
36
|
+
box-shadow: 0 2px 8px rgb(0 0 0 / 0.06);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.card:focus-within:has(.card-open:focus-visible) {
|
|
40
|
+
outline: 2px solid var(--c-focus);
|
|
41
|
+
outline-offset: 2px;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.card-lead {
|
|
45
|
+
grid-area: lead;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.card-head {
|
|
49
|
+
grid-area: head;
|
|
50
|
+
min-width: 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.card-list .card-title {
|
|
54
|
+
margin: 0;
|
|
55
|
+
color: var(--c-text);
|
|
56
|
+
font-family: var(--font-body);
|
|
57
|
+
font-size: 1.0625rem;
|
|
58
|
+
font-weight: 600;
|
|
59
|
+
line-height: 1.35;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* The title opens the card, and its ::after covers the whole card, so a click anywhere opens it.
|
|
63
|
+
* The actions sit above it and stay controls of their own. */
|
|
64
|
+
.card-open {
|
|
65
|
+
all: unset;
|
|
66
|
+
color: var(--c-text);
|
|
67
|
+
cursor: pointer;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.card-open::after {
|
|
71
|
+
content: '';
|
|
72
|
+
position: absolute;
|
|
73
|
+
inset: 0;
|
|
74
|
+
border-radius: inherit;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.card-open:focus-visible {
|
|
78
|
+
outline: none;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.card-sub {
|
|
82
|
+
margin: var(--sp-1) 0 0;
|
|
83
|
+
font-size: var(--text-body-sm);
|
|
84
|
+
color: var(--c-text-secondary);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.card-chips {
|
|
88
|
+
grid-area: chips;
|
|
89
|
+
display: flex;
|
|
90
|
+
flex-wrap: wrap;
|
|
91
|
+
gap: var(--sp-1);
|
|
92
|
+
margin: 0;
|
|
93
|
+
padding: 0;
|
|
94
|
+
list-style: none;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.fact-chip {
|
|
98
|
+
display: inline-flex;
|
|
99
|
+
align-items: center;
|
|
100
|
+
gap: 6px;
|
|
101
|
+
min-height: 26px;
|
|
102
|
+
padding: 0 var(--sp-2);
|
|
103
|
+
border-radius: 999px;
|
|
104
|
+
background: var(--c-surface-sunken, var(--c-bg));
|
|
105
|
+
color: var(--c-text-secondary);
|
|
106
|
+
font-size: var(--text-caption);
|
|
107
|
+
white-space: nowrap;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.fact-chip svg {
|
|
111
|
+
flex: none;
|
|
112
|
+
color: var(--c-text-muted);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* Something in the item's favour. The page decides what; the chip only says good. */
|
|
116
|
+
.fact-chip.good {
|
|
117
|
+
background: color-mix(in oklab, var(--c-status-good) 16%, var(--c-surface));
|
|
118
|
+
color: var(--c-text);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.fact-chip.good svg {
|
|
122
|
+
color: var(--c-status-good);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.card-actions {
|
|
126
|
+
grid-area: actions;
|
|
127
|
+
position: relative;
|
|
128
|
+
z-index: 1;
|
|
129
|
+
display: flex;
|
|
130
|
+
gap: var(--sp-1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/* reveal: the actions show on hover or focus, where there's a pointer that hovers. */
|
|
134
|
+
@media (hover: hover) and (pointer: fine) {
|
|
135
|
+
.card-list.reveal .card-actions {
|
|
136
|
+
opacity: 0;
|
|
137
|
+
transition: opacity var(--motion-hover) var(--ease-hover);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.card-list.reveal .card:hover .card-actions,
|
|
141
|
+
.card-list.reveal .card:focus-within .card-actions,
|
|
142
|
+
.card-list.reveal .card-actions:has([aria-pressed='true']) {
|
|
143
|
+
opacity: 1;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* ---- The score dial: a ring filled to the score, the figure in the middle ---- */
|
|
148
|
+
.score-dial {
|
|
149
|
+
--dial: var(--c-text-muted);
|
|
150
|
+
position: relative;
|
|
151
|
+
display: inline-grid;
|
|
152
|
+
place-items: center;
|
|
153
|
+
width: 48px;
|
|
154
|
+
height: 48px;
|
|
155
|
+
border-radius: 50%;
|
|
156
|
+
background: conic-gradient(var(--dial) calc(var(--p, 0) * 360deg), var(--c-border) 0);
|
|
157
|
+
font-family: var(--font-mono);
|
|
158
|
+
font-size: var(--text-mono);
|
|
159
|
+
font-variant-numeric: tabular-nums;
|
|
160
|
+
font-weight: 600;
|
|
161
|
+
color: var(--c-text);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.score-dial::before {
|
|
165
|
+
content: '';
|
|
166
|
+
position: absolute;
|
|
167
|
+
inset: 4px;
|
|
168
|
+
border-radius: 50%;
|
|
169
|
+
background: var(--c-surface);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.score-dial > span {
|
|
173
|
+
position: relative;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.score-dial.good {
|
|
177
|
+
--dial: var(--c-status-good);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.score-dial.warning {
|
|
181
|
+
--dial: var(--c-status-warning);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.score-dial.none {
|
|
185
|
+
background: var(--c-border);
|
|
186
|
+
color: var(--c-text-muted);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* ---- Below 640px: dial and title on top, chips under them, actions bottom right ---- */
|
|
190
|
+
@media (max-width: 639px) {
|
|
191
|
+
.card {
|
|
192
|
+
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
193
|
+
grid-template-areas:
|
|
194
|
+
'lead head head'
|
|
195
|
+
'chips chips actions';
|
|
196
|
+
padding: var(--sp-3) var(--sp-4);
|
|
197
|
+
column-gap: var(--sp-3);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.card-actions {
|
|
201
|
+
align-self: end;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.score-dial {
|
|
205
|
+
width: 44px;
|
|
206
|
+
height: 44px;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* ---- States, as DataTable's ---- */
|
|
211
|
+
.card-list-wrap > .state {
|
|
212
|
+
display: none;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
.card-list-wrap[data-state='empty'] > .state.empty,
|
|
216
|
+
.card-list-wrap[data-state='failed'] > .state.failed {
|
|
217
|
+
display: block;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.card-list-wrap[data-state='empty'] > .card-list,
|
|
221
|
+
.card-list-wrap[data-state='failed'] > .card-list {
|
|
222
|
+
display: none;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.card-item.placeholder {
|
|
226
|
+
opacity: 0;
|
|
227
|
+
animation: card-in var(--motion-base) var(--ease-out) 300ms forwards;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
.card-item.placeholder .card-head {
|
|
231
|
+
display: grid;
|
|
232
|
+
gap: var(--sp-2);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.card-item.placeholder .bar {
|
|
236
|
+
display: block;
|
|
237
|
+
width: 9ch;
|
|
238
|
+
height: 0.75em;
|
|
239
|
+
border-radius: var(--radius-sm);
|
|
240
|
+
background: color-mix(in oklab, var(--c-text) 9%, transparent);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.card-item.placeholder .bar.wide {
|
|
244
|
+
width: min(32ch, 80%);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
.card-item.placeholder .bar.chip {
|
|
248
|
+
width: 10ch;
|
|
249
|
+
height: 26px;
|
|
250
|
+
border-radius: 999px;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* ---- Motion: the first cards rise in, staggered ---- */
|
|
254
|
+
.card-item.enter {
|
|
255
|
+
animation: card-rise var(--motion-base) var(--ease-out) both;
|
|
256
|
+
animation-delay: calc(var(--i, 0) * 40ms);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
@keyframes card-rise {
|
|
260
|
+
from {
|
|
261
|
+
opacity: 0;
|
|
262
|
+
transform: translateY(8px);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
@keyframes card-in {
|
|
267
|
+
to {
|
|
268
|
+
opacity: 1;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
@media (prefers-reduced-motion: reduce) {
|
|
273
|
+
.card,
|
|
274
|
+
.card-list.reveal .card-actions {
|
|
275
|
+
transition: none;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
.card-item:not(.placeholder) .card:hover {
|
|
279
|
+
transform: none;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
.card-item.enter {
|
|
283
|
+
animation: none;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
.card-item.placeholder {
|
|
287
|
+
animation-duration: 1ms;
|
|
288
|
+
}
|
|
289
|
+
}
|
package/src/cardlist.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Drives CardList: one long card per item, with a score dial, fact chips and actions. The same
|
|
2
|
+
// markup at build time (CardList's `items`) and at runtime (setCards), so both look the same.
|
|
3
|
+
//
|
|
4
|
+
// import { setCards, setCardState, removeCard } from '@n1kname/components/cardlist.js';
|
|
5
|
+
// setCards(list, rooms.map((r) => ({
|
|
6
|
+
// id: r.id, title: r.name, subtitle: r.floor, score: r.score,
|
|
7
|
+
// chips: [{ icon: 'map-pin', label: 'Next to the hall', tone: 'good' }],
|
|
8
|
+
// actions: [{ action: 'like', icon: 'heart', label: 'Favourite', pressed: r.liked }, { action: 'remove', icon: 'x', label: 'Remove' }],
|
|
9
|
+
// })));
|
|
10
|
+
// list.addEventListener('card-open', (e) => open(e.detail.id));
|
|
11
|
+
// list.addEventListener('card-action', (e) => act(e.detail.id, e.detail.action, e.detail.button));
|
|
12
|
+
//
|
|
13
|
+
// An item: { id, title, subtitle?, href?, score?, scoreText?, chips?: [{ icon?, label, tone? }],
|
|
14
|
+
// actions?: [{ action, icon, label, pressed? }] }. Text is escaped; `href` makes the title a link,
|
|
15
|
+
// otherwise it's a button that fires `card-open`. The page sorts; the list keeps the order given.
|
|
16
|
+
import { ICONS } from './icons';
|
|
17
|
+
import { snapshot, flip } from './flip.js';
|
|
18
|
+
|
|
19
|
+
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
20
|
+
const svg = (name, size = 14, stroke = 2) =>
|
|
21
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${stroke}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name] ?? ''}</svg>`;
|
|
22
|
+
const reduced = () => typeof matchMedia !== 'undefined' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
23
|
+
|
|
24
|
+
/** The dial's tone from the list's thresholds: good from `good`, warning from `warning`. */
|
|
25
|
+
export const scoreTone = (score, { good = 7, warning = 5 } = {}) => (score === null || score === undefined ? 'none' : score >= good ? 'good' : score >= warning ? 'warning' : 'neutral');
|
|
26
|
+
|
|
27
|
+
export const dialHTML = (score, opts = {}) => {
|
|
28
|
+
if (score === null || score === undefined) return `<span class="score-dial none" role="img" aria-label="Not scored yet"><span>–</span></span>`;
|
|
29
|
+
const max = opts.max ?? 10;
|
|
30
|
+
const text = opts.text ?? (Number.isInteger(score) ? String(score) : score.toFixed(1));
|
|
31
|
+
return `<span class="score-dial ${scoreTone(score, opts)}" style="--p:${Math.max(0, Math.min(1, score / max))}" role="img" aria-label="Score ${esc(text)} out of ${max}"><span>${esc(text)}</span></span>`;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const cardHTML = (item, opts = {}) => {
|
|
35
|
+
const title = item.href
|
|
36
|
+
? `<a class="card-open" href="${esc(item.href)}">${esc(item.title)}</a>`
|
|
37
|
+
: `<button type="button" class="card-open">${esc(item.title)}</button>`;
|
|
38
|
+
const chips = (item.chips ?? []).map((c) => `<li class="fact-chip${c.tone === 'good' ? ' good' : ''}">${c.icon ? svg(c.icon) : ''}<span>${esc(c.label)}</span></li>`).join('');
|
|
39
|
+
const actions = (item.actions ?? [])
|
|
40
|
+
.map((a) => `<button type="button" class="icon-button" data-action="${esc(a.action)}" aria-label="${esc(a.label)}" title="${esc(a.label)}"${a.pressed === undefined ? '' : ` aria-pressed="${a.pressed}"`}>${svg(a.icon, 18, 1.75)}</button>`)
|
|
41
|
+
.join('');
|
|
42
|
+
return (
|
|
43
|
+
`<li class="card-item" data-key="${esc(item.id)}"><article class="card">` +
|
|
44
|
+
`<div class="card-lead">${item.lead ?? dialHTML(item.score, { ...opts, text: item.scoreText })}</div>` +
|
|
45
|
+
`<div class="card-head"><h${opts.level ?? 3} class="card-title">${title}</h${opts.level ?? 3}>${item.subtitle ? `<p class="card-sub">${esc(item.subtitle)}</p>` : ''}</div>` +
|
|
46
|
+
(chips ? `<ul class="card-chips">${chips}</ul>` : '') +
|
|
47
|
+
(actions ? `<div class="card-actions">${actions}</div>` : '') +
|
|
48
|
+
`</article></li>`
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const placeholdersHTML = (n) =>
|
|
53
|
+
Array.from({ length: n }, () => `<li class="card-item placeholder" aria-hidden="true"><div class="card"><div class="card-lead"><span class="score-dial none"></span></div><div class="card-head"><span class="bar wide"></span><span class="bar"></span></div><ul class="card-chips"><li class="bar chip"></li><li class="bar chip"></li></ul></div></li>`).join('');
|
|
54
|
+
|
|
55
|
+
const opts = (root) => ({ good: Number(root.dataset.good ?? 7), warning: Number(root.dataset.warning ?? 5), max: Number(root.dataset.max ?? 10), level: Number(root.dataset.level ?? 3) });
|
|
56
|
+
const wrapOf = (root) => root.closest('.card-list-wrap');
|
|
57
|
+
|
|
58
|
+
/** 'loading' shows placeholder cards (after 300ms), 'empty' and 'failed' their slots, 'ready' the cards. */
|
|
59
|
+
export const setCardState = (root, state, { cards = 4 } = {}) => {
|
|
60
|
+
if (state === 'loading') {
|
|
61
|
+
root.innerHTML = placeholdersHTML(cards);
|
|
62
|
+
root.setAttribute('aria-busy', 'true');
|
|
63
|
+
} else root.removeAttribute('aria-busy');
|
|
64
|
+
const wrap = wrapOf(root);
|
|
65
|
+
if (wrap) wrap.dataset.state = state;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** Fills the list. The first fill after loading rises in, staggered; later fills glide (flip). */
|
|
69
|
+
export const setCards = (root, items) => {
|
|
70
|
+
const first = !root.dataset.filled;
|
|
71
|
+
const before = first ? null : snapshot(root);
|
|
72
|
+
root.innerHTML = items.map((i) => cardHTML(i, opts(root))).join('');
|
|
73
|
+
root.removeAttribute('aria-busy');
|
|
74
|
+
const wrap = wrapOf(root);
|
|
75
|
+
if (wrap) wrap.dataset.state = items.length === 0 && wrap.querySelector('[data-state-for="empty"]') ? 'empty' : 'ready';
|
|
76
|
+
if (first) {
|
|
77
|
+
root.dataset.filled = '';
|
|
78
|
+
if (!reduced()) [...root.children].slice(0, 12).forEach((li, i) => { li.style.setProperty('--i', i); li.classList.add('enter'); });
|
|
79
|
+
} else flip(root, before);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Slides a card out, then closes the gap. Resolves when it's gone. */
|
|
83
|
+
export const removeCard = (root, id) =>
|
|
84
|
+
new Promise((done) => {
|
|
85
|
+
const li = [...root.children].find((x) => x.dataset.key === String(id));
|
|
86
|
+
if (!li) return done();
|
|
87
|
+
let gone = false;
|
|
88
|
+
const go = () => {
|
|
89
|
+
if (gone) return;
|
|
90
|
+
gone = true;
|
|
91
|
+
const before = snapshot(root);
|
|
92
|
+
li.remove();
|
|
93
|
+
flip(root, before);
|
|
94
|
+
done();
|
|
95
|
+
};
|
|
96
|
+
if (reduced()) return go();
|
|
97
|
+
li.animate([{ opacity: 1, transform: 'none' }, { opacity: 0, transform: 'translateX(24px)' }], { duration: 180, easing: 'ease-in' }).finished.then(go, go);
|
|
98
|
+
// A hidden or background tab doesn't paint, so `finished` may never come: don't let a caller hang.
|
|
99
|
+
setTimeout(go, 280);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const wireCards = (root) => {
|
|
103
|
+
root.addEventListener('click', (e) => {
|
|
104
|
+
const li = e.target.closest('.card-item');
|
|
105
|
+
if (!li || li.classList.contains('placeholder')) return;
|
|
106
|
+
const button = e.target.closest('[data-action]');
|
|
107
|
+
if (button) {
|
|
108
|
+
// The heart pops as it turns on; the page owns the state and sets aria-pressed.
|
|
109
|
+
if (button.getAttribute('aria-pressed') === 'false' && !reduced()) button.animate([{ transform: 'scale(1)' }, { transform: 'scale(1.25)' }, { transform: 'scale(1)' }], { duration: 320, easing: 'cubic-bezier(0.34, 1.56, 0.64, 1)' });
|
|
110
|
+
root.dispatchEvent(new CustomEvent('card-action', { detail: { id: li.dataset.key, action: button.dataset.action, button } }));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const open = e.target.closest('.card-open');
|
|
114
|
+
if (open?.tagName === 'A') return;
|
|
115
|
+
// A click anywhere on the card opens it, unless someone is selecting its text.
|
|
116
|
+
if (!open && getSelection()?.toString()) return;
|
|
117
|
+
root.dispatchEvent(new CustomEvent('card-open', { detail: { id: li.dataset.key } }));
|
|
118
|
+
});
|
|
119
|
+
};
|