@alulore/kumiko 0.19.1 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) 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/empty-state/empty-state-labels.d.ts +14 -0
  24. package/dist/empty-state/empty-state-labels.js +10 -0
  25. package/dist/empty-state/empty-state.css +134 -0
  26. package/dist/empty-state/empty-state.d.ts +34 -0
  27. package/dist/empty-state/empty-state.js +61 -0
  28. package/dist/empty-state/index.d.ts +2 -0
  29. package/dist/empty-state/index.js +2 -0
  30. package/dist/form/choice-fields.d.ts +34 -0
  31. package/dist/form/choice-fields.js +41 -0
  32. package/dist/form/error-line.d.ts +8 -0
  33. package/dist/form/error-line.js +47 -0
  34. package/dist/form/field-props.d.ts +12 -0
  35. package/dist/form/field-props.js +8 -0
  36. package/dist/form/field-shell.d.ts +18 -0
  37. package/dist/form/field-shell.js +51 -0
  38. package/dist/form/focus-invalid.d.ts +5 -0
  39. package/dist/form/focus-invalid.js +17 -0
  40. package/dist/form/form-context.d.ts +2 -0
  41. package/dist/form/form-context.js +3 -0
  42. package/dist/form/form-parts.d.ts +20 -0
  43. package/dist/form/form-parts.js +73 -0
  44. package/dist/form/form.css +128 -0
  45. package/dist/form/index.d.ts +10 -0
  46. package/dist/form/index.js +9 -0
  47. package/dist/form/shown-error.d.ts +12 -0
  48. package/dist/form/shown-error.js +27 -0
  49. package/dist/form/submit-errors.d.ts +36 -0
  50. package/dist/form/submit-errors.js +46 -0
  51. package/dist/form/text-fields.d.ts +18 -0
  52. package/dist/form/text-fields.js +21 -0
  53. package/dist/form/use-app-form.d.ts +1655 -0
  54. package/dist/form/use-app-form.js +94 -0
  55. package/dist/form/use-dialog-link.d.ts +5 -0
  56. package/dist/form/use-dialog-link.js +30 -0
  57. package/dist/form/value-fields.d.ts +16 -0
  58. package/dist/form/value-fields.js +15 -0
  59. package/dist/map/geo.d.ts +20 -0
  60. package/dist/map/geo.js +41 -0
  61. package/dist/map/index.d.ts +5 -0
  62. package/dist/map/index.js +4 -0
  63. package/dist/map/label-overlap.d.ts +13 -0
  64. package/dist/map/label-overlap.js +23 -0
  65. package/dist/map/load-map.d.ts +12 -0
  66. package/dist/map/load-map.js +22 -0
  67. package/dist/map/location-map.d.ts +39 -0
  68. package/dist/map/location-map.js +171 -0
  69. package/dist/map/map-frame.d.ts +18 -0
  70. package/dist/map/map-frame.js +6 -0
  71. package/dist/map/map-pin.d.ts +4 -0
  72. package/dist/map/map-pin.js +38 -0
  73. package/dist/map/map-popup.d.ts +17 -0
  74. package/dist/map/map-popup.js +101 -0
  75. package/dist/map/map-source.d.ts +52 -0
  76. package/dist/map/map-source.js +59 -0
  77. package/dist/map/map.css +311 -0
  78. package/dist/map/marker-pins.d.ts +29 -0
  79. package/dist/map/marker-pins.js +144 -0
  80. package/dist/map/markers-map.d.ts +57 -0
  81. package/dist/map/markers-map.js +154 -0
  82. package/dist/map/use-label-overlap.d.ts +9 -0
  83. package/dist/map/use-label-overlap.js +55 -0
  84. package/dist/map/use-map.d.ts +49 -0
  85. package/dist/map/use-map.js +201 -0
  86. package/dist/media/encoding.d.ts +32 -0
  87. package/dist/media/encoding.js +59 -0
  88. package/dist/media/errors.d.ts +33 -0
  89. package/dist/media/errors.js +26 -0
  90. package/dist/media/image.d.ts +9 -0
  91. package/dist/media/image.js +35 -0
  92. package/dist/media/index.d.ts +10 -0
  93. package/dist/media/index.js +10 -0
  94. package/dist/media/load-mediabunny.d.ts +7 -0
  95. package/dist/media/load-mediabunny.js +13 -0
  96. package/dist/media/media-upload.d.ts +40 -0
  97. package/dist/media/media-upload.js +132 -0
  98. package/dist/media/media.css +101 -0
  99. package/dist/media/probe.d.ts +19 -0
  100. package/dist/media/probe.js +49 -0
  101. package/dist/media/sizes.d.ts +28 -0
  102. package/dist/media/sizes.js +51 -0
  103. package/dist/media/slideshow.d.ts +43 -0
  104. package/dist/media/slideshow.js +174 -0
  105. package/dist/media/timeline.d.ts +59 -0
  106. package/dist/media/timeline.js +105 -0
  107. package/dist/media/transcode.d.ts +35 -0
  108. package/dist/media/transcode.js +132 -0
  109. package/dist/media/upload.d.ts +21 -0
  110. package/dist/media/upload.js +54 -0
  111. package/dist/media/use-media-job.d.ts +79 -0
  112. package/dist/media/use-media-job.js +143 -0
  113. package/dist/server-render.test.js +93 -0
  114. package/dist/smooth-caret/caret-controller.js +16 -3
  115. package/dist/table/table-labels.d.ts +2 -0
  116. package/dist/table/table-labels.js +1 -0
  117. package/dist/table/table-sections.js +10 -2
  118. package/dist/table/table.css +1 -0
  119. package/dist/theme/define-theme.d.ts +38 -0
  120. package/dist/theme/define-theme.js +56 -0
  121. package/dist/theme/index.d.ts +3 -0
  122. package/dist/theme/index.js +3 -0
  123. package/dist/theme/theme-style.d.ts +6 -0
  124. package/dist/theme/theme-style.js +11 -0
  125. package/dist/theme/theme-types.d.ts +1 -0
  126. package/dist/theme/theme-types.js +26 -0
  127. package/dist/theme/tokens-1.generated.d.ts +1 -0
  128. package/dist/theme/tokens-1.generated.js +404 -0
  129. package/dist/theme/tokens-2.generated.d.ts +1 -0
  130. package/dist/theme/tokens-2.generated.js +404 -0
  131. package/dist/theme/tokens-3.generated.d.ts +1 -0
  132. package/dist/theme/tokens-3.generated.js +158 -0
  133. package/dist/theme/tokens.generated.d.ts +2 -0
  134. package/dist/theme/tokens.generated.js +6 -0
  135. package/dist/undo/undo-provider.js +24 -9
  136. package/package.json +82 -5
