Making next/image component work with chakra's responsive syntax #2475
Replies: 10 comments 20 replies
-
What about other BoxProps, let's say we want to add a borderRadius to the image, any idea on how to achieve that ? |
Beta Was this translation helpful? Give feedback.
-
When I try this I get this error Error: Image with src "/images/logo_black.png" must use "width" and "height" properties or "layout='fill'" property. When I have this code
Is this the only way to make next/image have chakra responsiveness? |
Beta Was this translation helpful? Give feedback.
-
For me, wrapping the next Image component inside a Chakra Box worked quite well so far. You might also need
I found that using |
Beta Was this translation helpful? Give feedback.
-
Related: Just saw this example of using Chakra with import { Box,chakra } from '@chakra-ui/react'
import { shimmer } from '@/components/shimmer';
import { toBase64 } from '@/lib/toBase64';
import NextImage from "next/image";
const CoverImg = chakra(NextImage, {
shouldForwardProp: (prop) => ['width', 'height', 'src', 'alt','quality','placeholder','blurDataURL','loader '].includes(prop),
})
const myLoader = ({ src, width, quality }) => {
return `${src}?w=${width}&q=${quality}`;
};
export const BlogMedia = (props) => {
const { src, alt, ...rest } = props
return (
<Box pos="relative" cursor="pointer" className="group" {...rest}>
<CoverImg
w="auto"
h="auto"
loader={myLoader}
width={600}
quality={50}
height={384}
placeholder="blur"
blurDataURL={`data:image/svg+xml;base64,${toBase64(
shimmer(60, 60),
)}`}
src={src}
alt={alt}
transition="all 0.2s"
_groupHover={{
transform: 'scale(1.05)',
}}
/>
</Box>
)
} Source: https://github.com/perkinsjr/jamesperkins_dev/blob/main/components/blogmedia.js |
Beta Was this translation helpful? Give feedback.
-
I went for a full rewrite of
Why the
|
Beta Was this translation helpful? Give feedback.
-
I followed ptim reply: height={{ base: "full", lg: "720" }}
width={{ base: "full", lg: "100%" }} my <Image
src={src}
alt={altText}
height={{ base: "full", lg: "720" }}
width={{ base: "full", lg: "100%" }}
objectFit="cover"
border="1px"
borderColor={imageBorderColor}
placeholder="blur"
blurDataURL={src}
layout="fill"
/> Error
is there a way to make it work? |
Beta Was this translation helpful? Give feedback.
-
hi. for some reason some images don't work with my next + chakra image, the image is shown as broken, and alt text is displayed.
i'm creating this image with image from discogs:
i also tried using only next/image, but i get the same issue. using only chakra image works fine, image is displayed. i think the problem is using both together.
next & chakra versions:
|
Beta Was this translation helpful? Give feedback.
-
The short version is for those who just want some basic styling (border-radius, gradient...).
import { useState } from 'react';
import NextImage, { ImageProps } from 'next/image';
import { Skeleton, Box, BoxProps } from '@chakra-ui/react';
type Props = ImageProps &
BoxProps & {
alt: string;
fallbackSrc?: string;
};
const ShimmerImage: React.FC<Props> = (props) => {
const {
src,
alt,
width,
height,
fallbackSrc = 'https://storage.googleapis.com/proudcity/mebanenc/uploads/2021/03/placeholder-image.png',
bgGradient,
priority,
quality,
...rest
} = props;
const [isLoaded, setIsLoaded] = useState(false);
const [isError, setIsError] = useState(false);
const handleLoad = (result: { naturalWidth: number; naturalHeight: number }) => {
if (result.naturalWidth === 0) setIsError(true);
else setIsLoaded(true);
};
const handleError = () => setIsError(true);
return (
<Box width={width} height={height} position="relative" overflow="hidden" {...rest}>
<Skeleton position="absolute" top="0" left="0" width="100%" height="100%" isLoaded={isLoaded}>
<NextImage
src={isError ? fallbackSrc : src}
alt={alt}
layout="fill"
objectFit="cover"
onLoadingComplete={handleLoad}
onError={handleError}
priority={priority}
quality={quality}
/>
<Box position="absolute" top="0" left="0" width="100%" height="100%" bgGradient={bgGradient} />
</Skeleton>
</Box>
);
};
export default ShimmerImage; |
Beta Was this translation helpful? Give feedback.
-
The update of |
Beta Was this translation helpful? Give feedback.
-
For anyone still looking for a solution this is what I'm doing. /**
* Returns the avatar URL with specified width and quality.
*
* This is used to gain the benefits of next/image, but allow you to pass it in directly to other elements like Chakra's Avatar
*
* To see a list of valid width values, see here:
* https://github.com/vercel/next.js/blob/b9d0d6c2554bd39d74577c91a68e8723cbe77a80/packages/next/src/shared/lib/image-config.ts#L105
*
* @param url - The URL of the avatar image.
* @returns The formatted avatar URL or undefined if the URL is not provided.
*/
const getAvatarURL = (url?: string) => {
return url
? `${
process.env.NEXT_PUBLIC_ENV_ORIGIN
}/_next/image?url=${encodeURIComponent(url)}&w=${96}&q=95`
: undefined;
};
export default getAvatarURL; Basically, reverse engineer how NextImage works, pass it the url directly and return an optimised image URL. This can then be passed straight to your |
Beta Was this translation helpful? Give feedback.
-
Next
v10
came with a newnext/image
component which provides a lot of image optimization features.However, it doesn't work well with
chakra's
responsive syntax, because it requireswidth/height
property to have no unit, let alone accepting something like['400px', '800px', '1200px']
.Here's a quick wrapper component so that you could enjoy the best of both worlds♥️
Beta Was this translation helpful? Give feedback.
All reactions