'use client'
import { useRef } from 'react'
interface VimeoPlayerProps {
embed: any
}
function getAspectRatioPadding(className?: string): string | undefined {
if (!className) return undefined
const aspectRatios: Record<string, string> = {
'16-9': '56.25%',
'16/9': '56.25%',
'4-3': '75%',
'4/3': '75%',
'1-1': '100%',
'1/1': '100%',
'21-9': '42.86%',
'21/9': '42.86%',
'9-16': '177.78%',
'9/16': '177.78%',
}
const match = className.match(/aspect[-_]?(?:ratio[-_]?)?(\d+)[-_/](\d+)/i)
if (match) {
const [, width = '16', height = '9'] = match
const key = `${width}-${height}`
if (aspectRatios[key]) {
return aspectRatios[key]
}
return `${(parseInt(height) / parseInt(width)) * 100}%`
}
return undefined
}
export function VimeoPlayer({ embed }: VimeoPlayerProps) {
const playerRef = useRef<HTMLIFrameElement>(null)
const iframe = embed?.wpBlockEmbedWrapper?.iframe || undefined
const aspectRatio = getAspectRatioPadding(embed?._className)
const vimeoSrc = iframe?._src
? `${iframe._src}${
iframe._src.includes('?') ? '&' : '?'
}muted=0&autoplay=1&loop=0&autopause=1`
: undefined
return (
<div
className="relative w-full mx-auto max-w-6xl"
style={{
paddingTop: aspectRatio ? aspectRatio : '56.25%',
}}
>
<iframe
ref={playerRef}
src={vimeoSrc}
width={iframe?._width}
height={iframe?._height}
title={iframe?._title}
frameBorder={0}
className="rounded-md absolute inset-0 w-full h-full"
allow="autoplay; fullscreen"
/>
</div>
)
}