Skip to content

Commit d2109d9

Browse files
marjisoundJamieB-gu
andcommitted
make sure images always fit within the viewport without being cropped
Co-authored-by: Jamie B <[email protected]>
1 parent 35611dd commit d2109d9

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

dotcom-rendering/src/components/Picture.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { css } from '@emotion/react';
22
import { breakpoints, from, space } from '@guardian/source/foundations';
3-
import { Fragment, useCallback, useEffect, useState } from 'react';
3+
import {
4+
type CSSProperties,
5+
Fragment,
6+
useCallback,
7+
useEffect,
8+
useState,
9+
} from 'react';
410
import { grid } from '../grid';
511
import {
612
ArticleDesign,
@@ -482,6 +488,14 @@ const galleryBodyImageStyles = css`
482488
}
483489
`;
484490

491+
const imageMaxWidth = (
492+
design: ArticleDesign,
493+
ratio: number,
494+
): CSSProperties | undefined =>
495+
design === ArticleDesign.Gallery
496+
? { maxWidth: `calc(${ratio} * 96vh)` }
497+
: undefined;
498+
485499
const styles = (
486500
{ design }: ArticleFormat,
487501
isLightbox: boolean,
@@ -563,7 +577,10 @@ export const Picture = ({
563577
const fallbackSource = getFallbackSource(sources);
564578

565579
return (
566-
<picture css={styles(format, isLightbox, isMainMedia)}>
580+
<picture
581+
css={styles(format, isLightbox, isMainMedia)}
582+
style={imageMaxWidth(format.design, 1 / ratio)}
583+
>
567584
{/* Immersive Main Media images get additional sources specifically for when in portrait orientation */}
568585
{format.display === ArticleDisplay.Immersive && isMainMedia && (
569586
<>

0 commit comments

Comments
 (0)