mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-22 18:44:44 +08:00
Initial commit from Create Next App
This commit is contained in:
58
src/components/NextImage.tsx
Normal file
58
src/components/NextImage.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
import Image, { ImageProps } from 'next/image';
|
||||
import * as React from 'react';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
type NextImageProps = {
|
||||
useSkeleton?: boolean;
|
||||
classNames?: {
|
||||
image?: string;
|
||||
blur?: string;
|
||||
};
|
||||
alt: string;
|
||||
} & (
|
||||
| { width: string | number; height: string | number }
|
||||
| { layout: 'fill'; width?: string | number; height?: string | number }
|
||||
) &
|
||||
ImageProps;
|
||||
|
||||
/**
|
||||
*
|
||||
* @description Must set width using `w-` className
|
||||
* @param useSkeleton add background with pulse animation, don't use it if image is transparent
|
||||
*/
|
||||
export default function NextImage({
|
||||
useSkeleton = false,
|
||||
src,
|
||||
width,
|
||||
height,
|
||||
alt,
|
||||
className,
|
||||
classNames,
|
||||
...rest
|
||||
}: NextImageProps) {
|
||||
const [status, setStatus] = React.useState(
|
||||
useSkeleton ? 'loading' : 'complete'
|
||||
);
|
||||
const widthIsSet = className?.includes('w-') ?? false;
|
||||
|
||||
return (
|
||||
<figure
|
||||
style={!widthIsSet ? { width: `${width}px` } : undefined}
|
||||
className={className}
|
||||
>
|
||||
<Image
|
||||
className={cn(
|
||||
classNames?.image,
|
||||
status === 'loading' && cn('animate-pulse', classNames?.blur)
|
||||
)}
|
||||
src={src}
|
||||
width={width}
|
||||
height={height}
|
||||
alt={alt}
|
||||
onLoadingComplete={() => setStatus('complete')}
|
||||
{...rest}
|
||||
/>
|
||||
</figure>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user