Media performance tools: image/video props, upload placeholders, images preset, YouTube facade
Headless helpers for the project's own <Media> (look stays in the project): - mediaImageProps: dimensions from the doc (no CLS), lazy by default, LCP image gets preload + fetchPriority=high + eager (Next 16 replaced 'priority'), focal point → object-position, blur placeholder + dominant colour. - MEDIA_SIZES suggestions for 'sizes' (any string still works). - videoProps: 'background' (muted loop, respects reduced motion) or 'player' (controls, preload=none, poster). - buildMediaPlaceholderHook/Field: tiny WebP blur + dominant colour at upload via the configured sharp (no new dependency). - buildImagesConfig: AVIF/WebP, R2 remotePatterns, 31-day cache. - <YouTubeEmbed> (/client): poster + button, youtube-nocookie iframe on click. - buildCsp youtube allows the i.ytimg.com poster. kod-sekcji Media template uses the helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -137,40 +137,40 @@ export function buttonClass(appearance: LinkAppearance): string {
|
||||
}
|
||||
```
|
||||
|
||||
`src/components/ui/Media.tsx` — **każdy** obraz/wideo z panelu:
|
||||
`src/components/ui/Media.tsx` — **każdy** obraz/wideo z panelu. Szybkość liczy plugin
|
||||
(`mediaImageProps`, `videoProps`), wygląd zostaje tutaj:
|
||||
|
||||
```tsx
|
||||
import Image from 'next/image'
|
||||
import { mediaImageProps, MEDIA_SIZES, videoProps, type VideoMode } from '@intecion/ipal-kit'
|
||||
import type { Media as MediaDoc } from '@/payload-types'
|
||||
|
||||
type Props = {
|
||||
resource: MediaDoc | string | number | null | undefined
|
||||
/** Szerokość NA EKRANIE — MEDIA_SIZES.half / .third / własny string. Bez tego pobiera się pełna szerokość. */
|
||||
sizes?: string
|
||||
/** Tylko pierwszy widoczny obraz strony (hero, LCP). Reszta ładuje się leniwie. */
|
||||
priority?: boolean
|
||||
className?: string
|
||||
/** Poster image URL for videos (shown before playback). */
|
||||
poster?: string
|
||||
/** Wideo: 'background' (pętla bez dźwięku) albo 'player' (kontrolki, ładowanie po kliknięciu). */
|
||||
videoMode?: VideoMode
|
||||
/** Plakat wideo — upload z panelu. */
|
||||
poster?: MediaDoc | string | number | null
|
||||
}
|
||||
|
||||
export function Media({ resource, sizes = '100vw', priority = false, className, poster }: Props) {
|
||||
if (!resource || typeof resource !== 'object' || !resource.url) return null
|
||||
const { url, alt, width, height, mimeType } = resource
|
||||
|
||||
if (mimeType?.startsWith('video/')) {
|
||||
// preload="metadata": the browser fetches only headers until playback, not the whole file
|
||||
return (
|
||||
<video src={url} className={className} poster={poster} preload="metadata" autoPlay muted loop playsInline />
|
||||
)
|
||||
}
|
||||
if (!width || !height) return null // non-image file (PDF) — use CMSLink type 'file'
|
||||
|
||||
return (
|
||||
<Image src={url} alt={alt ?? ''} width={width} height={height}
|
||||
sizes={sizes} priority={priority} className={className} />
|
||||
)
|
||||
export function Media({ resource, sizes = MEDIA_SIZES.full, priority = false, className, videoMode = 'player', poster }: Props) {
|
||||
const video = videoProps(resource, { mode: videoMode, poster })
|
||||
if (video) return <video {...video} className={className} />
|
||||
const img = mediaImageProps(resource, { priority, sizes })
|
||||
if (!img) return null // PDF / plik — link typu 'file' (CMSLink), nie <Media>
|
||||
return <Image {...img} className={className} />
|
||||
}
|
||||
```
|
||||
|
||||
Media w kolekcji: `upload: { focalPoint: true, … }` (punkt ostrości → `object-position`) oraz
|
||||
placeholder z pluginu: `hooks.beforeOperation: [buildOptimizeUploadHook(), buildMediaPlaceholderHook()]`,
|
||||
pole `buildMediaPlaceholderField()` (rozmyty podgląd + kolor podczas ładowania).
|
||||
|
||||
`Section.tsx` i `SectionHeader.tsx`:
|
||||
|
||||
```tsx
|
||||
|
||||
Reference in New Issue
Block a user