Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 110 lines 3.2 KB
Raw
1 'use client';
2
3 import { useState } from 'react';
4 import Image from 'next/image';
5 import Zoom from 'react-medium-image-zoom';
6 import 'react-medium-image-zoom/dist/styles.css';
7 import { cn } from '@/lib/utils';
8
9 type FigureSize = 'sm' | 'md' | 'lg' | 'full';
10
11 interface FigureProps {
12 src: string;
13 /** Dark-mode variant of the image. Shown when the site theme is dark. */
14 srcDark?: string;
15 alt: string;
16 caption?: string;
17 size?: FigureSize;
18 className?: string;
19 width?: number;
20 height?: number;
21 /** Set to true for above-the-fold images to prioritize LCP */
22 priority?: boolean;
23 }
24
25 const sizeClasses: Record<FigureSize, string> = {
26 sm: 'max-w-[300px]', // Small dialogs, icons
27 md: 'max-w-[500px]', // Medium screenshots
28 lg: 'max-w-[700px]', // Large screenshots
29 full: 'max-w-full', // Full-width diagrams
30 };
31
32 // Default dimensions per size to minimize CLS
33 const defaultDimensions: Record<FigureSize, { width: number; height: number }> = {
34 sm: { width: 300, height: 200 },
35 md: { width: 500, height: 333 },
36 lg: { width: 700, height: 467 },
37 full: { width: 900, height: 600 },
38 };
39
40 // Responsive sizes for optimal image loading
41 const sizesAttr: Record<FigureSize, string> = {
42 sm: '(max-width: 640px) 100vw, 300px',
43 md: '(max-width: 640px) 100vw, 500px',
44 lg: '(max-width: 640px) 100vw, (max-width: 768px) 90vw, 700px',
45 full: '(max-width: 640px) 100vw, (max-width: 1024px) 90vw, min(900px, 70vw)',
46 };
47
48 export function Figure({ src, srcDark, alt, caption, size = 'full', className, width, height, priority = false }: FigureProps) {
49 const [isLoaded, setIsLoaded] = useState(false);
50 const isConstrained = size !== 'full';
51 const dimensions = defaultDimensions[size];
52
53 // Show image on load or error (so broken images are visible for debugging)
54 const handleReady = () => setIsLoaded(true);
55
56 const imgClasses = cn(
57 'rounded-lg border border-fd-border transition-opacity duration-300',
58 isLoaded ? 'opacity-100' : 'opacity-0',
59 sizeClasses[size],
60 isConstrained ? 'w-auto h-auto' : 'w-full h-auto'
61 );
62
63 const sharedProps = {
64 alt,
65 width: width || dimensions.width,
66 height: height || dimensions.height,
67 sizes: sizesAttr[size],
68 priority,
69 onError: handleReady,
70 };
71
72 return (
73 <figure className={cn('my-8', isConstrained && 'flex flex-col items-center', className)}>
74 {srcDark ? (
75 <>
76 <Zoom zoomImg={{ src }}>
77 <Image
78 src={src}
79 {...sharedProps}
80 onLoad={handleReady}
81 className={cn(imgClasses, 'dark:hidden')}
82 />
83 </Zoom>
84 <Zoom zoomImg={{ src: srcDark }}>
85 <Image
86 src={srcDark}
87 {...sharedProps}
88 onLoad={handleReady}
89 className={cn(imgClasses, 'hidden dark:block')}
90 />
91 </Zoom>
92 </>
93 ) : (
94 <Zoom zoomImg={{ src }}>
95 <Image
96 src={src}
97 {...sharedProps}
98 onLoad={handleReady}
99 className={imgClasses}
100 />
101 </Zoom>
102 )}
103 {caption && (
104 <figcaption className="mt-3 text-sm text-fd-muted-foreground text-center">
105 {caption}
106 </figcaption>
107 )}
108 </figure>
109 );
110 }