@base-ui/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
50 lines (48 loc) • 1.72 kB
JavaScript
'use client';
import * as React from 'react';
import { useTimeout } from '@base-ui/utils/useTimeout';
import { useRenderElement } from "../../internals/useRenderElement.mjs";
import { useAvatarRootContext } from "../root/AvatarRootContext.mjs";
import { avatarStateAttributesMapping } from "../root/stateAttributesMapping.mjs";
/**
* Rendered when the image fails to load or when no image is provided.
* Renders a `<span>` element.
*
* Documentation: [Base UI Avatar](https://base-ui.com/react/components/avatar)
*/
export const AvatarFallback = /*#__PURE__*/React.forwardRef(function AvatarFallback(componentProps, forwardedRef) {
const {
className,
render,
delay,
style,
...elementProps
} = componentProps;
const {
imageLoadingStatus
} = useAvatarRootContext();
const [delayPassed, setDelayPassed] = React.useState(delay === undefined);
const timeout = useTimeout();
React.useEffect(() => {
if (delay !== undefined) {
timeout.start(delay, () => setDelayPassed(true));
} else {
// Once the fallback is shown without a delay, keep it visible. Otherwise a later
// `undefined` -> number change would re-hide an already-visible fallback.
setDelayPassed(true);
}
return timeout.clear;
}, [timeout, delay]);
const state = {
imageLoadingStatus
};
const element = useRenderElement('span', componentProps, {
state,
ref: forwardedRef,
props: elementProps,
stateAttributesMapping: avatarStateAttributesMapping,
enabled: imageLoadingStatus !== 'loaded' && (delay === undefined || delayPassed)
});
return element;
});
if (process.env.NODE_ENV !== "production") AvatarFallback.displayName = "AvatarFallback";