Skip to content

Commit

Permalink
fix: image src
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Feb 23, 2024
1 parent c00b0df commit 34f5b73
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/components/ui/image/ZoomedImage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
'use client'

import { forwardRef, memo, useCallback, useMemo, useRef, useState } from 'react'
import {
cloneElement,
forwardRef,
memo,
useCallback,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react'
import clsx from 'clsx'
import { useIsomorphicLayoutEffect } from 'foxact/use-isomorphic-layout-effect'
import mediumZoom from 'medium-zoom'
Expand Down Expand Up @@ -296,14 +305,31 @@ const OptimizedImage = memo(
const { height, width } = useMarkdownImageRecord(src!) || rest
const hasDim = !!(height && width)

const placeholderImageRef = useRef<HTMLImageElement>(null)
const ImageEl = (
<img data-zoom-src={src} alt={alt} src={src} ref={ref} {...rest} />
<img
data-zoom-src={src}
alt={alt}
src={src}
ref={placeholderImageRef}
{...rest}
/>
)

useImperativeHandle(ref, () => placeholderImageRef.current!)

const optimizedImageRef = useRef<HTMLImageElement>(null)

useIsomorphicLayoutEffect(() => {
const $renderImage = optimizedImageRef.current
if (!$renderImage) return
if (!placeholderImageRef.current) return
placeholderImageRef.current.src = $renderImage.src
}, [src])
return (
<>
{hasDim ? (
<>
{/* @ts-expect-error */}
<Image
alt={alt || ''}
fetchPriority="high"
Expand All @@ -312,9 +338,12 @@ const OptimizedImage = memo(
{...rest}
height={+height}
width={+width}
ref={optimizedImageRef}
/>
<div className="absolute inset-0 flex justify-center opacity-0">
{ImageEl}
{cloneElement(ImageEl, {
src: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', // blank src
})}
</div>
</>
) : (
Expand Down

0 comments on commit 34f5b73

Please sign in to comment.