Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
perf: Lazy-load zoom library in Figure component
- Use next/dynamic to load react-medium-image-zoom only when Figure renders - Add CSS type declarations for clean imports - Pages without <Figure> no longer load ~30KB zoom library Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sushmithamallesh committed
Feb 2, 2026 at 12:28 UTC
6ec0023a8b24829b850db9b820c550c69481e9a6
2 files changed
+21
-5
docs/components/figure.tsx
+15
-5
@@ -1,11 +1,16 @@
1
'use client';
2
3
-import { useState } from 'react';
3
+import { useState, useEffect, type ReactNode } 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';
5
+import dynamic from 'next/dynamic';
6
import { cn } from '@/lib/utils';
7
8
+// Lazy-load zoom library - only loads when Figure component is rendered
9
+const ZoomWrapper = dynamic<{ children: ReactNode; zoomImg: { src: string } }>(
10
+ () => import('react-medium-image-zoom').then((mod) => mod.default),
11
+ { ssr: false }
12
+);
13
+
14
type FigureSize = 'sm' | 'md' | 'lg' | 'full';
15
16
interface FigureProps {
@@ -48,12 +53,17 @@ export function Figure({ src, alt, caption, size = 'full', className, width, hei
53
const isConstrained = size !== 'full';
54
const dimensions = defaultDimensions[size];
55
56
+ // Load zoom CSS on mount
57
+ useEffect(() => {
58
+ import('react-medium-image-zoom/dist/styles.css');
59
+ }, []);
60
+
61
// Show image on load or error (so broken images are visible for debugging)
62
const handleReady = () => setIsLoaded(true);
63
64
return (
65
<figure className={cn('my-8', isConstrained && 'flex flex-col items-center', className)}>
56
- <Zoom zoomImg={{ src }}>
66
+ <ZoomWrapper zoomImg={{ src }}>
67
<Image
68
src={src}
69
alt={alt}
@@ -70,7 +80,7 @@ export function Figure({ src, alt, caption, size = 'full', className, width, hei
80
isConstrained ? 'w-auto h-auto' : 'w-full h-auto'
81
)}
82
/>
73
- </Zoom>
83
+ </ZoomWrapper>
84
{caption && (
85
<figcaption className="mt-3 text-sm text-fd-muted-foreground text-center">
86
{caption}
docs/types/css.d.ts
new
+6
@@ -0,0 +1,6 @@
1
+declare module '*.css' {
2
+ const content: Record<string, string>;
3
+ export default content;
4
+}
5
+
6
+declare module 'react-medium-image-zoom/dist/styles.css';