@funstack/ui 0.1.0-alpha.20 → 0.1.0-alpha.21
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/dist/alert-dialog/alert-dialog-root.mjs +2 -1
- package/dist/alert-dialog/alert-dialog-root.mjs.map +1 -1
- package/dist/avatar/avatar-context.mjs +24 -0
- package/dist/avatar/avatar-context.mjs.map +1 -0
- package/dist/avatar/avatar-fallback.d.mts +32 -0
- package/dist/avatar/avatar-fallback.d.mts.map +1 -0
- package/dist/avatar/avatar-fallback.mjs +31 -0
- package/dist/avatar/avatar-fallback.mjs.map +1 -0
- package/dist/avatar/avatar-image.d.mts +37 -0
- package/dist/avatar/avatar-image.d.mts.map +1 -0
- package/dist/avatar/avatar-image.mjs +91 -0
- package/dist/avatar/avatar-image.mjs.map +1 -0
- package/dist/avatar/avatar-root.d.mts +48 -0
- package/dist/avatar/avatar-root.d.mts.map +1 -0
- package/dist/avatar/avatar-root.mjs +81 -0
- package/dist/avatar/avatar-root.mjs.map +1 -0
- package/dist/avatar/index.d.mts +24 -0
- package/dist/avatar/index.d.mts.map +1 -0
- package/dist/avatar/index.mjs +24 -0
- package/dist/avatar/index.mjs.map +1 -0
- package/dist/dialog/dialog-close.d.mts.map +1 -1
- package/dist/dialog/dialog-close.mjs +14 -3
- package/dist/dialog/dialog-close.mjs.map +1 -1
- package/dist/dialog/dialog-context.mjs +7 -1
- package/dist/dialog/dialog-context.mjs.map +1 -1
- package/dist/dialog/dialog-popup.d.mts +1 -0
- package/dist/dialog/dialog-popup.d.mts.map +1 -1
- package/dist/dialog/dialog-popup.mjs +20 -2
- package/dist/dialog/dialog-popup.mjs.map +1 -1
- package/dist/dialog/dialog-root.mjs +6 -4
- package/dist/dialog/dialog-root.mjs.map +1 -1
- package/dist/docs/components/avatar.md +354 -0
- package/dist/docs/components/drawer.md +622 -0
- package/dist/docs/components/otp-field.md +501 -0
- package/dist/docs/components/scroll-area.md +294 -0
- package/dist/docs/index.md +5 -1
- package/dist/docs/roadmap.md +6 -0
- package/dist/drawer/drawer-close.d.mts +26 -0
- package/dist/drawer/drawer-close.d.mts.map +1 -0
- package/dist/drawer/drawer-close.mjs +19 -0
- package/dist/drawer/drawer-close.mjs.map +1 -0
- package/dist/drawer/drawer-description.d.mts +23 -0
- package/dist/drawer/drawer-description.d.mts.map +1 -0
- package/dist/drawer/drawer-description.mjs +18 -0
- package/dist/drawer/drawer-description.mjs.map +1 -0
- package/dist/drawer/drawer-form.d.mts +24 -0
- package/dist/drawer/drawer-form.d.mts.map +1 -0
- package/dist/drawer/drawer-form.mjs +19 -0
- package/dist/drawer/drawer-form.mjs.map +1 -0
- package/dist/drawer/drawer-popup.d.mts +77 -0
- package/dist/drawer/drawer-popup.d.mts.map +1 -0
- package/dist/drawer/drawer-popup.mjs +124 -0
- package/dist/drawer/drawer-popup.mjs.map +1 -0
- package/dist/drawer/drawer-root.d.mts +31 -0
- package/dist/drawer/drawer-root.d.mts.map +1 -0
- package/dist/drawer/drawer-root.mjs +23 -0
- package/dist/drawer/drawer-root.mjs.map +1 -0
- package/dist/drawer/drawer-swipe.d.mts +5 -0
- package/dist/drawer/drawer-swipe.d.mts.map +1 -0
- package/dist/drawer/drawer-swipe.mjs +193 -0
- package/dist/drawer/drawer-swipe.mjs.map +1 -0
- package/dist/drawer/drawer-title.d.mts +23 -0
- package/dist/drawer/drawer-title.d.mts.map +1 -0
- package/dist/drawer/drawer-title.mjs +18 -0
- package/dist/drawer/drawer-title.mjs.map +1 -0
- package/dist/drawer/drawer-trigger.d.mts +23 -0
- package/dist/drawer/drawer-trigger.d.mts.map +1 -0
- package/dist/drawer/drawer-trigger.mjs +18 -0
- package/dist/drawer/drawer-trigger.mjs.map +1 -0
- package/dist/drawer/index.d.mts +34 -0
- package/dist/drawer/index.d.mts.map +1 -0
- package/dist/drawer/index.mjs +33 -0
- package/dist/drawer/index.mjs.map +1 -0
- package/dist/index.d.mts +19 -1
- package/dist/index.mjs +4 -1
- package/dist/internal/swipe-arithmetic.mjs +75 -0
- package/dist/internal/swipe-arithmetic.mjs.map +1 -0
- package/dist/internal/swipe.mjs +169 -0
- package/dist/internal/swipe.mjs.map +1 -0
- package/dist/menu/menu-context-trigger.mjs +1 -1
- package/dist/menu/menu-popup.mjs +1 -1
- package/dist/otp-field/index.d.mts +23 -0
- package/dist/otp-field/index.d.mts.map +1 -0
- package/dist/otp-field/index.mjs +23 -0
- package/dist/otp-field/index.mjs.map +1 -0
- package/dist/otp-field/otp-field-context.mjs +13 -0
- package/dist/otp-field/otp-field-context.mjs.map +1 -0
- package/dist/otp-field/otp-field-filter.d.mts +5 -0
- package/dist/otp-field/otp-field-filter.d.mts.map +1 -0
- package/dist/otp-field/otp-field-filter.mjs +135 -0
- package/dist/otp-field/otp-field-filter.mjs.map +1 -0
- package/dist/otp-field/otp-field-input.d.mts +41 -0
- package/dist/otp-field/otp-field-input.d.mts.map +1 -0
- package/dist/otp-field/otp-field-input.mjs +177 -0
- package/dist/otp-field/otp-field-input.mjs.map +1 -0
- package/dist/otp-field/otp-field-root.d.mts +143 -0
- package/dist/otp-field/otp-field-root.d.mts.map +1 -0
- package/dist/otp-field/otp-field-root.mjs +397 -0
- package/dist/otp-field/otp-field-root.mjs.map +1 -0
- package/dist/otp-field/otp-field-slot.d.mts +52 -0
- package/dist/otp-field/otp-field-slot.d.mts.map +1 -0
- package/dist/otp-field/otp-field-slot.mjs +67 -0
- package/dist/otp-field/otp-field-slot.mjs.map +1 -0
- package/package.json +4 -1
- package/skills/funstack-ui-knowledge/SKILL.md +5 -1
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
# Avatar
|
|
2
|
+
|
|
3
|
+
A picture of a person or a thing, with something to show in its place when there is no picture or it does not load. It has no action and nothing pending: its one state, whether the image loaded, is a fact the browser owns, and the avatar mirrors it as `data-status`. The `<img>` is in the server HTML from the first render, the fallback is under it, and the name is on the Root, so the same avatar works before hydration, without script, and when the picture fails.
|
|
4
|
+
|
|
5
|
+
```tsx title="AvatarPeople.tsx"
|
|
6
|
+
"use client";
|
|
7
|
+
// Three people: a picture that loads, one whose picture fails (initials take its place), and one
|
|
8
|
+
// with no picture at all. Each Root is named by `alt`; the status under each avatar comes from
|
|
9
|
+
// the Root's `render` function, so it sits outside the role="img" element. "Change picture"
|
|
10
|
+
// swaps Ada's source; the Root is keyed by it, so the new picture is a new avatar that fades in.
|
|
11
|
+
import { Avatar } from "@funstack/ui/avatar";
|
|
12
|
+
import { useState } from "react";
|
|
13
|
+
|
|
14
|
+
/** A self-contained picture (an SVG data URL), so the example needs no image host. */
|
|
15
|
+
function picture(from: string, to: string): string {
|
|
16
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" viewBox="0 0 80 80">
|
|
17
|
+
<defs><linearGradient id="g" x2="1" y2="1"><stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/></linearGradient></defs>
|
|
18
|
+
<rect width="80" height="80" fill="url(#g)"/><circle cx="40" cy="32" r="14" fill="#fff" fill-opacity=".85"/>
|
|
19
|
+
<path d="M14 80c4-18 14-26 26-26s22 8 26 26z" fill="#fff" fill-opacity=".85"/></svg>`;
|
|
20
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const pictures = [picture("#6366f1", "#ec4899"), picture("#0ea5e9", "#22c55e")];
|
|
24
|
+
|
|
25
|
+
const people = [
|
|
26
|
+
{ id: "ada", name: "Ada Lovelace", initials: "AL" },
|
|
27
|
+
// Bytes that are no image: decoding fails, as for a URL that is gone, and the Fallback stays.
|
|
28
|
+
{ id: "grace", name: "Grace Hopper", initials: "GH", src: "data:image/png;base64,AAAA" },
|
|
29
|
+
{ id: "alan", name: "Alan Turing", initials: "AT" },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
export function AvatarPeople() {
|
|
33
|
+
const [picked, setPicked] = useState(0);
|
|
34
|
+
return (
|
|
35
|
+
<div className="demo-stack">
|
|
36
|
+
<ul className="demo-avatar-people">
|
|
37
|
+
{people.map((person) => {
|
|
38
|
+
const src = person.id === "ada" ? pictures[picked] : person.src;
|
|
39
|
+
return (
|
|
40
|
+
<li key={person.id}>
|
|
41
|
+
<Avatar.Root
|
|
42
|
+
// Keyed by the source: with the fade-in recipe (avatar.css) a new picture on the
|
|
43
|
+
// same avatar would first fade the old one out; a new avatar fades in instead.
|
|
44
|
+
key={src}
|
|
45
|
+
alt={person.name}
|
|
46
|
+
className="demo-avatar"
|
|
47
|
+
// The function form wraps the avatar in a figure, so the caption is outside the
|
|
48
|
+
// role="img" element (its children are presentational).
|
|
49
|
+
render={(props, { status }) => (
|
|
50
|
+
<figure className="demo-avatar-figure">
|
|
51
|
+
<span {...props} />
|
|
52
|
+
<figcaption>
|
|
53
|
+
{person.name}
|
|
54
|
+
<code>{status}</code>
|
|
55
|
+
</figcaption>
|
|
56
|
+
</figure>
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
<Avatar.Image className="demo-avatar-image" src={src} width={80} height={80} />
|
|
60
|
+
<Avatar.Fallback className="demo-avatar-fallback">
|
|
61
|
+
{person.initials}
|
|
62
|
+
</Avatar.Fallback>
|
|
63
|
+
</Avatar.Root>
|
|
64
|
+
</li>
|
|
65
|
+
);
|
|
66
|
+
})}
|
|
67
|
+
</ul>
|
|
68
|
+
<p>
|
|
69
|
+
<button
|
|
70
|
+
type="button"
|
|
71
|
+
className="demo-button"
|
|
72
|
+
onClick={() => setPicked((p) => (p + 1) % pictures.length)}
|
|
73
|
+
>
|
|
74
|
+
Change Ada's picture
|
|
75
|
+
</button>
|
|
76
|
+
</p>
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```css title="avatar.css"
|
|
83
|
+
/* Avatar: the image and the fallback share one grid cell, the image above. A loaded picture
|
|
84
|
+
paints over the initials; a broken <img alt=""> paints nothing, so the initials show, with or
|
|
85
|
+
without script. */
|
|
86
|
+
.demo-avatar {
|
|
87
|
+
display: inline-grid;
|
|
88
|
+
inline-size: 4rem;
|
|
89
|
+
aspect-ratio: 1;
|
|
90
|
+
border-radius: 50%;
|
|
91
|
+
overflow: clip;
|
|
92
|
+
}
|
|
93
|
+
.demo-avatar > * {
|
|
94
|
+
grid-area: 1 / 1;
|
|
95
|
+
}
|
|
96
|
+
.demo-avatar-image {
|
|
97
|
+
inline-size: 100%;
|
|
98
|
+
block-size: 100%;
|
|
99
|
+
object-fit: cover;
|
|
100
|
+
}
|
|
101
|
+
.demo-avatar-fallback {
|
|
102
|
+
display: grid;
|
|
103
|
+
place-items: center;
|
|
104
|
+
background: var(--color-bg-secondary);
|
|
105
|
+
color: var(--color-text);
|
|
106
|
+
font-weight: 600;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* No flash of initials during a fast load: while an image may still arrive (:has(img)), the
|
|
110
|
+
fallback becomes visible only after 400ms. Works before hydration too. */
|
|
111
|
+
.demo-avatar:has(img) .demo-avatar-fallback {
|
|
112
|
+
animation: demo-avatar-fallback-in 1ms 400ms backwards;
|
|
113
|
+
}
|
|
114
|
+
@keyframes demo-avatar-fallback-in {
|
|
115
|
+
from {
|
|
116
|
+
opacity: 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* Fade the picture in, only where script runs (without it the status never leaves "idle").
|
|
121
|
+
It would also fade a loaded picture out when its src changes (the status goes back to
|
|
122
|
+
"loading"), so the example keys each Root by its source. */
|
|
123
|
+
@media (scripting: enabled) {
|
|
124
|
+
.demo-avatar-image {
|
|
125
|
+
transition: opacity 150ms;
|
|
126
|
+
}
|
|
127
|
+
.demo-avatar-image[data-status="loading"] {
|
|
128
|
+
opacity: 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
@media (prefers-reduced-motion: reduce) {
|
|
132
|
+
.demo-avatar-image {
|
|
133
|
+
transition: none;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/* The demo's layout. */
|
|
138
|
+
.demo-avatar-people {
|
|
139
|
+
display: flex;
|
|
140
|
+
flex-wrap: wrap;
|
|
141
|
+
gap: 1.5rem;
|
|
142
|
+
margin: 0;
|
|
143
|
+
padding: 0;
|
|
144
|
+
list-style: none;
|
|
145
|
+
}
|
|
146
|
+
.demo-avatar-figure {
|
|
147
|
+
display: grid;
|
|
148
|
+
justify-items: center;
|
|
149
|
+
gap: 0.5rem;
|
|
150
|
+
margin: 0;
|
|
151
|
+
}
|
|
152
|
+
.demo-avatar-figure figcaption {
|
|
153
|
+
display: grid;
|
|
154
|
+
justify-items: center;
|
|
155
|
+
font-size: 0.875rem;
|
|
156
|
+
}
|
|
157
|
+
.demo-avatar-figure code {
|
|
158
|
+
color: var(--color-text-secondary);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Anatomy
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { Avatar } from "@funstack/ui/avatar";
|
|
166
|
+
|
|
167
|
+
<Avatar.Root alt={user.name} className="avatar"> {/* span role="img" aria-label */}
|
|
168
|
+
<Avatar.Image src={user.photoUrl} className="avatar-image" /> {/* img alt="" */}
|
|
169
|
+
<Avatar.Fallback className="avatar-fallback">{initials(user.name)}</Avatar.Fallback>
|
|
170
|
+
</Avatar.Root>
|
|
171
|
+
|
|
172
|
+
// Next to the visible name: decorative.
|
|
173
|
+
<a href={`/users/${user.id}`}>
|
|
174
|
+
<Avatar.Root alt="" className="avatar"> {/* span aria-hidden="true" */}
|
|
175
|
+
<Avatar.Image src={user.photoUrl} />
|
|
176
|
+
<Avatar.Fallback><UserIcon /></Avatar.Fallback>
|
|
177
|
+
</Avatar.Root>
|
|
178
|
+
{user.name}
|
|
179
|
+
</a>
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
| Part | Element | Rendered when |
|
|
183
|
+
| - | - | - |
|
|
184
|
+
| `Avatar.Root` | `span` | always |
|
|
185
|
+
| `Avatar.Image` | `img`, `alt=""` | it has a `src` (or `srcSet`) and that source has not failed |
|
|
186
|
+
| `Avatar.Fallback` | `span` | the status is not `"loaded"` |
|
|
187
|
+
|
|
188
|
+
The library ships no CSS: stack the image over the fallback in one grid cell (see [Recipes](#recipes)). While the image loads both are in the DOM and the picture paints over the initials when it arrives; nothing is swapped, so there is no `delay` prop.
|
|
189
|
+
|
|
190
|
+
## Props
|
|
191
|
+
|
|
192
|
+
### `Avatar.Root`
|
|
193
|
+
|
|
194
|
+
| Prop | Type | Default | Description |
|
|
195
|
+
| - | - | - | - |
|
|
196
|
+
| `alt` | `string` | – | Required. The avatar's text alternative, with the contract of `<img alt>`: who or what is pictured ("Jane Doe", not "avatar"), or `""` when the avatar is decoration next to text that already says it. Non-empty: the Root is `role="img"` with this `aria-label`, in every status. Empty: `aria-hidden="true"`. |
|
|
197
|
+
| `className` | `string \| ((state: AvatarState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
198
|
+
| `style` | `CSSProperties \| ((state: AvatarState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
199
|
+
| `children` | `ReactNode \| ((state: AvatarState) => ReactNode)` | – | The `Avatar.Image` and `Avatar.Fallback`, or a function of the state. |
|
|
200
|
+
| `render` | `ReactElement \| ((props, state: AvatarState) => ReactNode)` | – | Replace the default \<span> (element form) or take over rendering (function form). |
|
|
201
|
+
|
|
202
|
+
- **State**: `AvatarState` is `{ status }`, shared by every part.
|
|
203
|
+
- **Owned**: `role`, `aria-label`, `aria-hidden`, `data-status`.
|
|
204
|
+
|
|
205
|
+
`role`, `aria-label`, `aria-labelledby` and `aria-hidden` are not in the props type: the name comes from `alt`. Everything else passes through to the `span`, `ref` included.
|
|
206
|
+
|
|
207
|
+
### `Avatar.Image`
|
|
208
|
+
|
|
209
|
+
| Prop | Type | Default | Description |
|
|
210
|
+
| - | - | - | - |
|
|
211
|
+
| `src` | `string` | – | The picture. Without `src` and `srcSet` (or with `""`) the part renders nothing and the Fallback is the avatar. A new `src` keeps the same element: the browser paints the old picture until the new one is available. |
|
|
212
|
+
| `className` | `string \| ((state: AvatarState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
213
|
+
| `style` | `CSSProperties \| ((state: AvatarState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
214
|
+
| `render` | `ReactElement \| ((props, state: AvatarState) => ReactNode)` | – | Replace the `<img>` (element form) or take over rendering (function form). It must render an `<img>` and pass the `ref` on: the part listens through it. |
|
|
215
|
+
|
|
216
|
+
- **State**: `AvatarState`.
|
|
217
|
+
- **Owned**: `alt` (always `""`: the Root carries the name), `data-status`.
|
|
218
|
+
|
|
219
|
+
Every other `<img>` prop passes through: `srcSet`, `sizes`, `width`, `height`, `loading`, `decoding`, `fetchPriority`, `crossOrigin`, `referrerPolicy`, and your own `onLoad` / `onError` (see [Suspense](#suspense) for what an `onLoad` costs). No `children`.
|
|
220
|
+
|
|
221
|
+
### `Avatar.Fallback`
|
|
222
|
+
|
|
223
|
+
| Prop | Type | Default | Description |
|
|
224
|
+
| - | - | - | - |
|
|
225
|
+
| `className` | `string \| ((state: AvatarState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
226
|
+
| `style` | `CSSProperties \| ((state: AvatarState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
227
|
+
| `children` | `ReactNode \| ((state: AvatarState) => ReactNode)` | – | Initials, an icon, or a function of the state. |
|
|
228
|
+
| `render` | `ReactElement \| ((props, state: AvatarState) => ReactNode)` | – | Replace the default \<span> (element form) or take over rendering (function form). |
|
|
229
|
+
|
|
230
|
+
- **State**: `AvatarState`.
|
|
231
|
+
- **Owned**: `data-status`.
|
|
232
|
+
|
|
233
|
+
Not rendered at all once the status is `"loaded"`; a function `render` or `children` does not run then.
|
|
234
|
+
|
|
235
|
+
### State
|
|
236
|
+
|
|
237
|
+
| \`status\` | Meaning | Image | Fallback |
|
|
238
|
+
| - | - | - | - |
|
|
239
|
+
| `"idle"` | No image has reported: the server render, the hydration render, no `Avatar.Image`, or one with no source. | rendered if it has a source | rendered |
|
|
240
|
+
| `"loading"` | The image has a source and is not complete (also a `loading="lazy"` image off screen). | rendered | rendered, under it |
|
|
241
|
+
| `"loaded"` | The image is available. | rendered | **not rendered** |
|
|
242
|
+
| `"error"` | The image's current source failed. It is not tried again; a different `src` mounts a new `<img>`. | **not rendered** | rendered |
|
|
243
|
+
|
|
244
|
+
## Data attributes
|
|
245
|
+
|
|
246
|
+
| Attribute | On | Meaning |
|
|
247
|
+
| - | - | - |
|
|
248
|
+
| `data-status` | every part | `"idle"`, `"loading"`, `"loaded"` or `"error"`: the same value as `state.status`. |
|
|
249
|
+
|
|
250
|
+
## Keyboard and ARIA
|
|
251
|
+
|
|
252
|
+
| Case | Rendered | Announced |
|
|
253
|
+
| - | - | - |
|
|
254
|
+
| `alt="Jane Doe"`, any status | `span[role=img][aria-label="Jane Doe"]` | "Jane Doe, image", once |
|
|
255
|
+
| `alt=""`, any status | `span[aria-hidden=true]` | nothing |
|
|
256
|
+
|
|
257
|
+
- The children of `role="img"` are presentational: the inner `<img alt="">` and the fallback's text never add a second name, and the name does not change when the picture fails. With the name on the `<img>` instead, a failed picture would leave "JD" as the only thing a screen reader can read.
|
|
258
|
+
- So **nothing interactive goes inside the Root**. A link or a button wraps the Root; a presence badge that must be announced is a sibling.
|
|
259
|
+
- Not focusable, no keyboard interaction.
|
|
260
|
+
- Initials are text: give them text contrast when the avatar is not decorative for sighted users either.
|
|
261
|
+
|
|
262
|
+
## Transition types
|
|
263
|
+
|
|
264
|
+
Concept: [Transitions & ViewTransition](../concepts/transitions.md#transition-types)
|
|
265
|
+
|
|
266
|
+
None, and no `transition` prop: the status changes in an urgent update, which starts no view transition. Animate the swap with CSS (the fade-in recipe below). A `<ViewTransition>` of your own around the avatar animates its mount, and makes React wait for the image (Suspense, below).
|
|
267
|
+
|
|
268
|
+
## Optimistic and pending
|
|
269
|
+
|
|
270
|
+
Concept: [Pending & optimistic state](../concepts/pending-and-optimistic.md)
|
|
271
|
+
|
|
272
|
+
None: no action, nothing pending, nothing optimistic. The status is written with a plain `setState` from the image's native `load` / `error` events, never in a transition, so an unrelated pending action elsewhere in the app does not hold initials over a picture that has loaded.
|
|
273
|
+
|
|
274
|
+
## Suspense
|
|
275
|
+
|
|
276
|
+
Concept: [Suspense](../concepts/suspense.md)
|
|
277
|
+
|
|
278
|
+
The avatar does not suspend and owns no boundary. React 19.3 already waits for images: when an `<img>` is inserted (or its `src` changes) in a transition under a `<ViewTransition>`, react-dom holds the commit until the image is ready or a short timeout passes. `Avatar.Image` is built so that this applies to it.
|
|
279
|
+
|
|
280
|
+
```tsx
|
|
281
|
+
// The card is revealed with the picture in place, or after the timeout with the initials.
|
|
282
|
+
<ViewTransition>
|
|
283
|
+
<Suspense fallback={<ProfileSkeleton />}>
|
|
284
|
+
<Profile /> {/* suspends on data, renders <Avatar.Root> */}
|
|
285
|
+
</Suspense>
|
|
286
|
+
</ViewTransition>
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
- React skips the wait for an image that has an `onLoad` prop or `loading="lazy"`. The part listens with native listeners through its ref and never renders `onLoad`, so the wait applies. Your own `onLoad` or `loading="lazy"` on `Avatar.Image` opts that image out, which is React's documented way to do so.
|
|
290
|
+
- When React waited and the picture arrived, the first status is already `"loaded"`: the fallback is never painted. When the timeout won, the avatar starts at `"loading"` with the initials under it.
|
|
291
|
+
- The library's own content regions with a `transition` prop (a `Tabs.Panel`, a `Dialog.Popup`) are under a `<ViewTransition>`, so switching to a panel waits for its avatars, bounded by the timeout, with `data-pending` on the trigger meanwhile. The timeout is one clock for the whole commit, not one per image.
|
|
292
|
+
- A library-owned Suspense wait was rejected: a server cannot load an image, so the server HTML would have no picture, and the wait would have no bound.
|
|
293
|
+
|
|
294
|
+
## Browser-owned state
|
|
295
|
+
|
|
296
|
+
Concept: [Browser-owned state](../concepts/browser-owned-state.md)
|
|
297
|
+
|
|
298
|
+
The image's load state. The avatar mirrors it from `load` / `error` and from `img.complete` at mount, so a picture that finished before hydration (whose event no listener heard) is right in the hydration commit: `"loaded"`, or `"error"` for one that failed. A complete image with no natural width is decided by `img.decode()`.
|
|
299
|
+
|
|
300
|
+
## Before hydration
|
|
301
|
+
|
|
302
|
+
Concept: [Server Components](../concepts/server-components.md#before-hydration)
|
|
303
|
+
|
|
304
|
+
The server HTML has the Root with its name, the `<img alt="">` and the Fallback, all `data-status="idle"`, so the browser starts the request from the HTML (react-dom also emits a `<link rel="preload" as="image">`). Without script the picture paints over the initials when it arrives, and a broken image with `alt=""` paints nothing, so the initials stay. Every part is a client module; a Server Component renders them with serialisable props.
|
|
305
|
+
|
|
306
|
+
The status stays `"idle"` without script, so a stylesheet that hides the image while `[data-status="loading"]` never hides it before hydration. Use `@media (scripting: enabled)` for anything that depends on the status leaving `"idle"`.
|
|
307
|
+
|
|
308
|
+
## Recipes
|
|
309
|
+
|
|
310
|
+
### CSS
|
|
311
|
+
|
|
312
|
+
One grid cell, the image above the fallback:
|
|
313
|
+
|
|
314
|
+
```css
|
|
315
|
+
.avatar { display: inline-grid; inline-size: 2.5rem; aspect-ratio: 1; border-radius: 50%; overflow: clip; }
|
|
316
|
+
.avatar > * { grid-area: 1 / 1; }
|
|
317
|
+
.avatar-image { inline-size: 100%; block-size: 100%; object-fit: cover; }
|
|
318
|
+
.avatar-fallback { display: grid; place-items: center; background: var(--avatar-bg); }
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### No flash of initials during a fast load
|
|
322
|
+
|
|
323
|
+
`:has(img)` is true exactly while a picture may still arrive (the part removes the `<img>` on error and renders none without a source), so the delay applies only then, and works before hydration:
|
|
324
|
+
|
|
325
|
+
```css
|
|
326
|
+
.avatar:has(img) .avatar-fallback { animation: avatar-fallback-in 1ms 400ms backwards; }
|
|
327
|
+
@keyframes avatar-fallback-in { from { opacity: 0; } }
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Fading the picture in
|
|
331
|
+
|
|
332
|
+
```css
|
|
333
|
+
@media (scripting: enabled) {
|
|
334
|
+
.avatar-image { transition: opacity 150ms; }
|
|
335
|
+
.avatar-image[data-status="loading"] { opacity: 0; }
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
This hides every image whose status is `"loading"`, so it also fades a loaded picture **out** when its `src` changes (the status goes back to `"loading"` on the same element) and shows the initials until the new one arrives. CSS cannot tell a first load from a change. If a mounted avatar can change its picture, leave the fade out, or key the Root by the source (`<Avatar.Root key={src} …>`): each picture is then a new avatar that fades in like a first load, as in the example above.
|
|
340
|
+
|
|
341
|
+
Transparent pictures (a PNG logo) let the initials show through until hydration removes the fallback: give `.avatar-image` a `background`. Tailwind: `data-[status=loading]:opacity-0`, `has-[img]:…`.
|
|
342
|
+
|
|
343
|
+
`avatar.css`: the same file as above.
|
|
344
|
+
|
|
345
|
+
## Browser notes
|
|
346
|
+
|
|
347
|
+
- Chromium and Firefox give an SVG with no width, height or `viewBox` the default object size (300 × 150) as its natural size; it reports `"loaded"` like any other picture.
|
|
348
|
+
|
|
349
|
+
### Not in this version
|
|
350
|
+
|
|
351
|
+
- A group of overlapping avatars ("+3"): layout, in your CSS (negative margins on the Roots of a flex row).
|
|
352
|
+
- A presence or status badge part: a sibling of the Root, with its own text.
|
|
353
|
+
- Initials or a colour derived from a name.
|
|
354
|
+
- A retry of a failed source, and an `onStatusChange` handler (the status is in every function prop; your native `onError` passes through).
|