@mapcairn/map 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +167 -2
- package/dist/chrome/Attribution/Attribution.js +304 -0
- package/dist/chrome/Attribution/Attribution.js.map +1 -0
- package/dist/chrome/Attribution/Attribution.styles.js +10 -0
- package/dist/chrome/Attribution/Attribution.styles.js.map +1 -0
- package/dist/chrome/Attribution/AttributionSkeleton.js +29 -0
- package/dist/chrome/Attribution/AttributionSkeleton.js.map +1 -0
- package/dist/chrome/ChromeSkeleton/ChromeSkeleton.js +83 -0
- package/dist/chrome/ChromeSkeleton/ChromeSkeleton.js.map +1 -0
- package/dist/chrome/EndStack/EndStack.js +157 -0
- package/dist/chrome/EndStack/EndStack.js.map +1 -0
- package/dist/chrome/EndStack/EndStack.styles.js +12 -0
- package/dist/chrome/EndStack/EndStack.styles.js.map +1 -0
- package/dist/chrome/EndStack/EndStackSkeleton.js +78 -0
- package/dist/chrome/EndStack/EndStackSkeleton.js.map +1 -0
- package/dist/chrome/EndStack/endStackHeight.js +22 -0
- package/dist/chrome/EndStack/endStackHeight.js.map +1 -0
- package/dist/chrome/MapChrome/MapChrome.js +488 -0
- package/dist/chrome/MapChrome/MapChrome.js.map +1 -0
- package/dist/chrome/MapChrome/MapChrome.styles.js +15 -0
- package/dist/chrome/MapChrome/MapChrome.styles.js.map +1 -0
- package/dist/chrome/MapChrome/chromeSurface.js +18 -0
- package/dist/chrome/MapChrome/chromeSurface.js.map +1 -0
- package/dist/chrome/MapChrome/chromeVars.js +71 -0
- package/dist/chrome/MapChrome/chromeVars.js.map +1 -0
- package/dist/chrome/MapChrome/mapKeyPlacement.js +41 -0
- package/dist/chrome/MapChrome/mapKeyPlacement.js.map +1 -0
- package/dist/chrome/MapChrome/sharesWidth.js +13 -0
- package/dist/chrome/MapChrome/sharesWidth.js.map +1 -0
- package/dist/chrome/MapReadout/MapReadout.js +199 -0
- package/dist/chrome/MapReadout/MapReadout.js.map +1 -0
- package/dist/chrome/MapReadout/MapReadout.styles.js +14 -0
- package/dist/chrome/MapReadout/MapReadout.styles.js.map +1 -0
- package/dist/chrome/Sheet/Sheet.js +412 -0
- package/dist/chrome/Sheet/Sheet.js.map +1 -0
- package/dist/chrome/Sheet/Sheet.styles.js +14 -0
- package/dist/chrome/Sheet/Sheet.styles.js.map +1 -0
- package/dist/chrome/focusRegistry.js +76 -0
- package/dist/chrome/focusRegistry.js.map +1 -0
- package/dist/chrome/layoutContext.js +30 -0
- package/dist/chrome/layoutContext.js.map +1 -0
- package/dist/chrome/useScrollEdges.js +53 -0
- package/dist/chrome/useScrollEdges.js.map +1 -0
- package/dist/host/MapContext.js +16 -0
- package/dist/host/MapContext.js.map +1 -0
- package/dist/host/MapView.js +189 -0
- package/dist/host/MapView.js.map +1 -0
- package/dist/host/MapView.styles.js +16 -0
- package/dist/host/MapView.styles.js.map +1 -0
- package/dist/host/overlays/AccuracyCircle.js +36 -0
- package/dist/host/overlays/AccuracyCircle.js.map +1 -0
- package/dist/host/overlays/CentreCrosshair.js +40 -0
- package/dist/host/overlays/CentreCrosshair.js.map +1 -0
- package/dist/host/overlays/DrawOverlay.js +221 -0
- package/dist/host/overlays/DrawOverlay.js.map +1 -0
- package/dist/host/overlays/MapOverlayLayer.js +27 -0
- package/dist/host/overlays/MapOverlayLayer.js.map +1 -0
- package/dist/host/overlays/MeasureOverlay.js +87 -0
- package/dist/host/overlays/MeasureOverlay.js.map +1 -0
- package/dist/host/overlays/PinMarker.js +40 -0
- package/dist/host/overlays/PinMarker.js.map +1 -0
- package/dist/host/overlays/geometry.js +41 -0
- package/dist/host/overlays/geometry.js.map +1 -0
- package/dist/host/overlays/overlays.styles.js +27 -0
- package/dist/host/overlays/overlays.styles.js.map +1 -0
- package/dist/host/pointerTargets.js +22 -0
- package/dist/host/pointerTargets.js.map +1 -0
- package/dist/host/useLongPress.js +102 -0
- package/dist/host/useLongPress.js.map +1 -0
- package/dist/host/useMapEngine.js +352 -0
- package/dist/host/useMapEngine.js.map +1 -0
- package/dist/host/usePointerPosition.js +60 -0
- package/dist/host/usePointerPosition.js.map +1 -0
- package/dist/index.d.ts +3845 -0
- package/dist/index.js +89 -0
- package/dist/layers/CatalogueStatus.js +138 -0
- package/dist/layers/CatalogueStatus.js.map +1 -0
- package/dist/layers/DateTimeControl.js +101 -0
- package/dist/layers/DateTimeControl.js.map +1 -0
- package/dist/layers/FeatureCard.js +147 -0
- package/dist/layers/FeatureCard.js.map +1 -0
- package/dist/layers/FeatureChoices.js +101 -0
- package/dist/layers/FeatureChoices.js.map +1 -0
- package/dist/layers/FeatureGlyph.js +56 -0
- package/dist/layers/FeatureGlyph.js.map +1 -0
- package/dist/layers/IdentifyStatus.js +36 -0
- package/dist/layers/IdentifyStatus.js.map +1 -0
- package/dist/layers/LayerItem.js +321 -0
- package/dist/layers/LayerItem.js.map +1 -0
- package/dist/layers/LayerKey.js +36 -0
- package/dist/layers/LayerKey.js.map +1 -0
- package/dist/layers/Layers.styles.js +50 -0
- package/dist/layers/Layers.styles.js.map +1 -0
- package/dist/layers/LayersSection.js +267 -0
- package/dist/layers/LayersSection.js.map +1 -0
- package/dist/layers/MapDataLayers.js +261 -0
- package/dist/layers/MapDataLayers.js.map +1 -0
- package/dist/layers/MapKey.js +131 -0
- package/dist/layers/MapKey.js.map +1 -0
- package/dist/layers/MapKey.styles.js +16 -0
- package/dist/layers/MapKey.styles.js.map +1 -0
- package/dist/layers/MarkerGlyph.js +84 -0
- package/dist/layers/MarkerGlyph.js.map +1 -0
- package/dist/layers/SelectionProvider.js +27 -0
- package/dist/layers/SelectionProvider.js.map +1 -0
- package/dist/layers/ShapeGlyph.js +122 -0
- package/dist/layers/ShapeGlyph.js.map +1 -0
- package/dist/layers/TurnOffActions.js +147 -0
- package/dist/layers/TurnOffActions.js.map +1 -0
- package/dist/layers/answersTap.js +16 -0
- package/dist/layers/answersTap.js.map +1 -0
- package/dist/layers/catalogueNotice.js +65 -0
- package/dist/layers/catalogueNotice.js.map +1 -0
- package/dist/layers/catalogueReconcile.js +55 -0
- package/dist/layers/catalogueReconcile.js.map +1 -0
- package/dist/layers/catalogueView.js +32 -0
- package/dist/layers/catalogueView.js.map +1 -0
- package/dist/layers/featureTitle.js +14 -0
- package/dist/layers/featureTitle.js.map +1 -0
- package/dist/layers/identifyFeedback.js +30 -0
- package/dist/layers/identifyFeedback.js.map +1 -0
- package/dist/layers/identifyRun.js +148 -0
- package/dist/layers/identifyRun.js.map +1 -0
- package/dist/layers/layerStatus.js +13 -0
- package/dist/layers/layerStatus.js.map +1 -0
- package/dist/layers/loadOutcome.js +107 -0
- package/dist/layers/loadOutcome.js.map +1 -0
- package/dist/layers/loadReport.js +33 -0
- package/dist/layers/loadReport.js.map +1 -0
- package/dist/layers/loadRequests.js +73 -0
- package/dist/layers/loadRequests.js.map +1 -0
- package/dist/layers/mapFeatures.js +307 -0
- package/dist/layers/mapFeatures.js.map +1 -0
- package/dist/layers/mapKeyBlocks.js +24 -0
- package/dist/layers/mapKeyBlocks.js.map +1 -0
- package/dist/layers/reveal.js +119 -0
- package/dist/layers/reveal.js.map +1 -0
- package/dist/layers/selectionMessages.js +53 -0
- package/dist/layers/selectionMessages.js.map +1 -0
- package/dist/layers/selectionResolve.js +111 -0
- package/dist/layers/selectionResolve.js.map +1 -0
- package/dist/layers/useDataLayers.js +940 -0
- package/dist/layers/useDataLayers.js.map +1 -0
- package/dist/layers/useFeatureSelection.js +179 -0
- package/dist/layers/useFeatureSelection.js.map +1 -0
- package/dist/layers/useScrolls.js +37 -0
- package/dist/layers/useScrolls.js.map +1 -0
- package/dist/panels/BasemapPicker/BasemapPicker.js +169 -0
- package/dist/panels/BasemapPicker/BasemapPicker.js.map +1 -0
- package/dist/panels/BasemapPicker/BasemapPicker.styles.js +42 -0
- package/dist/panels/BasemapPicker/BasemapPicker.styles.js.map +1 -0
- package/dist/panels/ExportSection/ExportSection.js +91 -0
- package/dist/panels/ExportSection/ExportSection.js.map +1 -0
- package/dist/panels/HelpSection/HelpSection.js +163 -0
- package/dist/panels/HelpSection/HelpSection.js.map +1 -0
- package/dist/panels/HelpSection/HelpSection.styles.js +9 -0
- package/dist/panels/HelpSection/HelpSection.styles.js.map +1 -0
- package/dist/panels/PinSection/PinSection.js +39 -0
- package/dist/panels/PinSection/PinSection.js.map +1 -0
- package/dist/panels/Section/Section.js +43 -0
- package/dist/panels/Section/Section.js.map +1 -0
- package/dist/panels/Section/Section.styles.js +13 -0
- package/dist/panels/Section/Section.styles.js.map +1 -0
- package/dist/panels/SettingsSection/LanguageField.js +126 -0
- package/dist/panels/SettingsSection/LanguageField.js.map +1 -0
- package/dist/panels/SettingsSection/SettingsSection.js +175 -0
- package/dist/panels/SettingsSection/SettingsSection.js.map +1 -0
- package/dist/panels/SettingsSection/SettingsSection.styles.js +9 -0
- package/dist/panels/SettingsSection/SettingsSection.styles.js.map +1 -0
- package/dist/panels/ShapesSection/ShapesSection.js +229 -0
- package/dist/panels/ShapesSection/ShapesSection.js.map +1 -0
- package/dist/panels/ShapesSection/ShapesSection.styles.js +7 -0
- package/dist/panels/ShapesSection/ShapesSection.styles.js.map +1 -0
- package/dist/panels/SheetContent/SheetContent.js +92 -0
- package/dist/panels/SheetContent/SheetContent.js.map +1 -0
- package/dist/panels/SheetContent/SheetContent.styles.js +6 -0
- package/dist/panels/SheetContent/SheetContent.styles.js.map +1 -0
- package/dist/services/ServicesProvider.js +42 -0
- package/dist/services/ServicesProvider.js.map +1 -0
- package/dist/services/activeMessages.js +140 -0
- package/dist/services/activeMessages.js.map +1 -0
- package/dist/services/basemapAvailability.js +78 -0
- package/dist/services/basemapAvailability.js.map +1 -0
- package/dist/services/englishMessages.js +10 -0
- package/dist/services/englishMessages.js.map +1 -0
- package/dist/services/hooks.js +214 -0
- package/dist/services/hooks.js.map +1 -0
- package/dist/styles.css +2866 -0
- package/dist/styles.css.d.ts +1 -0
- package/dist/tools/Directions/Directions.styles.js +25 -0
- package/dist/tools/Directions/Directions.styles.js.map +1 -0
- package/dist/tools/Directions/DirectionsOverlay.js +46 -0
- package/dist/tools/Directions/DirectionsOverlay.js.map +1 -0
- package/dist/tools/Directions/DirectionsPanel.js +428 -0
- package/dist/tools/Directions/DirectionsPanel.js.map +1 -0
- package/dist/tools/Directions/MapDirections.js +84 -0
- package/dist/tools/Directions/MapDirections.js.map +1 -0
- package/dist/tools/Directions/useDirections.js +226 -0
- package/dist/tools/Directions/useDirections.js.map +1 -0
- package/dist/tools/Directions/useFreeCentre.js +30 -0
- package/dist/tools/Directions/useFreeCentre.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawMapBindings.js +34 -0
- package/dist/tools/DrawToolbar/DrawMapBindings.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawToolbar.js +296 -0
- package/dist/tools/DrawToolbar/DrawToolbar.js.map +1 -0
- package/dist/tools/DrawToolbar/DrawToolbar.styles.js +21 -0
- package/dist/tools/DrawToolbar/DrawToolbar.styles.js.map +1 -0
- package/dist/tools/DrawToolbar/imageDrawing.js +24 -0
- package/dist/tools/DrawToolbar/imageDrawing.js.map +1 -0
- package/dist/tools/DrawToolbar/useDraw.js +543 -0
- package/dist/tools/DrawToolbar/useDraw.js.map +1 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.js +116 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.js.map +1 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.styles.js +12 -0
- package/dist/tools/MeasureToolbar/MeasureToolbar.styles.js.map +1 -0
- package/dist/tools/MeasureToolbar/useMeasure.js +75 -0
- package/dist/tools/MeasureToolbar/useMeasure.js.map +1 -0
- package/dist/tools/PinCard/PinCard.js +174 -0
- package/dist/tools/PinCard/PinCard.js.map +1 -0
- package/dist/tools/PinCard/PinCard.styles.js +16 -0
- package/dist/tools/PinCard/PinCard.styles.js.map +1 -0
- package/dist/tools/PinCard/useReverseAddress.js +77 -0
- package/dist/tools/PinCard/useReverseAddress.js.map +1 -0
- package/dist/tools/SearchPill/SearchPill.js +292 -0
- package/dist/tools/SearchPill/SearchPill.js.map +1 -0
- package/dist/tools/SearchPill/SearchPill.styles.js +16 -0
- package/dist/tools/SearchPill/SearchPill.styles.js.map +1 -0
- package/dist/tools/SearchPill/SearchPillSkeleton.js +41 -0
- package/dist/tools/SearchPill/SearchPillSkeleton.js.map +1 -0
- package/dist/tools/SearchPill/useSearch.js +143 -0
- package/dist/tools/SearchPill/useSearch.js.map +1 -0
- package/dist/tools/SearchPill/useSuggestions.js +105 -0
- package/dist/tools/SearchPill/useSuggestions.js.map +1 -0
- package/dist/tools/Tour/TourCard.js +156 -0
- package/dist/tools/Tour/TourCard.js.map +1 -0
- package/dist/tools/Tour/TourCard.styles.js +10 -0
- package/dist/tools/Tour/TourCard.styles.js.map +1 -0
- package/dist/tools/Tour/useTour.js +110 -0
- package/dist/tools/Tour/useTour.js.map +1 -0
- package/dist/tools/locate/useLocate.js +94 -0
- package/dist/tools/locate/useLocate.js.map +1 -0
- package/dist/ui/Button/Button.js +36 -0
- package/dist/ui/Button/Button.js.map +1 -0
- package/dist/ui/Button/Button.styles.js +17 -0
- package/dist/ui/Button/Button.styles.js.map +1 -0
- package/dist/ui/Chip/Chip.js +37 -0
- package/dist/ui/Chip/Chip.js.map +1 -0
- package/dist/ui/Chip/Chip.styles.js +29 -0
- package/dist/ui/Chip/Chip.styles.js.map +1 -0
- package/dist/ui/ControlGroup/ControlGroup.js +23 -0
- package/dist/ui/ControlGroup/ControlGroup.js.map +1 -0
- package/dist/ui/ControlGroup/ControlGroup.styles.js +6 -0
- package/dist/ui/ControlGroup/ControlGroup.styles.js.map +1 -0
- package/dist/ui/EmptyState/EmptyState.js +37 -0
- package/dist/ui/EmptyState/EmptyState.js.map +1 -0
- package/dist/ui/EmptyState/EmptyState.styles.js +10 -0
- package/dist/ui/EmptyState/EmptyState.styles.js.map +1 -0
- package/dist/ui/Icon/Icon.js +28 -0
- package/dist/ui/Icon/Icon.js.map +1 -0
- package/dist/ui/Icon/Icon.styles.js +8 -0
- package/dist/ui/Icon/Icon.styles.js.map +1 -0
- package/dist/ui/IconButton/IconButton.js +47 -0
- package/dist/ui/IconButton/IconButton.js.map +1 -0
- package/dist/ui/IconButton/IconButton.styles.js +27 -0
- package/dist/ui/IconButton/IconButton.styles.js.map +1 -0
- package/dist/ui/Isolate/Isolate.js +26 -0
- package/dist/ui/Isolate/Isolate.js.map +1 -0
- package/dist/ui/Isolate/fillSlots.js +27 -0
- package/dist/ui/Isolate/fillSlots.js.map +1 -0
- package/dist/ui/ListRow/ListRow.js +59 -0
- package/dist/ui/ListRow/ListRow.js.map +1 -0
- package/dist/ui/ListRow/ListRow.styles.js +30 -0
- package/dist/ui/ListRow/ListRow.styles.js.map +1 -0
- package/dist/ui/LiveRegion/LiveRegion.js +88 -0
- package/dist/ui/LiveRegion/LiveRegion.js.map +1 -0
- package/dist/ui/LiveRegion/announcer.js +114 -0
- package/dist/ui/LiveRegion/announcer.js.map +1 -0
- package/dist/ui/SegmentedControl/SegmentedControl.js +55 -0
- package/dist/ui/SegmentedControl/SegmentedControl.js.map +1 -0
- package/dist/ui/SegmentedControl/SegmentedControl.styles.js +9 -0
- package/dist/ui/SegmentedControl/SegmentedControl.styles.js.map +1 -0
- package/dist/ui/SkipLink/SkipLink.js +35 -0
- package/dist/ui/SkipLink/SkipLink.js.map +1 -0
- package/dist/ui/SkipLink/SkipLink.styles.js +6 -0
- package/dist/ui/SkipLink/SkipLink.styles.js.map +1 -0
- package/dist/ui/Slider/Slider.js +68 -0
- package/dist/ui/Slider/Slider.js.map +1 -0
- package/dist/ui/Slider/Slider.styles.js +13 -0
- package/dist/ui/Slider/Slider.styles.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessage.js +124 -0
- package/dist/ui/StatusMessage/StatusMessage.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessage.styles.js +28 -0
- package/dist/ui/StatusMessage/StatusMessage.styles.js.map +1 -0
- package/dist/ui/StatusMessage/StatusMessageFocus.js +30 -0
- package/dist/ui/StatusMessage/StatusMessageFocus.js.map +1 -0
- package/dist/ui/Surface/Surface.js +22 -0
- package/dist/ui/Surface/Surface.js.map +1 -0
- package/dist/ui/Surface/Surface.styles.js +27 -0
- package/dist/ui/Surface/Surface.styles.js.map +1 -0
- package/dist/ui/Switch/Switch.js +58 -0
- package/dist/ui/Switch/Switch.js.map +1 -0
- package/dist/ui/Switch/Switch.styles.js +13 -0
- package/dist/ui/Switch/Switch.styles.js.map +1 -0
- package/dist/ui/Tooltip/Tooltip.js +66 -0
- package/dist/ui/Tooltip/Tooltip.js.map +1 -0
- package/dist/ui/Tooltip/Tooltip.styles.js +6 -0
- package/dist/ui/Tooltip/Tooltip.styles.js.map +1 -0
- package/dist/ui/Tooltip/inheritColours.js +27 -0
- package/dist/ui/Tooltip/inheritColours.js.map +1 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.js +16 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.js.map +1 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.styles.js +6 -0
- package/dist/ui/VisuallyHidden/VisuallyHidden.styles.js.map +1 -0
- package/package.json +82 -4
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { useSuggestionProvider } from "../../services/hooks.js";
|
|
2
|
+
import { useAnnounce } from "../../ui/LiveRegion/LiveRegion.js";
|
|
3
|
+
import { PortAbortError, SearchError, announceNoSuggestions, announceSuggestions, announceSuggestionsUnavailable } from "@mapcairn/core";
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
import { useLatestRef } from "@mapcairn/react";
|
|
6
|
+
// src/tools/SearchPill/useSuggestions.ts
|
|
7
|
+
/** How long the text must stay unchanged before suggestions are asked for. */
|
|
8
|
+
var SUGGEST_DEBOUNCE_MS = 275;
|
|
9
|
+
/** The fewest characters (after trimming) that suggestions are asked for. */
|
|
10
|
+
var MIN_SUGGEST_LENGTH = 3;
|
|
11
|
+
/** How many requests in a row may fail before suggestions pause. */
|
|
12
|
+
var SUGGEST_FAILURE_LIMIT = 3;
|
|
13
|
+
/** How long suggestions stay paused after repeated failures, in milliseconds. */
|
|
14
|
+
var SUGGEST_COOLDOWN_MS = 6e4;
|
|
15
|
+
var NONE = [];
|
|
16
|
+
/**
|
|
17
|
+
* A run of letters that makes text a place name. Text with none (digits,
|
|
18
|
+
* signs, a degree mark and at most a compass letter or two) may be a
|
|
19
|
+
* coordinate pair still being typed, which is never sent to a service.
|
|
20
|
+
*/
|
|
21
|
+
var WORD = /\p{L}{3,}/u;
|
|
22
|
+
/**
|
|
23
|
+
* Suggests places for text as it is typed, over the services' optional
|
|
24
|
+
* `SuggestionProvider`. Returns the suggestions for the latest text that has
|
|
25
|
+
* settled, or none.
|
|
26
|
+
*
|
|
27
|
+
* A request starts only when the trimmed text has been unchanged for
|
|
28
|
+
* {@link SUGGEST_DEBOUNCE_MS}, is at least {@link MIN_SUGGEST_LENGTH}
|
|
29
|
+
* characters and has a word in it, so a coordinate pair typed digit by digit
|
|
30
|
+
* is never sent (the search itself answers a whole pair without a service).
|
|
31
|
+
* Each change of text aborts the request before it, so an
|
|
32
|
+
* answer for older text never arrives after a newer one. Nothing is asked
|
|
33
|
+
* while `enabled` is false, which a caller uses after the person has picked
|
|
34
|
+
* a suggestion so the box showing its label does not suggest again.
|
|
35
|
+
*
|
|
36
|
+
* When the service fails the person is not interrupted: no suggestions show,
|
|
37
|
+
* and Enter still searches. After {@link SUGGEST_FAILURE_LIMIT} failures in a
|
|
38
|
+
* row, or one `rate-limited` answer, it stops asking for
|
|
39
|
+
* {@link SUGGEST_COOLDOWN_MS} and announces once that suggestions are
|
|
40
|
+
* unavailable and Enter still works. It announces again only after a request
|
|
41
|
+
* has worked in between. A host without a provider gets no suggestions and no
|
|
42
|
+
* announcements.
|
|
43
|
+
*
|
|
44
|
+
* Counts go out through the app-wide announcer, politely.
|
|
45
|
+
*/
|
|
46
|
+
function useSuggestions(query, enabled) {
|
|
47
|
+
const provider = useSuggestionProvider();
|
|
48
|
+
const announce = useAnnounce();
|
|
49
|
+
const announceRef = useLatestRef(announce);
|
|
50
|
+
const text = query.trim();
|
|
51
|
+
const [suggestions, setSuggestions] = useState(NONE);
|
|
52
|
+
const failures = useRef(0);
|
|
53
|
+
const pausedUntil = useRef(0);
|
|
54
|
+
const toldUnavailable = useRef(false);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (provider === void 0 || !enabled || text.length < 3 || !WORD.test(text)) {
|
|
57
|
+
setSuggestions(NONE);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const own = new AbortController();
|
|
61
|
+
const fail = (error) => {
|
|
62
|
+
failures.current += 1;
|
|
63
|
+
setSuggestions(NONE);
|
|
64
|
+
if (!(error instanceof SearchError && error.kind === "rate-limited") && failures.current < 3) return;
|
|
65
|
+
pausedUntil.current = Date.now() + SUGGEST_COOLDOWN_MS;
|
|
66
|
+
if (!toldUnavailable.current) {
|
|
67
|
+
toldUnavailable.current = true;
|
|
68
|
+
announceRef.current(announceSuggestionsUnavailable(), { priority: "polite" });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const request = async () => {
|
|
72
|
+
if (Date.now() < pausedUntil.current) {
|
|
73
|
+
setSuggestions(NONE);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const results = await provider.suggest(text, { signal: own.signal });
|
|
78
|
+
if (own.signal.aborted) return;
|
|
79
|
+
failures.current = 0;
|
|
80
|
+
toldUnavailable.current = false;
|
|
81
|
+
setSuggestions(results);
|
|
82
|
+
announceRef.current(results.length === 0 ? announceNoSuggestions() : announceSuggestions(results.length), { priority: "polite" });
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (own.signal.aborted || error instanceof PortAbortError) return;
|
|
85
|
+
fail(error);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
request();
|
|
90
|
+
}, 275);
|
|
91
|
+
return () => {
|
|
92
|
+
clearTimeout(timer);
|
|
93
|
+
own.abort();
|
|
94
|
+
};
|
|
95
|
+
}, [
|
|
96
|
+
provider,
|
|
97
|
+
enabled,
|
|
98
|
+
text,
|
|
99
|
+
announceRef
|
|
100
|
+
]);
|
|
101
|
+
return suggestions;
|
|
102
|
+
}
|
|
103
|
+
export { MIN_SUGGEST_LENGTH, SUGGEST_COOLDOWN_MS, SUGGEST_DEBOUNCE_MS, SUGGEST_FAILURE_LIMIT, useSuggestions };
|
|
104
|
+
|
|
105
|
+
//# sourceMappingURL=useSuggestions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSuggestions.js","names":[],"sources":["../../../src/tools/SearchPill/useSuggestions.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n announceNoSuggestions,\n announceSuggestions,\n announceSuggestionsUnavailable,\n PortAbortError,\n SearchError,\n type SearchResult,\n} from \"@mapcairn/core\";\nimport { useLatestRef } from \"@mapcairn/react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport { useSuggestionProvider } from \"../../services/hooks\";\nimport { useAnnounce } from \"../../ui/LiveRegion\";\n\n/** How long the text must stay unchanged before suggestions are asked for. */\nexport const SUGGEST_DEBOUNCE_MS = 275;\n\n/** The fewest characters (after trimming) that suggestions are asked for. */\nexport const MIN_SUGGEST_LENGTH = 3;\n\n/** How many requests in a row may fail before suggestions pause. */\nexport const SUGGEST_FAILURE_LIMIT = 3;\n\n/** How long suggestions stay paused after repeated failures, in milliseconds. */\nexport const SUGGEST_COOLDOWN_MS = 60_000;\n\nconst NONE: readonly SearchResult[] = [];\n\n/**\n * A run of letters that makes text a place name. Text with none (digits,\n * signs, a degree mark and at most a compass letter or two) may be a\n * coordinate pair still being typed, which is never sent to a service.\n */\nconst WORD = /\\p{L}{3,}/u;\n\n/**\n * Suggests places for text as it is typed, over the services' optional\n * `SuggestionProvider`. Returns the suggestions for the latest text that has\n * settled, or none.\n *\n * A request starts only when the trimmed text has been unchanged for\n * {@link SUGGEST_DEBOUNCE_MS}, is at least {@link MIN_SUGGEST_LENGTH}\n * characters and has a word in it, so a coordinate pair typed digit by digit\n * is never sent (the search itself answers a whole pair without a service).\n * Each change of text aborts the request before it, so an\n * answer for older text never arrives after a newer one. Nothing is asked\n * while `enabled` is false, which a caller uses after the person has picked\n * a suggestion so the box showing its label does not suggest again.\n *\n * When the service fails the person is not interrupted: no suggestions show,\n * and Enter still searches. After {@link SUGGEST_FAILURE_LIMIT} failures in a\n * row, or one `rate-limited` answer, it stops asking for\n * {@link SUGGEST_COOLDOWN_MS} and announces once that suggestions are\n * unavailable and Enter still works. It announces again only after a request\n * has worked in between. A host without a provider gets no suggestions and no\n * announcements.\n *\n * Counts go out through the app-wide announcer, politely.\n */\nexport function useSuggestions(\n query: string,\n enabled: boolean,\n): readonly SearchResult[] {\n const provider = useSuggestionProvider();\n const announce = useAnnounce();\n const announceRef = useLatestRef(announce);\n const text = query.trim();\n const [suggestions, setSuggestions] = useState<readonly SearchResult[]>(NONE);\n const failures = useRef(0);\n const pausedUntil = useRef(0);\n const toldUnavailable = useRef(false);\n\n useEffect(() => {\n if (\n provider === undefined ||\n !enabled ||\n text.length < MIN_SUGGEST_LENGTH ||\n !WORD.test(text)\n ) {\n setSuggestions(NONE);\n return;\n }\n\n const own = new AbortController();\n\n const fail = (error: unknown) => {\n failures.current += 1;\n setSuggestions(NONE);\n const slowDown =\n error instanceof SearchError && error.kind === \"rate-limited\";\n if (!slowDown && failures.current < SUGGEST_FAILURE_LIMIT) {\n return;\n }\n pausedUntil.current = Date.now() + SUGGEST_COOLDOWN_MS;\n if (!toldUnavailable.current) {\n toldUnavailable.current = true;\n announceRef.current(announceSuggestionsUnavailable(), {\n priority: \"polite\",\n });\n }\n };\n\n const request = async () => {\n if (Date.now() < pausedUntil.current) {\n setSuggestions(NONE);\n return;\n }\n try {\n const results = await provider.suggest(text, { signal: own.signal });\n if (own.signal.aborted) {\n return;\n }\n failures.current = 0;\n toldUnavailable.current = false;\n setSuggestions(results);\n announceRef.current(\n results.length === 0\n ? announceNoSuggestions()\n : announceSuggestions(results.length),\n { priority: \"polite\" },\n );\n } catch (error) {\n if (own.signal.aborted || error instanceof PortAbortError) {\n return;\n }\n fail(error);\n }\n };\n\n const timer = setTimeout(() => {\n void request();\n }, SUGGEST_DEBOUNCE_MS);\n return () => {\n clearTimeout(timer);\n own.abort();\n };\n }, [provider, enabled, text, announceRef]);\n\n return suggestions;\n}\n"],"mappings":";;;;;;;AAgBA,IAAa,sBAAsB;;AAGnC,IAAa,qBAAqB;;AAGlC,IAAa,wBAAwB;;AAGrC,IAAa,sBAAsB;AAEnC,IAAM,OAAgC,CAAC;;;;;;AAOvC,IAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;AA0Bb,SAAgB,eACd,OACA,SACyB;CACzB,MAAM,WAAW,sBAAsB;CACvC,MAAM,WAAW,YAAY;CAC7B,MAAM,cAAc,aAAa,QAAQ;CACzC,MAAM,OAAO,MAAM,KAAK;CACxB,MAAM,CAAC,aAAa,kBAAkB,SAAkC,IAAI;CAC5E,MAAM,WAAW,OAAO,CAAC;CACzB,MAAM,cAAc,OAAO,CAAC;CAC5B,MAAM,kBAAkB,OAAO,KAAK;CAEpC,gBAAgB;EACd,IACE,aAAa,KAAA,KACb,CAAC,WACD,KAAK,SAAA,KACL,CAAC,KAAK,KAAK,IAAI,GACf;GACA,eAAe,IAAI;GACnB;EACF;EAEA,MAAM,MAAM,IAAI,gBAAgB;EAEhC,MAAM,QAAQ,UAAmB;GAC/B,SAAS,WAAW;GACpB,eAAe,IAAI;GAGnB,IAAI,EADF,iBAAiB,eAAe,MAAM,SAAS,mBAChC,SAAS,UAAA,GACxB;GAEF,YAAY,UAAU,KAAK,IAAI,IAAI;GACnC,IAAI,CAAC,gBAAgB,SAAS;IAC5B,gBAAgB,UAAU;IAC1B,YAAY,QAAQ,+BAA+B,GAAG,EACpD,UAAU,SACZ,CAAC;GACH;EACF;EAEA,MAAM,UAAU,YAAY;GAC1B,IAAI,KAAK,IAAI,IAAI,YAAY,SAAS;IACpC,eAAe,IAAI;IACnB;GACF;GACA,IAAI;IACF,MAAM,UAAU,MAAM,SAAS,QAAQ,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;IACnE,IAAI,IAAI,OAAO,SACb;IAEF,SAAS,UAAU;IACnB,gBAAgB,UAAU;IAC1B,eAAe,OAAO;IACtB,YAAY,QACV,QAAQ,WAAW,IACf,sBAAsB,IACtB,oBAAoB,QAAQ,MAAM,GACtC,EAAE,UAAU,SAAS,CACvB;GACF,SAAS,OAAO;IACd,IAAI,IAAI,OAAO,WAAW,iBAAiB,gBACzC;IAEF,KAAK,KAAK;GACZ;EACF;EAEA,MAAM,QAAQ,iBAAiB;GAC7B,QAAa;EACf,GAAA,GAAsB;EACtB,aAAa;GACX,aAAa,KAAK;GAClB,IAAI,MAAM;EACZ;CACF,GAAG;EAAC;EAAU;EAAS;EAAM;CAAW,CAAC;CAEzC,OAAO;AACT"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { useFocusElement, useFocusReturn } from "../../chrome/focusRegistry.js";
|
|
2
|
+
import { useChromeSurface } from "../../chrome/MapChrome/chromeSurface.js";
|
|
3
|
+
import { useMessages, usePreferences } from "../../services/hooks.js";
|
|
4
|
+
import { Button } from "../../ui/Button/Button.js";
|
|
5
|
+
import { useAnnounce } from "../../ui/LiveRegion/LiveRegion.js";
|
|
6
|
+
import { Surface } from "../../ui/Surface/Surface.js";
|
|
7
|
+
import { actions, card, heading, progress, text } from "./TourCard.styles.js";
|
|
8
|
+
import { resolveText } from "@mapcairn/core";
|
|
9
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
10
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
// src/tools/Tour/TourCard.tsx
|
|
12
|
+
/**
|
|
13
|
+
* The first-visit tour's card: the welcome card, a step, or the ending. It is
|
|
14
|
+
* a region named by its heading, not a dialog, because nothing is modal.
|
|
15
|
+
*
|
|
16
|
+
* @remarks The welcome card never takes focus. Taking the tour, Next and a
|
|
17
|
+
* restart from Help move focus to the heading; a step finished by doing it
|
|
18
|
+
* leaves focus with the person. Escape inside the card skips (or closes the
|
|
19
|
+
* ending) and prevents the default, so the app's own Escape does not also
|
|
20
|
+
* close a surface. When the card closes, or its slot hides because the sheet
|
|
21
|
+
* is modal or the bottom row has come up to it, with focus inside it, focus
|
|
22
|
+
* goes to the map. A request for focus made while the slot is hidden (a
|
|
23
|
+
* restart from Help that closes a sheet in the same update) waits until the
|
|
24
|
+
* slot shows, because an inert element cannot take it. While a step is current, its control carries
|
|
25
|
+
* `data-tour-highlight` and is scrolled into view.
|
|
26
|
+
*/
|
|
27
|
+
function TourCard({ tour, welcome, ending }) {
|
|
28
|
+
const headingId = useId();
|
|
29
|
+
const headingRef = useRef(null);
|
|
30
|
+
const handledFocus = useRef(0);
|
|
31
|
+
const [announced, setAnnounced] = useState(false);
|
|
32
|
+
const restoreFocus = useFocusReturn();
|
|
33
|
+
const elementFor = useFocusElement();
|
|
34
|
+
const announce = useAnnounce();
|
|
35
|
+
const { reducedMotion } = usePreferences();
|
|
36
|
+
const messages = useMessages();
|
|
37
|
+
const { t } = messages;
|
|
38
|
+
const { tourHidden } = useChromeSurface();
|
|
39
|
+
const { state, step, steps, focusRequest } = tour;
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (state.status === "welcome" && !announced) {
|
|
42
|
+
setAnnounced(true);
|
|
43
|
+
announce(resolveText(messages, welcome.announcement));
|
|
44
|
+
}
|
|
45
|
+
}, [
|
|
46
|
+
state.status,
|
|
47
|
+
announced,
|
|
48
|
+
announce,
|
|
49
|
+
welcome.announcement,
|
|
50
|
+
messages
|
|
51
|
+
]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const heading = headingRef.current;
|
|
54
|
+
if (focusRequest <= handledFocus.current || tourHidden || !heading) return;
|
|
55
|
+
heading.focus();
|
|
56
|
+
if (heading.ownerDocument.activeElement === heading) handledFocus.current = focusRequest;
|
|
57
|
+
});
|
|
58
|
+
useLayoutEffect(() => {
|
|
59
|
+
if (!tourHidden) return;
|
|
60
|
+
const card = headingRef.current?.parentElement;
|
|
61
|
+
if (card?.contains(card.ownerDocument.activeElement)) restoreFocus(null);
|
|
62
|
+
}, [tourHidden, restoreFocus]);
|
|
63
|
+
const target = step?.target;
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const element = target ? elementFor(target) : null;
|
|
66
|
+
if (!element) return;
|
|
67
|
+
element.setAttribute("data-tour-highlight", "true");
|
|
68
|
+
element.scrollIntoView({
|
|
69
|
+
block: "nearest",
|
|
70
|
+
behavior: reducedMotion ? "auto" : "smooth"
|
|
71
|
+
});
|
|
72
|
+
return () => {
|
|
73
|
+
element.removeAttribute("data-tour-highlight");
|
|
74
|
+
};
|
|
75
|
+
}, [
|
|
76
|
+
target,
|
|
77
|
+
elementFor,
|
|
78
|
+
reducedMotion
|
|
79
|
+
]);
|
|
80
|
+
if (state.status === "off") return null;
|
|
81
|
+
const leave = (action) => {
|
|
82
|
+
const card = headingRef.current?.parentElement;
|
|
83
|
+
const hadFocus = card?.contains(card.ownerDocument.activeElement) ?? false;
|
|
84
|
+
action();
|
|
85
|
+
if (hadFocus) restoreFocus(null);
|
|
86
|
+
};
|
|
87
|
+
const dismiss = () => leave(state.status === "ended" ? tour.close : tour.skip);
|
|
88
|
+
const onKeyDown = (event) => {
|
|
89
|
+
if (event.key === "Escape" && !event.defaultPrevented) {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
dismiss();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
let copy = ending;
|
|
95
|
+
if (state.status === "welcome") copy = welcome;
|
|
96
|
+
else if (step) copy = step;
|
|
97
|
+
const heading$1 = resolveText(messages, copy.heading);
|
|
98
|
+
const text$1 = resolveText(messages, copy.text);
|
|
99
|
+
const last = state.status === "running" && state.step === steps.length - 1;
|
|
100
|
+
return /* @__PURE__ */ jsxs(Surface, {
|
|
101
|
+
as: "section",
|
|
102
|
+
radius: "control",
|
|
103
|
+
"aria-labelledby": headingId,
|
|
104
|
+
"data-testid": "tour-card",
|
|
105
|
+
className: card,
|
|
106
|
+
onKeyDown,
|
|
107
|
+
children: [
|
|
108
|
+
/* @__PURE__ */ jsx("h2", {
|
|
109
|
+
id: headingId,
|
|
110
|
+
ref: headingRef,
|
|
111
|
+
tabIndex: -1,
|
|
112
|
+
className: heading,
|
|
113
|
+
children: heading$1
|
|
114
|
+
}),
|
|
115
|
+
state.status === "running" ? /* @__PURE__ */ jsx("p", {
|
|
116
|
+
className: progress,
|
|
117
|
+
children: t("tools.tour.progress", {
|
|
118
|
+
step: state.step + 1,
|
|
119
|
+
total: steps.length
|
|
120
|
+
})
|
|
121
|
+
}) : null,
|
|
122
|
+
/* @__PURE__ */ jsx("p", {
|
|
123
|
+
className: text,
|
|
124
|
+
children: text$1
|
|
125
|
+
}),
|
|
126
|
+
/* @__PURE__ */ jsxs("div", {
|
|
127
|
+
className: actions,
|
|
128
|
+
children: [
|
|
129
|
+
state.status === "welcome" ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Button, {
|
|
130
|
+
onClick: tour.start,
|
|
131
|
+
children: t("tools.tour.take", {})
|
|
132
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
133
|
+
variant: "secondary",
|
|
134
|
+
onClick: dismiss,
|
|
135
|
+
children: t("tools.tour.skip", {})
|
|
136
|
+
})] }) : null,
|
|
137
|
+
state.status === "running" ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Button, {
|
|
138
|
+
onClick: tour.next,
|
|
139
|
+
children: last ? t("tools.tour.finish", {}) : t("tools.tour.next", {})
|
|
140
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
141
|
+
variant: "quiet",
|
|
142
|
+
onClick: dismiss,
|
|
143
|
+
children: t("tools.tour.skipTour", {})
|
|
144
|
+
})] }) : null,
|
|
145
|
+
state.status === "ended" ? /* @__PURE__ */ jsx(Button, {
|
|
146
|
+
onClick: dismiss,
|
|
147
|
+
children: t("tools.tour.close", {})
|
|
148
|
+
}) : null
|
|
149
|
+
]
|
|
150
|
+
})
|
|
151
|
+
]
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
export { TourCard };
|
|
155
|
+
|
|
156
|
+
//# sourceMappingURL=TourCard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TourCard.js","names":["heading","text","styles.card","styles.heading","styles.progress","styles.text","styles.actions","_Fragment"],"sources":["../../../src/tools/Tour/TourCard.tsx"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { type DefinitionText, resolveText } from \"@mapcairn/core\";\nimport {\n type KeyboardEvent,\n useEffect,\n useId,\n useLayoutEffect,\n useRef,\n useState,\n} from \"react\";\nimport { useFocusElement, useFocusReturn } from \"../../chrome/focusRegistry\";\nimport { useChromeSurface } from \"../../chrome/MapChrome/chromeSurface\";\nimport { useMessages, usePreferences } from \"../../services\";\nimport { Button } from \"../../ui/Button\";\nimport { useAnnounce } from \"../../ui/LiveRegion\";\nimport { Surface } from \"../../ui/Surface\";\nimport * as styles from \"./TourCard.css\";\nimport type { UseTour } from \"./useTour\";\n\n/** The words of a card that is not a step. */\nexport interface TourCardCopy {\n readonly heading: DefinitionText;\n readonly text: DefinitionText;\n}\n\n/** Props for {@link TourCard}. */\nexport interface TourCardProps {\n readonly tour: UseTour;\n /** The welcome card, offered on a first visit. */\n readonly welcome: TourCardCopy & {\n /** Read out once when the welcome card appears, which takes no focus. */\n readonly announcement: DefinitionText;\n };\n /** The ending, after the last step. */\n readonly ending: TourCardCopy;\n}\n\n/**\n * The first-visit tour's card: the welcome card, a step, or the ending. It is\n * a region named by its heading, not a dialog, because nothing is modal.\n *\n * @remarks The welcome card never takes focus. Taking the tour, Next and a\n * restart from Help move focus to the heading; a step finished by doing it\n * leaves focus with the person. Escape inside the card skips (or closes the\n * ending) and prevents the default, so the app's own Escape does not also\n * close a surface. When the card closes, or its slot hides because the sheet\n * is modal or the bottom row has come up to it, with focus inside it, focus\n * goes to the map. A request for focus made while the slot is hidden (a\n * restart from Help that closes a sheet in the same update) waits until the\n * slot shows, because an inert element cannot take it. While a step is current, its control carries\n * `data-tour-highlight` and is scrolled into view.\n */\nexport function TourCard({ tour, welcome, ending }: TourCardProps) {\n const headingId = useId();\n const headingRef = useRef<HTMLHeadingElement>(null);\n const handledFocus = useRef(0);\n const [announced, setAnnounced] = useState(false);\n const restoreFocus = useFocusReturn();\n const elementFor = useFocusElement();\n const announce = useAnnounce();\n const { reducedMotion } = usePreferences();\n const messages = useMessages();\n const { t } = messages;\n const { tourHidden } = useChromeSurface();\n const { state, step, steps, focusRequest } = tour;\n\n useEffect(() => {\n if (state.status === \"welcome\" && !announced) {\n setAnnounced(true);\n announce(resolveText(messages, welcome.announcement));\n }\n }, [state.status, announced, announce, welcome.announcement, messages]);\n\n useEffect(() => {\n const heading = headingRef.current;\n if (focusRequest <= handledFocus.current || tourHidden || !heading) {\n return;\n }\n heading.focus();\n if (heading.ownerDocument.activeElement === heading) {\n handledFocus.current = focusRequest;\n }\n });\n\n // Runs before the browser applies `inert`, while focus is still in the card.\n useLayoutEffect(() => {\n if (!tourHidden) {\n return;\n }\n const card = headingRef.current?.parentElement;\n if (card?.contains(card.ownerDocument.activeElement)) {\n restoreFocus(null);\n }\n }, [tourHidden, restoreFocus]);\n\n const target = step?.target;\n useEffect(() => {\n const element = target ? elementFor(target) : null;\n if (!element) {\n return;\n }\n element.setAttribute(\"data-tour-highlight\", \"true\");\n element.scrollIntoView({\n block: \"nearest\",\n behavior: reducedMotion ? \"auto\" : \"smooth\",\n });\n return () => {\n element.removeAttribute(\"data-tour-highlight\");\n };\n }, [target, elementFor, reducedMotion]);\n\n if (state.status === \"off\") {\n return null;\n }\n\n const leave = (action: () => void) => {\n const card = headingRef.current?.parentElement;\n const hadFocus = card?.contains(card.ownerDocument.activeElement) ?? false;\n action();\n if (hadFocus) {\n restoreFocus(null);\n }\n };\n const dismiss = () =>\n leave(state.status === \"ended\" ? tour.close : tour.skip);\n\n const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {\n if (event.key === \"Escape\" && !event.defaultPrevented) {\n event.preventDefault();\n dismiss();\n }\n };\n\n let copy: TourCardCopy = ending;\n if (state.status === \"welcome\") {\n copy = welcome;\n } else if (step) {\n copy = step;\n }\n const heading = resolveText(messages, copy.heading);\n const text = resolveText(messages, copy.text);\n const last = state.status === \"running\" && state.step === steps.length - 1;\n\n return (\n <Surface\n as=\"section\"\n radius=\"control\"\n aria-labelledby={headingId}\n data-testid=\"tour-card\"\n className={styles.card}\n onKeyDown={onKeyDown}\n >\n <h2\n id={headingId}\n ref={headingRef}\n tabIndex={-1}\n className={styles.heading}\n >\n {heading}\n </h2>\n {state.status === \"running\" ? (\n <p className={styles.progress}>\n {t(\"tools.tour.progress\", {\n step: state.step + 1,\n total: steps.length,\n })}\n </p>\n ) : null}\n <p className={styles.text}>{text}</p>\n <div className={styles.actions}>\n {state.status === \"welcome\" ? (\n <>\n <Button onClick={tour.start}>{t(\"tools.tour.take\", {})}</Button>\n <Button variant=\"secondary\" onClick={dismiss}>\n {t(\"tools.tour.skip\", {})}\n </Button>\n </>\n ) : null}\n {state.status === \"running\" ? (\n <>\n <Button onClick={tour.next}>\n {last ? t(\"tools.tour.finish\", {}) : t(\"tools.tour.next\", {})}\n </Button>\n <Button variant=\"quiet\" onClick={dismiss}>\n {t(\"tools.tour.skipTour\", {})}\n </Button>\n </>\n ) : null}\n {state.status === \"ended\" ? (\n <Button onClick={dismiss}>{t(\"tools.tour.close\", {})}</Button>\n ) : null}\n </div>\n </Surface>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,SAAgB,SAAS,EAAE,MAAM,SAAS,UAAyB;CACjE,MAAM,YAAY,MAAM;CACxB,MAAM,aAAa,OAA2B,IAAI;CAClD,MAAM,eAAe,OAAO,CAAC;CAC7B,MAAM,CAAC,WAAW,gBAAgB,SAAS,KAAK;CAChD,MAAM,eAAe,eAAe;CACpC,MAAM,aAAa,gBAAgB;CACnC,MAAM,WAAW,YAAY;CAC7B,MAAM,EAAE,kBAAkB,eAAe;CACzC,MAAM,WAAW,YAAY;CAC7B,MAAM,EAAE,MAAM;CACd,MAAM,EAAE,eAAe,iBAAiB;CACxC,MAAM,EAAE,OAAO,MAAM,OAAO,iBAAiB;CAE7C,gBAAgB;EACd,IAAI,MAAM,WAAW,aAAa,CAAC,WAAW;GAC5C,aAAa,IAAI;GACjB,SAAS,YAAY,UAAU,QAAQ,YAAY,CAAC;EACtD;CACF,GAAG;EAAC,MAAM;EAAQ;EAAW;EAAU,QAAQ;EAAc;CAAQ,CAAC;CAEtE,gBAAgB;EACd,MAAM,UAAU,WAAW;EAC3B,IAAI,gBAAgB,aAAa,WAAW,cAAc,CAAC,SACzD;EAEF,QAAQ,MAAM;EACd,IAAI,QAAQ,cAAc,kBAAkB,SAC1C,aAAa,UAAU;CAE3B,CAAC;CAGD,sBAAsB;EACpB,IAAI,CAAC,YACH;EAEF,MAAM,OAAO,WAAW,SAAS;EACjC,IAAI,MAAM,SAAS,KAAK,cAAc,aAAa,GACjD,aAAa,IAAI;CAErB,GAAG,CAAC,YAAY,YAAY,CAAC;CAE7B,MAAM,SAAS,MAAM;CACrB,gBAAgB;EACd,MAAM,UAAU,SAAS,WAAW,MAAM,IAAI;EAC9C,IAAI,CAAC,SACH;EAEF,QAAQ,aAAa,uBAAuB,MAAM;EAClD,QAAQ,eAAe;GACrB,OAAO;GACP,UAAU,gBAAgB,SAAS;EACrC,CAAC;EACD,aAAa;GACX,QAAQ,gBAAgB,qBAAqB;EAC/C;CACF,GAAG;EAAC;EAAQ;EAAY;CAAa,CAAC;CAEtC,IAAI,MAAM,WAAW,OACnB,OAAO;CAGT,MAAM,SAAS,WAAuB;EACpC,MAAM,OAAO,WAAW,SAAS;EACjC,MAAM,WAAW,MAAM,SAAS,KAAK,cAAc,aAAa,KAAK;EACrE,OAAO;EACP,IAAI,UACF,aAAa,IAAI;CAErB;CACA,MAAM,gBACJ,MAAM,MAAM,WAAW,UAAU,KAAK,QAAQ,KAAK,IAAI;CAEzD,MAAM,aAAa,UAAsC;EACvD,IAAI,MAAM,QAAQ,YAAY,CAAC,MAAM,kBAAkB;GACrD,MAAM,eAAe;GACrB,QAAQ;EACV;CACF;CAEA,IAAI,OAAqB;CACzB,IAAI,MAAM,WAAW,WACnB,OAAO;MACF,IAAI,MACT,OAAO;CAET,MAAMA,YAAU,YAAY,UAAU,KAAK,OAAO;CAClD,MAAMC,SAAO,YAAY,UAAU,KAAK,IAAI;CAC5C,MAAM,OAAO,MAAM,WAAW,aAAa,MAAM,SAAS,MAAM,SAAS;CAEzE,OACE,qBAAC,SAAD;EACE,IAAG;EACH,QAAO;EACP,mBAAiB;EACjB,eAAY;EACZ,WAAWC;EACA;EANb,UAAA;GAQE,oBAAC,MAAD;IACE,IAAI;IACJ,KAAK;IACL,UAAU;IACV,WAAWC;IAEV,UAAAH;GACC,CAAA;GACH,MAAM,WAAW,YAChB,oBAAC,KAAD;IAAG,WAAWI;IACX,UAAA,EAAE,uBAAuB;KACxB,MAAM,MAAM,OAAO;KACnB,OAAO,MAAM;IACf,CAAC;GACA,CAAA,IACD;GACJ,oBAAC,KAAD;IAAG,WAAWC;IAAc,UAAAJ;GAAQ,CAAA;GACpC,qBAAC,OAAD;IAAK,WAAWK;IAAhB,UAAA;KACG,MAAM,WAAW,YAChB,qBAAAC,YAAA,EAAA,UAAA,CACE,oBAAC,QAAD;MAAQ,SAAS,KAAK;MAAQ,UAAA,EAAE,mBAAmB,CAAC,CAAC;KAAU,CAAA,GAC/D,oBAAC,QAAD;MAAQ,SAAQ;MAAY,SAAS;MAClC,UAAA,EAAE,mBAAmB,CAAC,CAAC;KAClB,CAAA,CACR,EAAA,CAAA,IACA;KACH,MAAM,WAAW,YAChB,qBAAAA,YAAA,EAAA,UAAA,CACE,oBAAC,QAAD;MAAQ,SAAS,KAAK;MACnB,UAAA,OAAO,EAAE,qBAAqB,CAAC,CAAC,IAAI,EAAE,mBAAmB,CAAC,CAAC;KACtD,CAAA,GACR,oBAAC,QAAD;MAAQ,SAAQ;MAAQ,SAAS;MAC9B,UAAA,EAAE,uBAAuB,CAAC,CAAC;KACtB,CAAA,CACR,EAAA,CAAA,IACA;KACH,MAAM,WAAW,UAChB,oBAAC,QAAD;MAAQ,SAAS;MAAU,UAAA,EAAE,oBAAoB,CAAC,CAAC;KAAU,CAAA,IAC3D;IACD;;EACE;;AAEb"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* empty css */
|
|
2
|
+
// src/tools/Tour/TourCard.css.ts
|
|
3
|
+
var card = "_130f6850";
|
|
4
|
+
var heading = "_130f6851";
|
|
5
|
+
var progress = "_130f6852";
|
|
6
|
+
var text = "_130f6853";
|
|
7
|
+
var actions = "_130f6854";
|
|
8
|
+
export { actions, card, heading, progress, text };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=TourCard.styles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TourCard.styles.js","names":[],"sources":[],"mappings":""}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { useMessages, useSettings } from "../../services/hooks.js";
|
|
2
|
+
import { useAnnounce } from "../../ui/LiveRegion/LiveRegion.js";
|
|
3
|
+
import { announceTourSkipped, initialTourState, message, resolveText, stepDone, tourReducer } from "@mapcairn/core";
|
|
4
|
+
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
|
|
5
|
+
// src/tools/Tour/useTour.ts
|
|
6
|
+
/**
|
|
7
|
+
* Runs the first-visit tour: the welcome card, the steps and the ending.
|
|
8
|
+
*
|
|
9
|
+
* @remarks A step advances when the person does what it asks, judged by
|
|
10
|
+
* `stepDone` against the snapshot taken when the step began, or on Next.
|
|
11
|
+
* An advance by doing announces the next step and leaves focus where it is;
|
|
12
|
+
* Next and start ask the card to focus its heading instead. Taking the
|
|
13
|
+
* tour, skipping it, or turning on a layer while the welcome card is up
|
|
14
|
+
* stores the version, so the tour is not offered again.
|
|
15
|
+
*/
|
|
16
|
+
function useTour({ steps, version, offer, snapshot }) {
|
|
17
|
+
const [state, dispatch] = useReducer(tourReducer, initialTourState);
|
|
18
|
+
const [focusRequest, setFocusRequest] = useState(0);
|
|
19
|
+
const { update } = useSettings();
|
|
20
|
+
const announce = useAnnounce();
|
|
21
|
+
const messages = useMessages();
|
|
22
|
+
const stepAtStart = useRef(null);
|
|
23
|
+
const markSeen = useCallback(() => {
|
|
24
|
+
update({ tourSeen: version });
|
|
25
|
+
}, [update, version]);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (offer) dispatch({ type: "offer" });
|
|
28
|
+
}, [offer]);
|
|
29
|
+
const key = `${state.status}:${state.step}`;
|
|
30
|
+
const { layersOn, projection, surface } = snapshot;
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const now = {
|
|
33
|
+
layersOn,
|
|
34
|
+
projection,
|
|
35
|
+
surface
|
|
36
|
+
};
|
|
37
|
+
const atStart = stepAtStart.current;
|
|
38
|
+
if (atStart === null || atStart.key !== key) {
|
|
39
|
+
stepAtStart.current = {
|
|
40
|
+
key,
|
|
41
|
+
snapshot: now
|
|
42
|
+
};
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (state.status === "welcome") {
|
|
46
|
+
if (stepDone({ kind: "layer-on" }, atStart.snapshot, now)) {
|
|
47
|
+
markSeen();
|
|
48
|
+
dispatch({ type: "close" });
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (state.status !== "running") return;
|
|
53
|
+
const current = steps[state.step];
|
|
54
|
+
if (current && stepDone(current.advance, atStart.snapshot, now)) {
|
|
55
|
+
const following = steps[state.step + 1];
|
|
56
|
+
announce(following ? message("tools.tour.stepAnnouncement", {
|
|
57
|
+
progress: message("tools.tour.progress", {
|
|
58
|
+
step: state.step + 2,
|
|
59
|
+
total: steps.length
|
|
60
|
+
}),
|
|
61
|
+
heading: resolveText(messages, following.heading),
|
|
62
|
+
text: resolveText(messages, following.text)
|
|
63
|
+
}) : message("tools.tour.finished", {}));
|
|
64
|
+
dispatch({ type: "next" });
|
|
65
|
+
}
|
|
66
|
+
}, [
|
|
67
|
+
key,
|
|
68
|
+
state,
|
|
69
|
+
steps,
|
|
70
|
+
layersOn,
|
|
71
|
+
projection,
|
|
72
|
+
surface,
|
|
73
|
+
announce,
|
|
74
|
+
markSeen,
|
|
75
|
+
messages
|
|
76
|
+
]);
|
|
77
|
+
const start = useCallback(() => {
|
|
78
|
+
markSeen();
|
|
79
|
+
dispatch({
|
|
80
|
+
type: "start",
|
|
81
|
+
steps: steps.length
|
|
82
|
+
});
|
|
83
|
+
setFocusRequest((count) => count + 1);
|
|
84
|
+
}, [markSeen, steps.length]);
|
|
85
|
+
const next = useCallback(() => {
|
|
86
|
+
dispatch({ type: "next" });
|
|
87
|
+
setFocusRequest((count) => count + 1);
|
|
88
|
+
}, []);
|
|
89
|
+
const skip = useCallback(() => {
|
|
90
|
+
markSeen();
|
|
91
|
+
dispatch({ type: "close" });
|
|
92
|
+
announce(announceTourSkipped());
|
|
93
|
+
}, [announce, markSeen]);
|
|
94
|
+
const close = useCallback(() => {
|
|
95
|
+
dispatch({ type: "close" });
|
|
96
|
+
}, []);
|
|
97
|
+
return {
|
|
98
|
+
state,
|
|
99
|
+
steps,
|
|
100
|
+
step: state.status === "running" ? steps[state.step] ?? null : null,
|
|
101
|
+
focusRequest,
|
|
102
|
+
start,
|
|
103
|
+
next,
|
|
104
|
+
skip,
|
|
105
|
+
close
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export { useTour };
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=useTour.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTour.js","names":[],"sources":["../../../src/tools/Tour/useTour.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n announceTourSkipped,\n type DefinitionText,\n initialTourState,\n message,\n resolveText,\n stepDone,\n type TourAdvance,\n type TourSnapshot,\n type TourState,\n tourReducer,\n} from \"@mapcairn/core\";\nimport { useCallback, useEffect, useReducer, useRef, useState } from \"react\";\nimport type { FocusName } from \"../../chrome/focusRegistry\";\nimport { useMessages, useSettings } from \"../../services\";\nimport { useAnnounce } from \"../../ui/LiveRegion\";\n\n/** One step of a tour, as data. The host writes the words. */\nexport interface TourStep {\n /** A stable id, for the host's own wiring (for example \"layers\"). */\n readonly id: string;\n /** A message key or plain text, read in the active language where the card and the announcement show it. */\n readonly heading: DefinitionText;\n /** As `heading`. */\n readonly text: DefinitionText;\n /** What the person does to finish the step. */\n readonly advance: TourAdvance;\n /** The registered control to ring while the step is current, if any. */\n readonly target?: FocusName;\n /** In the panel layout, which side the card sits against. Defaults to start. */\n readonly side?: \"start\" | \"end\";\n}\n\n/** Options for {@link useTour}. */\nexport interface UseTourOptions {\n readonly steps: readonly TourStep[];\n /** The host's tour version, stored in `Settings.tourSeen` once answered. */\n readonly version: number;\n /** Whether to show the welcome card on this page load (`shouldOfferTour`). */\n readonly offer: boolean;\n /** The app state now, which decides when a step is done. */\n readonly snapshot: TourSnapshot;\n}\n\n/** What {@link useTour} returns. */\nexport interface UseTour {\n readonly state: TourState;\n readonly steps: readonly TourStep[];\n /** The current step while running, else null. */\n readonly step: TourStep | null;\n /** Grows each time the card's heading should take focus. */\n readonly focusRequest: number;\n /** Starts at the first step, from the welcome card or from Help. */\n start(): void;\n /** Goes to the next step, or to the ending after the last. */\n next(): void;\n /** Ends the tour from the welcome card or any step. */\n skip(): void;\n /** Closes the ending card. */\n close(): void;\n}\n\n/**\n * Runs the first-visit tour: the welcome card, the steps and the ending.\n *\n * @remarks A step advances when the person does what it asks, judged by\n * `stepDone` against the snapshot taken when the step began, or on Next.\n * An advance by doing announces the next step and leaves focus where it is;\n * Next and start ask the card to focus its heading instead. Taking the\n * tour, skipping it, or turning on a layer while the welcome card is up\n * stores the version, so the tour is not offered again.\n */\nexport function useTour({\n steps,\n version,\n offer,\n snapshot,\n}: UseTourOptions): UseTour {\n const [state, dispatch] = useReducer(tourReducer, initialTourState);\n const [focusRequest, setFocusRequest] = useState(0);\n const { update } = useSettings();\n const announce = useAnnounce();\n const messages = useMessages();\n const stepAtStart = useRef<{ key: string; snapshot: TourSnapshot } | null>(\n null,\n );\n\n const markSeen = useCallback(() => {\n update({ tourSeen: version });\n }, [update, version]);\n\n useEffect(() => {\n if (offer) {\n dispatch({ type: \"offer\" });\n }\n }, [offer]);\n\n const key = `${state.status}:${state.step}`;\n const { layersOn, projection, surface } = snapshot;\n useEffect(() => {\n const now = { layersOn, projection, surface };\n const atStart = stepAtStart.current;\n if (atStart === null || atStart.key !== key) {\n stepAtStart.current = { key, snapshot: now };\n return;\n }\n if (state.status === \"welcome\") {\n // Someone who turns a layer on unprompted has found what the tour is for.\n if (stepDone({ kind: \"layer-on\" }, atStart.snapshot, now)) {\n markSeen();\n dispatch({ type: \"close\" });\n }\n return;\n }\n if (state.status !== \"running\") {\n return;\n }\n const current = steps[state.step];\n if (current && stepDone(current.advance, atStart.snapshot, now)) {\n const following = steps[state.step + 1];\n announce(\n following\n ? message(\"tools.tour.stepAnnouncement\", {\n progress: message(\"tools.tour.progress\", {\n step: state.step + 2,\n total: steps.length,\n }),\n heading: resolveText(messages, following.heading),\n text: resolveText(messages, following.text),\n })\n : message(\"tools.tour.finished\", {}),\n );\n dispatch({ type: \"next\" });\n }\n }, [\n key,\n state,\n steps,\n layersOn,\n projection,\n surface,\n announce,\n markSeen,\n messages,\n ]);\n\n const start = useCallback(() => {\n markSeen();\n dispatch({ type: \"start\", steps: steps.length });\n setFocusRequest((count) => count + 1);\n }, [markSeen, steps.length]);\n\n const next = useCallback(() => {\n dispatch({ type: \"next\" });\n setFocusRequest((count) => count + 1);\n }, []);\n\n const skip = useCallback(() => {\n markSeen();\n dispatch({ type: \"close\" });\n announce(announceTourSkipped());\n }, [announce, markSeen]);\n\n const close = useCallback(() => {\n dispatch({ type: \"close\" });\n }, []);\n\n return {\n state,\n steps,\n step: state.status === \"running\" ? (steps[state.step] ?? null) : null,\n focusRequest,\n start,\n next,\n skip,\n close,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;AA0EA,SAAgB,QAAQ,EACtB,OACA,SACA,OACA,YAC0B;CAC1B,MAAM,CAAC,OAAO,YAAY,WAAW,aAAa,gBAAgB;CAClE,MAAM,CAAC,cAAc,mBAAmB,SAAS,CAAC;CAClD,MAAM,EAAE,WAAW,YAAY;CAC/B,MAAM,WAAW,YAAY;CAC7B,MAAM,WAAW,YAAY;CAC7B,MAAM,cAAc,OAClB,IACF;CAEA,MAAM,WAAW,kBAAkB;EACjC,OAAO,EAAE,UAAU,QAAQ,CAAC;CAC9B,GAAG,CAAC,QAAQ,OAAO,CAAC;CAEpB,gBAAgB;EACd,IAAI,OACF,SAAS,EAAE,MAAM,QAAQ,CAAC;CAE9B,GAAG,CAAC,KAAK,CAAC;CAEV,MAAM,MAAM,GAAG,MAAM,OAAO,GAAG,MAAM;CACrC,MAAM,EAAE,UAAU,YAAY,YAAY;CAC1C,gBAAgB;EACd,MAAM,MAAM;GAAE;GAAU;GAAY;EAAQ;EAC5C,MAAM,UAAU,YAAY;EAC5B,IAAI,YAAY,QAAQ,QAAQ,QAAQ,KAAK;GAC3C,YAAY,UAAU;IAAE;IAAK,UAAU;GAAI;GAC3C;EACF;EACA,IAAI,MAAM,WAAW,WAAW;GAE9B,IAAI,SAAS,EAAE,MAAM,WAAW,GAAG,QAAQ,UAAU,GAAG,GAAG;IACzD,SAAS;IACT,SAAS,EAAE,MAAM,QAAQ,CAAC;GAC5B;GACA;EACF;EACA,IAAI,MAAM,WAAW,WACnB;EAEF,MAAM,UAAU,MAAM,MAAM;EAC5B,IAAI,WAAW,SAAS,QAAQ,SAAS,QAAQ,UAAU,GAAG,GAAG;GAC/D,MAAM,YAAY,MAAM,MAAM,OAAO;GACrC,SACE,YACI,QAAQ,+BAA+B;IACrC,UAAU,QAAQ,uBAAuB;KACvC,MAAM,MAAM,OAAO;KACnB,OAAO,MAAM;IACf,CAAC;IACD,SAAS,YAAY,UAAU,UAAU,OAAO;IAChD,MAAM,YAAY,UAAU,UAAU,IAAI;GAC5C,CAAC,IACD,QAAQ,uBAAuB,CAAC,CAAC,CACvC;GACA,SAAS,EAAE,MAAM,OAAO,CAAC;EAC3B;CACF,GAAG;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAED,MAAM,QAAQ,kBAAkB;EAC9B,SAAS;EACT,SAAS;GAAE,MAAM;GAAS,OAAO,MAAM;EAAO,CAAC;EAC/C,iBAAiB,UAAU,QAAQ,CAAC;CACtC,GAAG,CAAC,UAAU,MAAM,MAAM,CAAC;CAE3B,MAAM,OAAO,kBAAkB;EAC7B,SAAS,EAAE,MAAM,OAAO,CAAC;EACzB,iBAAiB,UAAU,QAAQ,CAAC;CACtC,GAAG,CAAC,CAAC;CAEL,MAAM,OAAO,kBAAkB;EAC7B,SAAS;EACT,SAAS,EAAE,MAAM,QAAQ,CAAC;EAC1B,SAAS,oBAAoB,CAAC;CAChC,GAAG,CAAC,UAAU,QAAQ,CAAC;CAEvB,MAAM,QAAQ,kBAAkB;EAC9B,SAAS,EAAE,MAAM,QAAQ,CAAC;CAC5B,GAAG,CAAC,CAAC;CAEL,OAAO;EACL;EACA;EACA,MAAM,MAAM,WAAW,YAAa,MAAM,MAAM,SAAS,OAAQ;EACjE;EACA;EACA;EACA;EACA;CACF;AACF"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useFormatMessage, useLocationProvider } from "../../services/hooks.js";
|
|
2
|
+
import { useAnnounce } from "../../ui/LiveRegion/LiveRegion.js";
|
|
3
|
+
import { LocationError, PortAbortError, announceLocationError, announceLocationFound, message } from "@mapcairn/core";
|
|
4
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
|
+
import { useLatestRef } from "@mapcairn/react";
|
|
6
|
+
// src/tools/locate/useLocate.ts
|
|
7
|
+
/** What is shown while a fix is being found, as a message to format. */
|
|
8
|
+
var LOCATING_MESSAGE = message("tools.locate.locating", {});
|
|
9
|
+
/**
|
|
10
|
+
* Finds the user's location through the services' `LocationProvider`, one
|
|
11
|
+
* request at a time. Each request has its own `AbortController`, aborted by
|
|
12
|
+
* a newer request, by `cancel` and on unmount; a cancelled request never
|
|
13
|
+
* changes state or announces anything.
|
|
14
|
+
*
|
|
15
|
+
* The outcome is announced through the app-wide announcer, and `message`
|
|
16
|
+
* carries the same wording for the visible status.
|
|
17
|
+
* @remarks Announcing and showing the same error means a screen reader may
|
|
18
|
+
* hear it twice if the app puts `message` in an alert `StatusMessage`;
|
|
19
|
+
* pass `announceErrors: false` in that case.
|
|
20
|
+
*/
|
|
21
|
+
function useLocate(units, { timeoutMs, announceErrors = true } = {}) {
|
|
22
|
+
const provider = useLocationProvider();
|
|
23
|
+
const announce = useAnnounce();
|
|
24
|
+
const providerRef = useLatestRef(provider);
|
|
25
|
+
const announceRef = useLatestRef(announce);
|
|
26
|
+
const formatRef = useLatestRef(useFormatMessage());
|
|
27
|
+
const unitsRef = useLatestRef(units);
|
|
28
|
+
const timeoutRef = useLatestRef(timeoutMs);
|
|
29
|
+
const announceErrorsRef = useLatestRef(announceErrors);
|
|
30
|
+
const controller = useRef(null);
|
|
31
|
+
const [state, setState] = useState("idle");
|
|
32
|
+
const [fix, setFix] = useState(null);
|
|
33
|
+
const [error, setError] = useState(null);
|
|
34
|
+
const [shown, setShown] = useState("");
|
|
35
|
+
const abort = useCallback(() => {
|
|
36
|
+
controller.current?.abort();
|
|
37
|
+
controller.current = null;
|
|
38
|
+
}, []);
|
|
39
|
+
const locate = useCallback(() => {
|
|
40
|
+
abort();
|
|
41
|
+
const own = new AbortController();
|
|
42
|
+
controller.current = own;
|
|
43
|
+
setState("locating");
|
|
44
|
+
setFix(null);
|
|
45
|
+
setError(null);
|
|
46
|
+
setShown(formatRef.current(LOCATING_MESSAGE));
|
|
47
|
+
providerRef.current.locate({
|
|
48
|
+
signal: own.signal,
|
|
49
|
+
timeoutMs: timeoutRef.current
|
|
50
|
+
}).then((found) => {
|
|
51
|
+
if (own.signal.aborted) return;
|
|
52
|
+
const said = announceLocationFound(found.accuracyMetres, unitsRef.current);
|
|
53
|
+
setState("found");
|
|
54
|
+
setFix(found);
|
|
55
|
+
setShown(formatRef.current(said));
|
|
56
|
+
announceRef.current(said);
|
|
57
|
+
}, (failure) => {
|
|
58
|
+
if (own.signal.aborted || failure instanceof PortAbortError) return;
|
|
59
|
+
const kind = failure instanceof LocationError ? failure.kind : "unavailable";
|
|
60
|
+
const said = announceLocationError(kind);
|
|
61
|
+
setState("error");
|
|
62
|
+
setError(kind);
|
|
63
|
+
setShown(formatRef.current(said));
|
|
64
|
+
if (announceErrorsRef.current) announceRef.current(said, { priority: "assertive" });
|
|
65
|
+
});
|
|
66
|
+
}, [
|
|
67
|
+
abort,
|
|
68
|
+
providerRef,
|
|
69
|
+
timeoutRef,
|
|
70
|
+
unitsRef,
|
|
71
|
+
announceRef,
|
|
72
|
+
formatRef,
|
|
73
|
+
announceErrorsRef
|
|
74
|
+
]);
|
|
75
|
+
const cancel = useCallback(() => {
|
|
76
|
+
abort();
|
|
77
|
+
setState("idle");
|
|
78
|
+
setFix(null);
|
|
79
|
+
setError(null);
|
|
80
|
+
setShown("");
|
|
81
|
+
}, [abort]);
|
|
82
|
+
useEffect(() => abort, [abort]);
|
|
83
|
+
return {
|
|
84
|
+
state,
|
|
85
|
+
fix,
|
|
86
|
+
error,
|
|
87
|
+
message: shown,
|
|
88
|
+
locate,
|
|
89
|
+
cancel
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export { useLocate };
|
|
93
|
+
|
|
94
|
+
//# sourceMappingURL=useLocate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLocate.js","names":[],"sources":["../../../src/tools/locate/useLocate.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport {\n announceLocationError,\n announceLocationFound,\n LocationError,\n type LocationErrorKind,\n type LocationFix,\n message,\n PortAbortError,\n type UnitSystem,\n} from \"@mapcairn/core\";\nimport { useLatestRef } from \"@mapcairn/react\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useFormatMessage, useLocationProvider } from \"../../services/hooks\";\nimport { useAnnounce } from \"../../ui/LiveRegion\";\n\n/** What is shown while a fix is being found, as a message to format. */\nconst LOCATING_MESSAGE = message(\"tools.locate.locating\", {});\n\n/** Where a locate request stands. */\nexport type LocateState = \"idle\" | \"locating\" | \"found\" | \"error\";\n\n/** Options for {@link useLocate}. */\nexport interface UseLocateOptions {\n /** Passed to the provider, which decides its own default when absent. */\n readonly timeoutMs?: number;\n /**\n * Whether to announce errors through the announcer. Defaults to true. Pass\n * false when the app shows `message` in an error `StatusMessage`, which is\n * an alert itself, so the error is spoken once. A fix is always announced.\n */\n readonly announceErrors?: boolean;\n}\n\n/** What {@link useLocate} returns. */\nexport interface UseLocate {\n readonly state: LocateState;\n /** The latest fix. Set only while `state` is `found`. */\n readonly fix: LocationFix | null;\n /** Why the last request failed. Set only while `state` is `error`. */\n readonly error: LocationErrorKind | null;\n /**\n * Visible status text for the app to show in a `StatusMessage`: empty when\n * idle, \"Finding your location...\" while locating, the accuracy when found\n * and what went wrong, with a next step, on an error. The text for an error\n * must stay on screen until the user dismisses it.\n */\n readonly message: string;\n /**\n * Asks for the user's location. Call it only from a user action, such as a\n * click on \"Locate me\": the browser may show a permission prompt. A second\n * call cancels the first.\n */\n readonly locate: () => void;\n /**\n * Cancels a request in flight and returns to `idle`. Calling it after a\n * result or an error also returns to `idle`, which is how the app dismisses\n * the status message.\n */\n readonly cancel: () => void;\n}\n\n/**\n * Finds the user's location through the services' `LocationProvider`, one\n * request at a time. Each request has its own `AbortController`, aborted by\n * a newer request, by `cancel` and on unmount; a cancelled request never\n * changes state or announces anything.\n *\n * The outcome is announced through the app-wide announcer, and `message`\n * carries the same wording for the visible status.\n * @remarks Announcing and showing the same error means a screen reader may\n * hear it twice if the app puts `message` in an alert `StatusMessage`;\n * pass `announceErrors: false` in that case.\n */\nexport function useLocate(\n units: UnitSystem,\n { timeoutMs, announceErrors = true }: UseLocateOptions = {},\n): UseLocate {\n const provider = useLocationProvider();\n const announce = useAnnounce();\n const providerRef = useLatestRef(provider);\n const announceRef = useLatestRef(announce);\n const formatRef = useLatestRef(useFormatMessage());\n const unitsRef = useLatestRef(units);\n const timeoutRef = useLatestRef(timeoutMs);\n const announceErrorsRef = useLatestRef(announceErrors);\n const controller = useRef<AbortController | null>(null);\n const [state, setState] = useState<LocateState>(\"idle\");\n const [fix, setFix] = useState<LocationFix | null>(null);\n const [error, setError] = useState<LocationErrorKind | null>(null);\n const [shown, setShown] = useState(\"\");\n\n const abort = useCallback(() => {\n controller.current?.abort();\n controller.current = null;\n }, []);\n\n const locate = useCallback(() => {\n abort();\n const own = new AbortController();\n controller.current = own;\n setState(\"locating\");\n setFix(null);\n setError(null);\n setShown(formatRef.current(LOCATING_MESSAGE));\n providerRef.current\n .locate({ signal: own.signal, timeoutMs: timeoutRef.current })\n .then(\n (found) => {\n if (own.signal.aborted) {\n return;\n }\n const said = announceLocationFound(\n found.accuracyMetres,\n unitsRef.current,\n );\n setState(\"found\");\n setFix(found);\n setShown(formatRef.current(said));\n announceRef.current(said);\n },\n (failure: unknown) => {\n if (own.signal.aborted || failure instanceof PortAbortError) {\n return;\n }\n const kind: LocationErrorKind =\n failure instanceof LocationError ? failure.kind : \"unavailable\";\n const said = announceLocationError(kind);\n setState(\"error\");\n setError(kind);\n setShown(formatRef.current(said));\n if (announceErrorsRef.current) {\n announceRef.current(said, { priority: \"assertive\" });\n }\n },\n );\n }, [\n abort,\n providerRef,\n timeoutRef,\n unitsRef,\n announceRef,\n formatRef,\n announceErrorsRef,\n ]);\n\n const cancel = useCallback(() => {\n abort();\n setState(\"idle\");\n setFix(null);\n setError(null);\n setShown(\"\");\n }, [abort]);\n\n useEffect(() => abort, [abort]);\n\n return { state, fix, error, message: shown, locate, cancel };\n}\n"],"mappings":";;;;;;;AAkBA,IAAM,mBAAmB,QAAQ,yBAAyB,CAAC,CAAC;;;;;;;;;;;;;AAyD5D,SAAgB,UACd,OACA,EAAE,WAAW,iBAAiB,SAA2B,CAAC,GAC/C;CACX,MAAM,WAAW,oBAAoB;CACrC,MAAM,WAAW,YAAY;CAC7B,MAAM,cAAc,aAAa,QAAQ;CACzC,MAAM,cAAc,aAAa,QAAQ;CACzC,MAAM,YAAY,aAAa,iBAAiB,CAAC;CACjD,MAAM,WAAW,aAAa,KAAK;CACnC,MAAM,aAAa,aAAa,SAAS;CACzC,MAAM,oBAAoB,aAAa,cAAc;CACrD,MAAM,aAAa,OAA+B,IAAI;CACtD,MAAM,CAAC,OAAO,YAAY,SAAsB,MAAM;CACtD,MAAM,CAAC,KAAK,UAAU,SAA6B,IAAI;CACvD,MAAM,CAAC,OAAO,YAAY,SAAmC,IAAI;CACjE,MAAM,CAAC,OAAO,YAAY,SAAS,EAAE;CAErC,MAAM,QAAQ,kBAAkB;EAC9B,WAAW,SAAS,MAAM;EAC1B,WAAW,UAAU;CACvB,GAAG,CAAC,CAAC;CAEL,MAAM,SAAS,kBAAkB;EAC/B,MAAM;EACN,MAAM,MAAM,IAAI,gBAAgB;EAChC,WAAW,UAAU;EACrB,SAAS,UAAU;EACnB,OAAO,IAAI;EACX,SAAS,IAAI;EACb,SAAS,UAAU,QAAQ,gBAAgB,CAAC;EAC5C,YAAY,QACT,OAAO;GAAE,QAAQ,IAAI;GAAQ,WAAW,WAAW;EAAQ,CAAC,CAAC,CAC7D,MACE,UAAU;GACT,IAAI,IAAI,OAAO,SACb;GAEF,MAAM,OAAO,sBACX,MAAM,gBACN,SAAS,OACX;GACA,SAAS,OAAO;GAChB,OAAO,KAAK;GACZ,SAAS,UAAU,QAAQ,IAAI,CAAC;GAChC,YAAY,QAAQ,IAAI;EAC1B,IACC,YAAqB;GACpB,IAAI,IAAI,OAAO,WAAW,mBAAmB,gBAC3C;GAEF,MAAM,OACJ,mBAAmB,gBAAgB,QAAQ,OAAO;GACpD,MAAM,OAAO,sBAAsB,IAAI;GACvC,SAAS,OAAO;GAChB,SAAS,IAAI;GACb,SAAS,UAAU,QAAQ,IAAI,CAAC;GAChC,IAAI,kBAAkB,SACpB,YAAY,QAAQ,MAAM,EAAE,UAAU,YAAY,CAAC;EAEvD,CACF;CACJ,GAAG;EACD;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAED,MAAM,SAAS,kBAAkB;EAC/B,MAAM;EACN,SAAS,MAAM;EACf,OAAO,IAAI;EACX,SAAS,IAAI;EACb,SAAS,EAAE;CACb,GAAG,CAAC,KAAK,CAAC;CAEV,gBAAgB,OAAO,CAAC,KAAK,CAAC;CAE9B,OAAO;EAAE;EAAO;EAAK;EAAO,SAAS;EAAO;EAAQ;CAAO;AAC7D"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Icon } from "../Icon/Icon.js";
|
|
2
|
+
import { button } from "./Button.styles.js";
|
|
3
|
+
import { LoaderCircle } from "lucide-react";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
// src/ui/Button/Button.tsx
|
|
6
|
+
/**
|
|
7
|
+
* A text button, 48 px tall at least. Defaults to `type="button"` so it never
|
|
8
|
+
* submits a form by accident.
|
|
9
|
+
*/
|
|
10
|
+
function Button({ variant = "primary", icon, mirrorInRtl = false, busy = false, disabled = false, type = "button", className, onClick, children, ...rest }) {
|
|
11
|
+
const classes = button({ variant });
|
|
12
|
+
const handleClick = (event) => {
|
|
13
|
+
if (disabled || busy) {
|
|
14
|
+
event.preventDefault();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
onClick?.(event);
|
|
18
|
+
};
|
|
19
|
+
const leading = busy ? LoaderCircle : icon;
|
|
20
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
21
|
+
...rest,
|
|
22
|
+
type,
|
|
23
|
+
className: className ? `${classes} ${className}` : classes,
|
|
24
|
+
"aria-busy": busy ? true : void 0,
|
|
25
|
+
"aria-disabled": disabled ? true : void 0,
|
|
26
|
+
onClick: handleClick,
|
|
27
|
+
children: [leading ? /* @__PURE__ */ jsx(Icon, {
|
|
28
|
+
icon: leading,
|
|
29
|
+
spinning: busy,
|
|
30
|
+
mirrorInRtl
|
|
31
|
+
}) : null, children]
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export { Button };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=Button.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Button.js","names":[],"sources":["../../../src/ui/Button/Button.tsx"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright MapCairn contributors\nimport { LoaderCircle } from \"lucide-react\";\nimport type { ButtonHTMLAttributes, MouseEvent } from \"react\";\nimport { Icon, type IconComponent } from \"../Icon\";\nimport { button } from \"./Button.css\";\n\n/** Props for {@link Button}. */\nexport interface ButtonProps\n extends Omit<\n ButtonHTMLAttributes<HTMLButtonElement>,\n \"aria-busy\" | \"aria-disabled\" | \"disabled\"\n > {\n /** `primary` is the filled accent button, `quiet` has no fill. Defaults to `primary`. */\n readonly variant?: \"primary\" | \"secondary\" | \"quiet\";\n /** A leading icon, imported by name from `lucide-react`. */\n readonly icon?: IconComponent;\n /** Flips the leading icon when the page reads right to left, for one that points along the reading direction. */\n readonly mirrorInRtl?: boolean;\n /**\n * Replaces the leading icon with a loader and sets `aria-busy`. Clicks are\n * ignored while busy. The visible label stays, so the name is stable.\n */\n readonly busy?: boolean;\n /**\n * Disables the action with `aria-disabled`. The button stays focusable and\n * clicks do nothing.\n */\n readonly disabled?: boolean;\n}\n\n/**\n * A text button, 48 px tall at least. Defaults to `type=\"button\"` so it never\n * submits a form by accident.\n */\nexport function Button({\n variant = \"primary\",\n icon,\n mirrorInRtl = false,\n busy = false,\n disabled = false,\n type = \"button\",\n className,\n onClick,\n children,\n ...rest\n}: ButtonProps) {\n const classes = button({ variant });\n const handleClick = (event: MouseEvent<HTMLButtonElement>) => {\n if (disabled || busy) {\n event.preventDefault();\n return;\n }\n onClick?.(event);\n };\n const leading = busy ? LoaderCircle : icon;\n return (\n <button\n {...rest}\n type={type}\n className={className ? `${classes} ${className}` : classes}\n aria-busy={busy ? true : undefined}\n aria-disabled={disabled ? true : undefined}\n onClick={handleClick}\n >\n {leading ? (\n <Icon icon={leading} spinning={busy} mirrorInRtl={mirrorInRtl} />\n ) : null}\n {children}\n </button>\n );\n}\n"],"mappings":";;;;;;;;;AAmCA,SAAgB,OAAO,EACrB,UAAU,WACV,MACA,cAAc,OACd,OAAO,OACP,WAAW,OACX,OAAO,UACP,WACA,SACA,UACA,GAAG,QACW;CACd,MAAM,UAAU,OAAO,EAAE,QAAQ,CAAC;CAClC,MAAM,eAAe,UAAyC;EAC5D,IAAI,YAAY,MAAM;GACpB,MAAM,eAAe;GACrB;EACF;EACA,UAAU,KAAK;CACjB;CACA,MAAM,UAAU,OAAO,eAAe;CACtC,OACE,qBAAC,UAAD;EACE,GAAI;EACE;EACN,WAAW,YAAY,GAAG,QAAQ,GAAG,cAAc;EACnD,aAAW,OAAO,OAAO,KAAA;EACzB,iBAAe,WAAW,OAAO,KAAA;EACjC,SAAS;EANX,UAAA,CAQG,UACC,oBAAC,MAAD;GAAM,MAAM;GAAS,UAAU;GAAmB;EAAc,CAAA,IAC9D,MACH,QACK;;AAEZ"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* empty css */
|
|
2
|
+
/* empty css */
|
|
3
|
+
import { createRuntimeFn } from "@vanilla-extract/recipes/createRuntimeFn";
|
|
4
|
+
// src/ui/Button/Button.css.ts
|
|
5
|
+
var button = createRuntimeFn({
|
|
6
|
+
defaultClassName: "hyk3vw0",
|
|
7
|
+
variantClassNames: { variant: {
|
|
8
|
+
primary: "hyk3vw1",
|
|
9
|
+
secondary: "hyk3vw2",
|
|
10
|
+
quiet: "hyk3vw3"
|
|
11
|
+
} },
|
|
12
|
+
defaultVariants: { variant: "primary" },
|
|
13
|
+
compoundVariants: []
|
|
14
|
+
});
|
|
15
|
+
export { button };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=Button.styles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Button.styles.js","names":[],"sources":[],"mappings":""}
|