panelui-native 0.107.0 → 0.107.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 (35) hide show
  1. package/lib/module/components/animated-badge/index.js +9 -2
  2. package/lib/module/components/animated-badge/index.js.map +1 -1
  3. package/lib/module/components/button/index.js +7 -2
  4. package/lib/module/components/button/index.js.map +1 -1
  5. package/lib/module/components/map/index.js +22 -4
  6. package/lib/module/components/map/index.js.map +1 -1
  7. package/lib/module/components/map/native-style.js +191 -0
  8. package/lib/module/components/map/native-style.js.map +1 -0
  9. package/lib/module/components/progress/index.js +5 -3
  10. package/lib/module/components/progress/index.js.map +1 -1
  11. package/lib/module/components/skeleton/index.js +3 -3
  12. package/lib/module/components/skeleton/index.js.map +1 -1
  13. package/lib/module/components/spinner/index.js +3 -3
  14. package/lib/module/components/spinner/index.js.map +1 -1
  15. package/lib/module/primitives/text.js +25 -0
  16. package/lib/module/primitives/text.js.map +1 -1
  17. package/lib/typescript/src/components/animated-badge/index.d.ts.map +1 -1
  18. package/lib/typescript/src/components/button/index.d.ts +8 -1
  19. package/lib/typescript/src/components/button/index.d.ts.map +1 -1
  20. package/lib/typescript/src/components/map/index.d.ts +12 -2
  21. package/lib/typescript/src/components/map/index.d.ts.map +1 -1
  22. package/lib/typescript/src/components/map/native-style.d.ts +25 -0
  23. package/lib/typescript/src/components/map/native-style.d.ts.map +1 -0
  24. package/lib/typescript/src/components/progress/index.d.ts.map +1 -1
  25. package/lib/typescript/src/primitives/text.d.ts +16 -0
  26. package/lib/typescript/src/primitives/text.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/components/animated-badge/index.tsx +8 -1
  29. package/src/components/button/index.tsx +13 -2
  30. package/src/components/map/index.tsx +80 -49
  31. package/src/components/map/native-style.ts +205 -0
  32. package/src/components/progress/index.tsx +3 -1
  33. package/src/components/skeleton/index.tsx +1 -1
  34. package/src/components/spinner/index.tsx +1 -1
  35. package/src/primitives/text.tsx +25 -0
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Fits a style written for the browser renderer to the native one.
3
+ *
4
+ * Hosted styles are written and checked in a browser, and the native renderer
5
+ * lags behind it in what it can parse. Where the two disagree, native does not
6
+ * skip the one property. It throws away the whole layer and logs a warning. A
7
+ * street style that labels its stations and bus stops with `text-overlap`
8
+ * reaches a phone with neither, and the only sign is a run of
9
+ * `layer doesn't support this property` lines in the terminal.
10
+ *
11
+ * Everything the style specification lists as unsupported on native is
12
+ * translated here:
13
+ *
14
+ * - `icon-overlap` and `text-overlap` become `icon-allow-overlap` and
15
+ * `text-allow-overlap`, the older names native has always read.
16
+ * - `resampling` becomes `raster-resampling` on a raster layer. Hillshade and
17
+ * color-relief layers have no older name for it, so there it is dropped and
18
+ * the layer keeps the default.
19
+ * - A tiled source with neither `url` nor `tiles` is removed. Hosted styles use
20
+ * one to carry attribution text. Native cannot parse it, and it draws
21
+ * nothing either way.
22
+ *
23
+ * The browser gives the newer name priority when a layer sets both, so the
24
+ * translation overwrites the older one rather than deferring to it.
25
+ */
26
+ import { useEffect, useMemo, useState } from 'react';
27
+ import type { StyleSpecification } from './maplibre';
28
+
29
+ type Json = Record<string, unknown>;
30
+
31
+ function isObject(value: unknown): value is Json {
32
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
33
+ }
34
+
35
+ const TILED_SOURCES = new Set(['vector', 'raster', 'raster-dem']);
36
+
37
+ /**
38
+ * Each overlap mode as the boolean the older property takes.
39
+ *
40
+ * Native has no `cooperative` mode, which lets a symbol overlap others that
41
+ * allow it but not the ones that refuse. It becomes `false`. A label that is
42
+ * hidden on a collision is how every map label behaves already, and a label
43
+ * printed over another cannot be read.
44
+ */
45
+ const OVERLAP = new Map<unknown, boolean>([
46
+ ['always', true],
47
+ ['never', false],
48
+ ['cooperative', false],
49
+ ]);
50
+
51
+ /** The layout with `-overlap` translated, or `null` when it has none. */
52
+ function adaptLayout(layout: Json): Json | null {
53
+ let next: Json | null = null;
54
+ for (const kind of ['icon', 'text']) {
55
+ const key = `${kind}-overlap`;
56
+ if (!(key in layout)) continue;
57
+ next ??= { ...layout };
58
+ const allowed = OVERLAP.get(layout[key]);
59
+ delete next[key];
60
+ // A zoom expression has no direct translation. Dropping it leaves
61
+ // `-allow-overlap` in charge, which is what a browser renderer older than
62
+ // the property did with the same style.
63
+ if (allowed !== undefined) next[`${kind}-allow-overlap`] = allowed;
64
+ }
65
+ return next;
66
+ }
67
+
68
+ /** The paint with `resampling` translated, or `null` when it has none. */
69
+ function adaptPaint(type: unknown, paint: Json): Json | null {
70
+ if (!('resampling' in paint)) return null;
71
+ const { resampling, ...next } = paint;
72
+ if (type === 'raster') next['raster-resampling'] = resampling;
73
+ return next;
74
+ }
75
+
76
+ /**
77
+ * The style with everything native cannot parse translated or removed.
78
+ *
79
+ * Returns the same object when there is nothing to change, and never mutates
80
+ * the one it is given — a caller passing a style constant would otherwise
81
+ * find it rewritten under them.
82
+ */
83
+ export function adaptStyleForNative(style: StyleSpecification): StyleSpecification {
84
+ let changed = false;
85
+
86
+ let sources = style.sources;
87
+ if (isObject(sources)) {
88
+ const kept = Object.entries(sources).filter(
89
+ ([, source]) =>
90
+ !isObject(source) ||
91
+ !TILED_SOURCES.has(source.type as string) ||
92
+ source.url != null ||
93
+ source.tiles != null
94
+ );
95
+ if (kept.length !== Object.keys(sources).length) {
96
+ sources = Object.fromEntries(kept);
97
+ changed = true;
98
+ }
99
+ }
100
+
101
+ let layers = style.layers;
102
+ if (Array.isArray(layers)) {
103
+ let layersChanged = false;
104
+ const next = layers.map((layer: unknown) => {
105
+ if (!isObject(layer)) return layer;
106
+ const layout = isObject(layer.layout) ? adaptLayout(layer.layout) : null;
107
+ const paint = isObject(layer.paint) ? adaptPaint(layer.type, layer.paint) : null;
108
+ if (!layout && !paint) return layer;
109
+ layersChanged = true;
110
+ return { ...layer, ...(layout ? { layout } : null), ...(paint ? { paint } : null) };
111
+ });
112
+ if (layersChanged) {
113
+ layers = next;
114
+ changed = true;
115
+ }
116
+ }
117
+
118
+ return changed ? { ...style, sources, layers } : style;
119
+ }
120
+
121
+ const REMOTE_STYLE = /^https?:\/\//i;
122
+
123
+ /**
124
+ * Adapted styles by URL. Bounded, because an app that switches between a
125
+ * handful of styles should not refetch them on every switch, and one that
126
+ * builds URLs on the fly should not keep every style it ever loaded.
127
+ */
128
+ const loaded = new Map<string, Promise<StyleSpecification | null>>();
129
+ const LOADED_LIMIT = 8;
130
+
131
+ /**
132
+ * Fetches a hosted style and adapts it. Resolves `null` when the style could
133
+ * not be fetched or read, so the caller can leave the URL to the renderer —
134
+ * which has its own cache and request headers, and reports its own errors.
135
+ */
136
+ export function loadStyleForNative(url: string): Promise<StyleSpecification | null> {
137
+ const cached = loaded.get(url);
138
+ if (cached) return cached;
139
+
140
+ const pending = fetch(url)
141
+ .then((response) => (response.ok ? response.json() : null))
142
+ .then((json: unknown) => (isObject(json) ? adaptStyleForNative(json) : null))
143
+ .catch(() => null)
144
+ .then((style) => {
145
+ // A failure is not remembered: the next map to ask should try again.
146
+ if (!style) loaded.delete(url);
147
+ return style;
148
+ });
149
+
150
+ loaded.set(url, pending);
151
+ if (loaded.size > LOADED_LIMIT) {
152
+ loaded.delete(loaded.keys().next().value as string);
153
+ }
154
+ return pending;
155
+ }
156
+
157
+ /** A style passed inline — as an object, or as JSON text — adapted in place. */
158
+ function adaptInline(style: string | StyleSpecification): string | StyleSpecification {
159
+ if (typeof style !== 'string') return adaptStyleForNative(style);
160
+ if (!style.trimStart().startsWith('{')) return style;
161
+ try {
162
+ const parsed: unknown = JSON.parse(style);
163
+ return isObject(parsed) ? adaptStyleForNative(parsed) : style;
164
+ } catch {
165
+ return style;
166
+ }
167
+ }
168
+
169
+ /**
170
+ * `mapStyle`, ready for the native renderer.
171
+ *
172
+ * `undefined` when no style was given, and `null` while a hosted style is
173
+ * still being fetched. Anything that is not an http(s) URL or a style document
174
+ * — an `asset://` path, a provider scheme — is passed through untouched,
175
+ * because only the renderer knows how to load it.
176
+ */
177
+ export function useNativeStyle(
178
+ style: string | StyleSpecification | undefined
179
+ ): string | StyleSpecification | null | undefined {
180
+ const remote = typeof style === 'string' && REMOTE_STYLE.test(style) ? style : undefined;
181
+
182
+ const inline = useMemo(
183
+ () => (style === undefined || remote ? undefined : adaptInline(style)),
184
+ [remote, style]
185
+ );
186
+
187
+ const [fetched, setFetched] = useState<{
188
+ url: string;
189
+ style: string | StyleSpecification;
190
+ }>();
191
+
192
+ useEffect(() => {
193
+ if (!remote) return undefined;
194
+ let live = true;
195
+ loadStyleForNative(remote).then((adapted) => {
196
+ if (live) setFetched({ url: remote, style: adapted ?? remote });
197
+ });
198
+ return () => {
199
+ live = false;
200
+ };
201
+ }, [remote]);
202
+
203
+ if (!remote) return inline;
204
+ return fetched?.url === remote ? fetched.style : null;
205
+ }
@@ -305,7 +305,9 @@ export const Progress = forwardRef<View, ProgressProps>(
305
305
  accessibilityLabel={label}
306
306
  // Indeterminate has no value to announce, so `busy` is the only thing
307
307
  // separating "working, length unknown" from "empty and going nowhere".
308
- accessibilityState={indeterminate ? { busy: true } : undefined}
308
+ // Always a boolean, never `undefined`: Android ignores a state that
309
+ // goes missing, so a bar turning determinate would still read as busy.
310
+ accessibilityState={{ busy: indeterminate }}
309
311
  accessibilityValue={
310
312
  indeterminate
311
313
  ? undefined
@@ -88,7 +88,7 @@ export const Skeleton = memo(function Skeleton({
88
88
  <Animated.View
89
89
  accessibilityRole={announced ? 'progressbar' : undefined}
90
90
  accessibilityLabel={label}
91
- accessibilityState={announced ? { busy: true } : undefined}
91
+ accessibilityState={{ busy: announced }}
92
92
  accessibilityElementsHidden={!announced}
93
93
  importantForAccessibility={announced ? 'auto' : 'no-hide-descendants'}
94
94
  style={[style, animatedStyle]}
@@ -129,7 +129,7 @@ export const Spinner = memo(function Spinner({
129
129
  <Animated.View
130
130
  accessibilityRole={announced ? 'progressbar' : undefined}
131
131
  accessibilityLabel={label}
132
- accessibilityState={announced ? { busy: true } : undefined}
132
+ accessibilityState={{ busy: announced }}
133
133
  accessibilityElementsHidden={!announced}
134
134
  importantForAccessibility={announced ? 'auto' : 'no-hide-descendants'}
135
135
  // After the class, so a colour passed here wins over the theme's. The
@@ -90,3 +90,28 @@ export function textChildren(
90
90
  typeof child === 'string' || typeof child === 'number' ? render(child) : child
91
91
  );
92
92
  }
93
+
94
+ /**
95
+ * The words among `children`, for a part that needs an accessibility label of
96
+ * its own — or `undefined` when the children are not all text.
97
+ *
98
+ * Android needs the label. React Native writes a state such as `busy` into a
99
+ * view's content description, and it rebuilds that description only when
100
+ * there is something to put in it. A view with no label that stops being busy
101
+ * has nothing to rebuild from, so it keeps saying "busy" and never reads its
102
+ * text again.
103
+ *
104
+ * All or nothing, unlike `textChildren`. An element among the children might
105
+ * carry words of its own, such as a count in a badge. A label built from the
106
+ * text beside it would leave those out, and a screen reader that already reads
107
+ * them would stop.
108
+ */
109
+ export function textLabel(children: ReactNode): string | undefined {
110
+ const parts = Children.toArray(children);
111
+ if (!parts.length) return undefined;
112
+ if (!parts.every((part) => typeof part === 'string' || typeof part === 'number')) {
113
+ return undefined;
114
+ }
115
+ const label = parts.join('').trim();
116
+ return label || undefined;
117
+ }