@delacour/react-native-ui 0.6.0-alpha.20261103132733 → 0.6.0-alpha.20261103135630

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@delacour/react-native-ui",
3
- "version": "0.6.0-alpha.20261103132733",
3
+ "version": "0.6.0-alpha.20261103135630",
4
4
  "description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -181,8 +181,8 @@
181
181
  "expo-blur": ">=15",
182
182
  "@maplibre/maplibre-react-native": "^11.5.0",
183
183
  "@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
184
- "@delacour/react-native-bottom-sheet": "0.6.0-alpha.20261103132733",
185
- "@delacour/react-native-charts": "0.6.0-alpha.20261103132733",
184
+ "@delacour/react-native-bottom-sheet": "0.6.0-alpha.20261103135630",
185
+ "@delacour/react-native-charts": "0.6.0-alpha.20261103135630",
186
186
  "@legendapp/list": ">=3.3",
187
187
  "react-native-skia": ">=3.9.0",
188
188
  "expo-linear-gradient": ">=15",
@@ -17,7 +17,7 @@ Compound root plus `Avatar.Badge` and `Avatar.Group`.
17
17
  | `avatar.context.tsx` | `AvatarProvider`, `AvatarGroupProvider`, `useAvatar()`, `useAvatarContext()`, `useAvatarGroupContext()`, `useAvatarPart()` |
18
18
  | `avatar.variants.ts` | Pure `tv()` slots, `AVATAR_SIZE_POINTS`, `resolveAvatarSize`, `resolveAvatarOverlap`, `resolveAvatarInteractive` |
19
19
  | `avatar.variants.test.ts` | |
20
- | `avatar.utils.ts` | Pure non-styling logic — initials, the image-retry key, group overflow maths, the spoken label |
20
+ | `avatar.utils.ts` | Pure non-styling logic — initials, the image-retry key, flattening a group's fragments, group overflow maths, the spoken label |
21
21
  | `avatar.utils.test.ts` | |
22
22
 
23
23
  ## Design
@@ -28,8 +28,9 @@ Compound root plus `Avatar.Badge` and `Avatar.Group`.
28
28
  showing rather than an empty circle — there is no loading state to manage
29
29
  because there is nothing to swap.
30
30
  - **A failure is remembered by key, not by object.** `resolveAvatarSourceKey`
31
- turns the source into a string — URI plus sorted headers, or the asset's module
32
- id — and `onError` stores the key that failed. Keyed by object identity, an
31
+ turns the source into a JSON string — every request field (URI, bundle,
32
+ method, sorted headers, body, cache mode), or the asset's module id — and
33
+ `onError` stores the key that failed. Keyed by object identity, an
33
34
  inline `source={{ uri }}` is new every render and a dead URL would retry
34
35
  forever; keyed by URI alone, a token refresh in the headers would never retry.
35
36
  A consumer's `imageProps.onError` fires after the fallback has taken over.
@@ -1,9 +1,9 @@
1
- import { Children, isValidElement, type ReactElement, type ReactNode, useMemo } from "react";
1
+ import { type ReactElement, type ReactNode, useMemo } from "react";
2
2
  import { View, type ViewProps } from "react-native";
3
3
  import { Pressable } from "../pressable";
4
4
  import { Text } from "../text";
5
5
  import { type AvatarGroupContextValue, AvatarGroupProvider } from "./avatar.context";
6
- import { resolveAvatarGroup, resolveAvatarOverflowLabel } from "./avatar.utils";
6
+ import { flattenAvatarChildren, resolveAvatarGroup, resolveAvatarOverflowLabel } from "./avatar.utils";
7
7
  import { type AvatarSize, avatarVariants, resolveAvatarOverlap } from "./avatar.variants";
8
8
 
