-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathavatar.tsx
39 lines (34 loc) · 848 Bytes
/
avatar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import * as avatar from "@zag-js/avatar"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"
export function Avatar(
props: Omit<avatar.Props, "id"> & { src: string; name: string },
) {
const { src, name } = props
const service = useMachine(avatar.machine, {
id: useId(),
...props,
})
const api = avatar.connect(service, normalizeProps)
const initial = name
.split(" ")
.map((s) => s[0])
.join("")
return (
<>
<main className="avatar">
<div {...api.getRootProps()}>
<div {...api.getFallbackProps()}>
<div>{initial}</div>
</div>
<img
alt={name}
referrerPolicy="no-referrer"
src={src}
{...api.getImageProps()}
/>
</div>
</main>
</>
)
}