-
Notifications
You must be signed in to change notification settings - Fork 0
feat(content): add video teaser across all layers #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
02fb14e
b8c5cec
45e9094
b6fd167
7e6478f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| 'use client' | ||
|
|
||
| import { useRef, useState, useEffect } from 'react' | ||
| import { cn } from '@/lib/utils' | ||
| import { useTranslations } from 'next-intl' | ||
|
|
||
| interface LazyVideoProps { | ||
| src?: string | ||
| poster?: string | ||
| autoPlay?: boolean | ||
| muted?: boolean | ||
| controls?: boolean | ||
| eager?: boolean | ||
| className?: string | ||
| aspectRatio?: string | ||
| } | ||
|
|
||
| export function LazyVideo({ | ||
| src, | ||
| poster, | ||
| autoPlay, | ||
| muted, | ||
| controls, | ||
| eager = false, | ||
| className, | ||
| aspectRatio = '16 / 9', | ||
| }: LazyVideoProps) { | ||
| const ref = useRef<HTMLVideoElement>(null) | ||
| const [inView, setInView] = useState(eager) | ||
| const [loaded, setLoaded] = useState(false) | ||
| const [error, setError] = useState(false) | ||
| const t = useTranslations('teaser') | ||
|
|
||
| useEffect(() => { | ||
| if (inView) { | ||
| return | ||
| } | ||
| const el = ref.current | ||
| if (!el) { | ||
| return | ||
| } | ||
| const observer = new IntersectionObserver( | ||
| ([entry]) => { | ||
| if (entry?.isIntersecting) { | ||
| setInView(true) | ||
| observer.disconnect() | ||
| } | ||
| }, | ||
| { rootMargin: '200px' } | ||
| ) | ||
| observer.observe(el) | ||
| return () => observer.disconnect() | ||
| }, [inView]) | ||
|
|
||
| return ( | ||
| <div | ||
| className='relative w-full overflow-hidden rounded-lg' | ||
| style={{ aspectRatio }} | ||
| > | ||
| {!loaded && !error && ( | ||
| <div className='absolute inset-0 animate-pulse bg-gray-200' /> | ||
| )} | ||
| {error && ( | ||
| <div className='absolute inset-0 flex items-center justify-center bg-gray-100 text-sm text-gray-500'> | ||
| {t('video.unavailable')} | ||
| </div> | ||
| )} | ||
| {!error && ( | ||
| <video | ||
| ref={ref} | ||
| src={inView ? src : undefined} | ||
| poster={poster} | ||
| autoPlay={autoPlay} | ||
| muted={muted} | ||
| loop={autoPlay} | ||
| controls={controls} | ||
| preload='none' | ||
| playsInline={autoPlay && muted} | ||
| onCanPlay={() => setLoaded(true)} | ||
| onError={() => setError(true)} | ||
| className={cn( | ||
| 'absolute inset-0 h-full w-full object-cover', | ||
| className | ||
| )} | ||
| /> | ||
| )} | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { VideoTeaser } from '@core/contracts/content/teaser-video' | ||
| import { LazyVideo } from './lazy-video' | ||
| import { CmsLink } from '../cms-link' | ||
|
|
||
| interface TeaserVideoProps { | ||
| teaser: VideoTeaser | ||
| imagePreload?: boolean | ||
| } | ||
|
|
||
| export function TeaserVideoBlock({ teaser, imagePreload }: TeaserVideoProps) { | ||
| const { videoUrl, thumbnailUrl, autoplay, controls, link } = teaser | ||
| return ( | ||
| <div className='relative'> | ||
| {link && ( | ||
| <CmsLink | ||
| link={link} | ||
| className='absolute inset-0 z-10' | ||
| aria-label={link.title} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| /> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| )} | ||
| <LazyVideo | ||
| src={videoUrl} | ||
| poster={thumbnailUrl} | ||
| autoPlay={autoplay} | ||
| muted={autoplay} | ||
| controls={controls} | ||
| eager={imagePreload} | ||
| /> | ||
| </div> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react' | ||
| import { TeaserVideoBlock } from '@/features/content/teasers/teaser-video-block' | ||
| import { VIDEO } from './teaser-mock-data' | ||
|
|
||
| const meta: Meta<typeof TeaserVideoBlock> = { | ||
| title: 'Teasers/Video', | ||
| component: TeaserVideoBlock, | ||
| tags: ['autodocs'], | ||
| parameters: { layout: 'padded' }, | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| export const Default: Story = { | ||
| args: { teaser: VIDEO }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { z } from 'zod' | ||
| import { CmsLinkSchema } from './cms-link' | ||
|
|
||
| export const VideoTeaserSchema = z.object({ | ||
| type: z.literal('video'), | ||
| title: z.string().optional(), | ||
| videoUrl: z.string().optional(), | ||
| thumbnailUrl: z.string().optional(), | ||
| autoplay: z.boolean(), | ||
| controls: z.boolean(), | ||
| caption: z.string().optional(), | ||
| link: CmsLinkSchema.optional(), | ||
| }) | ||
| export type VideoTeaser = z.infer<typeof VideoTeaserSchema> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { gql } from 'graphql-request' | ||
|
|
||
| export const TeaserVideoFragment = gql` | ||
| fragment TeaserVideoFragment on TeaserVideo { | ||
| __typename | ||
| title | ||
| video { | ||
| ...AssetFragment | ||
| } | ||
| thumbnail { | ||
| ...AssetFragment | ||
| } | ||
| autoplay | ||
| controls | ||
| caption | ||
| link { | ||
| ...LinkEntryFragment | ||
| } | ||
| } | ||
| ` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { VideoTeaser } from '@core/contracts/content/teaser-video' | ||
| import type { TeaserVideoApiResponse } from '../../schemas/teaser/teaser-video' | ||
| import { mapLinkEntryToCmsLink } from '../cms-link' | ||
|
|
||
| /** Maps Contentful TeaserVideo entry to contract VideoTeaser. */ | ||
| export function mapTeaserVideo(entry: TeaserVideoApiResponse): VideoTeaser { | ||
| return { | ||
| type: 'video', | ||
| title: entry.title ?? undefined, | ||
| videoUrl: entry.video?.url ?? undefined, | ||
| thumbnailUrl: entry.thumbnail?.url ?? undefined, | ||
| autoplay: entry.autoplay ?? false, | ||
| controls: entry.controls ?? true, | ||
| caption: entry.caption ?? undefined, | ||
| link: mapLinkEntryToCmsLink(entry.link), | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { z } from 'zod' | ||
| import { ContentfulImageApiResponseSchema } from '../image' | ||
| import { LinkEntryApiResponseSchema } from '../link' | ||
|
|
||
| export const TeaserVideoApiResponseSchema = z.object({ | ||
| __typename: z.literal('TeaserVideo'), | ||
| title: z.string().optional().nullable(), | ||
| video: ContentfulImageApiResponseSchema, | ||
| thumbnail: ContentfulImageApiResponseSchema.optional().nullable(), | ||
| autoplay: z.boolean().optional().nullable(), | ||
| controls: z.boolean().optional().nullable(), | ||
| caption: z.string().optional().nullable(), | ||
| link: LinkEntryApiResponseSchema.optional().nullable(), | ||
| }) | ||
|
|
||
| export type TeaserVideoApiResponse = z.infer< | ||
| typeof TeaserVideoApiResponseSchema | ||
| > |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think now we could have problems for combination of
linkandcontrolsas it wouldn't be possible to use controls at all due to overlay.