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.

hero.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { useStorefront } from '../context';
  2. import type { HeroSection } from '../types';
  3. import { Container, Eyebrow, ImagePlaceholder, SfButton } from '../ui';
  4. /**
  5. * The photo for a hero: the one the design names, or else the store's newest product photo.
  6. * Null when the store has no photos yet.
  7. */
  8. function useHeroPhoto(section: HeroSection): string | null {
  9. const { heroImages } = useStorefront();
  10. const ref = section.content.image;
  11. return (
  12. (ref ? heroImages.refs[ref] : undefined) ??
  13. heroImages.fallback[0] ??
  14. null
  15. );
  16. }
  17. export function Hero({ section }: { section: HeroSection }) {
  18. const photo = useHeroPhoto(section);
  19. if (section.variant === 'image-full') {
  20. return <FullPhotoHero section={section} photo={photo} />;
  21. }
  22. if (section.variant === 'split') {
  23. return <SplitHero section={section} photo={photo} />;
  24. }
  25. return <CenteredHero section={section} />;
  26. }
  27. function Buttons({
  28. section,
  29. onPhoto = false,
  30. }: {
  31. section: HeroSection;
  32. onPhoto?: boolean;
  33. }) {
  34. const { primaryCta, secondaryCta } = section.content;
  35. return (
  36. <div className="mt-8 flex flex-wrap gap-3">
  37. <SfButton href={primaryCta.href}>{primaryCta.label}</SfButton>
  38. {secondaryCta && (
  39. <SfButton
  40. href={secondaryCta.href}
  41. variant="outline"
  42. className={
  43. onPhoto
  44. ? 'border-white/70 text-white hover:bg-white/10'
  45. : undefined
  46. }
  47. >
  48. {secondaryCta.label}
  49. </SfButton>
  50. )}
  51. </div>
  52. );
  53. }
  54. /** A photo across the whole width, with the words on top of it. */
  55. function FullPhotoHero({
  56. section,
  57. photo,
  58. }: {
  59. section: HeroSection;
  60. photo: string | null;
  61. }) {
  62. const { eyebrow, headline, subheadline } = section.content;
  63. return (
  64. <section
  65. id={section.id}
  66. className="sf-on-photo relative isolate scroll-mt-20 overflow-hidden"
  67. data-hero="image-full"
  68. >
  69. {photo ? (
  70. <img
  71. src={photo}
  72. alt=""
  73. className="absolute inset-0 -z-20 h-full w-full object-cover"
  74. />
  75. ) : (
  76. <div
  77. className="absolute inset-0 -z-20"
  78. style={{
  79. background:
  80. 'linear-gradient(135deg, var(--sf-primary), color-mix(in srgb, var(--sf-accent) 55%, var(--sf-primary)))',
  81. }}
  82. />
  83. )}
  84. {/* Darkens the photo where the words sit, so they stay readable on any photo. */}
  85. <div className="absolute inset-0 -z-10 bg-linear-to-t from-black/75 via-black/35 to-black/5 md:bg-linear-to-r md:from-black/70 md:via-black/35 md:to-transparent" />
  86. <Container className="flex min-h-[480px] items-end py-14 md:min-h-[600px] md:items-center md:py-24">
  87. <div className="max-w-xl text-white">
  88. {eyebrow && (
  89. <p className="text-xs font-semibold tracking-[0.18em] text-white/85 uppercase">
  90. {eyebrow}
  91. </p>
  92. )}
  93. <h1 className="sf-heading mt-4 text-4xl leading-[1.05] font-semibold text-balance sm:text-5xl lg:text-6xl">
  94. {headline}
  95. </h1>
  96. <p className="mt-5 max-w-md text-lg text-white/85">
  97. {subheadline}
  98. </p>
  99. <Buttons section={section} onPhoto />
  100. </div>
  101. </Container>
  102. </section>
  103. );
  104. }
  105. /** Words on one side, a tall photo on the other. */
  106. function SplitHero({
  107. section,
  108. photo,
  109. }: {
  110. section: HeroSection;
  111. photo: string | null;
  112. }) {
  113. const { eyebrow, headline, subheadline } = section.content;
  114. return (
  115. <section id={section.id} className="scroll-mt-20" data-hero="split">
  116. <Container className="grid items-center gap-10 py-12 md:grid-cols-[1fr_1.1fr] md:gap-14 md:py-20">
  117. <div className="order-2 md:order-1">
  118. {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
  119. <h1 className="sf-heading mt-4 text-4xl leading-[1.05] font-semibold text-balance sm:text-5xl lg:text-6xl">
  120. {headline}
  121. </h1>
  122. <p className="mt-5 max-w-md text-lg text-(--sf-muted)">
  123. {subheadline}
  124. </p>
  125. <Buttons section={section} />
  126. </div>
  127. <div className="order-1 md:order-2">
  128. {photo ? (
  129. <img
  130. src={photo}
  131. alt=""
  132. className="sf-card aspect-[4/5] w-full rounded-(--sf-radius-card) object-cover md:aspect-[5/6]"
  133. />
  134. ) : (
  135. <ImagePlaceholder
  136. label={headline}
  137. className="aspect-[4/5] w-full md:aspect-[5/6]"
  138. />
  139. )}
  140. </div>
  141. </Container>
  142. </section>
  143. );
  144. }
  145. /** Words only, centred on a soft background. */
  146. function CenteredHero({ section }: { section: HeroSection }) {
  147. const { eyebrow, headline, subheadline } = section.content;
  148. return (
  149. <section
  150. id={section.id}
  151. className="scroll-mt-20 bg-(--sf-surface)"
  152. data-hero="centered"
  153. >
  154. <Container className="flex flex-col items-center py-20 text-center md:py-28">
  155. {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
  156. <h1 className="sf-heading mt-4 max-w-3xl text-4xl leading-[1.08] font-semibold text-balance sm:text-5xl lg:text-6xl">
  157. {headline}
  158. </h1>
  159. <p className="mt-5 max-w-xl text-lg text-(--sf-muted)">
  160. {subheadline}
  161. </p>
  162. <div className="flex justify-center">
  163. <Buttons section={section} />
  164. </div>
  165. </Container>
  166. </section>
  167. );
  168. }