Skip to content

Commit 40171db

Browse files
committed
use assets for image in hero modules
1 parent 7017e77 commit 40171db

File tree

6 files changed

+65
-107
lines changed

6 files changed

+65
-107
lines changed

src/sanity/schemaTypes/modules/hero.saas.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineType({
1010
type: 'object',
1111
groups: [
1212
{ name: 'content', default: true },
13-
{ name: 'image' },
13+
{ name: 'asset' },
1414
{ name: 'options' },
1515
],
1616
fields: [
@@ -49,38 +49,24 @@ export default defineType({
4949
group: 'content',
5050
}),
5151
defineField({
52-
name: 'image',
53-
type: 'image',
54-
options: {
55-
hotspot: true,
56-
},
57-
group: 'image',
58-
fields: [
59-
defineField({
60-
name: 'alt',
61-
type: 'string',
62-
}),
63-
defineField({
64-
name: 'faded',
65-
type: 'boolean',
66-
initialValue: true,
67-
}),
68-
defineField({
69-
name: 'loading',
70-
type: 'string',
71-
options: {
72-
list: ['lazy', 'eager'],
73-
layout: 'radio',
74-
},
75-
initialValue: 'lazy',
76-
}),
77-
],
52+
name: 'assets',
53+
title: 'Assets',
54+
type: 'array',
55+
of: [{ type: 'img' }],
56+
validation: (Rule) => Rule.max(1),
57+
group: 'asset',
58+
}),
59+
defineField({
60+
name: 'assetFaded',
61+
type: 'boolean',
62+
initialValue: true,
63+
group: 'asset',
7864
}),
7965
],
8066
preview: {
8167
select: {
8268
content: 'content',
83-
media: 'image',
69+
media: 'assets.0.image',
8470
},
8571
prepare: ({ content, media }) => ({
8672
title: getBlockText(content),

src/sanity/schemaTypes/modules/hero.ts

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineType({
1515
type: 'object',
1616
groups: [
1717
{ name: 'content', default: true },
18-
{ name: 'image' },
18+
{ name: 'asset' },
1919
{ name: 'options' },
2020
],
2121
fieldsets: [alignmentFieldset, { name: 'image', options: { columns: 2 } }],
@@ -44,50 +44,12 @@ export default defineType({
4444
group: 'content',
4545
}),
4646
defineField({
47-
name: 'bgImage',
48-
title: 'Background image',
49-
type: 'image',
50-
options: {
51-
hotspot: true,
52-
},
53-
fields: [
54-
defineField({
55-
name: 'alt',
56-
type: 'string',
57-
}),
58-
defineField({
59-
name: 'loading',
60-
type: 'string',
61-
options: {
62-
list: ['lazy', 'eager'],
63-
layout: 'radio',
64-
},
65-
initialValue: 'lazy',
66-
}),
67-
],
68-
group: 'image',
69-
fieldset: 'image',
70-
}),
71-
defineField({
72-
name: 'bgImageMobile',
73-
title: 'Background image (mobile)',
74-
type: 'image',
75-
options: {
76-
hotspot: true,
77-
},
78-
fields: [
79-
defineField({
80-
name: 'loading',
81-
type: 'string',
82-
options: {
83-
list: ['lazy', 'eager'],
84-
layout: 'radio',
85-
},
86-
initialValue: 'lazy',
87-
}),
88-
],
89-
group: 'image',
90-
fieldset: 'image',
47+
name: 'assets',
48+
title: 'Assets',
49+
type: 'array',
50+
of: [{ type: 'img' }],
51+
validation: (Rule) => Rule.max(1),
52+
group: 'asset',
9153
}),
9254
defineField({
9355
...alignItems,
@@ -101,7 +63,7 @@ export default defineType({
10163
preview: {
10264
select: {
10365
content: 'content',
104-
media: 'bgImage.asset',
66+
media: 'assets.0.image',
10567
},
10668
prepare: ({ content, media }) => ({
10769
title: getBlockText(content),

src/ui/Img.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ type ImgProps = { alt?: string } & Omit<ImageProps, 'src' | 'alt'>
99

1010
export function ResponsiveImg({
1111
img,
12+
className,
13+
imgClassName,
1214
...props
13-
}: { img?: Sanity.Img } & ImgProps) {
15+
}: { img?: Sanity.Img; imgClassName?: string } & ImgProps) {
1416
if (!img) return null
1517

1618
return (
17-
<picture>
19+
<picture className={className}>
1820
{img.responsive?.map((r, key) => <Source {...r} key={key} />)}
19-
<Img image={img.image} {...props} />
21+
<Img image={img.image} {...props} className={imgClassName} />
2022
</picture>
2123
)
2224
}

src/ui/modules/Hero.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moduleProps from '@/lib/moduleProps'
2-
import { Img, Source } from '@/ui/Img'
2+
import { ResponsiveImg } from '@/ui/Img'
33
import { PortableText, stegaClean } from 'next-sanity'
44
import CTAList from '@/ui/CTAList'
55
import Pretitle from '@/ui/Pretitle'
@@ -11,22 +11,21 @@ export default function Hero({
1111
pretitle,
1212
content,
1313
ctas,
14-
bgImage,
15-
bgImageMobile,
14+
assets,
1615
textAlign = 'center',
1716
alignItems,
1817
...props
1918
}: Partial<{
2019
pretitle: string
2120
content: any
2221
ctas: Sanity.CTA[]
23-
bgImage: Sanity.Image
24-
bgImageMobile: Sanity.Image
22+
assets: Sanity.Img[]
2523
textAlign: React.CSSProperties['textAlign']
2624
alignItems: React.CSSProperties['alignItems']
2725
}> &
2826
Sanity.Module) {
29-
const hasImage = !!bgImage?.asset
27+
const hasImage = !!assets?.[0]
28+
const asset = assets?.[0]
3029

3130
return (
3231
<section
@@ -37,15 +36,12 @@ export default function Hero({
3736
{...moduleProps(props)}
3837
>
3938
{hasImage && (
40-
<picture>
41-
<Source image={bgImageMobile} width={1200} />
42-
<Img
43-
className="max-h-fold size-full object-cover"
44-
image={bgImage}
45-
width={2400}
46-
draggable={false}
47-
/>
48-
</picture>
39+
<ResponsiveImg
40+
img={asset}
41+
imgClassName="max-h-fold size-full object-cover"
42+
width={2400}
43+
draggable={false}
44+
/>
4945
)}
5046

5147
{content && (

src/ui/modules/HeroSaaS.tsx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import Code from './RichtextModule/Code'
55
import CustomHTML from './CustomHTML'
66
import Reputation from '@/ui/Reputation'
77
import CTAList from '@/ui/CTAList'
8-
import { Img } from '@/ui/Img'
8+
import { ResponsiveImg } from '@/ui/Img'
99
import { cn } from '@/lib/utils'
1010

1111
export default function HeroSaaS({
1212
pretitle,
1313
content,
1414
ctas,
15-
image,
15+
assets,
16+
assetFaded,
1617
...props
1718
}: Partial<{
1819
pretitle: string
1920
content: any
2021
ctas: Sanity.CTA[]
21-
image: Sanity.Image & {
22-
faded?: boolean
23-
}
22+
assets: Sanity.Img[]
23+
assetFaded?: boolean
2424
}> &
2525
Sanity.Module) {
26+
const asset = assets?.[0]
27+
2628
return (
2729
<section className="section space-y-8 text-center" {...moduleProps(props)}>
2830
<div className="richtext mx-auto max-w-2xl text-balance">
@@ -51,15 +53,25 @@ export default function HeroSaaS({
5153
<CTAList ctas={ctas} className="!mt-8 justify-center" />
5254
</div>
5355

54-
<Img
55-
className={cn(
56-
'anim-fade-to-t w-full [animation-duration:1s]',
57-
image?.faded && '[mask:linear-gradient(to_top,transparent,#000_50%)]',
58-
)}
59-
image={image}
60-
width={2400}
61-
draggable={false}
62-
/>
56+
{(() => {
57+
switch (asset?._type) {
58+
case 'img':
59+
return (
60+
<ResponsiveImg
61+
img={asset}
62+
className={cn(
63+
'anim-fade-to-t w-full [animation-duration:1s]',
64+
assetFaded &&
65+
'[mask:linear-gradient(to_top,transparent,#000_50%)]',
66+
)}
67+
width={2400}
68+
draggable={false}
69+
/>
70+
)
71+
default:
72+
return null
73+
}
74+
})()}
6375
</section>
6476
)
6577
}

src/ui/modules/HeroSplit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function HeroSplit({
3636
switch (asset?._type) {
3737
case 'img':
3838
return (
39-
<ResponsiveImg img={asset} className="w-full" width={1200} />
39+
<ResponsiveImg img={asset} imgClassName="w-full" width={1200} />
4040
)
4141
default:
4242
return null

0 commit comments

Comments
 (0)