| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import { Play } from 'lucide-react';
- import { useState } from 'react';
- import { cn } from '@/lib/utils';
- import { useStorefront } from '../context';
- import { storefrontText } from '../text';
- import type { VideoSection } from '../types';
- import { Container, SfButton } from '../ui';
-
- type Video = { provider: 'youtube' | 'vimeo'; id: string };
-
- /** The video behind a YouTube or Vimeo address, or null for anything else. */
- export function parseVideo(url: string): Video | null {
- const youtube = url.match(
- /^https:\/\/(?:(?:www\.|m\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{6,20})/,
- );
-
- if (youtube) {
- return { provider: 'youtube', id: youtube[1] };
- }
-
- const vimeo = url.match(/^https:\/\/(?:www\.)?vimeo\.com\/([0-9]{5,12})/);
-
- return vimeo ? { provider: 'vimeo', id: vimeo[1] } : null;
- }
-
- /**
- * A YouTube or Vimeo video. Until the shopper presses play only a picture shows, so the page stays fast
- * and nothing from the video site loads for shoppers who never watch.
- */
- export function VideoBlock({ section }: { section: VideoSection }) {
- const { heading, text, videoUrl, cta } = section.content;
- const split = section.variant === 'split';
- const video = parseVideo(videoUrl);
-
- if (!video) {
- return null;
- }
-
- const words = (
- <div className={cn(!split && 'mx-auto max-w-2xl text-center')}>
- <h2 className="sf-heading text-3xl leading-tight font-semibold text-balance sm:text-4xl">
- {heading}
- </h2>
- {text && (
- <p className="mt-4 leading-relaxed text-(--sf-muted)">{text}</p>
- )}
- {cta && (
- <SfButton href={cta.href} className="mt-6">
- {cta.label}
- </SfButton>
- )}
- </div>
- );
-
- return (
- <section
- id={section.id}
- className="sf-section scroll-mt-20 py-16 md:py-20"
- data-layout={section.variant}
- >
- <Container
- className={cn(
- 'grid gap-10',
- split && 'items-center md:grid-cols-[2fr_3fr]',
- )}
- >
- {words}
- <Player video={video} title={heading} />
- </Container>
- </section>
- );
- }
-
- function Player({ video, title }: { video: Video; title: string }) {
- const { language } = useStorefront();
- const t = storefrontText(language);
- const [playing, setPlaying] = useState(false);
-
- const src =
- video.provider === 'youtube'
- ? `https://www.youtube-nocookie.com/embed/${video.id}?autoplay=1&rel=0`
- : `https://player.vimeo.com/video/${video.id}?autoplay=1&dnt=1`;
-
- return (
- <div className="sf-card relative aspect-video w-full overflow-hidden rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-fg)">
- {playing ? (
- <iframe
- src={src}
- title={title}
- allow="autoplay; encrypted-media; picture-in-picture; fullscreen"
- allowFullScreen
- className="absolute inset-0 h-full w-full"
- />
- ) : (
- <button
- type="button"
- onClick={() => setPlaying(true)}
- aria-label={t.playVideo(title)}
- className="group absolute inset-0 flex items-center justify-center"
- data-test="video-play"
- >
- {video.provider === 'youtube' ? (
- <img
- src={`https://i.ytimg.com/vi/${video.id}/hqdefault.jpg`}
- alt=""
- loading="lazy"
- className="absolute inset-0 h-full w-full object-cover opacity-90"
- />
- ) : (
- <span className="absolute inset-0 bg-linear-to-br from-(--sf-primary) to-(--sf-fg)" />
- )}
- <span className="relative flex size-16 items-center justify-center rounded-full bg-(--sf-primary) text-(--sf-primary-fg) shadow-lg transition-transform group-hover:scale-110">
- <Play className="ml-1 size-7" aria-hidden="true" />
- </span>
- </button>
- )}
- </div>
- );
- }
|