use padding-bottom trick to make giphy mp4s responsive (#3024)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Tessa Thornton
2020-07-16 17:59:09 +00:00
committed by GitHub
co-authored by kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 6e64e6a01c
commit 933d82e7c9
2 changed files with 45 additions and 8 deletions
@@ -0,0 +1,15 @@
.responsiveVideo {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
.responsiveContainer {
position: relative;
height: 0;
overflow: hidden;
max-width: 100%;
}
@@ -1,5 +1,8 @@
import cn from "classnames";
import React, { FunctionComponent } from "react";
import styles from "./GiphyMedia.css";
interface Props {
url: string;
width?: number | null;
@@ -8,6 +11,10 @@ interface Props {
title?: string | null;
}
function calculateBottomPadding(width: number, height: number) {
return `${(height / width) * 100}%`;
}
const GiphyMedia: FunctionComponent<Props> = ({
url,
title,
@@ -15,15 +22,30 @@ const GiphyMedia: FunctionComponent<Props> = ({
width,
height,
}) => {
const paddingBottom =
width && height ? calculateBottomPadding(width, height) : null;
return video ? (
<video
width={width || undefined}
height={height || undefined}
autoPlay
loop
>
<source src={video} type="video/mp4" />
</video>
<div style={{ maxWidth: `${width}px` }}>
<div
className={cn({
[styles.responsiveContainer]: paddingBottom,
})}
style={paddingBottom ? { paddingBottom, maxWidth: `${width}px` } : {}}
>
<video
className={cn({
[styles.responsiveVideo]: paddingBottom,
})}
width={width || undefined}
height={height || undefined}
autoPlay
loop
playsInline
>
<source src={video} type="video/mp4" />
</video>
</div>
</div>
) : (
<img src={url} alt={title || ""} />
);