🎥 Make videos programmatically with React
Elements: Add product collection (#10644)
Igor Samokhovetsb510019d32e3
diff --git a/packages/docs/elements/commerce/product-collection/index.mdx b/packages/docs/elements/commerce/product-collection/index.mdxnew file mode 100644index 0000000000..543c766acc--- /dev/null+++ b/packages/docs/elements/commerce/product-collection/index.mdx@@ -0,0 +1,12 @@+---+title: Product Collection+description: A rotating product-card carousel that adapts to variable catalog content.+image: https://remotion.media/elements/commerce-product-collection-preview.png+---++import {ElementPage} from '@site/src/components/Elements/ElementPage';+import {elementDefinitions} from '@site/src/components/Elements/element-definitions';++# Product Collection++<ElementPage definition={elementDefinitions['commerce/product-collection']} sourceFile="./product-collection.tsx" />diff --git a/packages/docs/elements/commerce/product-collection/product-collection.tsx b/packages/docs/elements/commerce/product-collection/product-collection.tsxnew file mode 100644index 0000000000..1394b43b68--- /dev/null+++ b/packages/docs/elements/commerce/product-collection/product-collection.tsx@@ -0,0 +1,461 @@+import {loadFont} from '@remotion/google-fonts/Inter';+import React, {forwardRef, useImperativeHandle, useRef} from 'react';+import {+ Easing,+ Img,+ Interactive,+ Sequence,+ interpolate,+ useCurrentFrame,+ useVideoConfig,+ type InteractiveBaseProps,+ type InteractivitySchema,+ type SequenceControls,+} from 'remotion';++loadFont('normal', {+ subsets: ['latin'],+ weights: ['500', '600', '700'],+});++type ProductCardProps = InteractiveBaseProps & {+ readonly count: number;+ readonly discount: string;+ readonly image: string;+ readonly imageFit: 'contain' | 'cover';+ readonly index: number;+ readonly originalPrice: string;+ readonly price: string;+ readonly title: string;+};++const productCardSchema = {+ ...Interactive.baseSchema,+ title: {+ type: 'text-content',+ default: 'Product title',+ description: 'Product title',+ },+ image: {+ type: 'asset',+ default:+ 'https://remotion.media/elements/product-collection-cloudline-runner.png',+ description: 'Product image',+ },+ imageFit: {+ type: 'enum',+ default: 'cover',+ description: 'Image fit',+ variants: {+ contain: {},+ cover: {},+ },+ },+ price: {+ type: 'text-content',+ default: '$100',+ description: 'Current price',+ },+ originalPrice: {+ type: 'text-content',+ default: '',+ description: 'Original price (leave empty to hide)',+ },+ discount: {+ type: 'text-content',+ default: '',+ description: 'Promotion (leave empty to hide)',+ },+ count: {type: 'hidden'},+ index: {type: 'hidden'},+} as const satisfies InteractivitySchema;++const ProductCardInner = forwardRef<+ HTMLDivElement,+ ProductCardProps & {readonly controls: SequenceControls | undefined}+>(+ (+ {+ controls,+ count,+ discount,+ image,+ imageFit,+ index,+ name,+ originalPrice,+ price,+ title,+ ...sequenceProps+ },+ ref,+ ) => {+ const outlineRef = useRef<HTMLDivElement>(null);+ const frame = useCurrentFrame();+ const {durationInFrames} = useVideoConfig();+ const lastProductIndex = Math.max(0, count - 1);+ const rawScrollPosition = interpolate(+ frame,+ [24, durationInFrames - 28],+ [0, lastProductIndex],+ {+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ );+ const transitionIndex = Math.min(+ Math.floor(rawScrollPosition),+ Math.max(0, lastProductIndex - 1),+ );+ const transitionProgress = interpolate(+ rawScrollPosition - transitionIndex,+ [0.18, 0.82],+ [0, 1],+ {+ easing: Easing.inOut(Easing.cubic),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ );+ const scrollPosition = transitionIndex + transitionProgress;+ const unwrappedSlotPosition = index - scrollPosition;+ const slotPosition =+ count <= 2+ ? unwrappedSlotPosition+ : ((((unwrappedSlotPosition + count / 2) % count) + count) % count) -+ count / 2;+ const distanceFromCenter = Math.abs(slotPosition);+ const visibility = interpolate(distanceFromCenter, [1.02, 1.18], [1, 0], {+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ });+ const entryStart = 8 + Math.min(index, 1) * 5;+ const entryProgress = interpolate(+ frame,+ [entryStart, entryStart + 18],+ [0, 1],+ {+ easing: Easing.bezier(0.16, 1, 0.3, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ );+ const x = slotPosition * 270;+ const y =+ Math.min(distanceFromCenter, 1.3) * 36 + (1 - entryProgress) * 180;+ const rotation = slotPosition * 5.5;+ const cardScale = interpolate(distanceFromCenter, [0, 1.5], [1.02, 0.84], {+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ });+ const obscuredSidePadding = interpolate(+ distanceFromCenter,+ [0.5, 1],+ [22, 48],+ {+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ );++ useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);++ return (+ <Sequence+ layout="none"+ {...sequenceProps}+ controls={controls}+ name={name ?? '<ProductCard>'}+ outlineRef={outlineRef}+ >+ <div+ ref={outlineRef}+ style={{+ height: 560,+ left: 300,+ top: 40,+ opacity: visibility * entryProgress,+ position: 'absolute',+ rotate: `${rotation}deg`,+ scale: cardScale * (0.86 + entryProgress * 0.14),+ transform: 'perspective(100px)',+ translate: `${x}px ${y}px`,+ width: 300,+ willChange: 'transform, opacity',+ zIndex: 100 - Math.round(distanceFromCenter * 20),+ }}+ >+ <div+ style={{+ backgroundColor: '#ffffff',+ boxSizing: 'border-box',+ color: '#1d1d19',+ display: 'flex',+ flexDirection: 'column',+ height: '100%',+ overflow: 'hidden',+ width: '100%',+ }}+ >+ <div+ style={{+ height: 335,+ overflow: 'hidden',+ position: 'relative',+ width: '100%',+ }}+ >+ <Img+ alt={title}+ name="Product image"+ showInTimeline={false}+ src={image}+ style={{+ height: '100%',+ objectFit: imageFit,+ objectPosition: '50% 50%',+ scale: interpolate(+ frame,+ [0, durationInFrames - 1],+ [1.06, 1.01],+ {+ easing: Easing.inOut(Easing.quad),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ ),+ transform: 'perspective(100px)',+ width: '100%',+ willChange: 'transform',+ }}+ />++ {discount.trim() === '' ? null : (+ <div+ style={{+ backgroundColor: '#1d1d19',+ color: '#f7c900',+ fontSize: 20,+ fontWeight: 700,+ letterSpacing: -0.3,+ lineHeight: 1,+ maxWidth: 132,+ overflow: 'hidden',+ padding: '10px 12px',+ position: 'absolute',+ right: 0,+ textOverflow: 'ellipsis',+ textTransform: 'uppercase',+ top: 0,+ whiteSpace: 'nowrap',+ }}+ >+ {discount}+ </div>+ )}+ </div>++ <div+ style={{+ boxSizing: 'border-box',+ display: 'flex',+ flex: 1,+ flexDirection: 'column',+ paddingBottom: 24,+ paddingLeft: slotPosition > 0 ? obscuredSidePadding : 22,+ paddingRight: slotPosition < 0 ? obscuredSidePadding : 22,+ paddingTop: 22,+ }}+ >+ <h3+ style={{+ WebkitBoxOrient: 'vertical',+ WebkitLineClamp: 3,+ display: '-webkit-box',+ fontSize: 32,+ fontWeight: 700,+ height: 98,+ letterSpacing: -1.4,+ lineHeight: 0.98,+ margin: 0,+ overflow: 'hidden',+ overflowWrap: 'anywhere',+ textTransform: 'uppercase',+ textWrap: 'balance',+ }}+ >+ {title}+ </h3>++ <div+ style={{+ alignItems: 'baseline',+ display: 'flex',+ fontVariantNumeric: 'tabular-nums',+ gap: 10,+ height: 48,+ marginTop: 'auto',+ minWidth: 0,+ }}+ >+ <div+ style={{+ fontSize: 48,+ fontWeight: 700,+ letterSpacing: -2.3,+ lineHeight: 1,+ maxWidth: 148,+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+ }}+ >+ {price}+ </div>++ {originalPrice.trim() === '' ? null : (+ <div+ style={{+ color: '#7b776e',+ fontSize: 22,+ fontWeight: 500,+ maxWidth: 74,+ overflow: 'hidden',+ textDecoration: 'line-through',+ textDecorationThickness: 1.5,+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+ }}+ >+ {originalPrice}+ </div>+ )}+ </div>+ </div>+ </div>+ </div>+ </Sequence>+ );+ },+);++const ProductCard = Interactive.withSchema({+ Component: ProductCardInner,+ componentName: '<ProductCard>',+ componentIdentity: null,+ schema: productCardSchema,+ supportsEffects: false,+}) as React.FC<ProductCardProps>;++export const ProductCollection = () => {+ const frame = useCurrentFrame();+ const {durationInFrames} = useVideoConfig();++ return (+ <Interactive.Div+ name="Container"+ style={{+ WebkitFontSmoothing: 'antialiased',+ boxSizing: 'border-box',+ color: '#1d1d19',+ fontFamily: 'Inter',+ height: 660,+ isolation: 'isolate',+ left: 60,+ opacity: interpolate(+ frame,+ [0, 10, durationInFrames - 8, durationInFrames - 1],+ [0, 1, 1, 0],+ {+ easing: Easing.bezier(0.16, 1, 0.3, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ ),+ overflow: 'hidden',+ position: 'absolute',+ scale: interpolate(+ frame,+ [0, 16, durationInFrames - 8, durationInFrames - 1],+ [0.97, 1, 1, 0.98],+ {+ easing: Easing.bezier(0.16, 1, 0.3, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ output: 'perceptual-scale',+ },+ ),+ top: 180,+ transform: 'perspective(100px)',+ translate: interpolate(+ frame,+ [0, 16, durationInFrames - 8, durationInFrames - 1],+ ['0px 30px', '0px 0px', '0px 0px', '0px -20px'],+ {+ easing: Easing.bezier(0.16, 1, 0.3, 1),+ extrapolateLeft: 'clamp',+ extrapolateRight: 'clamp',+ },+ ),+ width: 900,+ willChange: 'transform, opacity',+ }}+ >+ <ProductCard+ count={5}+ discount="20% off"+ image="https://remotion.media/elements/product-collection-cloudline-runner.png"+ imageFit="cover"+ index={0}+ name="Cloudline Runner card"+ originalPrice="$148"+ price="$118"+ title="Cloudline Runner"+ />+ <ProductCard+ count={5}+ discount=""+ image="https://remotion.media/elements/product-collection-minimal-steel-watch.png"+ imageFit="cover"+ index={1}+ name="Minimal Steel Watch card"+ originalPrice=""+ price="$185"+ title="Minimal Steel Watch"+ />+ <ProductCard+ count={5}+ discount="Save $31"+ image="https://images.unsplash.com/photo-1511499767150-a48a237f0083?fm=jpg&fit=crop&w=900&q=90"+ imageFit="cover"+ index={2}+ name="Studio Sunglasses card"+ originalPrice="$125"+ price="$94"+ title="Studio Sunglasses"+ />+ <ProductCard+ count={5}+ discount="New"+ image="https://images.unsplash.com/photo-1505740420928-5e560c06d30e?fm=jpg&fit=crop&w=900&q=90"+ imageFit="cover"+ index={3}+ name="Studio Headset card"+ originalPrice=""+ price="$179"+ title="Studio Headset"+ />+ <ProductCard+ count={5}+ discount=""+ image="https://images.unsplash.com/photo-1507473885765-e6ed057f782c?fm=jpg&fit=crop&w=900&q=90"+ imageFit="cover"+ index={4}+ name="Sculptural Table Lamp card"+ originalPrice=""+ price="$149"+ title="Sculptural Table Lamp"+ />+ </Interactive.Div>+ );+};diff --git a/packages/docs/src/components/Elements/element-definitions.ts b/packages/docs/src/components/Elements/element-definitions.tsindex a261db0742..4248f46a6a 100644--- a/packages/docs/src/components/Elements/element-definitions.ts+++ b/packages/docs/src/components/Elements/element-definitions.ts@@ -10,6 +10,7 @@ import {RotatingStarburst} from '../../../elements/backgrounds/rotating-starbursimport {MovingPillCaptions} from '../../../elements/captions/moving-pill-captions/moving-pill-captions';import {PoppingWordCaptions} from '../../../elements/captions/popping-word-captions/popping-word-captions';import {WordHighlightCaptions} from '../../../elements/captions/word-highlight-captions/word-highlight-captions';+import {ProductCollection} from '../../../elements/commerce/product-collection/product-collection';import {ProductDiscountCallout} from '../../../elements/commerce/product-discount-callout/product-discount-callout';import {ProductOffer} from '../../../elements/commerce/product-offer/product-offer';import {HorizontalBarChart} from '../../../elements/data/horizontal-bar-chart/horizontal-bar-chart';@@ -423,6 +424,28 @@ const elementImplementations = {installationMode: 'wrapped',width: 1920,},+ 'commerce/product-collection': {+ component: ProductCollection,+ contributors: [],+ description:+ 'An animated product carousel that adapts to changing catalog images, titles, prices, and promotions.',+ dependencies: [{name: '@remotion/google-fonts', version: null}],+ durationInFrames: 150,+ elementHeight: 1020,+ elementWidth: 1020,+ fps: 30,+ height: 1080,+ posterFrame: 90,+ preview: {+ posterUrl:+ 'https://remotion.media/elements/commerce-product-collection-preview.png',+ videoUrl:+ 'https://remotion.media/elements/commerce-product-collection-preview.mp4',+ },+ safeArea: 30,+ installationMode: 'wrapped',+ width: 1080,+ },'commerce/product-discount-callout': {component: ProductDiscountCallout,contributors: [],diff --git a/packages/docs/src/components/Elements/element-registry.ts b/packages/docs/src/components/Elements/element-registry.tsindex 1fb7761846..3bfd81b7f5 100644--- a/packages/docs/src/components/Elements/element-registry.ts+++ b/packages/docs/src/components/Elements/element-registry.ts@@ -41,6 +41,10 @@ export const elementRegistry = {category: 'captions',displayName: 'Word Highlight Captions',},+ 'commerce/product-collection': {+ category: 'commerce',+ displayName: 'Product Collection',+ },'commerce/product-discount-callout': {category: 'commerce',displayName: 'Product Discount Callout',diff --git a/packages/docs/src/test/elements.test.ts b/packages/docs/src/test/elements.test.tsindex c1909866b1..03404f3e35 100644--- a/packages/docs/src/test/elements.test.ts+++ b/packages/docs/src/test/elements.test.ts@@ -522,6 +522,7 @@ describe('Elements sidebar', () => {category: 'commerce',label: 'Commerce',items: [+ 'commerce/product-collection/index','commerce/product-discount-callout/index','commerce/product-offer/index',],