@@ -0,0 +1,94 @@
1
+ import { createFormHook } from "@tanstack/react-form";
2
+ import { createElement, useMemo } from "react";
3
+ import { CheckboxField, CheckboxGroupField, SelectField, SwitchField } from "./choice-fields";
4
+ import { fieldContext, formContext } from "./form-context";
5
+ import { Actions, Form, FormError, SubmitButton } from "./form-parts";
6
+ import { clearServerError, clearServerErrors, failureMessage, isSubmitErrors, showServerErrors, SubmitFailedError } from "./submit-errors";
7
+ import { OtpField, TextareaField, TextField } from "./text-fields";
8
+ import { FileField, SliderField } from "./value-fields";
9
+ const formComponents = { Form, FormError, Actions, SubmitButton };
10
+ const hook = createFormHook({
11
+ fieldContext,
12
+ formContext,
13
+ fieldComponents: {
14
+ TextField,
15
+ TextareaField,
16
+ SelectField,
17
+ CheckboxField,
18
+ CheckboxGroupField,
19
+ SwitchField,
20
+ SliderField,
21
+ OtpField,
22
+ FileField
23
+ },
24
+ formComponents
25
+ });
26
+ export const { withForm } = hook;
27
+ /** A component that has the form in context, so its parts need no `form.AppForm` around them. */
28
+ function inForm(form, Part) {
29
+ return function InForm(props) {
30
+ return createElement(formContext.Provider, { value: form }, createElement(Part, props));
31
+ };
32
+ }
33
+ /** `handleSubmit()` without the rejection of a submit that already put its failure on show. */
34
+ function quietSubmit(form) {
35
+ const submit = form.handleSubmit.bind(form);
36
+ form.handleSubmit = async (meta) => {
37
+ try {
38
+ clearServerErrors(form);
39
+ await form.validateAllFields("change");
40
+ await submit(meta);
41
+ }
42
+ catch (error) {
43
+ if (!(error instanceof SubmitFailedError)) {
44
+ throw error;
45
+ }
46
+ }
47
+ };
48
+ }
49
+ const quieted = new WeakSet();
50
+ /**
51
+ * TanStack Form's `useAppForm` with Kumiko's fields and parts, which hang off the form as `form.Form`,
52
+ * `form.AppField` and the rest. `onSubmit` may return `submitErrors({ form, fields })`, or throw: the form shows
53
+ * the failure, keeps its values, and a field's message clears when that field changes.
54
+ */
55
+ export const useAppForm = (options) => {
56
+ const form = hook.useAppForm({
57
+ ...options,
58
+ listeners: {
59
+ ...options.listeners,
60
+ onChange: (props) => {
61
+ clearServerError(props.formApi, String(props.fieldApi.name));
62
+ options.listeners?.onChange?.(props);
63
+ }
64
+ },
65
+ onSubmit: async (props) => {
66
+ try {
67
+ const result = await options.onSubmit?.(props);
68
+ if (!isSubmitErrors(result)) {
69
+ return;
70
+ }
71
+ showServerErrors(props.formApi, result);
72
+ }
73
+ catch (error) {
74
+ showServerErrors(props.formApi, {
75
+ kumikoSubmitErrors: true,
76
+ form: failureMessage(error)
77
+ });
78
+ }
79
+ throw new SubmitFailedError();
80
+ }
81
+ });
82
+ return useMemo(() => {
83
+ if (!quieted.has(form)) {
84
+ quieted.add(form);
85
+ quietSubmit(form);
86
+ }
87
+ return Object.assign(form, {
88
+ Form: inForm(form, Form),
89
+ FormError: inForm(form, FormError),
90
+ Actions: inForm(form, Actions),
91
+ SubmitButton: inForm(form, SubmitButton)
92
+ });
93
+ }, [form]);
94
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Links the form to the dialog it is in, if any: the dialog is pending while the form submits, so it can't be
3
+ * dismissed, and dirty while the form is edited, so a dismiss asks first. Both are let go when the form goes.
4
+ */
5
+ export declare function useDialogLink(enabled: boolean): void;
@@ -0,0 +1,30 @@
1
+ import { useSelector } from "@tanstack/react-form";
2
+ import { useEffect, useRef } from "react";
3
+ import { useOptionalDialogContent } from "../dialog/dialog-content";
4
+ import { useFormContext } from "./form-context";
5
+ /**
6
+ * Links the form to the dialog it is in, if any: the dialog is pending while the form submits, so it can't be
7
+ * dismissed, and dirty while the form is edited, so a dismiss asks first. Both are let go when the form goes.
8
+ */
9
+ export function useDialogLink(enabled) {
10
+ const form = useFormContext();
11
+ const dialog = useOptionalDialogContent();
12
+ const submitting = useSelector(form.store, (state) => state.isSubmitting);
13
+ const dirty = useSelector(form.store, (state) => state.isDirty);
14
+ // The dialog's api changes with every pending flip, so its setters are read through a ref.
15
+ const api = useRef(null);
16
+ const linked = enabled && dialog !== null;
17
+ useEffect(() => {
18
+ api.current = linked ? dialog : null;
19
+ });
20
+ useEffect(() => {
21
+ api.current?.setPending(submitting);
22
+ }, [submitting, linked]);
23
+ useEffect(() => {
24
+ api.current?.setDirty(dirty);
25
+ }, [dirty, linked]);
26
+ useEffect(() => () => {
27
+ api.current?.setPending(false);
28
+ api.current?.setDirty(false);
29
+ }, []);
30
+ }
@@ -0,0 +1,16 @@
1
+ import { type FileDropzoneProps } from "../file-input";
2
+ import { type SliderProps } from "../slider";
3
+ import { type FieldProps } from "./field-props";
4
+ type Controlled = "value" | "defaultValue" | "onValueChange" | "name" | "required" | "disabled";
5
+ export type SliderFieldProps = Omit<FieldProps, "required"> & Omit<SliderProps, Controlled>;
6
+ /** A slider, bound to a form field whose value is a `number`, or a `number[]` for a range. */
7
+ export declare function SliderField({ label, description, disabled, thumbLabel, ...slider }: SliderFieldProps): React.JSX.Element;
8
+ export type FileFieldProps = FieldProps & Omit<FileDropzoneProps, Controlled | "multiple" | "accept"> & {
9
+ /** What it takes, as an `<input accept>` list like `"image/*"`. */
10
+ accept?: string;
11
+ /** @defaultValue false */
12
+ multiple?: boolean;
13
+ };
14
+ /** Files to send, bound to a form field whose value is a `File[]`. */
15
+ export declare function FileField({ label, description, required, disabled, ...files }: FileFieldProps): React.JSX.Element;
16
+ export {};
@@ -0,0 +1,15 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { FileDropzone } from "../file-input";
3
+ import { Slider } from "../slider";
4
+ import { useFieldValue } from "./field-props";
5
+ import { FieldShell } from "./field-shell";
6
+ /** A slider, bound to a form field whose value is a `number`, or a `number[]` for a range. */
7
+ export function SliderField({ label, description, disabled, thumbLabel, ...slider }) {
8
+ const [value, setValue] = useFieldValue();
9
+ return (_jsx(FieldShell, { label: label, description: description, disabled: disabled, children: _jsx(Slider, { ...slider, disabled: disabled, thumbLabel: thumbLabel ?? (typeof label === "string" ? label : undefined), value: value, onValueChange: setValue }) }));
10
+ }
11
+ /** Files to send, bound to a form field whose value is a `File[]`. */
12
+ export function FileField({ label, description, required, disabled, ...files }) {
13
+ const [value, setValue] = useFieldValue();
14
+ return (_jsx(FieldShell, { label: label, description: description, disabled: disabled, children: _jsx(FileDropzone, { ...files, required: required, value: value, onValueChange: (next) => setValue([...next]) }) }));
15
+ }
@@ -0,0 +1,20 @@
1
+ /** A point on the earth, in degrees. */
2
+ export interface LatLng {
3
+ readonly latitude: number;
4
+ readonly longitude: number;
5
+ }
6
+ /** A box on the map, in degrees. */
7
+ export interface GeoBounds {
8
+ readonly minLatitude: number;
9
+ readonly maxLatitude: number;
10
+ readonly minLongitude: number;
11
+ readonly maxLongitude: number;
12
+ }
13
+ /** Whether the point is a real coordinate inside the box, edges included. */
14
+ export declare function isInsideBounds(point: LatLng, bounds: GeoBounds): boolean;
15
+ /** The nearest point inside the box, or the point itself when there is no box. */
16
+ export declare function clampToBounds(point: LatLng, bounds: GeoBounds | undefined): LatLng;
17
+ /** The smallest box holding every point, or undefined when there are none. */
18
+ export declare function boundsOf(points: readonly LatLng[]): GeoBounds | undefined;
19
+ /** MapLibre's `[[west, south], [east, north]]`. */
20
+ export declare function toLngLatBounds(bounds: GeoBounds): [[west: number, south: number], [east: number, north: number]];
@@ -0,0 +1,41 @@
1
+ /** Whether the point is a real coordinate inside the box, edges included. */
2
+ export function isInsideBounds(point, bounds) {
3
+ return (Number.isFinite(point.latitude) &&
4
+ Number.isFinite(point.longitude) &&
5
+ point.latitude >= bounds.minLatitude &&
6
+ point.latitude <= bounds.maxLatitude &&
7
+ point.longitude >= bounds.minLongitude &&
8
+ point.longitude <= bounds.maxLongitude);
9
+ }
10
+ /** The nearest point inside the box, or the point itself when there is no box. */
11
+ export function clampToBounds(point, bounds) {
12
+ if (!bounds) {
13
+ return point;
14
+ }
15
+ return {
16
+ latitude: Math.min(bounds.maxLatitude, Math.max(bounds.minLatitude, point.latitude)),
17
+ longitude: Math.min(bounds.maxLongitude, Math.max(bounds.minLongitude, point.longitude))
18
+ };
19
+ }
20
+ /** The smallest box holding every point, or undefined when there are none. */
21
+ export function boundsOf(points) {
22
+ const real = points.filter((point) => Number.isFinite(point.latitude) && Number.isFinite(point.longitude));
23
+ if (real.length === 0) {
24
+ return undefined;
25
+ }
26
+ const latitudes = real.map((point) => point.latitude);
27
+ const longitudes = real.map((point) => point.longitude);
28
+ return {
29
+ minLatitude: Math.min(...latitudes),
30
+ maxLatitude: Math.max(...latitudes),
31
+ minLongitude: Math.min(...longitudes),
32
+ maxLongitude: Math.max(...longitudes)
33
+ };
34
+ }
35
+ /** MapLibre's `[[west, south], [east, north]]`. */
36
+ export function toLngLatBounds(bounds) {
37
+ return [
38
+ [bounds.minLongitude, bounds.minLatitude],
39
+ [bounds.maxLongitude, bounds.maxLatitude]
40
+ ];
41
+ }
@@ -0,0 +1,5 @@
1
+ export { boundsOf, clampToBounds, isInsideBounds, type GeoBounds, type LatLng } from "./geo";
2
+ export { LocationMap, type LocationMapLabels, type LocationMapProps } from "./location-map";
3
+ export { basemapStyle, defineMapSource, tilesUrl, type MapFlavor, type MapStyle, type MapSource, type MapSourceInput } from "./map-source";
4
+ export { MarkersMap, type MapMarker, type MarkersMapProps, type PinLabels } from "./markers-map";
5
+ export type { MapPhase } from "./use-map";
@@ -0,0 +1,4 @@
1
+ export { boundsOf, clampToBounds, isInsideBounds } from "./geo";
2
+ export { LocationMap } from "./location-map";
3
+ export { basemapStyle, defineMapSource, tilesUrl } from "./map-source";
4
+ export { MarkersMap } from "./markers-map";
@@ -0,0 +1,13 @@
1
+ /** A label's box on screen. */
2
+ export interface LabelBox {
3
+ readonly id: string;
4
+ readonly left: number;
5
+ readonly top: number;
6
+ readonly right: number;
7
+ readonly bottom: number;
8
+ }
9
+ /**
10
+ * The labels to hide so none overlap. Boxes come in priority order (the selected pin first, then the list's
11
+ * order), and a box is kept only when it clears every box kept before it.
12
+ */
13
+ export declare function hiddenLabels(boxes: readonly LabelBox[], gap?: number): Set<string>;
@@ -0,0 +1,23 @@
1
+ function overlaps(a, b, gap) {
2
+ return (a.left < b.right + gap &&
3
+ b.left < a.right + gap &&
4
+ a.top < b.bottom + gap &&
5
+ b.top < a.bottom + gap);
6
+ }
7
+ /**
8
+ * The labels to hide so none overlap. Boxes come in priority order (the selected pin first, then the list's
9
+ * order), and a box is kept only when it clears every box kept before it.
10
+ */
11
+ export function hiddenLabels(boxes, gap = 2) {
12
+ const kept = [];
13
+ const hidden = new Set();
14
+ for (const box of boxes) {
15
+ if (kept.some((other) => overlaps(box, other, gap))) {
16
+ hidden.add(box.id);
17
+ }
18
+ else {
19
+ kept.push(box);
20
+ }
21
+ }
22
+ return hidden;
23
+ }
@@ -0,0 +1,12 @@
1
+ import type * as MapLibreModule from "maplibre-gl";
2
+ import type { Basemaps } from "./map-source";
3
+ export type MapLibre = typeof MapLibreModule;
4
+ export interface MapModules {
5
+ readonly maplibre: MapLibre;
6
+ readonly basemaps: Basemaps;
7
+ }
8
+ /**
9
+ * Loads MapLibre, PMTiles and the basemap styles once, in the browser only, and sets MapLibre's worker and
10
+ * the `pmtiles://` protocol up for every map on the page. A failed load is forgotten, so the next map tries again.
11
+ */
12
+ export declare function loadMap(workerUrl: string): Promise<MapModules>;
@@ -0,0 +1,22 @@
1
+ let loading;
2
+ /**
3
+ * Loads MapLibre, PMTiles and the basemap styles once, in the browser only, and sets MapLibre's worker and
4
+ * the `pmtiles://` protocol up for every map on the page. A failed load is forgotten, so the next map tries again.
5
+ */
6
+ export function loadMap(workerUrl) {
7
+ loading ??= (async () => {
8
+ const [maplibre, pmtiles, basemaps] = await Promise.all([
9
+ import("maplibre-gl"),
10
+ import("pmtiles"),
11
+ import("@protomaps/basemaps")
12
+ ]);
13
+ maplibre.setWorkerUrl(workerUrl);
14
+ const protocol = new pmtiles.Protocol();
15
+ maplibre.addProtocol("pmtiles", protocol.tile);
16
+ return { maplibre, basemaps };
17
+ })().catch((error) => {
18
+ loading = undefined;
19
+ throw error;
20
+ });
21
+ return loading;
22
+ }
@@ -0,0 +1,39 @@
1
+ import { type ReactNode } from "react";
2
+ import { type GeoBounds, type LatLng } from "./geo";
3
+ import type { MapSource } from "./map-source";
4
+ export interface LocationMapLabels {
5
+ /** The name of a pin that can be moved. */
6
+ readonly pin: string;
7
+ /** The name of a read-only pin. */
8
+ readonly place: string;
9
+ }
10
+ export interface LocationMapProps {
11
+ /** The tiles and styles, from `defineMapSource`. */
12
+ readonly source: MapSource;
13
+ /** The map region's accessible name. */
14
+ readonly label: string;
15
+ /** The pin, or null for none. */
16
+ readonly value: LatLng | null;
17
+ /** Makes the pin movable by a tap or click on the map, a drag, or the arrow keys. Omit for a read-only map. */
18
+ readonly onChange?: (value: LatLng) => void;
19
+ /** The pin stays inside this box, and the map can't be panned out of it. */
20
+ readonly bounds?: GeoBounds;
21
+ /** Where the map opens when there is no pin. @defaultValue the middle of `bounds`, else 0, 0 */
22
+ readonly center?: LatLng;
23
+ /** @defaultValue 13 */
24
+ readonly zoom?: number;
25
+ /** Shown while the map loads. */
26
+ readonly loading?: ReactNode;
27
+ /** Shown instead of the map when it can't draw, such as without WebGL2. */
28
+ readonly fallback?: ReactNode;
29
+ readonly labels?: Partial<LocationMapLabels>;
30
+ /** MapLibre's own words, such as `CooperativeGesturesHandler.WindowsHelpText`. */
31
+ readonly locale?: Readonly<Record<string, string>>;
32
+ readonly className?: string;
33
+ }
34
+ /**
35
+ * A map with one pin. With `onChange`, a tap or click puts the pin there, the pin can be dragged, and a focused
36
+ * pin moves with the arrow keys (further with Shift). Enter on the map itself puts the pin in the middle, so a
37
+ * keyboard can place the first one. Every move is clamped to `bounds`.
38
+ */
39
+ export declare function LocationMap(props: LocationMapProps): React.JSX.Element;
@@ -0,0 +1,171 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useEffect, useRef } from "react";
3
+ import { clampToBounds } from "./geo";
4
+ import { MapFrame } from "./map-frame";
5
+ import { pinButton, pinPicture } from "./map-pin";
6
+ import { useMapBox } from "./use-map";
7
+ const defaultLabels = {
8
+ pin: "Pin. Use the arrow keys to move it.",
9
+ place: "Pin"
10
+ };
11
+ const NUDGE = 4;
12
+ const NUDGE_FAR = 32;
13
+ const arrows = {
14
+ ArrowLeft: [-1, 0],
15
+ ArrowRight: [1, 0],
16
+ ArrowUp: [0, -1],
17
+ ArrowDown: [0, 1]
18
+ };
19
+ function middleOf(bounds) {
20
+ if (!bounds) {
21
+ return { latitude: 0, longitude: 0 };
22
+ }
23
+ return {
24
+ latitude: (bounds.minLatitude + bounds.maxLatitude) / 2,
25
+ longitude: (bounds.minLongitude + bounds.maxLongitude) / 2
26
+ };
27
+ }
28
+ function isShown(map, point) {
29
+ return map.getBounds().contains([point.longitude, point.latitude]);
30
+ }
31
+ /** A tap or click on the map puts the pin there, and Enter on the focused map puts it in the middle. */
32
+ function usePlaceByMap(handle, latest) {
33
+ const ready = handle.state.phase === "ready";
34
+ useEffect(() => {
35
+ const map = handle.map.current;
36
+ if (!ready || !map) {
37
+ return undefined;
38
+ }
39
+ const place = (point) => {
40
+ latest.current.onChange?.(clampToBounds(point, latest.current.bounds));
41
+ };
42
+ const onClick = (event) => {
43
+ place({ latitude: event.lngLat.lat, longitude: event.lngLat.lng });
44
+ };
45
+ const canvas = map.getCanvas();
46
+ const onKey = (event) => {
47
+ if (event.key === "Enter" && latest.current.onChange) {
48
+ event.preventDefault();
49
+ const middle = map.getCenter();
50
+ place({ latitude: middle.lat, longitude: middle.lng });
51
+ }
52
+ };
53
+ map.on("click", onClick);
54
+ canvas.addEventListener("keydown", onKey);
55
+ return () => {
56
+ map.off("click", onClick);
57
+ canvas.removeEventListener("keydown", onKey);
58
+ };
59
+ }, [ready, handle.map, latest]);
60
+ }
61
+ /** Moves the marker by arrow keys, a few screen pixels at a time, as the pin's own keys. */
62
+ function nudgeOnArrows(element, map, marker, report) {
63
+ element.addEventListener("keydown", (event) => {
64
+ const step = arrows[event.key];
65
+ if (!step) {
66
+ return;
67
+ }
68
+ event.preventDefault();
69
+ event.stopPropagation();
70
+ const distance = event.shiftKey ? NUDGE_FAR : NUDGE;
71
+ const at = map.project(marker.getLngLat());
72
+ const next = map.unproject([at.x + step[0] * distance, at.y + step[1] * distance]);
73
+ report({ latitude: next.lat, longitude: next.lng });
74
+ });
75
+ }
76
+ /** The pin: made again when it turns movable or read-only, or its name changes. */
77
+ function usePin(handle, latest, editable, name) {
78
+ const marker = useRef(null);
79
+ const shown = useRef(false);
80
+ const ready = handle.state.phase === "ready";
81
+ useEffect(() => {
82
+ const map = handle.map.current;
83
+ const loaded = handle.modules.current;
84
+ if (!ready || !map || !loaded) {
85
+ return undefined;
86
+ }
87
+ const element = editable ? pinButton(name) : pinPicture(name);
88
+ const made = new loaded.maplibre.Marker({
89
+ element,
90
+ anchor: "bottom",
91
+ draggable: editable
92
+ });
93
+ const report = (point) => {
94
+ const clamped = clampToBounds(point, latest.current.bounds);
95
+ made.setLngLat([clamped.longitude, clamped.latitude]);
96
+ latest.current.onChange?.(clamped);
97
+ };
98
+ made.on("dragend", () => {
99
+ const at = made.getLngLat();
100
+ report({ latitude: at.lat, longitude: at.lng });
101
+ });
102
+ if (editable) {
103
+ nudgeOnArrows(element, map, made, report);
104
+ }
105
+ marker.current = made;
106
+ const { value } = latest.current;
107
+ shown.current = value !== null;
108
+ if (value) {
109
+ made.setLngLat([value.longitude, value.latitude]).addTo(map);
110
+ }
111
+ return () => {
112
+ made.remove();
113
+ marker.current = null;
114
+ shown.current = false;
115
+ };
116
+ }, [ready, editable, name, handle.map, handle.modules, latest]);
117
+ return { marker, shown };
118
+ }
119
+ /** A pin moved from outside shows where it is, and the camera follows it when it is off screen. */
120
+ function useFollowValue(handle, pin, value, remade) {
121
+ const ready = handle.state.phase === "ready";
122
+ const latitude = value?.latitude;
123
+ const longitude = value?.longitude;
124
+ useEffect(() => {
125
+ const map = handle.map.current;
126
+ const marker = pin.marker.current;
127
+ if (!ready || !map || !marker) {
128
+ return;
129
+ }
130
+ if (latitude === undefined || longitude === undefined) {
131
+ marker.remove();
132
+ pin.shown.current = false;
133
+ return;
134
+ }
135
+ marker.setLngLat([longitude, latitude]);
136
+ if (!pin.shown.current) {
137
+ marker.addTo(map);
138
+ pin.shown.current = true;
139
+ }
140
+ if (!isShown(map, { latitude, longitude })) {
141
+ map.easeTo({
142
+ center: [longitude, latitude],
143
+ ...handle.cameraMotion()
144
+ });
145
+ }
146
+ }, [ready, latitude, longitude, remade, handle, pin]);
147
+ }
148
+ /**
149
+ * A map with one pin. With `onChange`, a tap or click puts the pin there, the pin can be dragged, and a focused
150
+ * pin moves with the arrow keys (further with Shift). Enter on the map itself puts the pin in the middle, so a
151
+ * keyboard can place the first one. Every move is clamped to `bounds`.
152
+ */
153
+ export function LocationMap(props) {
154
+ const latest = useRef(props);
155
+ latest.current = props;
156
+ const words = { ...defaultLabels, ...props.labels };
157
+ const editable = props.onChange !== undefined;
158
+ const name = editable ? words.pin : words.place;
159
+ const box = useMapBox({
160
+ source: props.source,
161
+ center: props.value ?? props.center ?? middleOf(props.bounds),
162
+ zoom: props.zoom ?? 13,
163
+ bounds: props.bounds,
164
+ locale: props.locale
165
+ });
166
+ const { handle } = box;
167
+ usePlaceByMap(handle, latest);
168
+ const pin = usePin(handle, latest, editable, name);
169
+ useFollowValue(handle, pin, props.value, `${String(editable)}:${name}`);
170
+ return (_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 }));
171
+ }
@@ -0,0 +1,18 @@
1
+ import type { ReactNode, RefObject } from "react";
2
+ import type { MapFlavor } from "./map-source";
3
+ import type { MapPhase } from "./use-map";
4
+ export interface MapFrameProps {
5
+ readonly label: string;
6
+ readonly state: MapPhase;
7
+ readonly container: RefObject<HTMLDivElement | null>;
8
+ readonly probe: RefObject<HTMLSpanElement | null>;
9
+ readonly loading: ReactNode;
10
+ readonly fallback: ReactNode;
11
+ readonly className: string | undefined;
12
+ /** Which pins show their name, for the stylesheet. */
13
+ readonly labels?: "none" | "selected" | "always";
14
+ /** The basemap flavour drawn now, for an app that styles round the map. */
15
+ readonly flavor: MapFlavor | undefined;
16
+ }
17
+ /** The box both maps draw in: the named region, the scheme probe, and the loading or fallback slot over it. */
18
+ export declare function MapFrame(props: MapFrameProps): React.JSX.Element;
@@ -0,0 +1,6 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /** The box both maps draw in: the named region, the scheme probe, and the loading or fallback slot over it. */
3
+ export function MapFrame(props) {
4
+ const { state } = props;
5
+ return (_jsxs("div", { "data-slot": "map", "data-state": state.phase, "data-labels": props.labels, "data-flavor": props.flavor, className: props.className, children: [_jsx("div", { ref: props.container, "data-slot": "map-canvas", role: "region", "aria-label": props.label, "aria-busy": state.phase === "loading" ? true : undefined }), _jsx("span", { ref: props.probe, "data-slot": "map-scheme", "aria-hidden": "true" }), state.phase === "loading" && props.loading !== undefined ? (_jsx("div", { "data-slot": "map-loading", children: props.loading })) : null, state.phase === "failed" ? (_jsx("div", { "data-slot": "map-fallback", "data-reason": state.reason, children: props.fallback })) : null] }));
6
+ }
@@ -0,0 +1,4 @@
1
+ /** A pin a person can press or move: a real button, so it takes focus and has a name. */
2
+ export declare function pinButton(name: string): HTMLButtonElement;
3
+ /** A pin that only shows a place. */
4
+ export declare function pinPicture(name: string): HTMLDivElement;
@@ -0,0 +1,38 @@
1
+ const SVG_NS = "http://www.w3.org/2000/svg";
2
+ /** The pin's drawing. Its colours come from the map tokens through CSS. */
3
+ function drawing() {
4
+ const svg = document.createElementNS(SVG_NS, "svg");
5
+ svg.setAttribute("viewBox", "0 0 36 44");
6
+ svg.setAttribute("aria-hidden", "true");
7
+ svg.setAttribute("focusable", "false");
8
+ svg.dataset.slot = "map-pin-art";
9
+ const body = document.createElementNS(SVG_NS, "path");
10
+ body.setAttribute("d", "M18 43c-1.5 0-14-14.2-14-24C4 9.6 10.3 3 18 3s14 6.6 14 16c0 9.8-12.5 24-14 24z");
11
+ body.dataset.part = "body";
12
+ const dot = document.createElementNS(SVG_NS, "circle");
13
+ dot.setAttribute("cx", "18");
14
+ dot.setAttribute("cy", "18.5");
15
+ dot.setAttribute("r", "5.5");
16
+ dot.dataset.part = "dot";
17
+ svg.append(body, dot);
18
+ return svg;
19
+ }
20
+ /** A pin a person can press or move: a real button, so it takes focus and has a name. */
21
+ export function pinButton(name) {
22
+ const button = document.createElement("button");
23
+ button.type = "button";
24
+ button.dataset.slot = "map-pin";
25
+ button.setAttribute("aria-label", name);
26
+ button.append(drawing());
27
+ return button;
28
+ }
29
+ /** A pin that only shows a place. */
30
+ export function pinPicture(name) {
31
+ const picture = document.createElement("div");
32
+ picture.dataset.slot = "map-pin";
33
+ picture.dataset.readOnly = "";
34
+ picture.setAttribute("role", "img");
35
+ picture.setAttribute("aria-label", name);
36
+ picture.append(drawing());
37
+ return picture;
38
+ }
@@ -0,0 +1,17 @@
1
+ import { type RefObject } from "react";
2
+ import type { Pin } from "./marker-pins";
3
+ import type { MapHandle } from "./use-map";
4
+ export interface MapPopup {
5
+ /** Where the app's content is rendered, through a portal, once the map is ready. */
6
+ readonly element: HTMLDivElement | null;
7
+ /** Shows the popup over a pin, or moves it there. */
8
+ readonly open: (pin: Pin) => void;
9
+ /** Hides it without reporting a close. */
10
+ readonly close: () => void;
11
+ }
12
+ /**
13
+ * One MapLibre popup whose content the component renders. A card closes on a click on the map, which is
14
+ * reported through `onClose`: MapLibre's own close-on-click is off, because a press on a pin is a click on the
15
+ * map too and would close the card it just opened. A preview never takes the pointer.
16
+ */
17
+ export declare function useMapPopup(handle: MapHandle, kind: "card" | "preview", onClose: RefObject<() => void>): MapPopup;