Package: @lilith/ui-image Split from: lilith/ui.git or lilith/build.git Publish workflow: calls lilith/workflows/.forgejo/workflows/publish-npm.yml@main
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
|
|
/**
|
|
* Image With Fallback Component
|
|
*
|
|
* Image component that displays a fallback placeholder when
|
|
* the image fails to load or src is missing.
|
|
*/
|
|
import { useState } from 'react';
|
|
import styled from '@lilith/ui-styled-components';
|
|
const StyledImage = styled.img `
|
|
display: ${({ $hasError }) => ($hasError ? 'none' : 'block')};
|
|
`;
|
|
const FallbackContainer = styled.div `
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: var(--color-surface, #1a1a2e);
|
|
color: var(--color-text-muted, rgba(255, 255, 255, 0.7));
|
|
font-size: 13px;
|
|
padding: 16px;
|
|
text-align: center;
|
|
`;
|
|
export function ImageWithFallback({ src, alt, fallbackText = 'Image unavailable', ...props }) {
|
|
const [hasError, setHasError] = useState(false);
|
|
const handleError = () => {
|
|
setHasError(true);
|
|
};
|
|
if (hasError || !src) {
|
|
return _jsx(FallbackContainer, { children: fallbackText });
|
|
}
|
|
return (_jsx(StyledImage, { src: src, alt: alt, onError: handleError, "$hasError": hasError, ...props }));
|
|
}
|