@alulore/kumiko 0.19.0 → 0.20.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.
Files changed (138) hide show
  1. package/dist/board/board-api.d.ts +2 -1
  2. package/dist/board/board-api.js +1 -1
  3. package/dist/board/board-motion.js +29 -1
  4. package/dist/board/board-slots.d.ts +14 -7
  5. package/dist/board/board-slots.js +5 -3
  6. package/dist/board/board.d.ts +20 -5
  7. package/dist/board/board.js +28 -5
  8. package/dist/board/keep-capture.d.ts +10 -5
  9. package/dist/board/keep-capture.js +48 -13
  10. package/dist/cooldown/cooldown-button.d.ts +30 -0
  11. package/dist/cooldown/cooldown-button.js +75 -0
  12. package/dist/cooldown/cooldown-labels.d.ts +8 -0
  13. package/dist/cooldown/cooldown-labels.js +4 -0
  14. package/dist/cooldown/cooldown-time.d.ts +8 -0
  15. package/dist/cooldown/cooldown-time.js +20 -0
  16. package/dist/cooldown/cooldown.css +21 -0
  17. package/dist/cooldown/index.d.ts +4 -0
  18. package/dist/cooldown/index.js +4 -0
  19. package/dist/cooldown/use-cooldown.d.ts +30 -0
  20. package/dist/cooldown/use-cooldown.js +63 -0
  21. package/dist/dialog/dialog-content.d.ts +5 -0
  22. package/dist/dialog/dialog-content.js +7 -0
  23. package/dist/dialog/overlay-host.d.ts +1 -1
  24. package/dist/dialog/overlay-host.js +10 -1
  25. package/dist/empty-state/empty-state-labels.d.ts +14 -0
  26. package/dist/empty-state/empty-state-labels.js +10 -0
  27. package/dist/empty-state/empty-state.css +134 -0
  28. package/dist/empty-state/empty-state.d.ts +34 -0
  29. package/dist/empty-state/empty-state.js +61 -0
  30. package/dist/empty-state/index.d.ts +2 -0
  31. package/dist/empty-state/index.js +2 -0
  32. package/dist/form/choice-fields.d.ts +34 -0
  33. package/dist/form/choice-fields.js +41 -0
  34. package/dist/form/error-line.d.ts +8 -0
  35. package/dist/form/error-line.js +47 -0
  36. package/dist/form/field-props.d.ts +12 -0
  37. package/dist/form/field-props.js +8 -0
  38. package/dist/form/field-shell.d.ts +18 -0
  39. package/dist/form/field-shell.js +51 -0
  40. package/dist/form/focus-invalid.d.ts +5 -0
  41. package/dist/form/focus-invalid.js +17 -0
  42. package/dist/form/form-context.d.ts +2 -0
  43. package/dist/form/form-context.js +3 -0
  44. package/dist/form/form-parts.d.ts +20 -0
  45. package/dist/form/form-parts.js +73 -0
  46. package/dist/form/form.css +128 -0
  47. package/dist/form/index.d.ts +10 -0
  48. package/dist/form/index.js +9 -0
  49. package/dist/form/shown-error.d.ts +12 -0
  50. package/dist/form/shown-error.js +27 -0
  51. package/dist/form/submit-errors.d.ts +36 -0
  52. package/dist/form/submit-errors.js +46 -0
  53. package/dist/form/text-fields.d.ts +18 -0
  54. package/dist/form/text-fields.js +21 -0
  55. package/dist/form/use-app-form.d.ts +1655 -0
  56. package/dist/form/use-app-form.js +94 -0
  57. package/dist/form/use-dialog-link.d.ts +5 -0
  58. package/dist/form/use-dialog-link.js +30 -0
  59. package/dist/form/value-fields.d.ts +16 -0
  60. package/dist/form/value-fields.js +15 -0
  61. package/dist/map/geo.d.ts +20 -0
  62. package/dist/map/geo.js +41 -0
  63. package/dist/map/index.d.ts +5 -0
  64. package/dist/map/index.js +4 -0
  65. package/dist/map/label-overlap.d.ts +13 -0
  66. package/dist/map/label-overlap.js +23 -0
  67. package/dist/map/load-map.d.ts +12 -0
  68. package/dist/map/load-map.js +22 -0
  69. package/dist/map/location-map.d.ts +39 -0
  70. package/dist/map/location-map.js +171 -0
  71. package/dist/map/map-frame.d.ts +18 -0
  72. package/dist/map/map-frame.js +6 -0
  73. package/dist/map/map-pin.d.ts +4 -0
  74. package/dist/map/map-pin.js +38 -0
  75. package/dist/map/map-popup.d.ts +17 -0
  76. package/dist/map/map-popup.js +101 -0
  77. package/dist/map/map-source.d.ts +52 -0
  78. package/dist/map/map-source.js +59 -0
  79. package/dist/map/map.css +311 -0
  80. package/dist/map/marker-pins.d.ts +29 -0
  81. package/dist/map/marker-pins.js +144 -0
  82. package/dist/map/markers-map.d.ts +57 -0
  83. package/dist/map/markers-map.js +154 -0
  84. package/dist/map/use-label-overlap.d.ts +9 -0
  85. package/dist/map/use-label-overlap.js +55 -0
  86. package/dist/map/use-map.d.ts +49 -0
  87. package/dist/map/use-map.js +201 -0
  88. package/dist/media/encoding.d.ts +32 -0
  89. package/dist/media/encoding.js +59 -0
  90. package/dist/media/errors.d.ts +33 -0
  91. package/dist/media/errors.js +26 -0
  92. package/dist/media/image.d.ts +9 -0
  93. package/dist/media/image.js +35 -0
  94. package/dist/media/index.d.ts +10 -0
  95. package/dist/media/index.js +10 -0
  96. package/dist/media/load-mediabunny.d.ts +7 -0
  97. package/dist/media/load-mediabunny.js +13 -0
  98. package/dist/media/media-upload.d.ts +40 -0
  99. package/dist/media/media-upload.js +132 -0
  100. package/dist/media/media.css +101 -0
  101. package/dist/media/probe.d.ts +19 -0
  102. package/dist/media/probe.js +49 -0
  103. package/dist/media/sizes.d.ts +28 -0
  104. package/dist/media/sizes.js +51 -0
  105. package/dist/media/slideshow.d.ts +43 -0
  106. package/dist/media/slideshow.js +174 -0
  107. package/dist/media/timeline.d.ts +59 -0
  108. package/dist/media/timeline.js +105 -0
  109. package/dist/media/transcode.d.ts +35 -0
  110. package/dist/media/transcode.js +132 -0
  111. package/dist/media/upload.d.ts +21 -0
  112. package/dist/media/upload.js +54 -0
  113. package/dist/media/use-media-job.d.ts +79 -0
  114. package/dist/media/use-media-job.js +143 -0
  115. package/dist/server-render.test.js +93 -0
  116. package/dist/smooth-caret/caret-controller.js +16 -3
  117. package/dist/table/table-labels.d.ts +2 -0
  118. package/dist/table/table-labels.js +1 -0
  119. package/dist/table/table-sections.js +10 -2
  120. package/dist/table/table.css +1 -0
  121. package/dist/theme/define-theme.d.ts +38 -0
  122. package/dist/theme/define-theme.js +56 -0
  123. package/dist/theme/index.d.ts +3 -0
  124. package/dist/theme/index.js +3 -0
  125. package/dist/theme/theme-style.d.ts +6 -0
  126. package/dist/theme/theme-style.js +11 -0
  127. package/dist/theme/theme-types.d.ts +1 -0
  128. package/dist/theme/theme-types.js +26 -0
  129. package/dist/theme/tokens-1.generated.d.ts +1 -0
  130. package/dist/theme/tokens-1.generated.js +404 -0
  131. package/dist/theme/tokens-2.generated.d.ts +1 -0
  132. package/dist/theme/tokens-2.generated.js +404 -0
  133. package/dist/theme/tokens-3.generated.d.ts +1 -0
  134. package/dist/theme/tokens-3.generated.js +158 -0
  135. package/dist/theme/tokens.generated.d.ts +2 -0
  136. package/dist/theme/tokens.generated.js +6 -0
  137. package/dist/undo/undo-provider.js +24 -9
  138. package/package.json +78 -1
