/
AspectRatio.tsx
51 lines (48 loc) · 1.09 KB
/
AspectRatio.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
import React from 'react'
import { Box, BoxProps } from './Box'
import type { ForwardRef } from './types'
import { __internalProps } from './util'
export interface AspectRatioProps extends BoxProps {
ratio?: number
}
/**
* Component for maintaining a fluid-width aspect ratio
* @see https://theme-ui.com/components/aspect-ratio
*/
export const AspectRatio: ForwardRef<HTMLDivElement, AspectRatioProps> =
React.forwardRef(function AspectRatio(
{ ratio = 4 / 3, children, ...props },
ref
) {
return (
<Box
ref={ref}
sx={{
position: 'relative',
overflow: 'hidden',
}}
>
<Box
sx={{
width: '100%',
height: 0,
paddingBottom: 100 / ratio + '%',
}}
/>
<Box
{...props}
{...__internalProps({
__css: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
},
})}
>
{children}
</Box>
</Box>
)
})