Skip to content

Commit

Permalink
react-components: video support percentage width for responsive layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
giannif committed Aug 1, 2024
1 parent 0380176 commit 4cfcc5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 11 additions & 3 deletions packages/react-components/src/components/video/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'
import { getGifHeight, Logger } from '@giphy/js-util'
import React, { ComponentProps, ElementType, useCallback, useEffect, useState } from 'react'
import React, { ComponentProps, CSSProperties, ElementType, useCallback, useEffect, useState } from 'react'
import useTimeoutFn from 'react-use/lib/useTimeoutFn'
import styled from 'styled-components'
import { GifOverlayProps } from '../types'
Expand All @@ -10,6 +10,7 @@ import ProgressBar from './progress-bar'
import Video from './video'

type Props = {
style?: CSSProperties
// turns on all controls
controls?: boolean
// if controls is true, hides progress bar
Expand Down Expand Up @@ -105,7 +106,9 @@ const LARGE_PLAYER_HEIGHT = 300
const AUTO_HIDE_TIMEOUT = 4000
const VideoPlayer = (props: ComponentProps<typeof VideoWrapper>) => {
const {
style,
width,
percentWidth,
hideMute,
hideAttribution,
hideProgressBar,
Expand All @@ -120,6 +123,11 @@ const VideoPlayer = (props: ComponentProps<typeof VideoWrapper>) => {
const [mutedByBrowser, setMutedByBrowser] = useState(false)
const { setVideoEl, onMuted, onUserMuted } = props
const height = props.height || getGifHeight(gif, width)
let percentHeight: string | undefined
if (percentWidth) {
const ratio = Math.round((height / width) * 100)
percentHeight = `${ratio}%`
}
const [, cancelHideTimeout, resetHideTimeout] = useTimeoutFn(() => {
setIsHovered(false)
}, AUTO_HIDE_TIMEOUT)
Expand Down Expand Up @@ -163,7 +171,7 @@ const VideoPlayer = (props: ComponentProps<typeof VideoWrapper>) => {
return (
<Container
className={className}
style={{ width, height }}
style={{ width: percentWidth || width, height: percentHeight || height, ...style }}
onMouseOver={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onMouseMove={() => {
Expand All @@ -181,7 +189,7 @@ const VideoPlayer = (props: ComponentProps<typeof VideoWrapper>) => {
toggleMute()
}}
>
<Video {...props} onMuted={combinedOnMuted} setVideoEl={combinedSetVideoEl} muted={muted} />
<Video {...props} isInPlayer onMuted={combinedOnMuted} setVideoEl={combinedSetVideoEl} muted={muted} />
{showControls && <Gradient $isLargePlayer={isLargePlayer} />}
<Controls $isHovered={showControls}>
<TitleContainer>
Expand Down
14 changes: 11 additions & 3 deletions packages/react-components/src/components/video/video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ type Props = {
loop?: boolean
gif: IGif
width: number
percentWidth?: string
height?: number
volume?: number
className?: string
isInPlayer?: boolean
}
const Video = ({
muted,
Expand All @@ -62,12 +64,18 @@ const Video = ({
setVideoEl,
gif,
width,
percentWidth,
height: height_,
volume = 0.7,
className = videoClassName,
isInPlayer,
}: Props) => {
const height = height_ || getGifHeight(gif, width)

let percentHeight: string | undefined
if (percentWidth) {
const ratio = Math.round((height / width) * 100)
percentHeight = `${ratio}%`
}
// state
const [media, setMedia] = useState(getBestVideo(gif.video, width, height))
const seek = useRef(0)
Expand Down Expand Up @@ -236,8 +244,8 @@ const Video = ({
crossOrigin="anonymous"
draggable={true}
className={className}
width={width}
height={height}
width={isInPlayer ? '100%' : percentWidth || width}
height={isInPlayer ? '100%' : percentHeight || height}
muted={muted}
autoPlay
playsInline
Expand Down
6 changes: 6 additions & 0 deletions packages/react-components/stories/video.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ export const VideoNoContent: Story = {
id: 'ZEU9ryYGZzttn0Cva7',
},
}

export const VideoPercentageWidth: Story = {
args: {
percentWidth: '80%',
},
}

0 comments on commit 4cfcc5f

Please # to comment.