@@ -0,0 +1,144 @@
1
+ import { useEffect, useRef } from "react";
2
+ import { pinButton } from "./map-pin";
3
+ /** MapLibre's mousedown keeps focus off a marker, so a mouse click focuses the pin as it would any button. */
4
+ function focusOnMouseClick(element) {
5
+ let pointer = "";
6
+ element.addEventListener("pointerdown", (event) => {
7
+ pointer = event.pointerType;
8
+ });
9
+ element.addEventListener("click", (event) => {
10
+ if (event.detail > 0 && pointer === "mouse") {
11
+ element.focus({ preventScroll: true });
12
+ }
13
+ });
14
+ }
15
+ function makePin(place, map, handle, events) {
16
+ const loaded = handle.modules.current;
17
+ if (!loaded) {
18
+ return undefined;
19
+ }
20
+ const element = pinButton(place.label);
21
+ const label = document.createElement("span");
22
+ label.dataset.slot = "map-pin-label";
23
+ label.setAttribute("aria-hidden", "true");
24
+ label.textContent = place.label;
25
+ element.append(label);
26
+ const { id } = place;
27
+ focusOnMouseClick(element);
28
+ element.addEventListener("click", (event) => {
29
+ events.current.select(id, event.detail === 0);
30
+ });
31
+ element.addEventListener("pointerenter", (event) => {
32
+ if (event.pointerType === "mouse") {
33
+ events.current.hover(id, "pointer");
34
+ }
35
+ });
36
+ element.addEventListener("pointerleave", (event) => {
37
+ if (event.pointerType === "mouse") {
38
+ events.current.hover(null, "pointer");
39
+ }
40
+ });
41
+ element.addEventListener("focus", () => {
42
+ events.current.hover(id, "focus");
43
+ });
44
+ element.addEventListener("blur", () => {
45
+ events.current.hover(null, "focus");
46
+ });
47
+ element.addEventListener("keydown", (event) => {
48
+ if (event.key === "Escape") {
49
+ events.current.escape(id);
50
+ }
51
+ });
52
+ const marker = new loaded.maplibre.Marker({ element, anchor: "bottom" })
53
+ .setLngLat([place.longitude, place.latitude])
54
+ .addTo(map);
55
+ return { marker, element, label };
56
+ }
57
+ function rename(pin, place) {
58
+ pin.element.setAttribute("aria-label", place.label);
59
+ pin.label.textContent = place.label;
60
+ pin.marker.setLngLat([place.longitude, place.latitude]);
61
+ }
62
+ /** Pins follow the list: added, moved, renamed and removed, and kept in its order so Tab walks them as it does. */
63
+ function syncPins(markers, pins, map, make) {
64
+ const wanted = new Set(markers.map((marker) => marker.id));
65
+ for (const [id, pin] of pins) {
66
+ if (!wanted.has(id)) {
67
+ pin.marker.remove();
68
+ pins.delete(id);
69
+ }
70
+ }
71
+ const order = [...pins.keys()];
72
+ const sameOrder = markers.every((marker, index) => order[index] === marker.id);
73
+ const kept = new Map(pins);
74
+ if (!sameOrder) {
75
+ for (const pin of pins.values()) {
76
+ pin.marker.remove();
77
+ }
78
+ pins.clear();
79
+ }
80
+ for (const place of markers) {
81
+ const existing = kept.get(place.id);
82
+ if (!existing) {
83
+ const pin = make(place);
84
+ if (pin) {
85
+ pins.set(place.id, pin);
86
+ }
87
+ continue;
88
+ }
89
+ rename(existing, place);
90
+ if (!sameOrder) {
91
+ pins.set(place.id, existing);
92
+ existing.marker.addTo(map);
93
+ }
94
+ }
95
+ }
96
+ /** The pins, kept in step with `markers` once the map is ready, and removed with the map. */
97
+ export function usePins(handle, events, markers) {
98
+ const pins = useRef(new Map());
99
+ const ready = handle.state.phase === "ready";
100
+ useEffect(() => {
101
+ const map = handle.map.current;
102
+ if (!ready || !map) {
103
+ return;
104
+ }
105
+ syncPins(markers, pins.current, map, (place) => makePin(place, map, handle, events));
106
+ }, [ready, markers, handle, events]);
107
+ useEffect(() => {
108
+ const current = pins.current;
109
+ return () => {
110
+ for (const pin of current.values()) {
111
+ pin.marker.remove();
112
+ }
113
+ current.clear();
114
+ };
115
+ }, [handle]);
116
+ return pins;
117
+ }
118
+ /** Marks the selected pin, and says on each pin whether pressing it opens a card. */
119
+ export function useSelected(pins, handle, markers, selectedId, card) {
120
+ const cardId = card?.id;
121
+ const cardOpen = card?.open ?? false;
122
+ useEffect(() => {
123
+ for (const [id, pin] of pins.current) {
124
+ const selected = id === selectedId;
125
+ pin.element.toggleAttribute("data-selected", selected);
126
+ /* A pin that opens a card says so with aria-expanded; one that only selects is a toggle. */
127
+ if (cardId === undefined) {
128
+ pin.element.setAttribute("aria-pressed", String(selected));
129
+ pin.element.removeAttribute("aria-expanded");
130
+ pin.element.removeAttribute("aria-controls");
131
+ continue;
132
+ }
133
+ const open = selected && cardOpen;
134
+ pin.element.removeAttribute("aria-pressed");
135
+ pin.element.setAttribute("aria-expanded", String(open));
136
+ if (open) {
137
+ pin.element.setAttribute("aria-controls", cardId);
138
+ }
139
+ else {
140
+ pin.element.removeAttribute("aria-controls");
141
+ }
142
+ }
143
+ }, [pins, handle, markers, selectedId, cardId, cardOpen]);
144
+ }
@@ -0,0 +1,57 @@
1
+ import { type ReactNode } from "react";
2
+ import { type LatLng } from "./geo";
3
+ import type { MapSource } from "./map-source";
4
+ import { type MapMarker } from "./marker-pins";
5
+ import { type PinLabels } from "./use-label-overlap";
6
+ export type { MapMarker } from "./marker-pins";
7
+ export type { PinLabels } from "./use-label-overlap";
8
+ export interface MarkersMapProps {
9
+ /** The tiles and styles, from `defineMapSource`. */
10
+ readonly source: MapSource;
11
+ /** The map region's accessible name. */
12
+ readonly label: string;
13
+ readonly markers: readonly MapMarker[];
14
+ /** The selected pin, shown lifted and larger. Leave it out to let the map keep its own. */
15
+ readonly selectedId?: string | null;
16
+ /** A pin was pressed, by pointer or keyboard, or null when its card was closed. */
17
+ readonly onSelect?: (id: string | null) => void;
18
+ /** The pointer or focus is on a pin, or null when it leaves. */
19
+ readonly onHover?: (id: string | null) => void;
20
+ /**
21
+ * Which pins show their name beside them. With `"always"`, a name that would overlap one before it hides,
22
+ * the selected pin's first and then the list's order. @defaultValue "none"
23
+ */
24
+ readonly labels?: PinLabels;
25
+ /**
26
+ * A card over the selected pin, such as a photo, an address and a link. It closes on Escape, a click on the
27
+ * map, or another pin. A pin pressed from the keyboard moves focus into it, and Escape brings focus back.
28
+ */
29
+ readonly renderDetail?: (marker: MapMarker) => ReactNode;
30
+ /**
31
+ * A light preview over a pin while a mouse is on it or it has focus: `true` shows its name. It never takes the
32
+ * pointer, and touch has no hover, so put anything a person needs in the card. @defaultValue false
33
+ */
34
+ readonly preview?: boolean | ((marker: MapMarker) => ReactNode);
35
+ /** The view fits the pins when the map opens and whenever this changes, such as a hash of the filters. */
36
+ readonly fitKey?: string;
37
+ /** Room kept round the pins when fitting, in pixels. @defaultValue 48 */
38
+ readonly fitPadding?: number;
39
+ /** The closest a fit zooms in, so one pin doesn't fill the map. @defaultValue 15 */
40
+ readonly fitMaxZoom?: number;
41
+ /** Where the map opens when there are no pins. @defaultValue 0, 0 */
42
+ readonly center?: LatLng;
43
+ /** @defaultValue 12 */
44
+ readonly zoom?: number;
45
+ /** Shown while the map loads. */
46
+ readonly loading?: ReactNode;
47
+ /** Shown instead of the map when it can't draw, such as without WebGL2. */
48
+ readonly fallback?: ReactNode;
49
+ /** MapLibre's own words, such as `CooperativeGesturesHandler.WindowsHelpText`. */
50
+ readonly locale?: Readonly<Record<string, string>>;
51
+ readonly className?: string;
52
+ }
53
+ /**
54
+ * A map of many places, for browsing a list beside it. Each pin is a button named after its place, in the
55
+ * order of `markers`, so Tab walks them as the list does.
56
+ */
57
+ export declare function MarkersMap(props: MarkersMapProps): React.JSX.Element;
@@ -0,0 +1,154 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useId, useRef, useState } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { boundsOf, toLngLatBounds } from "./geo";
5
+ import { MapFrame } from "./map-frame";
6
+ import { useMapPopup } from "./map-popup";
7
+ import { usePins, useSelected } from "./marker-pins";
8
+ import { useLabelOverlap } from "./use-label-overlap";
9
+ import { useMapBox } from "./use-map";
10
+ /** Selection, controlled or the map's own, and which pin the mouse or focus is on. */
11
+ function useSelection(latest, pins) {
12
+ const [own, setOwn] = useState(null);
13
+ const [hoveredId, setHovered] = useState(null);
14
+ const controlled = latest.current.selectedId !== undefined;
15
+ const selectedId = controlled ? (latest.current.selectedId ?? null) : own;
16
+ const focusCard = useRef(false);
17
+ const choose = (id) => {
18
+ if (!controlled) {
19
+ setOwn(id);
20
+ }
21
+ latest.current.onSelect?.(id);
22
+ };
23
+ const dismiss = () => {
24
+ const pin = selectedId === null ? undefined : pins.current?.current.get(selectedId);
25
+ const focusWasInside = pin?.element.ownerDocument.activeElement?.closest("[data-slot=map-card]");
26
+ choose(null);
27
+ if (focusWasInside) {
28
+ pin?.element.focus();
29
+ }
30
+ };
31
+ const events = useRef({
32
+ select: () => undefined,
33
+ hover: () => undefined,
34
+ escape: () => undefined
35
+ });
36
+ events.current = {
37
+ select: (id, keyboard) => {
38
+ focusCard.current = keyboard;
39
+ choose(id);
40
+ },
41
+ hover: (id) => {
42
+ setHovered(id);
43
+ latest.current.onHover?.(id);
44
+ },
45
+ escape: (id) => {
46
+ if (id === selectedId && latest.current.renderDetail) {
47
+ choose(null);
48
+ }
49
+ }
50
+ };
51
+ const dismissRef = useRef(dismiss);
52
+ dismissRef.current = dismiss;
53
+ return { selectedId, hoveredId, events, focusCard, dismiss: dismissRef };
54
+ }
55
+ /** The card over the selected pin, rendered by the app through a portal. */
56
+ function useCard(handle, pins, latest, selection, cardId) {
57
+ const popup = useMapPopup(handle, "card", selection.dismiss);
58
+ const { selectedId } = selection;
59
+ const marker = latest.current.markers.find((item) => item.id === selectedId);
60
+ const render = latest.current.renderDetail;
61
+ const pin = selectedId === null ? undefined : pins.current.get(selectedId);
62
+ const open = Boolean(render && marker && pin && popup.element);
63
+ useEffect(() => {
64
+ if (!open || !pin) {
65
+ popup.close();
66
+ return;
67
+ }
68
+ popup.open(pin);
69
+ if (selection.focusCard.current) {
70
+ selection.focusCard.current = false;
71
+ popup.element?.querySelector("[data-slot=map-card-body]")?.focus();
72
+ }
73
+ });
74
+ if (!open || !render || !marker || !popup.element) {
75
+ return null;
76
+ }
77
+ return createPortal(_jsx("div", { id: cardId, "data-slot": "map-card-body", role: "group", "aria-label": marker.label, tabIndex: -1, onKeyDown: (event) => {
78
+ if (event.key === "Escape") {
79
+ event.stopPropagation();
80
+ selection.dismiss.current();
81
+ }
82
+ }, children: render(marker) }), popup.element);
83
+ }
84
+ const never = { current: () => undefined };
85
+ /** The preview over the pin the mouse or focus is on, unless that pin's card is open. */
86
+ function usePreview(handle, pins, latest, selection, cardOpen) {
87
+ const popup = useMapPopup(handle, "preview", never);
88
+ const { hoveredId } = selection;
89
+ const show = latest.current.preview ?? false;
90
+ const marker = latest.current.markers.find((item) => item.id === hoveredId);
91
+ const pin = hoveredId === null ? undefined : pins.current.get(hoveredId);
92
+ const covered = cardOpen && hoveredId === selection.selectedId;
93
+ const open = Boolean(show !== false && marker && pin && popup.element && !covered);
94
+ useEffect(() => {
95
+ if (open && pin) {
96
+ popup.open(pin);
97
+ }
98
+ else {
99
+ popup.close();
100
+ }
101
+ });
102
+ if (!open || !marker || !popup.element) {
103
+ return null;
104
+ }
105
+ return createPortal(_jsx("div", { "data-slot": "map-preview-body", "aria-hidden": "true", children: typeof show === "function" ? show(marker) : marker.label }), popup.element);
106
+ }
107
+ /** The view fits the pins when the map opens, at once, and when `fitKey` changes, with a glide. */
108
+ function useFit(handle, latest, fitKey) {
109
+ const ready = handle.state.phase === "ready";
110
+ const fitted = useRef(null);
111
+ useEffect(() => {
112
+ const map = handle.map.current;
113
+ const box = boundsOf(latest.current.markers);
114
+ if (!ready || !map || !box) {
115
+ return;
116
+ }
117
+ const opening = fitted.current !== map;
118
+ fitted.current = map;
119
+ map.fitBounds(toLngLatBounds(box), {
120
+ padding: latest.current.fitPadding ?? 48,
121
+ maxZoom: latest.current.fitMaxZoom ?? 15,
122
+ ...(opening ? { duration: 0 } : handle.cameraMotion())
123
+ });
124
+ }, [ready, fitKey, handle, latest]);
125
+ }
126
+ /**
127
+ * A map of many places, for browsing a list beside it. Each pin is a button named after its place, in the
128
+ * order of `markers`, so Tab walks them as the list does.
129
+ */
130
+ export function MarkersMap(props) {
131
+ const latest = useRef(props);
132
+ latest.current = props;
133
+ const cardId = useId();
134
+ const box = useMapBox({
135
+ source: props.source,
136
+ center: props.center ?? { latitude: 0, longitude: 0 },
137
+ zoom: props.zoom ?? 12,
138
+ bounds: undefined,
139
+ locale: props.locale
140
+ });
141
+ const { handle } = box;
142
+ const pinsHolder = useRef(null);
143
+ const selection = useSelection(latest, pinsHolder);
144
+ const pins = usePins(handle, selection.events, props.markers);
145
+ pinsHolder.current = pins;
146
+ const labels = props.labels ?? "none";
147
+ const cardOpen = props.renderDetail !== undefined && selection.selectedId !== null;
148
+ useSelected(pins, handle, props.markers, selection.selectedId, props.renderDetail ? { id: cardId, open: cardOpen } : undefined);
149
+ useLabelOverlap(handle, pins, labels, selection.selectedId, props.markers);
150
+ const card = useCard(handle, pins, latest, selection, cardId);
151
+ const preview = usePreview(handle, pins, latest, selection, cardOpen);
152
+ useFit(handle, latest, props.fitKey);
153
+ return (_jsxs(_Fragment, { children: [_jsx(MapFrame, { label: props.label, state: handle.state, container: box.container, probe: box.probe, loading: props.loading, fallback: props.fallback, className: props.className, flavor: box.flavor, labels: labels }), card, preview] }));
154
+ }
@@ -0,0 +1,9 @@
1
+ import type { MapMarker, Pins } from "./marker-pins";
2
+ import type { MapHandle } from "./use-map";
3
+ /** Which pins show their name beside them. */
4
+ export type PinLabels = "none" | "selected" | "always";
5
+ /**
6
+ * With every label shown, hides the ones that would overlap a label before them, the selected pin's first,
7
+ * then the list's order. Worked out again once a frame while the map moves.
8
+ */
9
+ export declare function useLabelOverlap(handle: MapHandle, pins: Pins, labels: PinLabels, selectedId: string | null, markers: readonly MapMarker[]): void;
@@ -0,0 +1,55 @@
1
+ import { useEffect } from "react";
2
+ import { hiddenLabels } from "./label-overlap";
3
+ function measure(pins, selectedId) {
4
+ const order = [...pins.entries()].sort(([a], [b]) => {
5
+ if (a === selectedId) {
6
+ return -1;
7
+ }
8
+ return b === selectedId ? 1 : 0;
9
+ });
10
+ return order.map(([id, pin]) => {
11
+ const box = pin.label.getBoundingClientRect();
12
+ return { id, left: box.left, top: box.top, right: box.right, bottom: box.bottom };
13
+ });
14
+ }
15
+ /**
16
+ * With every label shown, hides the ones that would overlap a label before them, the selected pin's first,
17
+ * then the list's order. Worked out again once a frame while the map moves.
18
+ */
19
+ export function useLabelOverlap(handle, pins, labels, selectedId, markers) {
20
+ const ready = handle.state.phase === "ready";
21
+ useEffect(() => {
22
+ const map = handle.map.current;
23
+ const all = pins.current;
24
+ if (!ready || !map) {
25
+ return undefined;
26
+ }
27
+ if (labels !== "always") {
28
+ for (const pin of all.values()) {
29
+ delete pin.label.dataset.hidden;
30
+ }
31
+ return undefined;
32
+ }
33
+ let frame = 0;
34
+ const place = () => {
35
+ frame = 0;
36
+ const hidden = hiddenLabels(measure(all, selectedId));
37
+ for (const [id, pin] of all) {
38
+ pin.label.toggleAttribute("data-hidden", hidden.has(id));
39
+ }
40
+ };
41
+ const soon = () => {
42
+ if (frame === 0) {
43
+ frame = requestAnimationFrame(place);
44
+ }
45
+ };
46
+ place();
47
+ map.on("move", soon);
48
+ map.on("resize", soon);
49
+ return () => {
50
+ cancelAnimationFrame(frame);
51
+ map.off("move", soon);
52
+ map.off("resize", soon);
53
+ };
54
+ }, [ready, handle, pins, labels, selectedId, markers]);
55
+ }
@@ -0,0 +1,49 @@
1
+ import type { Map as MapLibreMap } from "maplibre-gl";
2
+ import { type RefObject } from "react";
3
+ import { type GeoBounds, type LatLng } from "./geo";
4
+ import { type MapModules } from "./load-map";
5
+ import { type MapFlavor, type MapSource } from "./map-source";
6
+ /** Where the map is: still loading, drawn, or unable to draw (no WebGL2, or its files would not load). */
7
+ export type MapPhase = {
8
+ readonly phase: "loading";
9
+ } | {
10
+ readonly phase: "ready";
11
+ } | {
12
+ readonly phase: "failed";
13
+ readonly reason: "webgl" | "load";
14
+ };
15
+ export interface MapSetup {
16
+ readonly source: MapSource;
17
+ readonly center: LatLng;
18
+ readonly zoom: number;
19
+ readonly bounds: GeoBounds | undefined;
20
+ /** MapLibre's own words, such as the cooperative gesture hints, by its locale keys. */
21
+ readonly locale: Readonly<Record<string, string>> | undefined;
22
+ }
23
+ export interface MapHandle {
24
+ readonly state: MapPhase;
25
+ /** The live map once it is ready. */
26
+ readonly map: RefObject<MapLibreMap | null>;
27
+ readonly modules: RefObject<MapModules | null>;
28
+ /**
29
+ * The camera options for a move: `duration: 0` under reduced motion or the still preset, else none, so MapLibre
30
+ * keeps its own. A `duration: undefined` would replace MapLibre's default and break the move.
31
+ */
32
+ readonly cameraMotion: () => {
33
+ readonly duration?: number;
34
+ };
35
+ }
36
+ /**
37
+ * Creates a MapLibre map in `container` once its modules load, keeps it sized to its box and its basemap on the
38
+ * page's light or dark scheme, and removes it on unmount. `setup` is read when the map is made; later changes to
39
+ * centre, zoom and bounds are the caller's to apply.
40
+ */
41
+ export declare function useMap(container: RefObject<HTMLDivElement | null>, probe: RefObject<HTMLSpanElement | null>, setup: MapSetup, setFlavor: (flavor: MapFlavor) => void): MapHandle;
42
+ export interface MapBox {
43
+ readonly container: RefObject<HTMLDivElement | null>;
44
+ readonly probe: RefObject<HTMLSpanElement | null>;
45
+ readonly handle: MapHandle;
46
+ readonly flavor: MapFlavor | undefined;
47
+ }
48
+ /** The refs a map draws in, the map itself, and the flavour it draws now. */
49
+ export declare function useMapBox(setup: MapSetup): MapBox;
@@ -0,0 +1,201 @@
1
+ import { useEffect, useMemo, useRef, useState } from "react";
2
+ import { readMotion } from "../motion/flip";
3
+ import { toLngLatBounds } from "./geo";
4
+ import { loadMap } from "./load-map";
5
+ import { flavorFor, followsScheme, styleFor } from "./map-source";
6
+ function sourceKeyOf(source) {
7
+ const style = typeof source.style === "string" ? source.style : typeof source.style;
8
+ return JSON.stringify([
9
+ source.tiles,
10
+ source.glyphs,
11
+ source.sprite,
12
+ source.language,
13
+ source.attribution,
14
+ source.workerUrl,
15
+ source.flavor ?? "",
16
+ style
17
+ ]);
18
+ }
19
+ function hasWebGL2() {
20
+ try {
21
+ return document.createElement("canvas").getContext("webgl2") !== null;
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ }
27
+ /** Whether the probe resolves `light-dark()` to its dark side, which follows `color-scheme` wherever it is set. */
28
+ function isDark(probe) {
29
+ const colour = getComputedStyle(probe).color.replaceAll(" ", "");
30
+ if (colour === "rgb(255,255,255)") {
31
+ return true;
32
+ }
33
+ if (colour === "rgb(0,0,0)") {
34
+ return false;
35
+ }
36
+ return matchMedia("(prefers-color-scheme: dark)").matches;
37
+ }
38
+ function makeMap(loaded, element, setup, style) {
39
+ try {
40
+ const made = new loaded.maplibre.Map({
41
+ container: element,
42
+ style,
43
+ center: [setup.center.longitude, setup.center.latitude],
44
+ zoom: setup.zoom,
45
+ maxBounds: setup.bounds ? toLngLatBounds(setup.bounds) : undefined,
46
+ cooperativeGestures: true,
47
+ dragRotate: false,
48
+ pitchWithRotate: false,
49
+ touchPitch: false,
50
+ attributionControl: { compact: false, customAttribution: setup.source.attribution },
51
+ locale: setup.locale ? { ...setup.locale } : undefined
52
+ });
53
+ made.touchZoomRotate.disableRotation();
54
+ made.keyboard.disableRotation();
55
+ made.addControl(new loaded.maplibre.NavigationControl({ showCompass: false }), "top-right");
56
+ return made;
57
+ }
58
+ catch {
59
+ return undefined;
60
+ }
61
+ }
62
+ /** Swaps the basemap between light and dark when the scheme the map sits in changes. Returns the stop. */
63
+ function followScheme(made, scheme, source, draw, tell) {
64
+ let flavor = flavorFor(source, isDark(scheme));
65
+ tell(flavor);
66
+ const restyle = () => {
67
+ const next = flavorFor(source, isDark(scheme));
68
+ if (next !== flavor && followsScheme(source)) {
69
+ flavor = next;
70
+ tell(next);
71
+ made.setStyle(draw(next));
72
+ }
73
+ };
74
+ const media = matchMedia("(prefers-color-scheme: dark)");
75
+ media.addEventListener("change", restyle);
76
+ /* Every box from the map up to the page, since any of them can set `color-scheme`. Not the subtree: the map's
77
+ own markers change their style on every frame. */
78
+ const watcher = new MutationObserver(restyle);
79
+ for (let box = made.getContainer().parentElement; box; box = box.parentElement) {
80
+ watcher.observe(box, { attributes: true });
81
+ }
82
+ const resize = new ResizeObserver(() => {
83
+ made.resize();
84
+ });
85
+ resize.observe(made.getContainer());
86
+ return () => {
87
+ media.removeEventListener("change", restyle);
88
+ watcher.disconnect();
89
+ resize.disconnect();
90
+ };
91
+ }
92
+ /** Makes the map and reports `ready` on its first load, or `failed` when it can't draw or load. */
93
+ function startMap(loaded, element, scheme, setup, report, tell) {
94
+ const { source } = setup;
95
+ const drawn = (flavor) => styleFor(source, flavor, loaded.basemaps);
96
+ const made = makeMap(loaded, element, setup, drawn(flavorFor(source, isDark(scheme))));
97
+ if (!made) {
98
+ report({ phase: "failed", reason: "webgl" });
99
+ return { stop: () => undefined };
100
+ }
101
+ let loadedOnce = false;
102
+ made.on("error", () => {
103
+ if (!loadedOnce) {
104
+ report({ phase: "failed", reason: "load" });
105
+ }
106
+ });
107
+ made.once("load", () => {
108
+ loadedOnce = true;
109
+ report({ phase: "ready" }, made);
110
+ });
111
+ const unfollow = followScheme(made, scheme, source, drawn, tell);
112
+ return {
113
+ stop: () => {
114
+ unfollow();
115
+ made.remove();
116
+ }
117
+ };
118
+ }
119
+ /** Loads the modules, then starts the map. Returns the stop, which also drops a load that ends after it. */
120
+ function loadAndStart(element, scheme, setup, live, setState, setFlavor) {
121
+ let running = true;
122
+ let started;
123
+ setState({ phase: "loading" });
124
+ const report = (next, made) => {
125
+ if (running) {
126
+ live.map.current = made ?? null;
127
+ setState(next);
128
+ }
129
+ };
130
+ void loadMap(setup.source.workerUrl)
131
+ .then((loaded) => {
132
+ if (running) {
133
+ live.modules.current = loaded;
134
+ started = startMap(loaded, element, scheme, setup, report, (flavor) => {
135
+ if (running) {
136
+ setFlavor(flavor);
137
+ }
138
+ });
139
+ }
140
+ })
141
+ .catch(() => {
142
+ report({ phase: "failed", reason: "load" });
143
+ });
144
+ return () => {
145
+ running = false;
146
+ started?.stop();
147
+ live.map.current = null;
148
+ live.modules.current = null;
149
+ };
150
+ }
151
+ /** The source to use: kept when an app rebuilds an equal one on every render, so the map isn't made again. */
152
+ function useSteadySource(source) {
153
+ const kept = useRef(source);
154
+ if (sourceKeyOf(kept.current) !== sourceKeyOf(source)) {
155
+ kept.current = source;
156
+ }
157
+ return kept.current;
158
+ }
159
+ /**
160
+ * Creates a MapLibre map in `container` once its modules load, keeps it sized to its box and its basemap on the
161
+ * page's light or dark scheme, and removes it on unmount. `setup` is read when the map is made; later changes to
162
+ * centre, zoom and bounds are the caller's to apply.
163
+ */
164
+ export function useMap(container, probe, setup, setFlavor) {
165
+ const [state, setState] = useState({ phase: "loading" });
166
+ const map = useRef(null);
167
+ const modules = useRef(null);
168
+ const setupRef = useRef(setup);
169
+ setupRef.current = setup;
170
+ const source = useSteadySource(setup.source);
171
+ useEffect(() => {
172
+ const element = container.current;
173
+ const scheme = probe.current;
174
+ if (!element || !scheme) {
175
+ return undefined;
176
+ }
177
+ if (!hasWebGL2()) {
178
+ setState({ phase: "failed", reason: "webgl" });
179
+ return undefined;
180
+ }
181
+ return loadAndStart(element, scheme, { ...setupRef.current, source }, { map, modules }, setState, setFlavor);
182
+ }, [container, probe, source, setFlavor]);
183
+ /* One handle per phase, so effects that list it run when the map changes phase and not on every render. */
184
+ return useMemo(() => ({
185
+ state,
186
+ map,
187
+ modules,
188
+ cameraMotion: () => {
189
+ const element = container.current;
190
+ return element && readMotion(element, "move").duration === 0 ? { duration: 0 } : {};
191
+ }
192
+ }), [state, container]);
193
+ }
194
+ /** The refs a map draws in, the map itself, and the flavour it draws now. */
195
+ export function useMapBox(setup) {
196
+ const container = useRef(null);
197
+ const probe = useRef(null);
198
+ const [flavor, setFlavor] = useState();
199
+ const handle = useMap(container, probe, setup, setFlavor);
200
+ return { container, probe, handle, flavor };
201
+ }