/
MediaContainer.tsx
79 lines (71 loc) · 1.74 KB
/
MediaContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { HTMLAttributes } from "react";
import { forwardRef } from "react";
import cn from "classnames";
import { bem } from "@react-md/utils";
export interface MediaContainerProps extends HTMLAttributes<HTMLDivElement> {
/**
* An optional aspect ratio height to enforce. This **must** be used alongside
* the `width` prop.
*/
height?: number;
/**
* An optional aspect ratio width to enforce. This **must** be used alongside
* the `height` prop.
*/
width?: number;
/**
* Boolean if any media element children should be updated to be responsive.
*/
auto?: boolean;
/**
* Boolean if the media container should have a `width: 100%;` applied.
*/
fullWidth?: boolean;
}
export interface MediaContainerWithAspectRatioProps
extends MediaContainerProps {
height: number;
width: number;
}
const block = bem("rmd-media-container");
/**
* The `MediaContainer` component is used to make responsive images and videos
* within your app. This component also allows for forcing a specific aspect
* ratio for these elements with both the `height` and `width` props are
* provided.
*/
export const MediaContainer = forwardRef<
HTMLDivElement,
MediaContainerProps | MediaContainerWithAspectRatioProps
>(function MediaContainer(
{
className,
height,
width,
children,
auto = true,
fullWidth = false,
...props
},
ref
) {
const aspectRatio =
height && width ? `rmd-media-container--${width}-${height}` : "";
return (
<div
{...props}
ref={ref}
className={cn(
block({
auto,
"aspect-ratio": aspectRatio,
"full-width": fullWidth,
}),
aspectRatio,
className
)}
>
{children}
</div>
);
});