9
9
  export type AvatarGroupProps = ViewProps & {
@@ -113,7 +113,7 @@ export function AvatarGroup({
113
113
  children,
114
114
  ...props
115
115
  }: AvatarGroupProps): ReactElement {
116
- const items = Children.toArray(children).filter(isValidElement);
116
+ const items = flattenAvatarChildren(children);
117
117
  const { visible, overflow } = resolveAvatarGroup({ count: items.length, max, total });
118
118
  const shift = resolveAvatarOverlap({ size: size ?? "md", overlap });
119
119
  const count = visible + (overflow > 0 ? 1 : 0);
@@ -1,5 +1,7 @@
1
1
  import { describe, expect, test } from "bun:test";
2
+ import { createElement, Fragment } from "react";
2
3
  import {
4
+ flattenAvatarChildren,
3
5
  resolveAvatarAccessibilityLabel,
4
6
  resolveAvatarGroup,
5
7
  resolveAvatarInitials,
@@ -55,7 +57,36 @@ describe("resolveAvatarSourceKey", () => {
55
57
  });
56
58
 
57
59
  test("names a bundled asset by its module id", () => {
58
- expect(resolveAvatarSourceKey(12)).toBe("asset:12");
60
+ expect(resolveAvatarSourceKey(12)).toBe(JSON.stringify([["asset", 12]]));
61
+ });
62
+
63
+ test("tells a bundled asset from a uri of the same text", () => {
64
+ expect(resolveAvatarSourceKey(3)).not.toBe(resolveAvatarSourceKey({ uri: "3" }));
65
+ });
66
+
67
+ test("changes when the cache mode changes, so a cache miss can retry", () => {
68
+ expect(resolveAvatarSourceKey({ uri: "https://a.test/1.png", cache: "only-if-cached" })).not.toBe(
69
+ resolveAvatarSourceKey({ uri: "https://a.test/1.png", cache: "reload" })
70
+ );
71
+ });
72
+
73
+ test("changes when the method or body changes", () => {
74
+ const get = resolveAvatarSourceKey({ uri: "https://a.test/1.png" });
75
+ const post = resolveAvatarSourceKey({ uri: "https://a.test/1.png", method: "POST" });
76
+ const body = resolveAvatarSourceKey({ uri: "https://a.test/1.png", method: "POST", body: "id=1" });
77
+ expect(new Set([get, post, body]).size).toBe(3);
78
+ });
79
+
80
+ test("does not let header values that look like separators collide", () => {
81
+ expect(resolveAvatarSourceKey({ uri: "https://a.test/1.png", headers: { "X-A": "1&X-B=2" } })).not.toBe(
82
+ resolveAvatarSourceKey({ uri: "https://a.test/1.png", headers: { "X-A": "1", "X-B": "2" } })
83
+ );
84
+ });
85
+
86
+ test("does not let a uri that looks like a separator collide across a list", () => {
87
+ expect(resolveAvatarSourceKey([{ uri: "https://a.test/1.png|uri:https://a.test/2.png" }])).not.toBe(
88
+ resolveAvatarSourceKey([{ uri: "https://a.test/1.png" }, { uri: "https://a.test/2.png" }])
89
+ );
59
90
  });
60
91
 
61
92
  test("changes when the uri changes", () => {
@@ -78,9 +109,9 @@ describe("resolveAvatarSourceKey", () => {
78
109
  });
79
110
 
80
111
  test("reads every entry of a multi-resolution list", () => {
81
- expect(resolveAvatarSourceKey([{ uri: "https://a.test/1x.png" }, { uri: "https://a.test/2x.png" }])).toBe(
82
- "uri:https://a.test/1x.png|uri:https://a.test/2x.png"
83
- );
112
+ const both = resolveAvatarSourceKey([{ uri: "https://a.test/1x.png" }, { uri: "https://a.test/2x.png" }]);
113
+ expect(both).not.toBe(resolveAvatarSourceKey([{ uri: "https://a.test/1x.png" }]));
114
+ expect(both).not.toBe(resolveAvatarSourceKey([{ uri: "https://a.test/2x.png" }]));
84
115
  expect(resolveAvatarSourceKey([])).toBeNull();
85
116
  });
86
117
  });
@@ -172,3 +203,56 @@ describe("resolveAvatarAccessibilityLabel", () => {
172
203
  expect(resolveAvatarAccessibilityLabel({ accessibilityLabel: "You", name: "Aria Whitlock" })).toBe("You");
173
204
  });
174
205
  });
206
+
207
+ describe("flattenAvatarChildren", () => {
208
+ const face = (name: string, key?: string) => createElement("Face", { key, name });
209
+ const names = (children: Parameters<typeof flattenAvatarChildren>[0]) =>
210
+ flattenAvatarChildren(children).map((child) => (child.props as { name: string }).name);
211
+
212
+ test("keeps direct children in the order they were written", () => {
213
+ expect(names([face("Ana"), face("Ben")])).toEqual(["Ana", "Ben"]);
214
+ });
215
+
216
+ test("counts each avatar inside a fragment as its own face", () => {
217
+ expect(names(createElement(Fragment, null, face("Ana"), face("Ben")))).toEqual(["Ana", "Ben"]);
218
+ });
219
+
220
+ test("opens nested fragments and keeps the written order", () => {
221
+ const nested = [
222
+ face("Ana"),
223
+ createElement(Fragment, null, face("Ben"), createElement(Fragment, null, face("Cai"))),
224
+ ];
225
+ expect(names(nested)).toEqual(["Ana", "Ben", "Cai"]);
226
+ });
227
+
228
+ test("drops text, null and booleans", () => {
229
+ expect(names([face("Ana"), "text", null, false, createElement(Fragment, null, true, face("Ben"))])).toEqual([
230
+ "Ana",
231
+ "Ben",
232
+ ]);
233
+ });
234
+
235
+ test("gives every face a unique key when two fragments reuse the same keys", () => {
236
+ const keys = flattenAvatarChildren([
237
+ createElement(Fragment, { key: "a" }, face("Ana", "x")),
238
+ createElement(Fragment, { key: "b" }, face("Ben", "x")),
239
+ ]).map((child) => child.key);
240
+ expect(new Set(keys).size).toBe(2);
241
+ });
242
+
243
+ test("does not let a fragment key and a face key run together into another pair", () => {
244
+ const keys = flattenAvatarChildren([
245
+ createElement(Fragment, { key: "a" }, face("Ana", "b.$c")),
246
+ createElement(Fragment, { key: "a.$b" }, face("Ben", "c")),
247
+ ]).map((child) => child.key);
248
+ expect(new Set(keys).size).toBe(2);
249
+ });
250
+
251
+ test("keeps keys unique through nested fragments", () => {
252
+ const keys = flattenAvatarChildren([
253
+ createElement(Fragment, { key: "a" }, createElement(Fragment, { key: "b" }, face("Ana", "c"))),
254
+ createElement(Fragment, { key: "a.$b" }, face("Ben", "c")),
255
+ ]).map((child) => child.key);
256
+ expect(new Set(keys).size).toBe(2);
257
+ });
258
+ });
@@ -7,8 +7,27 @@
7
7
  * `ImageSourcePropType` for the same reason. See AGENTS.md.
8
8
  */
9
9
 
10
+ import { Children, cloneElement, Fragment, isValidElement, type ReactElement, type ReactNode } from "react";
11
+
12
+ /** The cache modes `Image` accepts for a URI request. */
13
+ export type AvatarSourceCache = "default" | "reload" | "force-cache" | "only-if-cached";
14
+
15
+ /**
16
+ * A URI request, as `Image` accepts it. Holds only the fields that change what
17
+ * is fetched. `width`, `height` and `scale` are left out: they pick an entry,
18
+ * they do not change the request.
19
+ */
20
+ export type AvatarSourceRequest = {
21
+ uri?: string | null;
22
+ bundle?: string | null;
23
+ method?: string | null;
24
+ headers?: Readonly<Record<string, string>> | null;
25
+ body?: string | null;
26
+ cache?: AvatarSourceCache | null;
27
+ };
28
+
10
29
  /** One entry of an image source, as `Image` accepts it — a URI request or a bundled asset's module id. */
11
- export type AvatarSourceEntry = number | { uri?: string; headers?: Record<string, string> };
30
+ export type AvatarSourceEntry = number | AvatarSourceRequest;
12
31
 
13
32
  /** Anything `Image`'s `source` accepts: one entry or a list of resolutions. */
14
33
  export type AvatarSource = AvatarSourceEntry | readonly AvatarSourceEntry[];
@@ -38,15 +57,20 @@ export function resolveAvatarInitials(name: string | undefined): string {
38
57
  return `${first}${last}`.toUpperCase();
39
58
  }
40
59
 
41
- function entryKey(entry: AvatarSourceEntry): string | null {
42
- if (typeof entry === "number") return `asset:${entry}`;
60
+ /** One entry as a plain value, ready for `JSON.stringify`. Header pairs are sorted by name. */
61
+ function entryKey(entry: AvatarSourceEntry): unknown {
62
+ if (typeof entry === "number") return ["asset", entry];
43
63
  if (!entry.uri) return null;
44
- if (!entry.headers) return `uri:${entry.uri}`;
45
- const headers = Object.keys(entry.headers)
46
- .sort()
47
- .map((name) => `${name}=${entry.headers?.[name]}`)
48
- .join("&");
49
- return `uri:${entry.uri}#${headers}`;
64
+ const headers = Object.entries(entry.headers ?? {}).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
65
+ return [
66
+ "uri",
67
+ entry.uri,
68
+ entry.bundle ?? null,
69
+ entry.method ?? null,
70
+ headers,
71
+ entry.body ?? null,
72
+ entry.cache ?? null,
73
+ ];
50
74
  }
51
75
 
52
76
  /**
@@ -55,16 +79,19 @@ function entryKey(entry: AvatarSourceEntry): string | null {
55
79
  * The avatar remembers which source failed by this key rather than by object
56
80
  * identity, because an inline `source={{ uri }}` is a new object every render —
57
81
  * keyed by identity, a failed image would retry forever. Keyed by URI alone, a
58
- * token refresh in the headers would never retry at all. Header order is
59
- * normalised so an equal request is an equal key.
82
+ * token refresh in the headers would never retry at all. Every field that
83
+ * changes the request is part of the key, including `cache`, so a cache miss
84
+ * retries when the cache mode changes. The key is a JSON array, so no value can
85
+ * look like a separator, and header order is normalised so an equal request is
86
+ * an equal key.
60
87
  *
61
88
  * `null` means there is nothing to load.
62
89
  */
63
90
  export function resolveAvatarSourceKey(source: AvatarSource | null | undefined): string | null {
64
91
  if (source === null || source === undefined) return null;
65
92
  const entries: readonly AvatarSourceEntry[] = Array.isArray(source) ? source : [source as AvatarSourceEntry];
66
- const keys = entries.map(entryKey).filter((key): key is string => key !== null);
67
- return keys.length === 0 ? null : keys.join("|");
93
+ const keys = entries.map(entryKey).filter((key) => key !== null);
94
+ return keys.length === 0 ? null : JSON.stringify(keys);
68
95
  }
69
96
 
70
97
  /**
@@ -103,6 +130,26 @@ export function resolveAvatarGroup({ count, max, total }: { count: number; max?:
103
130
  return { visible, overflow: people - visible };
104
131
  }
105
132
 
133
+ /**
134
+ * The faces a group holds, with fragments opened up.
135
+ *
136
+ * `Children.toArray` treats a fragment as one child, so a fragment with two
137
+ * avatars would count as one face and skip `max`. This walks into each
138
+ * fragment, at any depth. A face from a fragment gets the key
139
+ * `JSON.stringify([fragmentKey, faceKey])`. A pair rather than a joined string,
140
+ * because `.$a` + `.$b.$c` and `.$a.$b` + `.$c` join to the same text.
141
+ */
142
+ export function flattenAvatarChildren(children: ReactNode): ReactElement[] {
143
+ return Children.toArray(children).flatMap((child) => {
144
+ if (!isValidElement(child)) return [];
145
+ if (child.type !== Fragment) return [child];
146
+ const { children: inner } = child.props as { children?: ReactNode };
147
+ return flattenAvatarChildren(inner).map((nested) =>
148
+ cloneElement(nested, { key: JSON.stringify([child.key, nested.key]) })
149
+ );
150
+ });
151
+ }
152
+
106
153
  /** The most the overflow tile draws before it reads `99+`. Three digits do not fit a small circle. */
107
154
  export const AVATAR_OVERFLOW_DISPLAY_MAX = 99;
108
155
 
@@ -255,3 +255,9 @@ describe("resolveAvatarInteractive", () => {
255
255
  expect(resolveAvatarInteractive({ onLongPress: () => {} })).toBe(true);
256
256
  });
257
257
  });
