Refactors image component to support ref forwarding

Enables ref forwarding for improved integration with parent components
and libraries that require direct DOM access. Enhances flexibility and
maintainability by switching to a forwardRef implementation.
This commit is contained in:
shibamudi
2026-01-29 13:46:31 +08:00
committed by dayuan.jiang
parent f1a3044224
commit feeb9ec0c5
+13 -11
View File
@@ -1,14 +1,16 @@
import NextImage from "next/image"
import type * as React from "react"
import NextImage, { type ImageProps } from "next/image"
import { forwardRef } from "react"
import { getAssetUrl } from "@/lib/base-path"
export default function Image(props: React.ComponentProps<typeof NextImage>) {
const src =
typeof props.src === "string" &&
props.src.startsWith("/") &&
!props.src.startsWith("//")
? getAssetUrl(props.src)
: props.src
export default forwardRef<HTMLImageElement, ImageProps>(
function Image(props, ref) {
const src =
typeof props.src === "string" &&
props.src.startsWith("/") &&
!props.src.startsWith("//")
? getAssetUrl(props.src)
: props.src
return <NextImage {...props} src={src} />
}
return <NextImage {...props} src={src} ref={ref} />
},
)