@n1kname/components 0.17.2 → 0.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.17.2",
3
+ "version": "0.18.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -22,12 +22,14 @@
22
22
  "./facts.js": "./src/facts.js",
23
23
  "./legend.js": "./src/legend.js",
24
24
  "./sidepanel.js": "./src/sidepanel.js",
25
+ "./mapstage.js": "./src/mapstage.js",
25
26
  "./match.css": "./src/match.css"
26
27
  },
27
28
  "dependencies": {
28
- "@n1kname/charts": "0.9.1"
29
+ "@n1kname/charts": "0.10.0"
29
30
  },
30
31
  "peerDependencies": {
32
+ "@n1kname/tokens": "^0.5.0",
31
33
  "astro": "^7.4.4"
32
34
  },
33
35
  "publishConfig": {
@@ -0,0 +1,150 @@
1
+ ---
2
+ /**
3
+ * A panel over a map: the surface, a little see-through and blurred, so the map reads under its
4
+ * edge and the words keep their contrast. Put it inside a MapStage.
5
+ *
6
+ * top-left, top-right, bottom-left, bottom-right a corner: controls, a legend
7
+ * head the page's title, in MapStage's `head` slot: the stage places it (fill mode) or
8
+ * lays it out as a plain header (framed, and on a phone)
9
+ * side the right edge, full height, scrolling inside when taller. It goes in MapStage's
10
+ * `aside` slot, and below 760px it stops floating and follows the map on the page.
11
+ *
12
+ * `layout="row"` lines controls up and wraps them (Segmenteds, a FloatToggle); `stack`, the default,
13
+ * stacks its content with room around it. Give it a `label` and it is a named group.
14
+ *
15
+ * A side panel can be put away with a FloatToggle. It fires `float-toggle` with { open }, so the
16
+ * map can fit what it shows to the room the panels leave (room() in mapstage.js).
17
+ */
18
+ interface Props {
19
+ place: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'side' | 'head';
20
+ layout?: 'row' | 'stack';
21
+ /** Names the panel for a screen reader: "Map controls". A row of controls becomes a group. */
22
+ label?: string;
23
+ as?: 'div' | 'aside' | 'section';
24
+ id?: string;
25
+ [key: string]: unknown;
26
+ }
27
+
28
+ const { place, layout = 'stack', label, as: Tag = place === 'side' ? 'aside' : 'div', id, class: className, ...rest } = Astro.props;
29
+ const role = label && layout === 'row' && Tag === 'div' ? 'group' : undefined;
30
+ ---
31
+
32
+ <Tag class:list={['float', place, layout, className]} id={id} data-float={place} role={role} aria-label={label} {...rest}><slot /></Tag>
33
+
34
+ <script>
35
+ import { wireFloatToggles } from './mapstage.js';
36
+ wireFloatToggles();
37
+ </script>
38
+
39
+ <style>
40
+ /* The surface at 93%: over any map, pure black and white included, every text token keeps AA
41
+ (tokens' check.mjs, "heat and map panels"). */
42
+ .float {
43
+ position: absolute;
44
+ z-index: 2;
45
+ border: 1px solid var(--c-border);
46
+ border-radius: var(--radius-lg);
47
+ background: color-mix(in srgb, var(--c-surface) 93%, transparent);
48
+ -webkit-backdrop-filter: blur(12px) saturate(1.2);
49
+ backdrop-filter: blur(12px) saturate(1.2);
50
+ box-shadow: 0 8px 28px rgb(0 0 0 / 0.16);
51
+ color: var(--c-text);
52
+ }
53
+
54
+ .row {
55
+ display: flex;
56
+ flex-wrap: wrap;
57
+ align-items: center;
58
+ gap: var(--sp-2);
59
+ padding: var(--sp-2);
60
+ }
61
+
62
+ .stack {
63
+ display: grid;
64
+ align-content: start;
65
+ gap: var(--sp-4);
66
+ padding: var(--sp-3) var(--sp-4);
67
+ }
68
+
69
+ /* Corners leave room at the far side for the map's own buttons. */
70
+ .top-left,
71
+ .top-right,
72
+ .bottom-left,
73
+ .bottom-right {
74
+ max-width: calc(100% - var(--sp-3) * 2 - 48px);
75
+ }
76
+
77
+ /* Under the title when it floats too (MapStage fill mode sets the room it takes). */
78
+ .top-left {
79
+ top: calc(var(--sp-3) + var(--map-stage-head-room, 0px));
80
+ left: var(--sp-3);
81
+ }
82
+
83
+ .top-right {
84
+ top: var(--sp-3);
85
+ right: var(--sp-3);
86
+ }
87
+
88
+ .bottom-left {
89
+ bottom: var(--sp-3);
90
+ left: var(--sp-3);
91
+ }
92
+
93
+ .bottom-right {
94
+ bottom: var(--sp-3);
95
+ right: var(--sp-3);
96
+ }
97
+
98
+ .side {
99
+ top: var(--sp-3);
100
+ right: var(--sp-3);
101
+ width: var(--float-side-width, 300px);
102
+ max-height: calc(100% - var(--sp-3) * 2);
103
+ overflow-y: auto;
104
+ overscroll-behavior: contain;
105
+ gap: var(--sp-5);
106
+ padding: var(--sp-4);
107
+ }
108
+
109
+ .side[data-closed] {
110
+ display: none;
111
+ }
112
+
113
+ /* On a phone the map keeps the width, and the side panel follows it on the page, always open:
114
+ FloatToggle hides itself there, so nothing can be put away out of reach. */
115
+ @media (max-width: 760px) {
116
+ .side,
117
+ .side[data-closed] {
118
+ position: static;
119
+ display: grid;
120
+ width: auto;
121
+ max-height: none;
122
+ margin-top: var(--sp-4);
123
+ background: var(--c-surface);
124
+ box-shadow: none;
125
+ -webkit-backdrop-filter: none;
126
+ backdrop-filter: none;
127
+ }
128
+ }
129
+
130
+ /* Where the blur isn't drawn, the panel is solid instead. */
131
+ @supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
132
+ .float {
133
+ background: var(--c-surface);
134
+ }
135
+ }
136
+
137
+ @media (prefers-reduced-transparency: reduce) {
138
+ .float {
139
+ background: var(--c-surface);
140
+ -webkit-backdrop-filter: none;
141
+ backdrop-filter: none;
142
+ }
143
+ }
144
+
145
+ @media (forced-colors: active) {
146
+ .float {
147
+ border-color: CanvasText;
148
+ }
149
+ }
150
+ </style>
@@ -0,0 +1,60 @@
1
+ ---
2
+ /**
3
+ * Puts a side FloatPanel away and brings it back. Sits in a row of map controls, the same height
4
+ * as a Segmented. Hidden below 760px, where the side panel follows the map and is always open.
5
+ */
6
+ interface Props {
7
+ /** The side panel's id. */
8
+ for: string;
9
+ /** Open when the page loads. */
10
+ open?: boolean;
11
+ [key: string]: unknown;
12
+ }
13
+
14
+ const { for: target, open = true, class: className, ...rest } = Astro.props;
15
+ ---
16
+
17
+ <button type="button" class:list={['float-toggle', className]} aria-controls={target} aria-expanded={String(open)} data-float-toggle={target} {...rest}><slot /></button>
18
+
19
+ <style>
20
+ .float-toggle {
21
+ min-height: 40px;
22
+ padding: 0 var(--sp-3);
23
+ border: 1px solid var(--c-border);
24
+ border-radius: var(--radius-md);
25
+ background: var(--c-surface);
26
+ color: var(--c-text-secondary);
27
+ font: inherit;
28
+ font-size: var(--text-body-sm);
29
+ cursor: pointer;
30
+ transition:
31
+ color var(--motion-hover) var(--ease-hover),
32
+ border-color var(--motion-hover) var(--ease-hover);
33
+ }
34
+
35
+ .float-toggle:hover,
36
+ .float-toggle[aria-expanded='true'] {
37
+ color: var(--c-text);
38
+ }
39
+
40
+ .float-toggle[aria-expanded='true'] {
41
+ border-color: var(--c-border-strong);
42
+ }
43
+
44
+ .float-toggle:focus-visible {
45
+ outline: 2px solid var(--c-focus);
46
+ outline-offset: 2px;
47
+ }
48
+
49
+ @media (max-width: 760px) {
50
+ .float-toggle {
51
+ display: none;
52
+ }
53
+ }
54
+
55
+ @media (pointer: coarse) {
56
+ .float-toggle {
57
+ min-height: 44px;
58
+ }
59
+ }
60
+ </style>
@@ -0,0 +1,284 @@
1
+ ---
2
+ /**
3
+ * A page that is one big map, in one of two modes:
4
+ *
5
+ * framed the default. The map sits in the content column under the page's header, at least
6
+ * 28rem tall and otherwise down to the bottom of the window, with a border and radius.
7
+ * fill the map covers the whole column: past its gutters, from the top of the column to the
8
+ * bottom of the window, up to the side nav. The page's title floats over it, from the
9
+ * `head` slot. Put the stage first in the column, with nothing above it. Below 760px it
10
+ * falls back to framed, with the title above the map.
11
+ *
12
+ * What goes with the map floats over it in FloatPanels, put in the default slot; a side panel goes
13
+ * in the `aside` slot, so on a phone it can leave the map and follow it on the page. The title goes
14
+ * in the `head` slot, as a FloatPanel with place="head" holding a compact PageHeader: it comes
15
+ * before the map in the page, so the h1 is the first thing read. `head` says how it floats in fill
16
+ * mode: on a band that fades into the map (`scrim`, the default), or in a panel like the others
17
+ * (`box`). Every panel at the top starts below it. In framed mode, and on a phone, it is a plain
18
+ * header above the map.
19
+ *
20
+ * Give the map to MapLibre (or any canvas map) by the id: new maplibregl.Map({ container: id }).
21
+ * While it reads, or when it has nothing, a message covers it: setStage() in mapstage.js picks one.
22
+ * Hover cards in a MapLibre popup with className 'place-card' wear the charts' pointer card.
23
+ *
24
+ * The framed height comes from custom properties a page can set: --map-stage-offset (what sits
25
+ * above it: the header and the page title, 10.5rem), --map-stage-min (28rem), and below 1024px
26
+ * --map-stage-offset-narrow and --map-stage-min-narrow (14rem and 26rem). --map-stage-height
27
+ * overrides them all, for a map that is one part of a page. In fill mode --map-stage-fill-offset
28
+ * (0) and, from 760 to 1023px, --map-stage-fill-offset-narrow (57px, the shell's top bar) are what
29
+ * sits above the column, and --map-stage-bleed (the shell's --layout-gutter) is how far it reaches
30
+ * past each side of the column.
31
+ */
32
+ import '@n1kname/charts/pointer.css';
33
+
34
+ interface Props {
35
+ /** The map element's id, for the map library. */
36
+ id: string;
37
+ /** What the map shows, for a screen reader: "Routes in November". */
38
+ label: string;
39
+ /** In the column under the header, or over the whole column. */
40
+ mode?: 'framed' | 'fill';
41
+ /** In fill mode, how the title floats: on a band that fades into the map (the default), or in a panel. */
42
+ head?: 'box' | 'scrim';
43
+ /** Shown over the map while it reads. Shown from the start; setStage(stage, 'ready') hides it. */
44
+ loading?: string;
45
+ /** Shown over the map when it has nothing to show. */
46
+ empty?: string;
47
+ /** Shown instead of the map when it can't start, such as without WebGL. */
48
+ failed?: string;
49
+ [key: string]: unknown;
50
+ }
51
+
52
+ const { id, label, mode = 'framed', head = 'scrim', loading, empty, failed, class: className, ...rest } = Astro.props;
53
+ // Rendered, not just asked: a slot filled by an expression that renders nothing still "has" it.
54
+ const headHtml = Astro.slots.has('head') ? (await Astro.slots.render('head')).trim() : '';
55
+ const hasHead = headHtml !== '';
56
+ ---
57
+
58
+ <div
59
+ class:list={['map-stage', className]}
60
+ data-map-stage
61
+ data-mode={mode}
62
+ data-head={hasHead ? head : undefined}
63
+ data-loading={loading ? '' : undefined}
64
+ {...rest}
65
+ >
66
+ {hasHead && <div class="head" set:html={headHtml} />}
67
+ <div class="frame">
68
+ <div id={id} class="map" role="region" aria-label={label}></div>
69
+ {loading && <p class="over" data-over="loading" role="status"><span>{loading}</span></p>}
70
+ {empty && <p class="over" data-over="empty" hidden><span>{empty}</span></p>}
71
+ {failed && <p class="over" data-over="failed" hidden><span>{failed}</span></p>}
72
+ <slot />
73
+ </div>
74
+ <slot name="aside" />
75
+ </div>
76
+
77
+ <script>
78
+ // In fill mode the title floats over the map, and the panels at the top start below it: the room
79
+ // it takes, as --map-stage-head-room, measured from the stage's top inset. In a box, the panels
80
+ // stack --sp-2 under the title's box; on a scrim, they start --sp-4 under the band's lower edge,
81
+ // where its fade ends, so no panel sits under it. Nothing while the title doesn't float.
82
+ document.querySelectorAll<HTMLElement>('[data-map-stage][data-head]').forEach((stage) => {
83
+ const head = stage.querySelector<HTMLElement>(':scope > .head')!;
84
+ const px = (name: string, fallback: number) => parseFloat(getComputedStyle(stage).getPropertyValue(name)) || fallback;
85
+ const measure = () => {
86
+ if (getComputedStyle(head).position !== 'absolute') return stage.style.setProperty('--map-stage-head-room', '0px');
87
+ const scrim = stage.dataset.head === 'scrim';
88
+ // The band itself, or the panel that holds the title (Astro can put a script first in the slot).
89
+ const edge = scrim ? head : (head.querySelector<HTMLElement>(':scope > :not(script, style)') ?? head);
90
+ const room = edge.getBoundingClientRect().bottom - stage.getBoundingClientRect().top - px('--sp-3', 12) + (scrim ? px('--sp-4', 16) : px('--sp-2', 8));
91
+ stage.style.setProperty('--map-stage-head-room', `${Math.round(room)}px`);
92
+ };
93
+ new ResizeObserver(measure).observe(head);
94
+ matchMedia('(max-width: 760px)').addEventListener('change', measure);
95
+ measure();
96
+ });
97
+ </script>
98
+
99
+ <style>
100
+ /* The wrapper holds the title, the frame and a side panel, which floats over the frame's right
101
+ edge on a wide screen and comes after it on a phone. */
102
+ .map-stage {
103
+ position: relative;
104
+ }
105
+
106
+ .frame {
107
+ position: relative;
108
+ height: var(--map-stage-height, max(var(--map-stage-min, 28rem), calc(100dvh - var(--map-stage-offset, 10.5rem))));
109
+ }
110
+
111
+ @media (max-width: 1023px) {
112
+ .frame {
113
+ height: var(--map-stage-height, max(var(--map-stage-min-narrow, 26rem), calc(100dvh - var(--map-stage-offset-narrow, 14rem))));
114
+ }
115
+ }
116
+
117
+ /* Absolute over MapLibre's own position: relative on the same element. */
118
+ .frame > .map {
119
+ position: absolute;
120
+ inset: 0;
121
+ border: 1px solid var(--c-border);
122
+ border-radius: var(--radius-lg);
123
+ background: var(--c-surface-sunken, var(--c-bg));
124
+ overflow: hidden;
125
+ transition: opacity var(--motion-base) var(--ease-out);
126
+ }
127
+
128
+ .map-stage[data-loading] .map {
129
+ opacity: 0.5;
130
+ }
131
+
132
+ /* The title, when it doesn't float: a plain header above the map, unboxed, as a page header
133
+ stands. In framed mode, and in fill mode on a phone. */
134
+ .head {
135
+ margin-bottom: var(--sp-5);
136
+ }
137
+
138
+ .head :global(.float) {
139
+ position: static;
140
+ max-width: none;
141
+ padding: 0;
142
+ border: 0;
143
+ background: none;
144
+ box-shadow: none;
145
+ -webkit-backdrop-filter: none;
146
+ backdrop-filter: none;
147
+ }
148
+
149
+ /* Fill: the whole column, past its gutters and down to the window's bottom, with no frame of its
150
+ own. From 760px up; narrower, it is framed. */
151
+ @media (min-width: 761px) {
152
+ .map-stage[data-mode='fill'] {
153
+ margin-inline: calc(-1 * var(--map-stage-bleed, var(--layout-gutter, 0px)));
154
+ }
155
+
156
+ .map-stage[data-mode='fill'] .frame {
157
+ height: calc(100dvh - var(--map-stage-fill-offset, 0px));
158
+ }
159
+
160
+ .map-stage[data-mode='fill'] .map,
161
+ .map-stage[data-mode='fill'] .over {
162
+ border: 0;
163
+ border-radius: 0;
164
+ }
165
+
166
+ .map-stage[data-mode='fill'] .head {
167
+ position: absolute;
168
+ top: var(--sp-3);
169
+ left: var(--sp-3);
170
+ z-index: 3;
171
+ max-width: calc(100% - var(--sp-3) * 2 - 48px);
172
+ margin: 0;
173
+ }
174
+
175
+ /* In a panel, like everything else over the map. */
176
+ .map-stage[data-mode='fill'][data-head='box'] .head :global(.float) {
177
+ padding: var(--sp-3) var(--sp-4);
178
+ border: 1px solid var(--c-border);
179
+ background: color-mix(in srgb, var(--c-surface) 93%, transparent);
180
+ -webkit-backdrop-filter: blur(12px) saturate(1.2);
181
+ backdrop-filter: blur(12px) saturate(1.2);
182
+ box-shadow: 0 8px 28px rgb(0 0 0 / 0.16);
183
+ }
184
+
185
+ /* On a band across the top of the map: the page's own background, solid behind the words and
186
+ --sp-3 below them, then fading out over --sp-5, so the title reads as the page's and the map
187
+ runs on under it. Its height is the title row's; the panels start below it (head-room). */
188
+ .map-stage[data-mode='fill'][data-head='scrim'] .head {
189
+ top: 0;
190
+ left: 0;
191
+ right: 0;
192
+ max-width: none;
193
+ padding: var(--sp-4) var(--sp-5) calc(var(--sp-3) + var(--sp-5));
194
+ background: linear-gradient(
195
+ to bottom,
196
+ color-mix(in srgb, var(--c-bg) 95%, transparent) 0,
197
+ color-mix(in srgb, var(--c-bg) 95%, transparent) calc(100% - var(--sp-5)),
198
+ transparent 100%
199
+ );
200
+ pointer-events: none;
201
+ }
202
+
203
+ .map-stage[data-mode='fill'][data-head='scrim'] .head :global(*) {
204
+ pointer-events: auto;
205
+ }
206
+
207
+ /* The band spans the map, so everything at its top starts below it: the side panel, a
208
+ top-right panel, and the map's own buttons in the top corners. */
209
+ .map-stage[data-mode='fill'][data-head='scrim'] > :global(.side) {
210
+ top: calc(var(--sp-3) + var(--map-stage-head-room, 0px));
211
+ max-height: calc(100% - var(--map-stage-head-room, 0px) - var(--sp-3) * 2);
212
+ }
213
+
214
+ .map-stage[data-mode='fill'][data-head='scrim'] .frame > :global(.top-right) {
215
+ top: calc(var(--sp-3) + var(--map-stage-head-room, 0px));
216
+ }
217
+
218
+ .map-stage[data-mode='fill'][data-head='scrim'] :global(.maplibregl-ctrl-top-left),
219
+ .map-stage[data-mode='fill'][data-head='scrim'] :global(.maplibregl-ctrl-top-right) {
220
+ top: var(--map-stage-head-room, 0px);
221
+ }
222
+ }
223
+
224
+ @media (min-width: 761px) and (max-width: 1023px) {
225
+ .map-stage[data-mode='fill'] .frame {
226
+ height: calc(100dvh - var(--map-stage-fill-offset-narrow, 57px));
227
+ }
228
+ }
229
+
230
+ /* Fill falls back to framed on a phone: room above for the title, as a page has. */
231
+ @media (max-width: 760px) {
232
+ .map-stage[data-mode='fill'] {
233
+ padding-top: var(--sp-5);
234
+ }
235
+ }
236
+
237
+ /* What covers the map while it reads, or when it has nothing: a veil that dims the map, and
238
+ the words on a solid chip, because no veil keeps text readable over every map under it. */
239
+ .over {
240
+ position: absolute;
241
+ inset: 0;
242
+ z-index: 1;
243
+ display: grid;
244
+ place-items: center;
245
+ margin: 0;
246
+ padding: var(--sp-4);
247
+ border-radius: var(--radius-lg);
248
+ background: color-mix(in srgb, var(--c-bg) 55%, transparent);
249
+ pointer-events: none;
250
+ }
251
+
252
+ .over[hidden] {
253
+ display: none;
254
+ }
255
+
256
+ .over span {
257
+ max-width: 40ch;
258
+ padding: var(--sp-2) var(--sp-3);
259
+ border: 1px solid var(--c-border);
260
+ border-radius: var(--radius-md);
261
+ background: var(--c-surface);
262
+ color: var(--c-text-secondary);
263
+ font-size: var(--text-body-sm);
264
+ line-height: var(--leading-body-sm);
265
+ text-align: center;
266
+ }
267
+
268
+ /* The hover card: the charts' pointer card, without MapLibre's own box and tip. */
269
+ .map-stage :global(.place-card .maplibregl-popup-content) {
270
+ padding: 0;
271
+ background: none;
272
+ box-shadow: none;
273
+ }
274
+
275
+ .map-stage :global(.place-card .maplibregl-popup-tip) {
276
+ display: none;
277
+ }
278
+
279
+ @media (prefers-reduced-motion: reduce) {
280
+ .frame > .map {
281
+ transition: none;
282
+ }
283
+ }
284
+ </style>
@@ -8,6 +8,9 @@
8
8
  * title or else the lede, gets a highlighter that sweeps in once. A page that writes its title