258
+
259
+ describe("avatarVariants image", () => {
260
+ test("sizes the image to the face, so a bundled asset is scaled rather than drawn at its own size", () => {
261
+ expect(avatarVariants().image().split(" ")).toEqual(expect.arrayContaining(["absolute", "inset-0", "size-full"]));
262
+ });
263
+ });
@@ -109,7 +109,8 @@ export const avatarVariants = tv({
109
109
  slots: {
110
110
  root: "relative",
111
111
  face: "size-full items-center justify-center overflow-hidden rounded-full",
112
- image: "absolute inset-0",
112
+ /** `size-full` too: a bundled asset brings its own width and height, which `inset-0` does not override. */
113
+ image: "absolute inset-0 size-full",
113
114
  fallbackLabel: "text-center font-medium",
114
115
  /** Edge length the fallback glyph inherits. */
115
116
  icon: "",
@@ -10,7 +10,10 @@ export {
10
10
  export {
11
11
  AVATAR_OVERFLOW_DISPLAY_MAX,
12
12
  type AvatarSource,
13
+ type AvatarSourceCache,
13
14
  type AvatarSourceEntry,
15
+ type AvatarSourceRequest,
16
+ flattenAvatarChildren,
14
17
  resolveAvatarAccessibilityLabel,
15
18
  resolveAvatarGroup,
16
19
  resolveAvatarInitials,