@delacour/react-native-ui 0.1.0-alpha.20260925062531 → 0.1.0-alpha.20260925062901
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 -2
- package/src/components/avatar/AGENTS.md +106 -0
- package/src/components/avatar/avatar-badge.tsx +51 -0
- package/src/components/avatar/avatar-group.tsx +139 -0
- package/src/components/avatar/avatar.context.tsx +93 -0
- package/src/components/avatar/avatar.tsx +201 -0
- package/src/components/avatar/avatar.utils.test.ts +174 -0
- package/src/components/avatar/avatar.utils.ts +141 -0
- package/src/components/avatar/avatar.variants.test.ts +251 -0
- package/src/components/avatar/avatar.variants.ts +270 -0
- package/src/components/avatar/index.ts +39 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.20260925062901",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"./styles/tokens": "./src/styles/tokens.css",
|
|
35
35
|
"./styles/theme": "./src/styles/theme.css",
|
|
36
36
|
"./accordion": "./src/components/accordion/index.ts",
|
|
37
|
+
"./avatar": "./src/components/avatar/index.ts",
|
|
37
38
|
"./badge": "./src/components/badge/index.ts",
|
|
38
39
|
"./bottom-sheet": "./src/components/bottom-sheet/index.ts",
|
|
39
40
|
"./button": "./src/components/button/index.ts",
|
|
@@ -90,7 +91,7 @@
|
|
|
90
91
|
},
|
|
91
92
|
"peerDependencies": {
|
|
92
93
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
93
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
94
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20260925062901",
|
|
94
95
|
"@gorhom/bottom-sheet": "^5.2.8",
|
|
95
96
|
"@legendapp/list": ">=3.3",
|
|
96
97
|
"expo-linear-gradient": ">=15",
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Avatar
|
|
2
|
+
|
|
3
|
+
A person, as a picture — with an initials fallback, an overlay slot for a count
|
|
4
|
+
or a presence dot, and a group that stacks several faces and counts the rest.
|
|
5
|
+
Compound root plus `Avatar.Badge` and `Avatar.Group`.
|
|
6
|
+
|
|
7
|
+
`import { Avatar } from "@delacour/react-native-ui/avatar";`
|
|
8
|
+
|
|
9
|
+
## Files
|
|
10
|
+
|
|
11
|
+
| File | What it holds |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `index.ts` | → `@delacour/react-native-ui/avatar` |
|
|
14
|
+
| `avatar.tsx` | Root + the `Object.assign` compound surface |
|
|
15
|
+
| `avatar-badge.tsx` | `Avatar.Badge` — the corner overlay, a presence dot when empty |
|
|
16
|
+
| `avatar-group.tsx` | `Avatar.Group`, plus its internal `Item` and `Overflow` leaves |
|
|
17
|
+
| `avatar.context.tsx` | `AvatarProvider`, `AvatarGroupProvider`, `useAvatar()`, `useAvatarContext()`, `useAvatarGroupContext()`, `useAvatarPart()` |
|
|
18
|
+
| `avatar.variants.ts` | Pure `tv()` slots, `AVATAR_SIZE_POINTS`, `resolveAvatarSize`, `resolveAvatarOverlap`, `resolveAvatarInteractive` |
|
|
19
|
+
| `avatar.variants.test.ts` | |
|
|
20
|
+
| `avatar.utils.ts` | Pure non-styling logic — initials, the image-retry key, group overflow maths, the spoken label |
|
|
21
|
+
| `avatar.utils.test.ts` | |
|
|
22
|
+
|
|
23
|
+
## Design
|
|
24
|
+
|
|
25
|
+
- **The fallback is always painted; the image goes on top.** The face renders
|
|
26
|
+
its initials (or a person glyph) first and mounts the `Image` absolutely over
|
|
27
|
+
them. A picture that is still loading, or that never loads, leaves the fallback
|
|
28
|
+
showing rather than an empty circle — there is no loading state to manage
|
|
29
|
+
because there is nothing to swap.
|
|
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
|
|
33
|
+
inline `source={{ uri }}` is new every render and a dead URL would retry
|
|
34
|
+
forever; keyed by URI alone, a token refresh in the headers would never retry.
|
|
35
|
+
A consumer's `imageProps.onError` fires after the fallback has taken over.
|
|
36
|
+
- **The root carries the edge; there is no `self-start`.** A fixed width and
|
|
37
|
+
height is something a column's `stretch` never overrides, so the avatar keeps
|
|
38
|
+
its size without the `self-start` a [`Badge`](../badge/AGENTS.md) needs — and
|
|
39
|
+
it leaves the parent's alignment alone. With `self-start`, every avatar in an
|
|
40
|
+
`items-end` or `items-center` row was pinned to the row's top: mixed sizes lost
|
|
41
|
+
their baseline and a list row's avatar sat above its title. Found on the
|
|
42
|
+
simulator; a test now forbids any `self-*` on the root or the group.
|
|
43
|
+
- **Two boxes, not one.** `root` is unclipped and holds the overlays; `face` is
|
|
44
|
+
the clipped circle. An `Avatar.Badge` hangs over the circle's edge, and a
|
|
45
|
+
single clipped box would cut it in half. A test asserts `overflow-hidden` is on
|
|
46
|
+
`face` and never on `root`.
|
|
47
|
+
- **Initials are the first letter of the first and last word.** `Mary Jane
|
|
48
|
+
Watson` is `MW`, the way a person signs, and `Cher` is `C`.
|
|
49
|
+
`resolveAvatarInitials` walks code points rather than UTF-16 units, so a letter
|
|
50
|
+
outside the basic plane is never split into a replacement box, and skips
|
|
51
|
+
leading punctuation so `"(Kate)"` still gives `K`. `fallback` overrides the
|
|
52
|
+
drawn text only; `name` is still what a screen reader reads.
|
|
53
|
+
- **Two axes paint the fallback, on `Badge`'s six colours.** `variant` is `soft`
|
|
54
|
+
or `solid`, `color` is `default` … `info`, and the twelve cells live in
|
|
55
|
+
`compoundVariants` with the same fills `Badge` uses, so an avatar and a badge
|
|
56
|
+
of one colour read as one family. `AVATAR_FOREGROUND_TOKEN` gives the person
|
|
57
|
+
glyph the same shade as the initials; a test pins each entry to the token the
|
|
58
|
+
`fallbackLabel` slot resolves to, and checks every token exists in both themes.
|
|
59
|
+
- **A neutral face has a hairline edge; a photo does not.** `muted` and
|
|
60
|
+
`secondary` sit a percent or two from the page in light — on the simulator a
|
|
61
|
+
`default` fallback and the `+N` tile all but vanished. They draw
|
|
62
|
+
`border-border` inside the box, so nothing moves, and the edge goes while a
|
|
63
|
+
photo is mounted (`hasImage`), since a grey line around a face reads as a
|
|
64
|
+
frame. The coloured fallbacks have fill enough without one.
|
|
65
|
+
- **A size is a fixed edge, unlike a badge.** `sm`/`md`/`lg`/`xl` are
|
|
66
|
+
32/40/48/64 points. Faces line up against each other and against list rows, and
|
|
67
|
+
a circle that grew with OS font scaling would break the stack; the initials
|
|
68
|
+
therefore set `allowFontScaling={false}` and are sized to fit the smallest
|
|
69
|
+
step. `AVATAR_SIZE_POINTS` restates the scale as numbers because the group's
|
|
70
|
+
overlap is a runtime margin and cannot be a class; a test pins the two
|
|
71
|
+
together.
|
|
72
|
+
- **An avatar is content until it is given something to do** — the rule
|
|
73
|
+
[`Badge`](../badge/AGENTS.md) follows. With no `onPress` or `onLongPress` it
|
|
74
|
+
is a plain `View` announced as an image (or not announced at all when it has
|
|
75
|
+
no name); supply either and it becomes a [`Pressable`](../pressable/AGENTS.md)
|
|
76
|
+
announced as a button. `resolveAvatarInteractive` is that decision.
|
|
77
|
+
- **The avatar is one element to assistive technology.** The root is
|
|
78
|
+
`accessible`, so an `Avatar.Badge` inside it is never focused on its own. A
|
|
79
|
+
presence dot has no words; put the status into the avatar's
|
|
80
|
+
`accessibilityLabel` (`"Kate Austen, online"`).
|
|
81
|
+
- **`Avatar.Badge` is a pin, and a dot only when empty.** Given children — a
|
|
82
|
+
`Badge` with a count, an `Icon` — it positions them and adds nothing else, so a
|
|
83
|
+
count keeps the look it has everywhere else. Empty, it draws a dot sized to
|
|
84
|
+
the face and ringed in `border-background`, which separates it from the photo
|
|
85
|
+
underneath. `placement` is `top-right` or `bottom-right`.
|
|
86
|
+
- **The group's ring belongs to the slot, not the face.** Each child is wrapped
|
|
87
|
+
in an item with `border-2 border-background`, so a face in a group keeps the
|
|
88
|
+
same edge it has alone, and the stack reads as separate people on any surface.
|
|
89
|
+
The `+N` tile sits in the same wrapper, so it lines up with the faces.
|
|
90
|
+
- **The first face is on top, and the list still reads first to last.**
|
|
91
|
+
`zIndex` counts down across the items instead of the children being reversed,
|
|
92
|
+
and the overlap is a negative `marginStart`, so the order in the tree — and
|
|
93
|
+
therefore the order a screen reader walks — is the written order in both
|
|
94
|
+
writing directions. The group is `role="list"` and each item `listitem`.
|
|
95
|
+
- **`max` caps the faces, not the row; `total` counts people never passed.**
|
|
96
|
+
Five children at `max={3}` is three faces and `+2`; three children with
|
|
97
|
+
`total={40}` is `+37`, without forty elements in the tree.
|
|
98
|
+
`resolveAvatarGroup` clamps a negative or fractional `max` and ignores a
|
|
99
|
+
`total` below the child count rather than drawing `+-2`.
|
|
100
|
+
- **The overflow tile draws at most `99+` and reads the real number.** Three
|
|
101
|
+
digits do not fit a small circle; a screen reader has room for `1234 more`.
|
|
102
|
+
`onOverflowPress` turns the tile into a button — to open the full list.
|
|
103
|
+
- **The overlap defaults to a third of the edge and is clamped.**
|
|
104
|
+
`resolveAvatarOverlap` never slides a face further than a whole face nor
|
|
105
|
+
backwards, and `overlap={0}` closes the stack into a plain row. A child's own
|
|
106
|
+
`size` still beats the group's, but the overlap is computed from the group's.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from "react";
|
|
2
|
+
import { View, type ViewProps } from "react-native";
|
|
3
|
+
import { cn } from "../../lib/cn";
|
|
4
|
+
import { useAvatarPart } from "./avatar.context";
|
|
5
|
+
import { type AvatarBadgePlacement, type AvatarColor, avatarVariants } from "./avatar.variants";
|
|
6
|
+
|
|
7
|
+
export type AvatarBadgeProps = ViewProps & {
|
|
8
|
+
/** Which corner the overlay is pinned to. Default `top-right`. */
|
|
9
|
+
placement?: AvatarBadgePlacement;
|
|
10
|
+
/** The presence dot's colour, when there are no children. Default `success`. */
|
|
11
|
+
color?: AvatarColor;
|
|
12
|
+
className?: string;
|
|
13
|
+
/** What to pin — a `Badge` with a count, an `Icon`. Leave empty for a presence dot. */
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* An overlay pinned to a corner of the avatar — an unread count, a presence dot.
|
|
19
|
+
*
|
|
20
|
+
* **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. Given
|
|
22
|
+
* children, it is only the pin: a `Badge` or an `Icon` dropped in keeps its own
|
|
23
|
+
* look, which is what lets a count and a dot share one part.
|
|
24
|
+
*
|
|
25
|
+
* It sits in the avatar's unclipped root, never inside the circle, so it can
|
|
26
|
+
* hang over the edge without being cut in half.
|
|
27
|
+
*
|
|
28
|
+
* A dot says nothing to a screen reader on its own: the avatar is one element to
|
|
29
|
+
* assistive technology, so put the status into its `accessibilityLabel`.
|
|
30
|
+
*/
|
|
31
|
+
export function AvatarBadge({
|
|
32
|
+
placement = "top-right",
|
|
33
|
+
color = "success",
|
|
34
|
+
className,
|
|
35
|
+
children,
|
|
36
|
+
...props
|
|
37
|
+
}: AvatarBadgeProps): ReactElement {
|
|
38
|
+
const { size } = useAvatarPart("Avatar.Badge");
|
|
39
|
+
const slots = avatarVariants({ size, placement, color });
|
|
40
|
+
|
|
41
|
+
if (children === undefined || children === null || children === false) {
|
|
42
|
+
return <View className={cn(slots.badge(), slots.dot(), className)} pointerEvents="none" {...props} />;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<View className={slots.badge({ className })} pointerEvents="box-none" {...props}>
|
|
47
|
+
{children}
|
|
48
|
+
</View>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
AvatarBadge.displayName = "DelacourUI.Avatar.Badge";
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Children, isValidElement, type ReactElement, type ReactNode, useMemo } from "react";
|
|
2
|
+
import { View, type ViewProps } from "react-native";
|
|
3
|
+
import { Pressable } from "../pressable";
|
|
4
|
+
import { Text } from "../text";
|
|
5
|
+
import { type AvatarGroupContextValue, AvatarGroupProvider } from "./avatar.context";
|
|
6
|
+
import { resolveAvatarGroup, resolveAvatarOverflowLabel } from "./avatar.utils";
|
|
7
|
+
import { type AvatarSize, avatarVariants, resolveAvatarOverlap } from "./avatar.variants";
|
|
8
|
+
|
|
9
|
+
export type AvatarGroupProps = ViewProps & {
|
|
10
|
+
/** Size for every avatar in the stack. A child's own `size` still wins. Default `md`. */
|
|
11
|
+
size?: AvatarSize;
|
|
12
|
+
/** How many faces to show; the rest are counted into a trailing `+N`. Caps the faces, not the row. */
|
|
13
|
+
max?: number;
|
|
14
|
+
/** How many people there are, when the children are only the first few of them. */
|
|
15
|
+
total?: number;
|
|
16
|
+
/** Points each face slides under the one before it. Default a third of the size; `0` makes a plain row. */
|
|
17
|
+
overlap?: number;
|
|
18
|
+
/** Makes the `+N` tile a button — to open the full list, say. */
|
|
19
|
+
onOverflowPress?: () => void;
|
|
20
|
+
className?: string;
|
|
21
|
+
children?: ReactNode;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type AvatarGroupItemProps = {
|
|
25
|
+
/** Position in the stack, first face first. */
|
|
26
|
+
index: number;
|
|
27
|
+
/** How many items the stack holds, overflow tile included. */
|
|
28
|
+
count: number;
|
|
29
|
+
/** The overlap, in points. */
|
|
30
|
+
shift: number;
|
|
31
|
+
children: ReactNode;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* One slot in the stack: the ring, the overlap and the stacking order.
|
|
36
|
+
*
|
|
37
|
+
* The ring is here rather than on the face so a face in a group is the same edge
|
|
38
|
+
* as one outside it. `zIndex` counts down so the first face sits on top without
|
|
39
|
+
* reversing the order a screen reader walks — the list is still read first to
|
|
40
|
+
* last, in both writing directions, because the overlap is a `marginStart`.
|
|
41
|
+
*/
|
|
42
|
+
function AvatarGroupItem({ index, count, shift, children }: AvatarGroupItemProps): ReactElement {
|
|
43
|
+
return (
|
|
44
|
+
<View
|
|
45
|
+
className={avatarVariants().groupItem()}
|
|
46
|
+
role="listitem"
|
|
47
|
+
style={{ marginStart: index === 0 ? 0 : -shift, zIndex: count - index }}
|
|
48
|
+
>
|
|
49
|
+
{children}
|
|
50
|
+
</View>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
AvatarGroupItem.displayName = "DelacourUI.Avatar.Group.Item";
|
|
54
|
+
|
|
55
|
+
type AvatarGroupOverflowProps = {
|
|
56
|
+
size: AvatarSize;
|
|
57
|
+
overflow: number;
|
|
58
|
+
onPress?: () => void;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The trailing `+N` tile, the same edge as a face.
|
|
63
|
+
*
|
|
64
|
+
* It draws at most `99+` and reads the real number, because three digits do not
|
|
65
|
+
* fit a small circle and a screen reader has room for all of them.
|
|
66
|
+
*/
|
|
67
|
+
function AvatarGroupOverflow({ size, overflow, onPress }: AvatarGroupOverflowProps): ReactElement {
|
|
68
|
+
const slots = avatarVariants({ size });
|
|
69
|
+
const { text, accessibilityLabel } = resolveAvatarOverflowLabel(overflow);
|
|
70
|
+
const label = (
|
|
71
|
+
<Text allowFontScaling={false} className={slots.overflowLabel()} numberOfLines={1}>
|
|
72
|
+
{text}
|
|
73
|
+
</Text>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (onPress) {
|
|
77
|
+
return (
|
|
78
|
+
<Pressable
|
|
79
|
+
accessibilityLabel={accessibilityLabel}
|
|
80
|
+
accessibilityRole="button"
|
|
81
|
+
className={slots.overflow()}
|
|
82
|
+
feedback="scale"
|
|
83
|
+
onPress={onPress}
|
|
84
|
+
>
|
|
85
|
+
{label}
|
|
86
|
+
</Pressable>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<View accessibilityLabel={accessibilityLabel} accessible className={slots.overflow()}>
|
|
92
|
+
{label}
|
|
93
|
+
</View>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
AvatarGroupOverflow.displayName = "DelacourUI.Avatar.Group.Overflow";
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A row of avatars, each overlapping the one after it, with the people who did
|
|
100
|
+
* not fit counted at the end.
|
|
101
|
+
*
|
|
102
|
+
* Children are kept in the order they were written. `max` caps the faces and
|
|
103
|
+
* `total` says how many people there really are, so three children out of forty
|
|
104
|
+
* reads `+37` without forty elements in the tree.
|
|
105
|
+
*/
|
|
106
|
+
export function AvatarGroup({
|
|
107
|
+
size,
|
|
108
|
+
max,
|
|
109
|
+
total,
|
|
110
|
+
overlap,
|
|
111
|
+
onOverflowPress,
|
|
112
|
+
className,
|
|
113
|
+
children,
|
|
114
|
+
...props
|
|
115
|
+
}: AvatarGroupProps): ReactElement {
|
|
116
|
+
const items = Children.toArray(children).filter(isValidElement);
|
|
117
|
+
const { visible, overflow } = resolveAvatarGroup({ count: items.length, max, total });
|
|
118
|
+
const shift = resolveAvatarOverlap({ size: size ?? "md", overlap });
|
|
119
|
+
const count = visible + (overflow > 0 ? 1 : 0);
|
|
120
|
+
const context = useMemo<AvatarGroupContextValue>(() => ({ size }), [size]);
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<AvatarGroupProvider value={context}>
|
|
124
|
+
<View className={avatarVariants().group({ className })} role="list" {...props}>
|
|
125
|
+
{items.slice(0, visible).map((child, index) => (
|
|
126
|
+
<AvatarGroupItem count={count} index={index} key={child.key ?? index} shift={shift}>
|
|
127
|
+
{child}
|
|
128
|
+
</AvatarGroupItem>
|
|
129
|
+
))}
|
|
130
|
+
{overflow > 0 ? (
|
|
131
|
+
<AvatarGroupItem count={count} index={visible} shift={shift}>
|
|
132
|
+
<AvatarGroupOverflow onPress={onOverflowPress} overflow={overflow} size={size ?? "md"} />
|
|
133
|
+
</AvatarGroupItem>
|
|
134
|
+
) : null}
|
|
135
|
+
</View>
|
|
136
|
+
</AvatarGroupProvider>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
AvatarGroup.displayName = "DelacourUI.Avatar.Group";
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createContext, type ReactElement, type ReactNode, use } from "react";
|
|
2
|
+
import type { AvatarColor, AvatarSize, AvatarVariant } from "./avatar.variants";
|
|
3
|
+
|
|
4
|
+
export type AvatarContextValue = {
|
|
5
|
+
/** The avatar's resolved size — its own, else its group's, else `md`. */
|
|
6
|
+
size: AvatarSize;
|
|
7
|
+
/** How the fallback surface is painted. */
|
|
8
|
+
variant: AvatarVariant;
|
|
9
|
+
/** What the fallback surface means. */
|
|
10
|
+
color: AvatarColor;
|
|
11
|
+
/** Whether the avatar is disabled. */
|
|
12
|
+
isDisabled: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type AvatarGroupContextValue = {
|
|
16
|
+
/** The size every avatar in the group takes unless it states its own. */
|
|
17
|
+
size?: AvatarSize;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const AvatarContext = createContext<AvatarContextValue | null>(null);
|
|
21
|
+
const AvatarGroupContext = createContext<AvatarGroupContextValue | null>(null);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Supplies the enclosing avatar's size, paint and state to its subtree.
|
|
25
|
+
*
|
|
26
|
+
* Lives in its own module, importing nothing but `avatar.variants`, so a part
|
|
27
|
+
* can read it without importing `./avatar`. That import would close a cycle, and
|
|
28
|
+
* Metro serves a partially initialised module for a cycle — leaving the context
|
|
29
|
+
* `undefined` at import time and red-boxing the app on a cold start.
|
|
30
|
+
*/
|
|
31
|
+
export function AvatarProvider({ value, children }: { value: AvatarContextValue; children: ReactNode }): ReactElement {
|
|
32
|
+
return <AvatarContext value={value}>{children}</AvatarContext>;
|
|
33
|
+
}
|
|
34
|
+
AvatarProvider.displayName = "DelacourUI.Avatar.Provider";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Supplies a group's size to every avatar inside it.
|
|
38
|
+
*
|
|
39
|
+
* Separate from {@link AvatarProvider} because the two nest the other way round:
|
|
40
|
+
* a group holds avatars, and an avatar must not read a size its group never set
|
|
41
|
+
* as though it had been told one.
|
|
42
|
+
*/
|
|
43
|
+
export function AvatarGroupProvider({
|
|
44
|
+
value,
|
|
45
|
+
children,
|
|
46
|
+
}: {
|
|
47
|
+
value: AvatarGroupContextValue;
|
|
48
|
+
children: ReactNode;
|
|
49
|
+
}): ReactElement {
|
|
50
|
+
return <AvatarGroupContext value={value}>{children}</AvatarGroupContext>;
|
|
51
|
+
}
|
|
52
|
+
AvatarGroupProvider.displayName = "DelacourUI.Avatar.Group.Provider";
|
|
53
|
+
|
|
54
|
+
/** The enclosing avatar's context, or null outside an `<Avatar>`. */
|
|
55
|
+
export function useAvatarContext(): AvatarContextValue | null {
|
|
56
|
+
return use(AvatarContext);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The enclosing group's context, or null outside an `<Avatar.Group>`. */
|
|
60
|
+
export function useAvatarGroupContext(): AvatarGroupContextValue | null {
|
|
61
|
+
return use(AvatarGroupContext);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Reads the enclosing avatar's size, paint and state.
|
|
66
|
+
*
|
|
67
|
+
* Lets a custom overlay size itself to the face it sits on without the avatar
|
|
68
|
+
* passing props down. Throws outside an `<Avatar>` — use
|
|
69
|
+
* {@link useAvatarContext} where the enclosing avatar is optional.
|
|
70
|
+
*/
|
|
71
|
+
export function useAvatar(): AvatarContextValue {
|
|
72
|
+
const context = useAvatarContext();
|
|
73
|
+
if (!context) {
|
|
74
|
+
throw new Error("useAvatar must be called inside an <Avatar>.");
|
|
75
|
+
}
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The enclosing avatar's context, for a compound part that cannot work without
|
|
81
|
+
* one.
|
|
82
|
+
*
|
|
83
|
+
* Internal: deliberately not re-exported from `index.ts`. A caller outside the
|
|
84
|
+
* library wants {@link useAvatar}, whose error message names the hook rather
|
|
85
|
+
* than a part.
|
|
86
|
+
*/
|
|
87
|
+
export function useAvatarPart(component: string): AvatarContextValue {
|
|
88
|
+
const context = useAvatarContext();
|
|
89
|
+
if (!context) {
|
|
90
|
+
throw new Error(`${component} must be rendered inside an <Avatar>.`);
|
|
91
|
+
}
|
|
92
|
+
return context;
|
|
93
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useMemo, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Image,
|
|
4
|
+
type ImageErrorEventData,
|
|
5
|
+
type ImageProps,
|
|
6
|
+
type ImageSourcePropType,
|
|
7
|
+
type NativeSyntheticEvent,
|
|
8
|
+
View,
|
|
9
|
+
} from "react-native";
|
|
10
|
+
import { IconPeople } from "../../icons/central";
|
|
11
|
+
import { cn } from "../../lib/cn";
|
|
12
|
+
import { Icon } from "../icon";
|
|
13
|
+
import { Pressable, type PressableProps } from "../pressable";
|
|
14
|
+
import { Text } from "../text";
|
|
15
|
+
import { type AvatarContextValue, AvatarProvider, useAvatarGroupContext } from "./avatar.context";
|
|
16
|
+
import {
|
|
17
|
+
resolveAvatarAccessibilityLabel,
|
|
18
|
+
resolveAvatarInitials,
|
|
19
|
+
resolveAvatarShowsImage,
|
|
20
|
+
resolveAvatarSourceKey,
|
|
21
|
+
} from "./avatar.utils";
|
|
22
|
+
import {
|
|
23
|
+
AVATAR_FOREGROUND_TOKEN,
|
|
24
|
+
type AvatarColor,
|
|
25
|
+
type AvatarSize,
|
|
26
|
+
type AvatarVariant,
|
|
27
|
+
avatarVariants,
|
|
28
|
+
resolveAvatarInteractive,
|
|
29
|
+
resolveAvatarSize,
|
|
30
|
+
} from "./avatar.variants";
|
|
31
|
+
import { AvatarBadge } from "./avatar-badge";
|
|
32
|
+
import { AvatarGroup } from "./avatar-group";
|
|
33
|
+
|
|
34
|
+
export type AvatarProps = Omit<PressableProps, "asChild" | "busy" | "children" | "disabled"> & {
|
|
35
|
+
/** The picture. The fallback shows while it loads, and in its place if it fails. */
|
|
36
|
+
source?: ImageSourcePropType;
|
|
37
|
+
/** The person's name. Read by screen readers, and its initials are the default fallback. */
|
|
38
|
+
name?: string;
|
|
39
|
+
/** Fallback text, when the initials of `name` are not what should show. */
|
|
40
|
+
fallback?: string;
|
|
41
|
+
/** How the fallback surface is painted. */
|
|
42
|
+
variant?: AvatarVariant;
|
|
43
|
+
/** What the fallback surface means. */
|
|
44
|
+
color?: AvatarColor;
|
|
45
|
+
/** Default `md`, or the enclosing group's size. */
|
|
46
|
+
size?: AvatarSize;
|
|
47
|
+
isDisabled?: boolean;
|
|
48
|
+
/** Passed to the `Image`. `onError` still fires, after the fallback has taken over. */
|
|
49
|
+
imageProps?: Omit<ImageProps, "source">;
|
|
50
|
+
/** Overlays — an `Avatar.Badge`. */
|
|
51
|
+
children?: ReactNode;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function AvatarRoot({
|
|
55
|
+
source,
|
|
56
|
+
name,
|
|
57
|
+
fallback,
|
|
58
|
+
variant = "soft",
|
|
59
|
+
color = "default",
|
|
60
|
+
size: sizeProp,
|
|
61
|
+
isDisabled = false,
|
|
62
|
+
imageProps,
|
|
63
|
+
accessibilityLabel,
|
|
64
|
+
onPress,
|
|
65
|
+
onLongPress,
|
|
66
|
+
haptic,
|
|
67
|
+
feedback = "scale",
|
|
68
|
+
pressedScale,
|
|
69
|
+
pressedOpacity,
|
|
70
|
+
className,
|
|
71
|
+
children,
|
|
72
|
+
...props
|
|
73
|
+
}: AvatarProps): ReactElement {
|
|
74
|
+
const group = useAvatarGroupContext();
|
|
75
|
+
const size = resolveAvatarSize({ size: sizeProp, groupSize: group?.size });
|
|
76
|
+
const context = useMemo<AvatarContextValue>(
|
|
77
|
+
() => ({ size, variant, color, isDisabled }),
|
|
78
|
+
[size, variant, color, isDisabled]
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Remembered by key, not by object: an inline `source={{ uri }}` is a new
|
|
82
|
+
// object every render, and a new URI has to retry on its own.
|
|
83
|
+
const sourceKey = resolveAvatarSourceKey(source);
|
|
84
|
+
const [failedKey, setFailedKey] = useState<string | null>(null);
|
|
85
|
+
const showsImage = resolveAvatarShowsImage({ sourceKey, failedKey });
|
|
86
|
+
const slots = avatarVariants({ size, variant, color, isDisabled, hasImage: showsImage });
|
|
87
|
+
|
|
88
|
+
const initials = fallback?.trim() || resolveAvatarInitials(name);
|
|
89
|
+
const label = resolveAvatarAccessibilityLabel({ accessibilityLabel, name, fallback });
|
|
90
|
+
|
|
91
|
+
const onImageError = (event: NativeSyntheticEvent<ImageErrorEventData>) => {
|
|
92
|
+
setFailedKey(sourceKey);
|
|
93
|
+
imageProps?.onError?.(event);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// The fallback is always painted and the image goes over it, so the circle is
|
|
97
|
+
// never empty while a picture loads or after it fails.
|
|
98
|
+
const face = (
|
|
99
|
+
<View className={slots.face()}>
|
|
100
|
+
{initials ? (
|
|
101
|
+
<Text allowFontScaling={false} className={slots.fallbackLabel()} numberOfLines={1}>
|
|
102
|
+
{initials}
|
|
103
|
+
</Text>
|
|
104
|
+
) : (
|
|
105
|
+
<Icon className={slots.icon()} color={AVATAR_FOREGROUND_TOKEN[variant][color]} icon={IconPeople} />
|
|
106
|
+
)}
|
|
107
|
+
{showsImage && source !== undefined ? (
|
|
108
|
+
<Image
|
|
109
|
+
accessibilityIgnoresInvertColors
|
|
110
|
+
{...imageProps}
|
|
111
|
+
className={cn(slots.image(), imageProps?.className)}
|
|
112
|
+
onError={onImageError}
|
|
113
|
+
source={source}
|
|
114
|
+
/>
|
|
115
|
+
) : null}
|
|
116
|
+
</View>
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
if (!resolveAvatarInteractive({ onPress, onLongPress })) {
|
|
120
|
+
return (
|
|
121
|
+
<AvatarProvider value={context}>
|
|
122
|
+
<View
|
|
123
|
+
accessibilityLabel={label}
|
|
124
|
+
accessibilityRole={label ? "image" : undefined}
|
|
125
|
+
accessible={label !== undefined}
|
|
126
|
+
className={slots.root({ className })}
|
|
127
|
+
{...props}
|
|
128
|
+
>
|
|
129
|
+
{face}
|
|
130
|
+
{children}
|
|
131
|
+
</View>
|
|
132
|
+
</AvatarProvider>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<AvatarProvider value={context}>
|
|
138
|
+
<Pressable
|
|
139
|
+
accessibilityLabel={label}
|
|
140
|
+
accessibilityRole="button"
|
|
141
|
+
className={slots.root({ className })}
|
|
142
|
+
disabled={isDisabled}
|
|
143
|
+
feedback={feedback}
|
|
144
|
+
haptic={haptic}
|
|
145
|
+
onLongPress={onLongPress}
|
|
146
|
+
onPress={onPress}
|
|
147
|
+
pressedOpacity={pressedOpacity}
|
|
148
|
+
pressedScale={pressedScale}
|
|
149
|
+
{...props}
|
|
150
|
+
>
|
|
151
|
+
{face}
|
|
152
|
+
{children}
|
|
153
|
+
</Pressable>
|
|
154
|
+
</AvatarProvider>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A person, as a picture — with their initials underneath for when there is no
|
|
160
|
+
* picture, it is still loading, or it failed.
|
|
161
|
+
*
|
|
162
|
+
* **The fallback is always there.** It is painted first and the image goes on
|
|
163
|
+
* top, so a slow or dead URL never leaves an empty circle. A failure is
|
|
164
|
+
* remembered for that source only: change the URI or its headers and the image
|
|
165
|
+
* is tried again. With no `fallback` and no `name` the fallback is a person glyph.
|
|
166
|
+
*
|
|
167
|
+
* `name` is what a screen reader reads, and its initials — first and last word,
|
|
168
|
+
* `Mary Jane Watson` → `MW` — are the default fallback. `fallback` overrides the
|
|
169
|
+
* drawn text only.
|
|
170
|
+
*
|
|
171
|
+
* `variant` and `color` paint the fallback surface, on the same six colours a
|
|
172
|
+
* `Badge` takes. A size is a fixed edge: faces line up against each other, and a
|
|
173
|
+
* circle cannot grow with OS font scaling the way a label can.
|
|
174
|
+
*
|
|
175
|
+
* **An avatar is content until it is given something to do.** With no `onPress`
|
|
176
|
+
* or `onLongPress` it is a plain `View` announced as an image; supply either and
|
|
177
|
+
* it becomes a `Pressable` announced as a button.
|
|
178
|
+
*
|
|
179
|
+
* Children are overlays: `Avatar.Badge` pins a count or a presence dot to a
|
|
180
|
+
* corner, outside the clipped circle so it is never cut in half.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* <Avatar name="Kate Austen" source={{ uri: user.avatarUrl }} />
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* <Avatar accessibilityLabel="Kate Austen, online" name="Kate Austen">
|
|
187
|
+
* <Avatar.Badge placement="bottom-right" />
|
|
188
|
+
* </Avatar>
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* <Avatar.Group max={3} total={team.length}>
|
|
192
|
+
* {team.map((person) => <Avatar key={person.id} name={person.name} source={person.photo} />)}
|
|
193
|
+
* </Avatar.Group>
|
|
194
|
+
*/
|
|
195
|
+
export const Avatar = Object.assign(AvatarRoot, {
|
|
196
|
+
/** An overlay pinned to a corner — a count, or a presence dot when empty. */
|
|
197
|
+
Badge: AvatarBadge,
|
|
198
|
+
/** A row of overlapping avatars, with the people who did not fit counted at the end. */
|
|
199
|
+
Group: AvatarGroup,
|
|
200
|
+
displayName: "DelacourUI.Avatar",
|
|
201
|
+
});
|