forked from nuotsu/sanitypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImg.tsx
61 lines (54 loc) · 1.12 KB
/
Img.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use client'
import { client } from '@/lib/sanity'
import {
useNextSanityImage,
type UseNextSanityImageOptions,
} from 'next-sanity-image'
import Image, { type ImageProps } from 'next/image'
export default function Img({
image,
imageWidth,
alt = '',
options,
...props
}: {
image?: Sanity.Image
imageWidth?: number
alt?: string
options?: UseNextSanityImageOptions
} & Omit<ImageProps, 'src' | 'alt'>) {
if (!image) return null
const imageProps = useNextSanityImage(
client,
image,
imageWidth ? { imageBuilder: (b) => b.width(imageWidth) } : options,
)
return (
<Image
{...imageProps}
alt={image.alt || alt}
loading={image.loading || 'lazy'}
unoptimized
{...props}
/>
)
}
export function Source({
image,
imageWidth,
options,
media = '(max-width: 768px)',
}: {
image?: Sanity.Image
imageWidth?: number
options?: UseNextSanityImageOptions
media?: string
}) {
if (!image) return null
const { src, ...imageProps } = useNextSanityImage(
client,
image,
imageWidth ? { imageBuilder: (b) => b.width(imageWidth) } : options,
)
return <source srcSet={src} {...imageProps} media={media} />
}