@delacour/react-native-ui 0.6.0 → 0.6.1-alpha.20261104101519
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 +3 -3
- package/src/components/avatar/AGENTS.md +2 -1
- package/src/components/avatar/avatar-badge.tsx +4 -2
- package/src/components/avatar/avatar.render.test.tsx +37 -1
- package/src/components/avatar/avatar.tsx +17 -7
- package/src/components/avatar/avatar.utils.test.ts +51 -0
- package/src/components/avatar/avatar.utils.ts +41 -9
- package/src/components/avatar/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1-alpha.20261104101519",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -182,8 +182,8 @@
|
|
|
182
182
|
"expo-blur": ">=15",
|
|
183
183
|
"@maplibre/maplibre-react-native": "^11.5.0",
|
|
184
184
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
185
|
-
"@delacour/react-native-bottom-sheet": "
|
|
186
|
-
"@delacour/react-native-charts": "
|
|
185
|
+
"@delacour/react-native-bottom-sheet": "0.6.1-alpha.20261104101519",
|
|
186
|
+
"@delacour/react-native-charts": "0.6.1-alpha.20261104101519",
|
|
187
187
|
"@legendapp/list": ">=3.3",
|
|
188
188
|
"react-native-skia": ">=3.9.0",
|
|
189
189
|
"expo-linear-gradient": ">=15",
|
|
@@ -30,7 +30,8 @@ Compound root plus `Avatar.Badge` and `Avatar.Group`.
|
|
|
30
30
|
because there is nothing to swap.
|
|
31
31
|
- **A failure is remembered by key, not by object.** `resolveAvatarSourceKey`
|
|
32
32
|
turns the source into a JSON string — every request field (URI, bundle,
|
|
33
|
-
method, sorted headers, body, cache mode)
|
|
33
|
+
method, sorted headers, body, cache mode) and the entry's width, height and
|
|
34
|
+
scale, or the asset's module id — and
|
|
34
35
|
`onError` stores the key that failed. Keyed by object identity, an
|
|
35
36
|
inline `source={{ uri }}` is new every render and a dead URL would retry
|
|
36
37
|
forever; keyed by URI alone, a token refresh in the headers would never retry.
|
|
@@ -2,6 +2,7 @@ import type { ReactElement, ReactNode } from "react";
|
|
|
2
2
|
import { View, type ViewProps } from "react-native";
|
|
3
3
|
import { cn } from "../../lib/cn";
|
|
4
4
|
import { useAvatarPart } from "./avatar.context";
|
|
5
|
+
import { resolveAvatarBadgeHasContent } from "./avatar.utils";
|
|
5
6
|
import { type AvatarBadgePlacement, type AvatarColor, avatarVariants } from "./avatar.variants";
|
|
6
7
|
|
|
7
8
|
export type AvatarBadgeProps = ViewProps & {
|
|
@@ -18,7 +19,8 @@ export type AvatarBadgeProps = ViewProps & {
|
|
|
18
19
|
* An overlay pinned to a corner of the avatar — an unread count, a presence dot.
|
|
19
20
|
*
|
|
20
21
|
* **Empty, it is a presence dot** sized to the face it sits on and ringed in the
|
|
21
|
-
* page background, so it reads as separate from the photo underneath.
|
|
22
|
+
* page background, so it reads as separate from the photo underneath. Empty
|
|
23
|
+
* means no element: a `0` or `""` from a conditional count is a dot too. Given
|
|
22
24
|
* children, it is only the pin: a `Badge` or an `Icon` dropped in keeps its own
|
|
23
25
|
* look, which is what lets a count and a dot share one part.
|
|
24
26
|
*
|
|
@@ -38,7 +40,7 @@ export function AvatarBadge({
|
|
|
38
40
|
const { size } = useAvatarPart("Avatar.Badge");
|
|
39
41
|
const slots = avatarVariants({ size, placement, color });
|
|
40
42
|
|
|
41
|
-
if (children
|
|
43
|
+
if (!resolveAvatarBadgeHasContent(children)) {
|
|
42
44
|
return <View className={cn(slots.badge(), slots.dot(), className)} pointerEvents="none" {...props} />;
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, jest, test } from "@jest/globals";
|
|
2
2
|
import { Platform, Text } from "react-native";
|
|
3
3
|
import { fireEvent, renderWithProviders, screen } from "../../test/render";
|
|
4
|
-
import { Avatar } from "./avatar";
|
|
4
|
+
import { Avatar, type AvatarProps } from "./avatar";
|
|
5
5
|
import { useAvatar } from "./avatar.context";
|
|
6
6
|
|
|
7
7
|
const PHOTO = { uri: "https://example.com/aria.png" };
|
|
@@ -92,6 +92,16 @@ describe("Avatar", () => {
|
|
|
92
92
|
expect(screen.queryByTestId("photo")).not.toBeOnTheScreen();
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
+
test("imageProps takes no source override, so source alone drives the request and the retry key", () => {
|
|
96
|
+
const overrides: NonNullable<AvatarProps["imageProps"]>[] = [
|
|
97
|
+
// @ts-expect-error `src` would win over `source` and skip the retry key.
|
|
98
|
+
{ src: "https://example.com/other.png" },
|
|
99
|
+
// @ts-expect-error `srcSet` would win over `source` and skip the retry key.
|
|
100
|
+
{ srcSet: "https://example.com/other.png 1x" },
|
|
101
|
+
];
|
|
102
|
+
expect(overrides).toHaveLength(2);
|
|
103
|
+
});
|
|
104
|
+
|
|
95
105
|
test("imageProps are forwarded to the image", async () => {
|
|
96
106
|
await renderWithProviders(
|
|
97
107
|
<Avatar imageProps={{ accessibilityLabel: "portrait", testID: "photo" }} name="Aria Whitlock" source={PHOTO} />
|
|
@@ -119,6 +129,17 @@ describe("Avatar", () => {
|
|
|
119
129
|
expect(screen.queryByTestId("photo")).not.toBeOnTheScreen();
|
|
120
130
|
});
|
|
121
131
|
|
|
132
|
+
test("a new source mounts a new image, so a late error from the old one cannot fail it", async () => {
|
|
133
|
+
const view = await renderWithProviders(
|
|
134
|
+
<Avatar imageProps={{ testID: "photo" }} name="Aria Whitlock" source={PHOTO} />
|
|
135
|
+
);
|
|
136
|
+
const expired = screen.getByTestId("photo");
|
|
137
|
+
await view.rerender(<Avatar imageProps={{ testID: "photo" }} name="Aria Whitlock" source={OTHER_PHOTO} />);
|
|
138
|
+
expect(screen.getByTestId("photo")).not.toBe(expired);
|
|
139
|
+
await fireEvent(expired, "error", { nativeEvent: { error: "expired" } });
|
|
140
|
+
expect(screen.getByTestId("photo")).toBeOnTheScreen();
|
|
141
|
+
});
|
|
142
|
+
|
|
122
143
|
test("a different source is tried again", async () => {
|
|
123
144
|
const view = await renderWithProviders(
|
|
124
145
|
<Avatar imageProps={{ testID: "photo" }} name="Aria Whitlock" source={PHOTO} />
|
|
@@ -154,6 +175,11 @@ describe("Avatar", () => {
|
|
|
154
175
|
}
|
|
155
176
|
});
|
|
156
177
|
|
|
178
|
+
test("a pressable avatar with no name, fallback or label is still named", async () => {
|
|
179
|
+
await renderWithProviders(<Avatar onPress={jest.fn()} />);
|
|
180
|
+
expect(screen.getByRole("button", { name: "Avatar" })).toBeOnTheScreen();
|
|
181
|
+
});
|
|
182
|
+
|
|
157
183
|
test("isDisabled is announced and blocks onPress and onLongPress", async () => {
|
|
158
184
|
const onPress = jest.fn();
|
|
159
185
|
const onLongPress = jest.fn();
|
|
@@ -204,6 +230,16 @@ describe("Avatar", () => {
|
|
|
204
230
|
expect(screen.getByTestId("dot")).toHaveProp("pointerEvents", "none");
|
|
205
231
|
});
|
|
206
232
|
|
|
233
|
+
test("an Avatar.Badge given a 0 from a conditional count is a dot, not a raw 0", async () => {
|
|
234
|
+
const unread = 0;
|
|
235
|
+
await renderWithProviders(
|
|
236
|
+
<Avatar name="Aria Whitlock">
|
|
237
|
+
<Avatar.Badge testID="dot">{unread && <Text>{unread}</Text>}</Avatar.Badge>
|
|
238
|
+
</Avatar>
|
|
239
|
+
);
|
|
240
|
+
expect(screen.getByTestId("dot")).toHaveProp("pointerEvents", "none");
|
|
241
|
+
});
|
|
242
|
+
|
|
207
243
|
test("Avatar.Badge with children pins them and lets touches through", async () => {
|
|
208
244
|
await renderWithProviders(
|
|
209
245
|
<Avatar name="Aria Whitlock">
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from "./avatar.utils";
|
|
15
15
|
import {
|
|
16
16
|
AVATAR_FOREGROUND_TOKEN,
|
|
17
|
+
AVATAR_SIZE_POINTS,
|
|
17
18
|
type AvatarColor,
|
|
18
19
|
type AvatarSize,
|
|
19
20
|
type AvatarVariant,
|
|
@@ -38,8 +39,12 @@ export type AvatarProps = Omit<PressableProps, "asChild" | "busy" | "children" |
|
|
|
38
39
|
/** Default `md`, or the enclosing group's size. */
|
|
39
40
|
size?: AvatarSize;
|
|
40
41
|
isDisabled?: boolean;
|
|
41
|
-
/**
|
|
42
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Passed to the `Image`. `onError` still fires, after the fallback has taken
|
|
44
|
+
* over. `src` and `srcSet` are left out: `Image` prefers them to `source`, and
|
|
45
|
+
* `source` is what the retry key is built from.
|
|
46
|
+
*/
|
|
47
|
+
imageProps?: Omit<ImageProps, "source" | "src" | "srcSet">;
|
|
43
48
|
/** Overlays — an `Avatar.Badge`. */
|
|
44
49
|
children?: ReactNode;
|
|
45
50
|
};
|
|
@@ -72,14 +77,16 @@ function AvatarRoot({
|
|
|
72
77
|
);
|
|
73
78
|
|
|
74
79
|
// Remembered by key, not by object: an inline `source={{ uri }}` is a new
|
|
75
|
-
// object every render, and a new URI has to retry on its own.
|
|
76
|
-
|
|
80
|
+
// object every render, and a new URI has to retry on its own. The edge is in
|
|
81
|
+
// the key because a list of resolutions picks its entry by the face's size.
|
|
82
|
+
const sourceKey = resolveAvatarSourceKey(source, { edge: AVATAR_SIZE_POINTS[size] });
|
|
77
83
|
const [failedKey, setFailedKey] = useState<string | null>(null);
|
|
78
84
|
const showsImage = resolveAvatarShowsImage({ sourceKey, failedKey });
|
|
79
85
|
const slots = avatarVariants({ size, variant, color, isDisabled, hasImage: showsImage });
|
|
80
86
|
|
|
81
87
|
const initials = fallback?.trim() || resolveAvatarInitials(name);
|
|
82
|
-
const
|
|
88
|
+
const isInteractive = resolveAvatarInteractive({ onPress, onLongPress });
|
|
89
|
+
const label = resolveAvatarAccessibilityLabel({ accessibilityLabel, name, fallback, isInteractive });
|
|
83
90
|
|
|
84
91
|
const onImageError = (event: ImageErrorEvent) => {
|
|
85
92
|
setFailedKey(sourceKey);
|
|
@@ -87,7 +94,9 @@ function AvatarRoot({
|
|
|
87
94
|
};
|
|
88
95
|
|
|
89
96
|
// The fallback is always painted and the image goes over it, so the circle is
|
|
90
|
-
// never empty while a picture loads or after it fails.
|
|
97
|
+
// never empty while a picture loads or after it fails. The image is keyed by
|
|
98
|
+
// its source, so a new source mounts a new image: a late error from the old
|
|
99
|
+
// request cannot mark the new one as failed.
|
|
91
100
|
const face = (
|
|
92
101
|
<View className={slots.face()}>
|
|
93
102
|
{initials ? (
|
|
@@ -99,6 +108,7 @@ function AvatarRoot({
|
|
|
99
108
|
)}
|
|
100
109
|
{showsImage && source !== undefined ? (
|
|
101
110
|
<Image
|
|
111
|
+
key={sourceKey}
|
|
102
112
|
accessibilityIgnoresInvertColors
|
|
103
113
|
{...imageProps}
|
|
104
114
|
className={cn(slots.image(), imageProps?.className)}
|
|
@@ -109,7 +119,7 @@ function AvatarRoot({
|
|
|
109
119
|
</View>
|
|
110
120
|
);
|
|
111
121
|
|
|
112
|
-
if (!
|
|
122
|
+
if (!isInteractive) {
|
|
113
123
|
return (
|
|
114
124
|
<AvatarProvider value={context}>
|
|
115
125
|
<View
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
import { createElement, Fragment } from "react";
|
|
3
3
|
import {
|
|
4
|
+
AVATAR_BUTTON_FALLBACK_LABEL,
|
|
4
5
|
flattenAvatarChildren,
|
|
5
6
|
resolveAvatarAccessibilityLabel,
|
|
7
|
+
resolveAvatarBadgeHasContent,
|
|
6
8
|
resolveAvatarGroup,
|
|
7
9
|
resolveAvatarInitials,
|
|
8
10
|
resolveAvatarOverflowLabel,
|
|
@@ -36,6 +38,10 @@ describe("resolveAvatarInitials", () => {
|
|
|
36
38
|
expect(resolveAvatarInitials("𝒜ria 𝒲hitlock")).toBe("𝒜𝒲");
|
|
37
39
|
});
|
|
38
40
|
|
|
41
|
+
test("keeps an accent from a decomposed (NFD) name", () => {
|
|
42
|
+
expect(resolveAvatarInitials("E\u0301mile Zola")).toBe("\u00C9Z");
|
|
43
|
+
});
|
|
44
|
+
|
|
39
45
|
test("keeps letters in scripts with no case", () => {
|
|
40
46
|
expect(resolveAvatarInitials("森山 健二")).toBe("森健");
|
|
41
47
|
});
|
|
@@ -89,6 +95,32 @@ describe("resolveAvatarSourceKey", () => {
|
|
|
89
95
|
);
|
|
90
96
|
});
|
|
91
97
|
|
|
98
|
+
test("changes when entry dimensions change, so a list that picks another uri can retry", () => {
|
|
99
|
+
const before = resolveAvatarSourceKey([
|
|
100
|
+
{ uri: "https://a.test/1x.png", width: 40, height: 40 },
|
|
101
|
+
{ uri: "https://a.test/2x.png", width: 80, height: 80 },
|
|
102
|
+
]);
|
|
103
|
+
const swapped = resolveAvatarSourceKey([
|
|
104
|
+
{ uri: "https://a.test/1x.png", width: 80, height: 80 },
|
|
105
|
+
{ uri: "https://a.test/2x.png", width: 40, height: 40 },
|
|
106
|
+
]);
|
|
107
|
+
expect(before).not.toBe(swapped);
|
|
108
|
+
expect(resolveAvatarSourceKey({ uri: "https://a.test/1.png", scale: 1 })).not.toBe(
|
|
109
|
+
resolveAvatarSourceKey({ uri: "https://a.test/1.png", scale: 2 })
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("changes with the face's edge for a list, so a failure at one size does not block another resolution", () => {
|
|
114
|
+
const list = [{ uri: "https://a.test/1x.png" }, { uri: "https://a.test/3x.png" }];
|
|
115
|
+
expect(resolveAvatarSourceKey(list, { edge: 32 })).not.toBe(resolveAvatarSourceKey(list, { edge: 96 }));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("ignores the edge for a single source, which loads the same image at every size", () => {
|
|
119
|
+
const one = { uri: "https://a.test/1.png" };
|
|
120
|
+
expect(resolveAvatarSourceKey(one, { edge: 32 })).toBe(resolveAvatarSourceKey(one, { edge: 96 }));
|
|
121
|
+
expect(resolveAvatarSourceKey([one], { edge: 32 })).toBe(resolveAvatarSourceKey([one], { edge: 96 }));
|
|
122
|
+
});
|
|
123
|
+
|
|
92
124
|
test("changes when the uri changes", () => {
|
|
93
125
|
expect(resolveAvatarSourceKey({ uri: "https://a.test/1.png" })).not.toBe(
|
|
94
126
|
resolveAvatarSourceKey({ uri: "https://a.test/2.png" })
|
|
@@ -186,6 +218,12 @@ describe("resolveAvatarOverflowLabel", () => {
|
|
|
186
218
|
});
|
|
187
219
|
|
|
188
220
|
describe("resolveAvatarAccessibilityLabel", () => {
|
|
221
|
+
test("a pressable avatar with nothing to read gets a generic name, never a bare button", () => {
|
|
222
|
+
expect(resolveAvatarAccessibilityLabel({ isInteractive: true })).toBe(AVATAR_BUTTON_FALLBACK_LABEL);
|
|
223
|
+
expect(resolveAvatarAccessibilityLabel({ isInteractive: true, name: " " })).toBe(AVATAR_BUTTON_FALLBACK_LABEL);
|
|
224
|
+
expect(resolveAvatarAccessibilityLabel({ isInteractive: true, name: "Aria Whitlock" })).toBe("Aria Whitlock");
|
|
225
|
+
});
|
|
226
|
+
|
|
189
227
|
test("prefers the name", () => {
|
|
190
228
|
expect(resolveAvatarAccessibilityLabel({ name: "Aria Whitlock", fallback: "AW" })).toBe("Aria Whitlock");
|
|
191
229
|
});
|
|
@@ -204,6 +242,19 @@ describe("resolveAvatarAccessibilityLabel", () => {
|
|
|
204
242
|
});
|
|
205
243
|
});
|
|
206
244
|
|
|
245
|
+
describe("resolveAvatarBadgeHasContent", () => {
|
|
246
|
+
test("is false for nothing to pin, including a 0 or an empty string from a conditional", () => {
|
|
247
|
+
for (const children of [undefined, null, false, true, 0, "", [0, ""]]) {
|
|
248
|
+
expect(resolveAvatarBadgeHasContent(children)).toBe(false);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("is true when there is an element to pin", () => {
|
|
253
|
+
expect(resolveAvatarBadgeHasContent(createElement("Badge"))).toBe(true);
|
|
254
|
+
expect(resolveAvatarBadgeHasContent([0, createElement("Badge", { key: "badge" })])).toBe(true);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
207
258
|
describe("flattenAvatarChildren", () => {
|
|
208
259
|
const face = (name: string, key?: string) => createElement("Face", { key, name });
|
|
209
260
|
const names = (children: Parameters<typeof flattenAvatarChildren>[0]) =>
|
|
@@ -13,9 +13,9 @@ import { Children, cloneElement, Fragment, isValidElement, type ReactElement, ty
|
|
|
13
13
|
export type AvatarSourceCache = "default" | "reload" | "force-cache" | "only-if-cached";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* A URI request, as `Image` accepts it. Holds
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* A URI request, as `Image` accepts it. Holds the fields that change what is
|
|
17
|
+
* fetched, and `width`, `height` and `scale`, which pick the entry of a
|
|
18
|
+
* multi-resolution list that is fetched.
|
|
19
19
|
*/
|
|
20
20
|
export type AvatarSourceRequest = {
|
|
21
21
|
uri?: string | null;
|
|
@@ -24,6 +24,9 @@ export type AvatarSourceRequest = {
|
|
|
24
24
|
headers?: Readonly<Record<string, string>> | null;
|
|
25
25
|
body?: string | null;
|
|
26
26
|
cache?: AvatarSourceCache | null;
|
|
27
|
+
width?: number | null;
|
|
28
|
+
height?: number | null;
|
|
29
|
+
scale?: number | null;
|
|
27
30
|
};
|
|
28
31
|
|
|
29
32
|
/** One entry of an image source, as `Image` accepts it — a URI request or a bundled asset's module id. */
|
|
@@ -47,10 +50,12 @@ function initialOf(word: string): string {
|
|
|
47
50
|
*
|
|
48
51
|
* Walks code points rather than UTF-16 units, so a letter outside the basic
|
|
49
52
|
* plane is kept whole instead of drawn as a replacement box, and skips leading
|
|
50
|
-
* punctuation so a quoted nickname still gives a letter.
|
|
53
|
+
* punctuation so a quoted nickname still gives a letter. The name is put in
|
|
54
|
+
* NFC form first: in NFD form, as servers and macOS input often send it,
|
|
55
|
+
* `É` is `E` plus a combining accent, and only the `E` would be kept.
|
|
51
56
|
*/
|
|
52
57
|
export function resolveAvatarInitials(name: string | undefined): string {
|
|
53
|
-
const words = (name ?? "").trim().split(/\s+/).filter(Boolean);
|
|
58
|
+
const words = (name ?? "").normalize("NFC").trim().split(/\s+/).filter(Boolean);
|
|
54
59
|
if (words.length === 0) return "";
|
|
55
60
|
const first = initialOf(words[0] ?? "");
|
|
56
61
|
const last = words.length > 1 ? initialOf(words[words.length - 1] ?? "") : "";
|
|
@@ -70,6 +75,9 @@ function entryKey(entry: AvatarSourceEntry): unknown {
|
|
|
70
75
|
headers,
|
|
71
76
|
entry.body ?? null,
|
|
72
77
|
entry.cache ?? null,
|
|
78
|
+
entry.width ?? null,
|
|
79
|
+
entry.height ?? null,
|
|
80
|
+
entry.scale ?? null,
|
|
73
81
|
];
|
|
74
82
|
}
|
|
75
83
|
|
|
@@ -81,17 +89,22 @@ function entryKey(entry: AvatarSourceEntry): unknown {
|
|
|
81
89
|
* keyed by identity, a failed image would retry forever. Keyed by URI alone, a
|
|
82
90
|
* token refresh in the headers would never retry at all. Every field that
|
|
83
91
|
* changes the request is part of the key, including `cache`, so a cache miss
|
|
84
|
-
* retries when the cache mode changes.
|
|
92
|
+
* retries when the cache mode changes. So are `width`, `height` and `scale`:
|
|
93
|
+
* with the same URIs, new dimensions can pick another entry of a list. The key is a JSON array, so no value can
|
|
85
94
|
* look like a separator, and header order is normalised so an equal request is
|
|
86
95
|
* an equal key.
|
|
87
96
|
*
|
|
88
97
|
* `null` means there is nothing to load.
|
|
89
98
|
*/
|
|
90
|
-
export function resolveAvatarSourceKey(
|
|
99
|
+
export function resolveAvatarSourceKey(
|
|
100
|
+
source: AvatarSource | null | undefined,
|
|
101
|
+
{ edge }: { edge?: number } = {}
|
|
102
|
+
): string | null {
|
|
91
103
|
if (source === null || source === undefined) return null;
|
|
92
104
|
const entries: readonly AvatarSourceEntry[] = Array.isArray(source) ? source : [source as AvatarSourceEntry];
|
|
93
105
|
const keys = entries.map(entryKey).filter((key) => key !== null);
|
|
94
|
-
|
|
106
|
+
if (keys.length === 0) return null;
|
|
107
|
+
return JSON.stringify(keys.length > 1 && edge !== undefined ? [...keys, ["edge", edge]] : keys);
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
/**
|
|
@@ -150,6 +163,17 @@ export function flattenAvatarChildren(children: ReactNode): ReactElement[] {
|
|
|
150
163
|
});
|
|
151
164
|
}
|
|
152
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Whether an `Avatar.Badge` has something to pin, or is a presence dot.
|
|
168
|
+
*
|
|
169
|
+
* Only an element counts. `{unread && <Badge />}` with `unread = 0` passes a
|
|
170
|
+
* raw `0`, and a raw string or number in a `View` throws on native. So `0`,
|
|
171
|
+
* `""` and other non-elements mean the dot.
|
|
172
|
+
*/
|
|
173
|
+
export function resolveAvatarBadgeHasContent(children: ReactNode): boolean {
|
|
174
|
+
return Children.toArray(children).some(isValidElement);
|
|
175
|
+
}
|
|
176
|
+
|
|
153
177
|
/** The most the overflow tile draws before it reads `99+`. Three digits do not fit a small circle. */
|
|
154
178
|
export const AVATAR_OVERFLOW_DISPLAY_MAX = 99;
|
|
155
179
|
|
|
@@ -166,23 +190,31 @@ export function resolveAvatarOverflowLabel(overflow: number): { text: string; ac
|
|
|
166
190
|
};
|
|
167
191
|
}
|
|
168
192
|
|
|
193
|
+
/** What a pressable avatar is called when it has no label, name or fallback. */
|
|
194
|
+
export const AVATAR_BUTTON_FALLBACK_LABEL = "Avatar";
|
|
195
|
+
|
|
169
196
|
/**
|
|
170
197
|
* The name a screen reader gives the avatar: an explicit label, else the
|
|
171
198
|
* person's name, else the fallback text. Blank strings count as absent, so an
|
|
172
199
|
* avatar with nothing to say is not announced as an empty image.
|
|
200
|
+
*
|
|
201
|
+
* A pressable avatar always has a name. With nothing else to read it is
|
|
202
|
+
* {@link AVATAR_BUTTON_FALLBACK_LABEL}, so it is never announced as only "button".
|
|
173
203
|
*/
|
|
174
204
|
export function resolveAvatarAccessibilityLabel({
|
|
175
205
|
accessibilityLabel,
|
|
176
206
|
name,
|
|
177
207
|
fallback,
|
|
208
|
+
isInteractive = false,
|
|
178
209
|
}: {
|
|
179
210
|
accessibilityLabel?: string;
|
|
180
211
|
name?: string;
|
|
181
212
|
fallback?: string;
|
|
213
|
+
isInteractive?: boolean;
|
|
182
214
|
}): string | undefined {
|
|
183
215
|
for (const candidate of [accessibilityLabel, name, fallback]) {
|
|
184
216
|
const trimmed = candidate?.trim();
|
|
185
217
|
if (trimmed) return trimmed;
|
|
186
218
|
}
|
|
187
|
-
return undefined;
|
|
219
|
+
return isInteractive ? AVATAR_BUTTON_FALLBACK_LABEL : undefined;
|
|
188
220
|
}
|
|
@@ -8,6 +8,7 @@ export {
|
|
|
8
8
|
useAvatarGroupContext,
|
|
9
9
|
} from "./avatar.context";
|
|
10
10
|
export {
|
|
11
|
+
AVATAR_BUTTON_FALLBACK_LABEL,
|
|
11
12
|
AVATAR_OVERFLOW_DISPLAY_MAX,
|
|
12
13
|
type AvatarSource,
|
|
13
14
|
type AvatarSourceCache,
|
|
@@ -15,6 +16,7 @@ export {
|
|
|
15
16
|
type AvatarSourceRequest,
|
|
16
17
|
flattenAvatarChildren,
|
|
17
18
|
resolveAvatarAccessibilityLabel,
|
|
19
|
+
resolveAvatarBadgeHasContent,
|
|
18
20
|
resolveAvatarGroup,
|
|
19
21
|
resolveAvatarInitials,
|
|
20
22
|
resolveAvatarOverflowLabel,
|