384 lines
10 KiB
TypeScript
384 lines
10 KiB
TypeScript
import {useCallback, useEffect, useMemo, useState} from 'react';
|
||
import {Image} from '@shopify/hydrogen';
|
||
import {motion} from 'motion/react';
|
||
import {Instagram} from 'lucide-react';
|
||
import communityVertical2Src from '~/assets/instagram/vertical2.png';
|
||
import communityVertical3Src from '~/assets/instagram/vertical3.png';
|
||
import type {LifestyleGridSectionCms, LifestyleGridTileCms} from '~/lib/lifestyleGrid';
|
||
import {MOCK_LIFESTYLE_GRID_SECTION_CMS} from '~/lib/lifestyleGrid';
|
||
import {PAGE_TITLE_TYPOGRAPHY_CLASS} from '~/lib/pageTitle';
|
||
import {useSwipeNavigate} from '~/lib/useSwipeNavigate';
|
||
|
||
/**
|
||
* GROQ: `home` singleton → lifestyle / Instagram grid.
|
||
* Homepage loader merges in live IG for the first slot when 3 tiles are available (see
|
||
* `~/lib/instagramFeed.ts`). Slots 2–3 always use `vertical2.png` and `vertical3.png`.
|
||
*/
|
||
export const LIFESTYLE_GRID_SECTION_GROQ = `*[_type == "home" && _id == "home"][0].lifestyleGridSection{
|
||
eyebrow,
|
||
headline,
|
||
instagramUrl,
|
||
followLabel,
|
||
sectionBackground,
|
||
footerPrefix,
|
||
hashtag,
|
||
footerSuffix,
|
||
hashtagColor,
|
||
tiles[]{
|
||
caption,
|
||
tall,
|
||
externalImageUrl,
|
||
image{
|
||
alt,
|
||
asset->{
|
||
_id,
|
||
url,
|
||
metadata{dimensions{width,height}}
|
||
}
|
||
}
|
||
}
|
||
}` as const;
|
||
|
||
export {MOCK_LIFESTYLE_GRID_SECTION_CMS};
|
||
export type {LifestyleGridSectionCms};
|
||
|
||
function isRemoteImageUrl(url: string) {
|
||
return /^https?:\/\//i.test(url);
|
||
}
|
||
|
||
function LifestyleGridImage({
|
||
tile,
|
||
sizes,
|
||
}: {
|
||
tile: LifestyleGridTileCms;
|
||
sizes: string;
|
||
}) {
|
||
const className =
|
||
'h-full w-full object-cover transition-transform duration-700 group-hover:scale-105';
|
||
|
||
if (isRemoteImageUrl(tile.imageUrl)) {
|
||
const w = tile.width ?? 800;
|
||
const h = tile.height ?? 1200;
|
||
return (
|
||
<Image
|
||
alt={tile.alt}
|
||
className={className}
|
||
data={{
|
||
url: tile.imageUrl,
|
||
altText: tile.alt,
|
||
width: w,
|
||
height: h,
|
||
}}
|
||
loader={({src}) => src ?? ''}
|
||
sizes={sizes}
|
||
/>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<Image
|
||
alt={tile.alt}
|
||
className={className}
|
||
height={tile.height ?? 1600}
|
||
sizes={sizes}
|
||
src={tile.imageUrl}
|
||
width={tile.width ?? 1200}
|
||
/>
|
||
);
|
||
}
|
||
|
||
const OVERLAY_BG =
|
||
'linear-gradient(to top, rgba(26,26,24,0.6) 0%, transparent 60%)';
|
||
|
||
const LIFESTYLE_MOBILE_SLIDE_MS = 2000;
|
||
const LIFESTYLE_DOT_ACTIVE = '#5D0B24';
|
||
const LIFESTYLE_DOT_INACTIVE = 'rgba(26,26,24,0.22)';
|
||
|
||
function LifestyleGridMobileSlider({tiles}: {tiles: LifestyleGridTileCms[]}) {
|
||
const [active, setActive] = useState(0);
|
||
const tilesKey = useMemo(
|
||
() => tiles.map((t) => `${t.imageUrl}\0${t.caption}`).join('|'),
|
||
[tiles],
|
||
);
|
||
|
||
useEffect(() => {
|
||
setActive(0);
|
||
}, [tilesKey]);
|
||
|
||
useEffect(() => {
|
||
if (tiles.length <= 1) return;
|
||
if (typeof window === 'undefined') return;
|
||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||
|
||
const id = window.setInterval(() => {
|
||
setActive((i) => (i + 1) % tiles.length);
|
||
}, LIFESTYLE_MOBILE_SLIDE_MS);
|
||
return () => window.clearInterval(id);
|
||
}, [tiles.length]);
|
||
|
||
const goPrev = useCallback(() => {
|
||
if (tiles.length <= 1) return;
|
||
setActive((i) => (i - 1 + tiles.length) % tiles.length);
|
||
}, [tiles.length]);
|
||
|
||
const goNext = useCallback(() => {
|
||
if (tiles.length <= 1) return;
|
||
setActive((i) => (i + 1) % tiles.length);
|
||
}, [tiles.length]);
|
||
|
||
const swipe = useSwipeNavigate(goPrev, goNext);
|
||
|
||
return (
|
||
<div
|
||
className="w-full overflow-hidden lg:hidden"
|
||
aria-roledescription="carousel"
|
||
aria-label="Community gallery"
|
||
{...(tiles.length > 1 ? swipe : {})}
|
||
>
|
||
<div
|
||
className="flex h-[min(520px,88vw)] transition-transform duration-500 ease-out motion-reduce:transition-none"
|
||
style={{transform: `translateX(-${active * 100}%)`}}
|
||
>
|
||
{tiles.map((tile, index) => (
|
||
<div
|
||
key={`${tile.imageUrl}-${index}`}
|
||
className="h-[min(520px,88vw)] w-full shrink-0 basis-full px-0.5"
|
||
aria-hidden={index !== active}
|
||
>
|
||
<TileCard
|
||
delay={index * 0.06}
|
||
layout="slide"
|
||
sizes="100vw"
|
||
smallOverlay
|
||
tile={tile}
|
||
/>
|
||
</div>
|
||
))}
|
||
</div>
|
||
<div className="mt-5 flex justify-center gap-1.5" aria-hidden>
|
||
{tiles.map((_, i) => (
|
||
<span
|
||
key={i}
|
||
role="presentation"
|
||
className="h-1.5 rounded-full transition-all duration-300"
|
||
style={{
|
||
width: i === active ? 22 : 6,
|
||
background: i === active ? LIFESTYLE_DOT_ACTIVE : LIFESTYLE_DOT_INACTIVE,
|
||
}}
|
||
/>
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
type TileCardLayout = 'equalRow' | 'slide';
|
||
|
||
function TileCard({
|
||
tile,
|
||
sizes,
|
||
layout = 'slide',
|
||
smallOverlay,
|
||
delay,
|
||
}: {
|
||
tile: LifestyleGridTileCms;
|
||
sizes: string;
|
||
/** `equalRow`: one of three equal columns (desktop). `slide`: full-width slide (mobile). */
|
||
layout?: TileCardLayout;
|
||
smallOverlay?: boolean;
|
||
delay: number;
|
||
}) {
|
||
const overlay = (
|
||
<div
|
||
className={`absolute inset-0 flex items-end opacity-0 transition-opacity duration-300 group-hover:opacity-100 ${smallOverlay ? 'p-3' : 'p-4'}`}
|
||
style={{background: OVERLAY_BG}}
|
||
>
|
||
<div
|
||
className={`flex items-center ${smallOverlay ? 'gap-1.5' : 'gap-2'}`}
|
||
>
|
||
<Instagram
|
||
aria-hidden
|
||
size={smallOverlay ? 10 : 12}
|
||
strokeWidth={1.5}
|
||
style={{color: '#F8F4EF'}}
|
||
/>
|
||
<span
|
||
className={`font-['Jost',sans-serif] not-italic ${smallOverlay ? 'text-[0.6rem]' : 'text-[0.65rem]'}`}
|
||
style={{color: '#F8F4EF'}}
|
||
>
|
||
{tile.caption}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
);
|
||
|
||
const inner = (
|
||
<>
|
||
<LifestyleGridImage sizes={sizes} tile={tile} />
|
||
{overlay}
|
||
</>
|
||
);
|
||
|
||
const className =
|
||
layout === 'equalRow'
|
||
? 'group relative block h-full min-h-0 min-w-0 w-full flex-1 basis-0 cursor-pointer overflow-hidden'
|
||
: 'group relative block h-full min-h-0 w-full cursor-pointer overflow-hidden';
|
||
|
||
const motionProps = {
|
||
initial: false,
|
||
viewport: {once: true},
|
||
whileInView: {opacity: 1, scale: 1},
|
||
transition: {duration: 0.5, delay},
|
||
className,
|
||
};
|
||
|
||
if (tile.postUrl) {
|
||
return (
|
||
<motion.a
|
||
{...motionProps}
|
||
href={tile.postUrl}
|
||
rel="noopener noreferrer"
|
||
target="_blank"
|
||
>
|
||
{inner}
|
||
</motion.a>
|
||
);
|
||
}
|
||
|
||
return <motion.div {...motionProps}>{inner}</motion.div>;
|
||
}
|
||
|
||
export type LifestyleGridProps = {
|
||
cms: LifestyleGridSectionCms;
|
||
};
|
||
|
||
/**
|
||
* `React/src/app/components/LifestyleGrid.tsx` — Tailwind + Hydrogen `Image`.
|
||
*/
|
||
/** Slot 1 from CMS/IG; slots 2–3 always use bundled vertical art. */
|
||
function withStaticSecondThirdTiles(
|
||
tiles: LifestyleGridTileCms[],
|
||
): LifestyleGridTileCms[] {
|
||
if (tiles.length < 3) return tiles;
|
||
const [a, b, c] = tiles;
|
||
return [
|
||
a,
|
||
{
|
||
...b,
|
||
imageUrl: communityVertical2Src,
|
||
width: 455,
|
||
height: 580,
|
||
alt: b.alt || 'ABiiR Skincare — community',
|
||
},
|
||
{
|
||
...c,
|
||
imageUrl: communityVertical3Src,
|
||
width: 455,
|
||
height: 580,
|
||
alt: c.alt || 'ABiiR Skincare — community',
|
||
},
|
||
];
|
||
}
|
||
|
||
export function LifestyleGrid({cms}: LifestyleGridProps) {
|
||
const tiles = useMemo(() => {
|
||
if (cms.tiles.length < 3) return [];
|
||
return withStaticSecondThirdTiles(cms.tiles);
|
||
}, [cms.tiles]);
|
||
|
||
if (cms.tiles.length < 3 || tiles.length < 3) {
|
||
return null;
|
||
}
|
||
|
||
const [first, second, third] = tiles;
|
||
const sectionBg = cms.sectionBackground;
|
||
const burgundy = '#5D0B24';
|
||
const charcoal = '#1A1A18';
|
||
|
||
return (
|
||
<section className="w-full py-20 lg:py-28" style={{background: sectionBg}}>
|
||
<div className="mx-auto max-w-[1400px] px-6 lg:px-12">
|
||
<div className="mb-4 flex flex-col justify-between gap-3 sm:flex-row sm:items-end sm:mb-8 lg:mb-10">
|
||
<div>
|
||
<motion.span
|
||
initial={false}
|
||
viewport={{once: true}}
|
||
whileInView={{opacity: 1, y: 0}}
|
||
className="mb-2 inline-block font-['Jost',sans-serif] text-[0.65rem] font-medium uppercase not-italic sm:mb-3"
|
||
style={{letterSpacing: '0.25em', color: burgundy}}
|
||
>
|
||
{cms.eyebrow}
|
||
</motion.span>
|
||
<h2
|
||
className={`m-0 ${PAGE_TITLE_TYPOGRAPHY_CLASS}`}
|
||
style={{color: charcoal}}
|
||
>
|
||
<a
|
||
href={cms.instagramUrl}
|
||
target="_blank"
|
||
rel="noopener noreferrer"
|
||
className="text-inherit no-underline transition-opacity hover:opacity-70"
|
||
>
|
||
{cms.headline}
|
||
</a>
|
||
</h2>
|
||
</div>
|
||
<a
|
||
href={cms.instagramUrl}
|
||
target="_blank"
|
||
rel="noopener noreferrer"
|
||
className="inline-flex items-center gap-2 font-['Jost',sans-serif] text-[0.65rem] font-medium uppercase no-underline transition-opacity hover:opacity-60 not-italic"
|
||
style={{letterSpacing: '0.12em', color: charcoal}}
|
||
>
|
||
<Instagram size={14} strokeWidth={1.5} aria-hidden />
|
||
{cms.followLabel}
|
||
</a>
|
||
</div>
|
||
|
||
<LifestyleGridMobileSlider tiles={tiles} />
|
||
|
||
<div className="hidden h-[480px] min-h-0 w-full gap-3 lg:flex lg:h-[580px]">
|
||
<TileCard
|
||
delay={0}
|
||
layout="equalRow"
|
||
sizes="(min-width: 1024px) 30vw, 100vw"
|
||
smallOverlay
|
||
tile={first}
|
||
/>
|
||
<TileCard
|
||
delay={0.08}
|
||
layout="equalRow"
|
||
sizes="(min-width: 1024px) 30vw, 100vw"
|
||
smallOverlay
|
||
tile={second}
|
||
/>
|
||
<TileCard
|
||
delay={0.16}
|
||
layout="equalRow"
|
||
sizes="(min-width: 1024px) 30vw, 100vw"
|
||
smallOverlay
|
||
tile={third}
|
||
/>
|
||
</div>
|
||
|
||
<div className="mt-8 text-center">
|
||
<p
|
||
className="m-0 font-['Jost',sans-serif] font-light not-italic"
|
||
style={{
|
||
color: '#7A7068',
|
||
fontSize: '0.78rem',
|
||
fontWeight: 300,
|
||
}}
|
||
>
|
||
{cms.footerPrefix}
|
||
<strong style={{color: cms.hashtagColor, fontWeight: 500}}>
|
||
{cms.hashtag}
|
||
</strong>
|
||
{cms.footerSuffix}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
);
|
||
}
|