9
9
  * or lede once its data arrives wraps those words in <mark> itself; the sweep plays as they
10
10
  * land.
11
+ *
12
+ * `compact` is the header made small enough to float over a map (MapStage fill mode): the title
13
+ * at the h3 size, a shorter rule, and the actions beside it in one row while there is room.
11
14
  */
12
15
  import './highlight.css';
13
16
 
@@ -23,8 +26,10 @@ interface Props {
23
26
  rule?: boolean;
24
27
  /** Words in the title or lede to highlight. */
25
28
  mark?: string;
29
+ /** Small enough to float over a map. */
30
+ compact?: boolean;
26
31
  }
27
- const { eyebrow, title, lede, meta, ids = {}, rule = false, mark } = Astro.props;
32
+ const { eyebrow, title, lede, meta, ids = {}, rule = false, mark, compact = false } = Astro.props;
28
33
 
29
34
  // The text around the marked words, or the text alone when they aren't in it.
30
35
  const split = (text?: string) => {
@@ -35,7 +40,7 @@ const inTitle = split(title);
35
40
  const inLede = inTitle ? null : split(lede);
36
41
  ---
37
42
 
38
- <header class="page-header">
43
+ <header class:list={['page-header', { compact }]}>
39
44
  <div class="text">
40
45
  {(eyebrow || ids.eyebrow) && <p class="eyebrow" id={ids.eyebrow}>{eyebrow}</p>}
41
46
  <h1 id={ids.title}>{inTitle ? <>{inTitle[0]}<mark>{inTitle[1]}</mark>{inTitle[2]}</> : title}</h1>
@@ -125,4 +130,30 @@ const inLede = inTitle ? null : split(lede);
125
130
  flex-wrap: wrap;
126
131
  gap: var(--sp-2);
127
132
  }
