Skip to content

Commit

Permalink
change aspect ratio if its a flex container
Browse files Browse the repository at this point in the history
  • Loading branch information
abeddow91 committed Oct 22, 2024
1 parent 0fea327 commit 2ed431a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions dotcom-rendering/src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ export const Card = ({
: imageSize
}
enableAds={false}
aspectRatio={
isFlexibleContainer
? '5/4'
: ''
}
/>
</Island>
</div>
Expand Down
13 changes: 10 additions & 3 deletions dotcom-rendering/src/components/MaintainAspectRatio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ import { css } from '@emotion/react';
type Props = {
height: number;
width: number;
aspectRatio?: string;
children: React.ReactNode;
};

export const MaintainAspectRatio = ({ height, width, children }: Props) => (
export const MaintainAspectRatio = ({
height,
width,
aspectRatio,
children,
}: Props) => (
/* https://css-tricks.com/aspect-ratio-boxes/ */
<div
css={css`
/* position relative to contain the absolutely positioned iframe plus any Overlay image */
position: relative;
padding-bottom: ${(height / width) * 100}%;
${aspectRatio
? `aspect-ratio: ${aspectRatio};`
: `padding-bottom: ${(height / width) * 100}% `}
& > iframe,
& > video {
width: 100%;
Expand Down
8 changes: 7 additions & 1 deletion dotcom-rendering/src/components/YoutubeAtom/YoutubeAtom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type Props = {
imageSize: ImageSizeType;
imagePositionOnMobile: ImagePositionType;
renderingTarget: RenderingTarget;
aspectRatio?: string;
};

export const YoutubeAtom = ({
Expand Down Expand Up @@ -79,6 +80,7 @@ export const YoutubeAtom = ({
imageSize,
imagePositionOnMobile,
renderingTarget,
aspectRatio,
}: Props): JSX.Element => {
const [overlayClicked, setOverlayClicked] = useState<boolean>(false);
const [playerReady, setPlayerReady] = useState<boolean>(false);
Expand Down Expand Up @@ -196,7 +198,11 @@ export const YoutubeAtom = ({
setIsClosed={setIsClosed}
shouldPauseOutOfView={shouldPauseOutOfView}
>
<MaintainAspectRatio height={height} width={width}>
<MaintainAspectRatio
height={height}
width={width}
aspectRatio={aspectRatio}
>
{
/**
* Consent and ad targeting are initially undefined and set by subsequent re-renders
Expand Down
27 changes: 19 additions & 8 deletions dotcom-rendering/src/components/YoutubeAtom/YoutubeAtomPicture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ type Props = {
alt: string;
height: number;
width: number;
aspectRatio?: string;
};

export const YoutubeAtomPicture = ({ image, alt, height, width }: Props) => {
const sources = generateSources(getSourceImageUrl(image), [
{ breakpoint: breakpoints.mobile, width: 465 },
{ breakpoint: breakpoints.mobileLandscape, width: 645 },
{ breakpoint: breakpoints.phablet, width: 620 },
{ breakpoint: breakpoints.tablet, width: 700 },
{ breakpoint: breakpoints.desktop, width: 620 },
]);
export const YoutubeAtomPicture = ({
image,
alt,
height,
width,
aspectRatio = '5:4',
}: Props) => {
const sources = generateSources(
getSourceImageUrl(image),
[
{ breakpoint: breakpoints.mobile, width: 465 },
{ breakpoint: breakpoints.mobileLandscape, width: 645 },
{ breakpoint: breakpoints.phablet, width: 620 },
{ breakpoint: breakpoints.tablet, width: 700 },
{ breakpoint: breakpoints.desktop, width: 620 },
],
aspectRatio,
);
const fallbackSource = getFallbackSource(sources);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Props = {
imageSize?: ImageSizeType;
imagePositionOnMobile?: ImagePositionType;
enableAds: boolean;
aspectRatio?: string;
};

/**
Expand Down Expand Up @@ -81,6 +82,7 @@ export const YoutubeBlockComponent = ({
imageSize = 'large',
imagePositionOnMobile = 'none',
enableAds,
aspectRatio,
}: Props) => {
const [consentState, setConsentState] = useState<ConsentState | undefined>(
undefined,
Expand Down Expand Up @@ -173,6 +175,7 @@ export const YoutubeBlockComponent = ({
imageSize={imageSize}
imagePositionOnMobile={imagePositionOnMobile}
renderingTarget={renderingTarget}
aspectRatio={aspectRatio}
/>
{!hideCaption && (
<Caption
Expand Down

0 comments on commit 2ed431a

Please sign in to comment.