Skip to content

Commit 4065b0c

Browse files
committed
port image logic to imageLightbox
1 parent 9bffc3c commit 4065b0c

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/components/docImage.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ export default function DocImage({
1818

1919
// Handle external images early - pass through without processing
2020
if (src.startsWith('http')) {
21-
// Use provided props or defaults for external images
21+
// For external images, let ImageLightbox decide whether to use Next.js Image or regular img
22+
// Parse dimensions if provided
2223
const width =
2324
typeof propsWidth === 'number'
2425
? propsWidth
2526
: typeof propsWidth === 'string'
26-
? parseInt(propsWidth, 10) || 800
27-
: 800;
27+
? parseInt(propsWidth, 10) || undefined
28+
: undefined;
2829
const height =
2930
typeof propsHeight === 'number'
3031
? propsHeight
3132
: typeof propsHeight === 'string'
32-
? parseInt(propsHeight, 10) || 800
33-
: 800;
33+
? parseInt(propsHeight, 10) || undefined
34+
: undefined;
3435

3536
return (
3637
<ImageLightbox

src/components/imageLightbox/index.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ interface ImageLightboxProps
1313
'ref' | 'src' | 'width' | 'height' | 'alt'
1414
> {
1515
alt: string;
16-
height: number;
1716
imgPath: string;
1817
src: string;
19-
width: number;
18+
height?: number;
19+
width?: number;
2020
}
2121

2222
export function ImageLightbox({
@@ -31,8 +31,17 @@ export function ImageLightbox({
3131
}: ImageLightboxProps) {
3232
const [open, setOpen] = useState(false);
3333

34-
// Check if dimensions are valid (not NaN) for Next.js Image
35-
const isValidDimensions = !isNaN(width) && !isNaN(height) && width > 0 && height > 0;
34+
// Check if we should use Next.js Image or regular img
35+
// Use Next.js Image for internal images with valid dimensions
36+
// Use regular img for external images or when dimensions are invalid/missing
37+
const shouldUseNextImage =
38+
!src.startsWith('http') && // Internal image
39+
width != null &&
40+
height != null && // Dimensions provided
41+
!isNaN(width) &&
42+
!isNaN(height) && // Valid numbers
43+
width > 0 &&
44+
height > 0; // Positive values
3645

3746
const handleClick = (e: React.MouseEvent) => {
3847
// If Ctrl/Cmd+click, open image in new tab
@@ -70,14 +79,14 @@ export function ImageLightbox({
7079
// Next.js Image has stricter typing for certain props like 'placeholder'
7180
const {placeholder: _placeholder, ...imageCompatibleProps} = props;
7281

73-
// Render the appropriate image component based on dimension validity
82+
// Render the appropriate image component
7483
const renderImage = () => {
75-
if (isValidDimensions) {
84+
if (shouldUseNextImage) {
7685
return (
7786
<Image
7887
src={src}
79-
width={width}
80-
height={height}
88+
width={width!}
89+
height={height!}
8190
style={{
8291
width: '100%',
8392
height: 'auto',
@@ -131,12 +140,12 @@ export function ImageLightbox({
131140

132141
{/* Image container */}
133142
<div className="relative flex items-center justify-center">
134-
{isValidDimensions ? (
143+
{shouldUseNextImage ? (
135144
<Image
136145
src={src}
137146
alt={alt}
138-
width={width}
139-
height={height}
147+
width={width!}
148+
height={height!}
140149
className="max-h-[90vh] max-w-[90vw] object-contain"
141150
style={{
142151
width: 'auto',

0 commit comments

Comments
 (0)