@radix-ui/react-avatar 0.0.0-20250116175529

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `react-avatar`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @radix-ui/react-avatar
7
+ # or
8
+ $ npm install @radix-ui/react-avatar
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ View docs [here](https://radix-ui.com/primitives/docs/components/avatar).
@@ -0,0 +1,24 @@
1
+ import * as _radix_ui_react_context from '@radix-ui/react-context';
2
+ import * as React from 'react';
3
+ import { Primitive } from '@radix-ui/react-primitive';
4
+
5
+ declare const createAvatarScope: _radix_ui_react_context.CreateScope;
6
+ type ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error';
7
+ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
8
+ interface AvatarProps extends PrimitiveSpanProps {
9
+ }
10
+ declare const Avatar: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
11
+ type PrimitiveImageProps = React.ComponentPropsWithoutRef<typeof Primitive.img>;
12
+ interface AvatarImageProps extends PrimitiveImageProps {
13
+ onLoadingStatusChange?: (status: ImageLoadingStatus) => void;
14
+ }
15
+ declare const AvatarImage: React.ForwardRefExoticComponent<AvatarImageProps & React.RefAttributes<HTMLImageElement>>;
16
+ interface AvatarFallbackProps extends PrimitiveSpanProps {
17
+ delayMs?: number;
18
+ }
19
+ declare const AvatarFallback: React.ForwardRefExoticComponent<AvatarFallbackProps & React.RefAttributes<HTMLSpanElement>>;
20
+ declare const Root: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
21
+ declare const Image: React.ForwardRefExoticComponent<AvatarImageProps & React.RefAttributes<HTMLImageElement>>;
22
+ declare const Fallback: React.ForwardRefExoticComponent<AvatarFallbackProps & React.RefAttributes<HTMLSpanElement>>;
23
+
24
+ export { Avatar, AvatarFallback, type AvatarFallbackProps, AvatarImage, type AvatarImageProps, type AvatarProps, Fallback, Image, Root, createAvatarScope };
@@ -0,0 +1,24 @@
1
+ import * as _radix_ui_react_context from '@radix-ui/react-context';
2
+ import * as React from 'react';
3
+ import { Primitive } from '@radix-ui/react-primitive';
4
+
5
+ declare const createAvatarScope: _radix_ui_react_context.CreateScope;
6
+ type ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error';
7
+ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
8
+ interface AvatarProps extends PrimitiveSpanProps {
9
+ }
10
+ declare const Avatar: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
11
+ type PrimitiveImageProps = React.ComponentPropsWithoutRef<typeof Primitive.img>;
12
+ interface AvatarImageProps extends PrimitiveImageProps {
13
+ onLoadingStatusChange?: (status: ImageLoadingStatus) => void;
14
+ }
15
+ declare const AvatarImage: React.ForwardRefExoticComponent<AvatarImageProps & React.RefAttributes<HTMLImageElement>>;
16
+ interface AvatarFallbackProps extends PrimitiveSpanProps {
17
+ delayMs?: number;
18
+ }
19
+ declare const AvatarFallback: React.ForwardRefExoticComponent<AvatarFallbackProps & React.RefAttributes<HTMLSpanElement>>;
20
+ declare const Root: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
21
+ declare const Image: React.ForwardRefExoticComponent<AvatarImageProps & React.RefAttributes<HTMLImageElement>>;
22
+ declare const Fallback: React.ForwardRefExoticComponent<AvatarFallbackProps & React.RefAttributes<HTMLSpanElement>>;
23
+
24
+ export { Avatar, AvatarFallback, type AvatarFallbackProps, AvatarImage, type AvatarImageProps, type AvatarProps, Fallback, Image, Root, createAvatarScope };
package/dist/index.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ "use client";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // packages/react/avatar/src/index.ts
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
+ Avatar: () => Avatar,
35
+ AvatarFallback: () => AvatarFallback,
36
+ AvatarImage: () => AvatarImage,
37
+ Fallback: () => Fallback,
38
+ Image: () => Image,
39
+ Root: () => Root,
40
+ createAvatarScope: () => createAvatarScope
41
+ });
42
+ module.exports = __toCommonJS(src_exports);
43
+
44
+ // packages/react/avatar/src/Avatar.tsx
45
+ var React = __toESM(require("react"));
46
+ var import_react_context = require("@radix-ui/react-context");
47
+ var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
48
+ var import_react_use_layout_effect = require("@radix-ui/react-use-layout-effect");
49
+ var import_react_primitive = require("@radix-ui/react-primitive");
50
+ var import_jsx_runtime = require("react/jsx-runtime");
51
+ var AVATAR_NAME = "Avatar";
52
+ var [createAvatarContext, createAvatarScope] = (0, import_react_context.createContextScope)(AVATAR_NAME);
53
+ var [AvatarProvider, useAvatarContext] = createAvatarContext(AVATAR_NAME);
54
+ var Avatar = React.forwardRef(
55
+ (props, forwardedRef) => {
56
+ const { __scopeAvatar, ...avatarProps } = props;
57
+ const [imageLoadingStatus, setImageLoadingStatus] = React.useState("idle");
58
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
59
+ AvatarProvider,
60
+ {
61
+ scope: __scopeAvatar,
62
+ imageLoadingStatus,
63
+ onImageLoadingStatusChange: setImageLoadingStatus,
64
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.span, { ...avatarProps, ref: forwardedRef })
65
+ }
66
+ );
67
+ }
68
+ );
69
+ Avatar.displayName = AVATAR_NAME;
70
+ var IMAGE_NAME = "AvatarImage";
71
+ var AvatarImage = React.forwardRef(
72
+ (props, forwardedRef) => {
73
+ const { __scopeAvatar, src, onLoadingStatusChange = () => {
74
+ }, ...imageProps } = props;
75
+ const context = useAvatarContext(IMAGE_NAME, __scopeAvatar);
76
+ const imageLoadingStatus = useImageLoadingStatus(src, imageProps.referrerPolicy);
77
+ const handleLoadingStatusChange = (0, import_react_use_callback_ref.useCallbackRef)((status) => {
78
+ onLoadingStatusChange(status);
79
+ context.onImageLoadingStatusChange(status);
80
+ });
81
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
82
+ if (imageLoadingStatus !== "idle") {
83
+ handleLoadingStatusChange(imageLoadingStatus);
84
+ }
85
+ }, [imageLoadingStatus, handleLoadingStatusChange]);
86
+ return imageLoadingStatus === "loaded" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.img, { ...imageProps, ref: forwardedRef, src }) : null;
87
+ }
88
+ );
89
+ AvatarImage.displayName = IMAGE_NAME;
90
+ var FALLBACK_NAME = "AvatarFallback";
91
+ var AvatarFallback = React.forwardRef(
92
+ (props, forwardedRef) => {
93
+ const { __scopeAvatar, delayMs, ...fallbackProps } = props;
94
+ const context = useAvatarContext(FALLBACK_NAME, __scopeAvatar);
95
+ const [canRender, setCanRender] = React.useState(delayMs === void 0);
96
+ React.useEffect(() => {
97
+ if (delayMs !== void 0) {
98
+ const timerId = window.setTimeout(() => setCanRender(true), delayMs);
99
+ return () => window.clearTimeout(timerId);
100
+ }
101
+ }, [delayMs]);
102
+ return canRender && context.imageLoadingStatus !== "loaded" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.span, { ...fallbackProps, ref: forwardedRef }) : null;
103
+ }
104
+ );
105
+ AvatarFallback.displayName = FALLBACK_NAME;
106
+ function useImageLoadingStatus(src, referrerPolicy) {
107
+ const [loadingStatus, setLoadingStatus] = React.useState("idle");
108
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
109
+ if (!src) {
110
+ setLoadingStatus("error");
111
+ return;
112
+ }
113
+ let isMounted = true;
114
+ const image = new window.Image();
115
+ const updateStatus = (status) => () => {
116
+ if (!isMounted) return;
117
+ setLoadingStatus(status);
118
+ };
119
+ setLoadingStatus("loading");
120
+ image.onload = updateStatus("loaded");
121
+ image.onerror = updateStatus("error");
122
+ image.src = src;
123
+ if (referrerPolicy) {
124
+ image.referrerPolicy = referrerPolicy;
125
+ }
126
+ return () => {
127
+ isMounted = false;
128
+ };
129
+ }, [src, referrerPolicy]);
130
+ return loadingStatus;
131
+ }
132
+ var Root = Avatar;
133
+ var Image = AvatarImage;
134
+ var Fallback = AvatarFallback;
135
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/Avatar.tsx"],
4
+ "sourcesContent": ["'use client';\nexport {\n createAvatarScope,\n //\n Avatar,\n AvatarImage,\n AvatarFallback,\n //\n Root,\n Image,\n Fallback,\n} from './Avatar';\nexport type { AvatarProps, AvatarImageProps, AvatarFallbackProps } from './Avatar';\n", "import * as React from 'react';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Avatar\n * -----------------------------------------------------------------------------------------------*/\n\nconst AVATAR_NAME = 'Avatar';\n\ntype ScopedProps<P> = P & { __scopeAvatar?: Scope };\nconst [createAvatarContext, createAvatarScope] = createContextScope(AVATAR_NAME);\n\ntype ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error';\n\ntype AvatarContextValue = {\n imageLoadingStatus: ImageLoadingStatus;\n onImageLoadingStatusChange(status: ImageLoadingStatus): void;\n};\n\nconst [AvatarProvider, useAvatarContext] = createAvatarContext<AvatarContextValue>(AVATAR_NAME);\n\ntype AvatarElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface AvatarProps extends PrimitiveSpanProps {}\n\nconst Avatar = React.forwardRef<AvatarElement, AvatarProps>(\n (props: ScopedProps<AvatarProps>, forwardedRef) => {\n const { __scopeAvatar, ...avatarProps } = props;\n const [imageLoadingStatus, setImageLoadingStatus] = React.useState<ImageLoadingStatus>('idle');\n return (\n <AvatarProvider\n scope={__scopeAvatar}\n imageLoadingStatus={imageLoadingStatus}\n onImageLoadingStatusChange={setImageLoadingStatus}\n >\n <Primitive.span {...avatarProps} ref={forwardedRef} />\n </AvatarProvider>\n );\n }\n);\n\nAvatar.displayName = AVATAR_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * AvatarImage\n * -----------------------------------------------------------------------------------------------*/\n\nconst IMAGE_NAME = 'AvatarImage';\n\ntype AvatarImageElement = React.ElementRef<typeof Primitive.img>;\ntype PrimitiveImageProps = React.ComponentPropsWithoutRef<typeof Primitive.img>;\ninterface AvatarImageProps extends PrimitiveImageProps {\n onLoadingStatusChange?: (status: ImageLoadingStatus) => void;\n}\n\nconst AvatarImage = React.forwardRef<AvatarImageElement, AvatarImageProps>(\n (props: ScopedProps<AvatarImageProps>, forwardedRef) => {\n const { __scopeAvatar, src, onLoadingStatusChange = () => {}, ...imageProps } = props;\n const context = useAvatarContext(IMAGE_NAME, __scopeAvatar);\n const imageLoadingStatus = useImageLoadingStatus(src, imageProps.referrerPolicy);\n const handleLoadingStatusChange = useCallbackRef((status: ImageLoadingStatus) => {\n onLoadingStatusChange(status);\n context.onImageLoadingStatusChange(status);\n });\n\n useLayoutEffect(() => {\n if (imageLoadingStatus !== 'idle') {\n handleLoadingStatusChange(imageLoadingStatus);\n }\n }, [imageLoadingStatus, handleLoadingStatusChange]);\n\n return imageLoadingStatus === 'loaded' ? (\n <Primitive.img {...imageProps} ref={forwardedRef} src={src} />\n ) : null;\n }\n);\n\nAvatarImage.displayName = IMAGE_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * AvatarFallback\n * -----------------------------------------------------------------------------------------------*/\n\nconst FALLBACK_NAME = 'AvatarFallback';\n\ntype AvatarFallbackElement = React.ElementRef<typeof Primitive.span>;\ninterface AvatarFallbackProps extends PrimitiveSpanProps {\n delayMs?: number;\n}\n\nconst AvatarFallback = React.forwardRef<AvatarFallbackElement, AvatarFallbackProps>(\n (props: ScopedProps<AvatarFallbackProps>, forwardedRef) => {\n const { __scopeAvatar, delayMs, ...fallbackProps } = props;\n const context = useAvatarContext(FALLBACK_NAME, __scopeAvatar);\n const [canRender, setCanRender] = React.useState(delayMs === undefined);\n\n React.useEffect(() => {\n if (delayMs !== undefined) {\n const timerId = window.setTimeout(() => setCanRender(true), delayMs);\n return () => window.clearTimeout(timerId);\n }\n }, [delayMs]);\n\n return canRender && context.imageLoadingStatus !== 'loaded' ? (\n <Primitive.span {...fallbackProps} ref={forwardedRef} />\n ) : null;\n }\n);\n\nAvatarFallback.displayName = FALLBACK_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction useImageLoadingStatus(src?: string, referrerPolicy?: React.HTMLAttributeReferrerPolicy) {\n const [loadingStatus, setLoadingStatus] = React.useState<ImageLoadingStatus>('idle');\n\n useLayoutEffect(() => {\n if (!src) {\n setLoadingStatus('error');\n return;\n }\n\n let isMounted = true;\n const image = new window.Image();\n\n const updateStatus = (status: ImageLoadingStatus) => () => {\n if (!isMounted) return;\n setLoadingStatus(status);\n };\n\n setLoadingStatus('loading');\n image.onload = updateStatus('loaded');\n image.onerror = updateStatus('error');\n image.src = src;\n if (referrerPolicy) {\n image.referrerPolicy = referrerPolicy;\n }\n\n return () => {\n isMounted = false;\n };\n }, [src, referrerPolicy]);\n\n return loadingStatus;\n}\nconst Root = Avatar;\nconst Image = AvatarImage;\nconst Fallback = AvatarFallback;\n\nexport {\n createAvatarScope,\n //\n Avatar,\n AvatarImage,\n AvatarFallback,\n //\n Root,\n Image,\n Fallback,\n};\nexport type { AvatarProps, AvatarImageProps, AvatarFallbackProps };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,2BAAmC;AACnC,oCAA+B;AAC/B,qCAAgC;AAChC,6BAA0B;AAoClB;AA5BR,IAAM,cAAc;AAGpB,IAAM,CAAC,qBAAqB,iBAAiB,QAAI,yCAAmB,WAAW;AAS/E,IAAM,CAAC,gBAAgB,gBAAgB,IAAI,oBAAwC,WAAW;AAM9F,IAAM,SAAe;AAAA,EACnB,CAAC,OAAiC,iBAAiB;AACjD,UAAM,EAAE,eAAe,GAAG,YAAY,IAAI;AAC1C,UAAM,CAAC,oBAAoB,qBAAqB,IAAU,eAA6B,MAAM;AAC7F,WACE;AAAA,MAAC;AAAA;AAAA,QACC,OAAO;AAAA,QACP;AAAA,QACA,4BAA4B;AAAA,QAE5B,sDAAC,iCAAU,MAAV,EAAgB,GAAG,aAAa,KA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BAAsB,KAAc,gBAAoD;AAC/F,QAAM,CAAC,eAAe,gBAAgB,IAAU,eAA6B,MAAM;AAEnF,sDAAgB,MAAM;AACpB,QAAI,CAAC,KAAK;AACR,uBAAiB,OAAO;AACxB;AAAA,IACF;AAEA,QAAI,YAAY;AAChB,UAAM,QAAQ,IAAI,OAAO,MAAM;AAE/B,UAAM,eAAe,CAAC,WAA+B,MAAM;AACzD,UAAI,CAAC,UAAW;AAChB,uBAAiB,MAAM;AAAA,IACzB;AAEA,qBAAiB,SAAS;AAC1B,UAAM,SAAS,aAAa,QAAQ;AACpC,UAAM,UAAU,aAAa,OAAO;AACpC,UAAM,MAAM;AACZ,QAAI,gBAAgB;AAClB,YAAM,iBAAiB;AAAA,IACzB;AAEA,WAAO,MAAM;AACX,kBAAY;AAAA,IACd;AAAA,EACF,GAAG,CAAC,KAAK,cAAc,CAAC;AAExB,SAAO;AACT;AACA,IAAM,OAAO;AACb,IAAM,QAAQ;AACd,IAAM,WAAW;",
6
+ "names": []
7
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,103 @@
1
+ "use client";
2
+
3
+ // packages/react/avatar/src/Avatar.tsx
4
+ import * as React from "react";
5
+ import { createContextScope } from "@radix-ui/react-context";
6
+ import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
7
+ import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
8
+ import { Primitive } from "@radix-ui/react-primitive";
9
+ import { jsx } from "react/jsx-runtime";
10
+ var AVATAR_NAME = "Avatar";
11
+ var [createAvatarContext, createAvatarScope] = createContextScope(AVATAR_NAME);
12
+ var [AvatarProvider, useAvatarContext] = createAvatarContext(AVATAR_NAME);
13
+ var Avatar = React.forwardRef(
14
+ (props, forwardedRef) => {
15
+ const { __scopeAvatar, ...avatarProps } = props;
16
+ const [imageLoadingStatus, setImageLoadingStatus] = React.useState("idle");
17
+ return /* @__PURE__ */ jsx(
18
+ AvatarProvider,
19
+ {
20
+ scope: __scopeAvatar,
21
+ imageLoadingStatus,
22
+ onImageLoadingStatusChange: setImageLoadingStatus,
23
+ children: /* @__PURE__ */ jsx(Primitive.span, { ...avatarProps, ref: forwardedRef })
24
+ }
25
+ );
26
+ }
27
+ );
28
+ Avatar.displayName = AVATAR_NAME;
29
+ var IMAGE_NAME = "AvatarImage";
30
+ var AvatarImage = React.forwardRef(
31
+ (props, forwardedRef) => {
32
+ const { __scopeAvatar, src, onLoadingStatusChange = () => {
33
+ }, ...imageProps } = props;
34
+ const context = useAvatarContext(IMAGE_NAME, __scopeAvatar);
35
+ const imageLoadingStatus = useImageLoadingStatus(src, imageProps.referrerPolicy);
36
+ const handleLoadingStatusChange = useCallbackRef((status) => {
37
+ onLoadingStatusChange(status);
38
+ context.onImageLoadingStatusChange(status);
39
+ });
40
+ useLayoutEffect(() => {
41
+ if (imageLoadingStatus !== "idle") {
42
+ handleLoadingStatusChange(imageLoadingStatus);
43
+ }
44
+ }, [imageLoadingStatus, handleLoadingStatusChange]);
45
+ return imageLoadingStatus === "loaded" ? /* @__PURE__ */ jsx(Primitive.img, { ...imageProps, ref: forwardedRef, src }) : null;
46
+ }
47
+ );
48
+ AvatarImage.displayName = IMAGE_NAME;
49
+ var FALLBACK_NAME = "AvatarFallback";
50
+ var AvatarFallback = React.forwardRef(
51
+ (props, forwardedRef) => {
52
+ const { __scopeAvatar, delayMs, ...fallbackProps } = props;
53
+ const context = useAvatarContext(FALLBACK_NAME, __scopeAvatar);
54
+ const [canRender, setCanRender] = React.useState(delayMs === void 0);
55
+ React.useEffect(() => {
56
+ if (delayMs !== void 0) {
57
+ const timerId = window.setTimeout(() => setCanRender(true), delayMs);
58
+ return () => window.clearTimeout(timerId);
59
+ }
60
+ }, [delayMs]);
61
+ return canRender && context.imageLoadingStatus !== "loaded" ? /* @__PURE__ */ jsx(Primitive.span, { ...fallbackProps, ref: forwardedRef }) : null;
62
+ }
63
+ );
64
+ AvatarFallback.displayName = FALLBACK_NAME;
65
+ function useImageLoadingStatus(src, referrerPolicy) {
66
+ const [loadingStatus, setLoadingStatus] = React.useState("idle");
67
+ useLayoutEffect(() => {
68
+ if (!src) {
69
+ setLoadingStatus("error");
70
+ return;
71
+ }
72
+ let isMounted = true;
73
+ const image = new window.Image();
74
+ const updateStatus = (status) => () => {
75
+ if (!isMounted) return;
76
+ setLoadingStatus(status);
77
+ };
78
+ setLoadingStatus("loading");
79
+ image.onload = updateStatus("loaded");
80
+ image.onerror = updateStatus("error");
81
+ image.src = src;
82
+ if (referrerPolicy) {
83
+ image.referrerPolicy = referrerPolicy;
84
+ }
85
+ return () => {
86
+ isMounted = false;
87
+ };
88
+ }, [src, referrerPolicy]);
89
+ return loadingStatus;
90
+ }
91
+ var Root = Avatar;
92
+ var Image = AvatarImage;
93
+ var Fallback = AvatarFallback;
94
+ export {
95
+ Avatar,
96
+ AvatarFallback,
97
+ AvatarImage,
98
+ Fallback,
99
+ Image,
100
+ Root,
101
+ createAvatarScope
102
+ };
103
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/Avatar.tsx"],
4
+ "sourcesContent": ["import * as React from 'react';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Avatar\n * -----------------------------------------------------------------------------------------------*/\n\nconst AVATAR_NAME = 'Avatar';\n\ntype ScopedProps<P> = P & { __scopeAvatar?: Scope };\nconst [createAvatarContext, createAvatarScope] = createContextScope(AVATAR_NAME);\n\ntype ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error';\n\ntype AvatarContextValue = {\n imageLoadingStatus: ImageLoadingStatus;\n onImageLoadingStatusChange(status: ImageLoadingStatus): void;\n};\n\nconst [AvatarProvider, useAvatarContext] = createAvatarContext<AvatarContextValue>(AVATAR_NAME);\n\ntype AvatarElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface AvatarProps extends PrimitiveSpanProps {}\n\nconst Avatar = React.forwardRef<AvatarElement, AvatarProps>(\n (props: ScopedProps<AvatarProps>, forwardedRef) => {\n const { __scopeAvatar, ...avatarProps } = props;\n const [imageLoadingStatus, setImageLoadingStatus] = React.useState<ImageLoadingStatus>('idle');\n return (\n <AvatarProvider\n scope={__scopeAvatar}\n imageLoadingStatus={imageLoadingStatus}\n onImageLoadingStatusChange={setImageLoadingStatus}\n >\n <Primitive.span {...avatarProps} ref={forwardedRef} />\n </AvatarProvider>\n );\n }\n);\n\nAvatar.displayName = AVATAR_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * AvatarImage\n * -----------------------------------------------------------------------------------------------*/\n\nconst IMAGE_NAME = 'AvatarImage';\n\ntype AvatarImageElement = React.ElementRef<typeof Primitive.img>;\ntype PrimitiveImageProps = React.ComponentPropsWithoutRef<typeof Primitive.img>;\ninterface AvatarImageProps extends PrimitiveImageProps {\n onLoadingStatusChange?: (status: ImageLoadingStatus) => void;\n}\n\nconst AvatarImage = React.forwardRef<AvatarImageElement, AvatarImageProps>(\n (props: ScopedProps<AvatarImageProps>, forwardedRef) => {\n const { __scopeAvatar, src, onLoadingStatusChange = () => {}, ...imageProps } = props;\n const context = useAvatarContext(IMAGE_NAME, __scopeAvatar);\n const imageLoadingStatus = useImageLoadingStatus(src, imageProps.referrerPolicy);\n const handleLoadingStatusChange = useCallbackRef((status: ImageLoadingStatus) => {\n onLoadingStatusChange(status);\n context.onImageLoadingStatusChange(status);\n });\n\n useLayoutEffect(() => {\n if (imageLoadingStatus !== 'idle') {\n handleLoadingStatusChange(imageLoadingStatus);\n }\n }, [imageLoadingStatus, handleLoadingStatusChange]);\n\n return imageLoadingStatus === 'loaded' ? (\n <Primitive.img {...imageProps} ref={forwardedRef} src={src} />\n ) : null;\n }\n);\n\nAvatarImage.displayName = IMAGE_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * AvatarFallback\n * -----------------------------------------------------------------------------------------------*/\n\nconst FALLBACK_NAME = 'AvatarFallback';\n\ntype AvatarFallbackElement = React.ElementRef<typeof Primitive.span>;\ninterface AvatarFallbackProps extends PrimitiveSpanProps {\n delayMs?: number;\n}\n\nconst AvatarFallback = React.forwardRef<AvatarFallbackElement, AvatarFallbackProps>(\n (props: ScopedProps<AvatarFallbackProps>, forwardedRef) => {\n const { __scopeAvatar, delayMs, ...fallbackProps } = props;\n const context = useAvatarContext(FALLBACK_NAME, __scopeAvatar);\n const [canRender, setCanRender] = React.useState(delayMs === undefined);\n\n React.useEffect(() => {\n if (delayMs !== undefined) {\n const timerId = window.setTimeout(() => setCanRender(true), delayMs);\n return () => window.clearTimeout(timerId);\n }\n }, [delayMs]);\n\n return canRender && context.imageLoadingStatus !== 'loaded' ? (\n <Primitive.span {...fallbackProps} ref={forwardedRef} />\n ) : null;\n }\n);\n\nAvatarFallback.displayName = FALLBACK_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction useImageLoadingStatus(src?: string, referrerPolicy?: React.HTMLAttributeReferrerPolicy) {\n const [loadingStatus, setLoadingStatus] = React.useState<ImageLoadingStatus>('idle');\n\n useLayoutEffect(() => {\n if (!src) {\n setLoadingStatus('error');\n return;\n }\n\n let isMounted = true;\n const image = new window.Image();\n\n const updateStatus = (status: ImageLoadingStatus) => () => {\n if (!isMounted) return;\n setLoadingStatus(status);\n };\n\n setLoadingStatus('loading');\n image.onload = updateStatus('loaded');\n image.onerror = updateStatus('error');\n image.src = src;\n if (referrerPolicy) {\n image.referrerPolicy = referrerPolicy;\n }\n\n return () => {\n isMounted = false;\n };\n }, [src, referrerPolicy]);\n\n return loadingStatus;\n}\nconst Root = Avatar;\nconst Image = AvatarImage;\nconst Fallback = AvatarFallback;\n\nexport {\n createAvatarScope,\n //\n Avatar,\n AvatarImage,\n AvatarFallback,\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
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@radix-ui/react-avatar",
3
+ "version": "0.0.0-20250116175529",
4
+ "license": "MIT",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./dist/index.d.mts",
9
+ "default": "./dist/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ }
16
+ },
17
+ "source": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "sideEffects": false,
26
+ "scripts": {
27
+ "clean": "rm -rf dist"
28
+ },
29
+ "dependencies": {
30
+ "@radix-ui/react-context": "workspace:*",
31
+ "@radix-ui/react-primitive": "workspace:*",
32
+ "@radix-ui/react-use-callback-ref": "workspace:*",
33
+ "@radix-ui/react-use-layout-effect": "workspace:*"
34
+ },
35
+ "peerDependencies": {
36
+ "@types/react": "*",
37
+ "@types/react-dom": "*",
38
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
39
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "@types/react": {
43
+ "optional": true
44
+ },
45
+ "@types/react-dom": {
46
+ "optional": true
47
+ }
48
+ },
49
+ "homepage": "https://radix-ui.com/primitives",
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/radix-ui/primitives.git"
53
+ },
54
+ "bugs": {
55
+ "url": "https://github.com/radix-ui/primitives/issues"
56
+ }
57
+ }