133
+
134
+ /* Over a map: the title a step down, the rule shorter and closer, the actions centred beside it. */
135
+ .compact {
136
+ align-items: center;
137
+ gap: var(--sp-3) var(--sp-5);
138
+ }
139
+
140
+ .compact .text {
141
+ flex: 0 1 auto;
142
+ }
143
+
144
+ .compact h1 {
145
+ font-size: var(--text-h3);
146
+ line-height: var(--leading-h3);
147
+ letter-spacing: var(--tracking-h3);
148
+ }
149
+
150
+ .compact .rule {
151
+ width: var(--sp-6);
152
+ height: 4px;
153
+ margin: var(--sp-2) 0 0;
154
+ }
155
+
156
+ .compact .lede {
157
+ min-height: 0;
158
+ }
128
159
  </style>
@@ -0,0 +1,87 @@
1
+ ---
2
+ /**
3
+ * What a colour scale means, low to high: a title, an optional note (how many), and the ramp
4
+ * between its two ends. The heat ramp by default; pass `colors` for another, such as the
5
+ * sequential tokens. On a map it goes in a FloatPanel; under a chart it stands alone.
6
+ *
7
+ * `steps` draws the colours as blocks, for a scale that is binned; the default is a smooth
8
+ * gradient, for one that isn't. Change the note from script through its data-ramp-note element.
9
+ */
10
+ interface Props {
11
+ /** What the colour counts: "GPS points". */
12
+ label: string;
13
+ /** After the title, such as how many: "1,284 cells". */
14
+ note?: string;
15
+ low?: string;
16
+ high?: string;
17
+ /** Low to high. Any CSS colours, best tokens. */
18
+ colors?: string[];
19
+ steps?: boolean;
20
+ id?: string;
21
+ [key: string]: unknown;
22
+ }
23
+
24
+ const HEAT = [1, 2, 3, 4, 5, 6].map((i) => `var(--c-heat-${i})`);
25
+ const { label, note, low = 'Low', high = 'High', colors = HEAT, steps = false, id, class: className, ...rest } = Astro.props;
26
+ const stops = steps
27
+ ? colors.map((c, i) => `${c} ${(i / colors.length) * 100}% ${((i + 1) / colors.length) * 100}%`).join(', ')
28
+ : colors.join(', ');
29
+ ---
30
+
31
+ <div class:list={['ramp-legend', className]} id={id} {...rest}>
32
+ <p class="what"><strong>{label}</strong><span data-ramp-note>{note ? ` · ${note}` : ''}</span></p>
33
+ <p class="scale">
34
+ <span>{low}</span><span class="sr"> to </span><i aria-hidden="true" style={`--ramp: linear-gradient(to right, ${stops})`}></i><span>{high}</span>
35
+ </p>
36
+ </div>
37
+
38
+ <style>
39
+ .ramp-legend {
40
+ display: grid;
41
+ gap: var(--sp-2);
42
+ color: var(--c-text-secondary);
43
+ font-size: var(--text-caption);
44
+ line-height: var(--leading-caption);
45
+ }
46
+
47
+ p {
48
+ margin: 0;
49
+ }
50
+
51
+ strong {
52
+ color: var(--c-text);
53
+ font-weight: 600;
54
+ }
55
+
56
+ .scale {
57
+ display: flex;
58
+ align-items: center;
59
+ gap: var(--sp-2);
60
+ }
61
+
62
+ /* A hairline edge, so the yellow keeps its shape on a light surface. */
63
+ i {
64
+ flex: none;
65
+ width: 120px;
66
+ height: 10px;
67
+ border-radius: 3px;
68
+ background: var(--ramp);
69
+ box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--c-text) 14%, transparent);
70
+ }
71
+
72
+ .sr {
73
+ position: absolute;
74
+ width: 1px;
75
+ height: 1px;
76
+ overflow: hidden;
77
+ clip-path: inset(50%);
78
+ white-space: nowrap;
79
+ }
80
+
81
+ @media (forced-colors: active) {
82
+ i {
83
+ forced-color-adjust: none;
84
+ outline: 1px solid CanvasText;
85
+ }
86
+ }
87
+ </style>
@@ -0,0 +1,100 @@
1
+ // What a map in a MapStage needs from script: which message covers it, the room its floating panels
2
+ // leave, side panels put away and brought back, and tokens resolved for a canvas. None of it
3
+ // imports a map library: it hands back plain values MapLibre takes as they are.
4
+
5
+ /** The heat ramp, cold to warm, as tokens. The same in both themes. */
6
+ export const HEAT = [1, 2, 3, 4, 5, 6].map((i) => `var(--c-heat-${i})`);
7
+
8
+ /**
9
+ * A canvas reads colours, not tokens: a token (or any CSS colour), resolved now, as rgb(). Call it
10
+ * again when the theme changes (onTheme).
11
+ */
12
+ export const resolve = (value) => {
13
+ const probe = document.createElement('i');
14
+ probe.style.color = value;
15
+ probe.hidden = true;
16
+ document.body.append(probe);
17
+ const colour = getComputedStyle(probe).color;
18
+ probe.remove();
19
+ return colour;
20
+ };
21
+
22
+ /** Calls fn when the theme changes, by the switch (data-theme) or by the system. */
23
+ export const onTheme = (fn) => {
24
+ new MutationObserver(fn).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
25
+ matchMedia('(prefers-color-scheme: dark)').addEventListener('change', fn);
26
+ };
27
+
28
+ /**
29
+ * A MapLibre expression that colours a feature by a number from 0 to 1 in `property`, along the
30
+ * heat ramp or any list of colours: ['interpolate', ['linear'], ['get', property], 0, …, 1, …].
31
+ * The colours are resolved now; build it again when the theme changes.
32
+ */
33
+ export const ramp = (property, colours = HEAT) => [
34
+ 'interpolate',
35
+ ['linear'],
36
+ ['get', property],
37
+ ...colours.flatMap((c, i) => [i / (colours.length - 1), resolve(c)]),
38
+ ];
39
+
40
+ /**
41
+ * Which message covers the map: 'loading' (and the map dims), 'empty', 'failed', or 'ready' for
42
+ * none. A message the MapStage wasn't given is skipped.
43
+ */
44
+ export const setStage = (stage, state) => {
45
+ const root = stage.closest('[data-map-stage]') ?? stage;
46
+ root.toggleAttribute('data-loading', state === 'loading');
47
+ root.querySelectorAll('[data-over]').forEach((p) => (p.hidden = p.dataset.over !== state));
48
+ };
49
+
50
+ /**
51
+ * Padding for fitBounds and easeTo, so what the map shows stays clear of the panels floating over
52
+ * it: under a floating title (MapStage fill mode) and a top panel, above a bottom one, left of a
53
+ * side panel while it floats. `edge` is the
54
+ * least on every side; `gap` the room kept from a panel. Each axis keeps at least a fifth of the map.
55
+ */
56
+ export const room = (stage, { edge = 48, gap = 16 } = {}) => {
57
+ const root = stage.closest('[data-map-stage]') ?? stage;
58
+ const frame = root.querySelector('.frame').getBoundingClientRect();
59
+ const pad = { top: edge, right: edge, bottom: edge, left: edge };
60
+ const head = root.querySelector(':scope > .head');
61
+ if (head && getComputedStyle(head).position === 'absolute') pad.top = Math.max(pad.top, head.getBoundingClientRect().bottom - frame.top + gap);
62
+ for (const panel of root.querySelectorAll('[data-float]')) {
63
+ const style = getComputedStyle(panel);
64
+ if (style.position !== 'absolute' || style.display === 'none') continue;
65
+ const r = panel.getBoundingClientRect();
66
+ const place = panel.dataset.float;
67
+ if (place === 'side') pad.right = Math.max(pad.right, frame.right - r.left + gap);
68
+ else if (place.startsWith('top') || place === 'head') pad.top = Math.max(pad.top, r.bottom - frame.top + gap);
69
+ else pad.bottom = Math.max(pad.bottom, frame.bottom - r.top + gap);
70
+ }
71
+ const fit = (a, b, size) => {
72
+ const most = size * 0.8;
73
+ if (pad[a] + pad[b] <= most) return;
74
+ const k = most / (pad[a] + pad[b]);
75
+ pad[a] = Math.floor(pad[a] * k);
76
+ pad[b] = Math.floor(pad[b] * k);
77
+ };
78
+ fit('top', 'bottom', frame.height);
79
+ fit('left', 'right', frame.width);
80
+ return pad;
81
+ };
82
+
83
+ /**
84
+ * Wires every FloatToggle on the page once: it opens and closes its side panel, and the panel fires
85
+ * `float-toggle` with { open }. FloatPanel calls it; call it again after adding toggles from script.
86
+ */
87
+ export const wireFloatToggles = (root = document) => {
88
+ root.querySelectorAll('[data-float-toggle]:not([data-wired])').forEach((button) => {
89
+ button.dataset.wired = '';
90
+ const panel = document.getElementById(button.dataset.floatToggle);
91
+ if (!panel) return;
92
+ panel.toggleAttribute('data-closed', button.getAttribute('aria-expanded') !== 'true');
93
+ button.addEventListener('click', () => {
94
+ const open = button.getAttribute('aria-expanded') !== 'true';
95
+ button.setAttribute('aria-expanded', String(open));
96
+ panel.toggleAttribute('data-closed', !open);
97
+ panel.dispatchEvent(new CustomEvent('float-toggle', { bubbles: true, detail: { open } }));
98
+ });
99
+ });
100
+ };