Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for podcast image i n Picture.tsx #12568

Merged
merged 17 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions dotcom-rendering/src/components/Picture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import type { Loading } from './CardPicture';

export type Orientation = 'portrait' | 'landscape';

type Props = {
role: RoleType;
interface BaseProps {
format: ArticleFormat;
master: string;
alt: string;
Expand All @@ -29,7 +28,19 @@ type Props = {
orientation?: Orientation;
onLoad?: () => void;
aspectRatio?: string;
};
}

interface StandardProps extends BaseProps {
role: RoleType;
imageWidths?: never;
}

interface CustomProps extends BaseProps {
role: 'custom';
imageWidths: [ImageWidthType, ...ImageWidthType[]];
}

type Props = StandardProps | CustomProps;

export type ImageWidthType = { breakpoint: number; width: number };

Expand Down Expand Up @@ -331,6 +342,7 @@ export const Picture = ({
orientation = 'landscape',
onLoad,
aspectRatio,
imageWidths,
}: Props) => {
const [loaded, setLoaded] = useState(false);
const ref = useCallback((node: HTMLImageElement | null) => {
Expand All @@ -348,13 +360,14 @@ export const Picture = ({

const sources = generateSources(
master,
decideImageWidths({
role,
format,
isMainMedia,
isLightbox,
orientation,
}),
imageWidths ??
decideImageWidths({
role,
format,
isMainMedia,
isLightbox,
orientation,
}),
Copy link
Contributor

@SiAdcock SiAdcock Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to add more customisation to the Picture component, do we want to do it like this, where we add an override that says anything goes, or do we want to add a new flag for isPodcastCover or something like that?

Reason I ask is because of the disclaimer at the top of decideImageWidths:

All business logic for image sizing is contained in this one function. This is the source of truth.

With this change, this is no longer the case.

I'll admit it doesn't feel sustainable to have a function that receives a growing series of arguments to account for a possibly endless list of weird and wonderful new use cases. It also doesn't feel good for that function to canonise some widths as "source of truth" and for everything else, there's a custom role. And since this podcast cover image has been around for at least 8 years and is canonised in CAPI and tag manager, maybe dotcom-rendering needs to bite the bullet and accept it as a thing also.

PS I'm away until Thursday so please overrule me if, in general, there's disagreement on this point. I don't want my absence to be a blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra argument way you suggested seems to be how many of these components work in DCR. Also I don't want to break that contract about the source of truth. I will change it happily to use a prop through the Picture component as suggested.
image

aspectRatio,
);

Expand Down
51 changes: 49 additions & 2 deletions dotcom-rendering/src/layouts/StandardLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { css } from '@emotion/react';
import { isUndefined } from '@guardian/libs';
import {
breakpoints,
from,
palette as sourcePalette,
space,
Expand Down Expand Up @@ -40,6 +41,8 @@ import { MostViewedFooterData } from '../components/MostViewedFooterData.importa
import { MostViewedFooterLayout } from '../components/MostViewedFooterLayout';
import { MostViewedRightWithAd } from '../components/MostViewedRightWithAd.importable';
import { OnwardsUpper } from '../components/OnwardsUpper.importable';
import type { ImageWidthType } from '../components/Picture';
import { Picture } from '../components/Picture';
import { RightColumn } from '../components/RightColumn';
import { Section } from '../components/Section';
import { SlotBodyEnd } from '../components/SlotBodyEnd.importable';
Expand Down Expand Up @@ -120,7 +123,7 @@ const StandardGrid = ({
? css`
grid-template-areas:
'title border headline headline .'
'. border disclaimer disclaimer right-column'
'image border disclaimer disclaimer right-column'
'meta border media media right-column'
'meta border standfirst standfirst right-column'
'. border body body right-column'
Expand Down Expand Up @@ -163,7 +166,7 @@ const StandardGrid = ({
? css`
grid-template-areas:
'title border headline .'
'. border disclaimer right-column'
'image border disclaimer right-column'
'meta border media right-column'
'meta border standfirst right-column'
'meta border body right-column'
Expand Down Expand Up @@ -209,6 +212,7 @@ const StandardGrid = ({
'disclaimer right-column'
'media right-column'
'standfirst right-column'
'image right-column'
'meta right-column'
'body right-column'
'. right-column';
Expand Down Expand Up @@ -248,6 +252,7 @@ const StandardGrid = ({
'disclaimer'
'media'
'standfirst'
'image'
'meta'
'body';
`
Expand Down Expand Up @@ -287,6 +292,7 @@ const StandardGrid = ({
'disclaimer'
'media'
'standfirst'
'image'
'meta'
'body';
`
Expand Down Expand Up @@ -344,6 +350,20 @@ const stretchLines = css`
}
`;

const podcastResponsiveCoverImage = css`
img {
width: 140px;
height: 140px;
}
margin: 0.375rem 0 0.375rem 0;
${from.wide} {
img {
width: 219px;
height: 219px;
}
}
`;

const starWrapper = css`
background-color: ${themePalette('--star-rating-background')};
color: ${themePalette('--star-rating-fill')};
Expand Down Expand Up @@ -417,6 +437,13 @@ export const StandardLayout = (props: WebProps | AppProps) => {

const renderAds = isWeb && canRenderAds(article);

const podcastSeries = article.tags.find((tag) => tag.type === 'Series');
arelra marked this conversation as resolved.
Show resolved Hide resolved

const podcastImageWidths: [ImageWidthType, ...ImageWidthType[]] = [
{ breakpoint: breakpoints.mobile, width: 140 },
{ breakpoint: breakpoints.wide, width: 219 },
];

return (
<>
{isWeb && (
Expand Down Expand Up @@ -736,6 +763,26 @@ export const StandardLayout = (props: WebProps | AppProps) => {
</>
)}
</GridItem>
{format.design === ArticleDesign.Audio &&
podcastSeries && (
<GridItem area="image" element="aside">
<div css={podcastResponsiveCoverImage}>
<Picture
role={'custom'}
format={format}
master={
podcastSeries.podcast?.image ??
''
}
alt={podcastSeries.title}
height={1}
width={1}
loading="lazy"
imageWidths={podcastImageWidths}
/>
</div>
</GridItem>
)}
<GridItem area="body">
{isWeb && (
<Hide from="leftCol">
Expand Down
Loading