AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Play } from 'lucide-react';
  2. import { useState } from 'react';
  3. import { cn } from '@/lib/utils';
  4. import { useStorefront } from '../context';
  5. import { storefrontText } from '../text';
  6. import type { VideoSection } from '../types';
  7. import { Container, SfButton } from '../ui';
  8. type Video = { provider: 'youtube' | 'vimeo'; id: string };
  9. /** The video behind a YouTube or Vimeo address, or null for anything else. */
  10. export function parseVideo(url: string): Video | null {
  11. const youtube = url.match(
  12. /^https:\/\/(?:(?:www\.|m\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{6,20})/,
  13. );
  14. if (youtube) {
  15. return { provider: 'youtube', id: youtube[1] };
  16. }
  17. const vimeo = url.match(/^https:\/\/(?:www\.)?vimeo\.com\/([0-9]{5,12})/);
  18. return vimeo ? { provider: 'vimeo', id: vimeo[1] } : null;
  19. }
  20. /**
  21. * A YouTube or Vimeo video. Until the shopper presses play only a picture shows, so the page stays fast
  22. * and nothing from the video site loads for shoppers who never watch.
  23. */
  24. export function VideoBlock({ section }: { section: VideoSection }) {
  25. const { heading, text, videoUrl, cta } = section.content;
  26. const split = section.variant === 'split';
  27. const video = parseVideo(videoUrl);
  28. if (!video) {
  29. return null;
  30. }
  31. const words = (
  32. <div className={cn(!split && 'mx-auto max-w-2xl text-center')}>
  33. <h2 className="sf-heading text-3xl leading-tight font-semibold text-balance sm:text-4xl">
  34. {heading}
  35. </h2>
  36. {text && (
  37. <p className="mt-4 leading-relaxed text-(--sf-muted)">{text}</p>
  38. )}
  39. {cta && (
  40. <SfButton href={cta.href} className="mt-6">
  41. {cta.label}
  42. </SfButton>
  43. )}
  44. </div>
  45. );
  46. return (
  47. <section
  48. id={section.id}
  49. className="sf-section scroll-mt-20 py-16 md:py-20"
  50. data-layout={section.variant}
  51. >
  52. <Container
  53. className={cn(
  54. 'grid gap-10',
  55. split && 'items-center md:grid-cols-[2fr_3fr]',
  56. )}
  57. >
  58. {words}
  59. <Player video={video} title={heading} />
  60. </Container>
  61. </section>
  62. );
  63. }
  64. function Player({ video, title }: { video: Video; title: string }) {
  65. const { language } = useStorefront();
  66. const t = storefrontText(language);
  67. const [playing, setPlaying] = useState(false);
  68. const src =
  69. video.provider === 'youtube'
  70. ? `https://www.youtube-nocookie.com/embed/${video.id}?autoplay=1&rel=0`
  71. : `https://player.vimeo.com/video/${video.id}?autoplay=1&dnt=1`;
  72. return (
  73. <div className="sf-card relative aspect-video w-full overflow-hidden rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-fg)">
  74. {playing ? (
  75. <iframe
  76. src={src}
  77. title={title}
  78. allow="autoplay; encrypted-media; picture-in-picture; fullscreen"
  79. allowFullScreen
  80. className="absolute inset-0 h-full w-full"
  81. />
  82. ) : (
  83. <button
  84. type="button"
  85. onClick={() => setPlaying(true)}
  86. aria-label={t.playVideo(title)}
  87. className="group absolute inset-0 flex items-center justify-center"
  88. data-test="video-play"
  89. >
  90. {video.provider === 'youtube' ? (
  91. <img
  92. src={`https://i.ytimg.com/vi/${video.id}/hqdefault.jpg`}
  93. alt=""
  94. loading="lazy"
  95. className="absolute inset-0 h-full w-full object-cover opacity-90"
  96. />
  97. ) : (
  98. <span className="absolute inset-0 bg-linear-to-br from-(--sf-primary) to-(--sf-fg)" />
  99. )}
  100. <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">
  101. <Play className="ml-1 size-7" aria-hidden="true" />
  102. </span>
  103. </button>
  104. )}
  105. </div>
  106. );
  107. }