import { forwardRef } from 'react'; import { css } from '@emotion/react'; import styled from '@emotion/styled'; import { px, round } from '@gilbarbara/helpers'; import { baseStyles, getStyledOptions, marginStyles } from './modules/system'; import { ComponentProps, StyledProps, WithChildren, WithMargin } from './types'; export interface AspectRatioKnownProps extends StyledProps, WithChildren, WithMargin { maxWidth?: number; ratio: number; } export type AspectRatioProps = ComponentProps; export const StyledAspectRatio = styled( 'div', getStyledOptions(), )(props => { const { maxWidth, ratio } = props; return css` ${baseStyles(props)}; max-width: ${maxWidth ? px(maxWidth) : 'none'}; overflow: hidden; position: relative; width: 100%; ${marginStyles(props)}; &:before { content: ''; display: flex; padding-bottom: ${`${round((1 / ratio) * 100)}%`}; width: 100%; } > img, > video { object-fit: cover; } > * { align-self: center; display: flex; height: 100%; inset: 0; justify-content: center; position: absolute; width: 100%; } `; }); export const AspectRatio = forwardRef((props, ref) => { const { children, ...rest } = props; return ( {children} ); }); AspectRatio.displayName = 'AspectRatio';