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

feat(PosterCard, DisplayMediaCard): allow using srcSet for backgroundImage #1253

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 30 additions & 9 deletions src/__private_stories__/image-srcset-story.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import * as React from 'react';
import {Image, Stack, Text3} from '..';
import {DisplayMediaCard, Image, PosterCard, Stack, Text3} from '..';
import usingVrImg from '../__stories__/images/using-vr.jpg';
import beachImg from '../__stories__/images/beach.jpg';
import laptopImg from '../__stories__/images/laptop.jpg';
import {sprinkles} from '../sprinkles.css';

export default {
title: 'Private/Image/Image with srcSet attribute is responsive',
};

export const Default: StoryComponent = () => {
const src = usingVrImg;
const srcSet = `${usingVrImg} 600w, ${beachImg} 1300w, ${laptopImg} 3000w`;

return (
<Stack space={16}>
<Text3 medium>When using srcSet prop, Image src should be responsive</Text3>
<Text3 medium>
When using srcSet prop, Image src should be responsive. this attribute can also be used for
the backgroundImage in DisplayMediaCard and PosterCard components
</Text3>

<Stack
space={16}
className={sprinkles({display: 'inline-block'})}
dataAttributes={{testid: 'content'}}
>
<Image aspectRatio="16:9" width={300} src={src} srcSet={srcSet} />

<PosterCard
aspectRatio="16:9"
width={300}
title="Poster Card"
backgroundImage={{src, srcSet}}
/>

<Image
src={usingVrImg}
aspectRatio="16:9"
width={400}
dataAttributes={{testid: 'image'}}
srcSet={`${usingVrImg} 600w, ${beachImg} 1300w, ${laptopImg} 3000w`}
/>
<DisplayMediaCard
aspectRatio="16:9"
width={300}
title="Display Media Card"
backgroundImage={{src, srcSet}}
/>
</Stack>
</Stack>
);
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ test.each(DEVICES)('Image with srcSet is responsive (%s)', async (device) => {
device,
});

const element = await screen.findByTestId('image');
const element = await screen.findByTestId('content');
expect(await element.screenshot()).toMatchImageSnapshot();
});
12 changes: 11 additions & 1 deletion src/__type_tests__/card-type-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import {ButtonPrimary} from '../button';
import {DisplayMediaCard, NakedCard, PosterCard, SmallNakedCard, SnapCard} from '../card';
import HighlightedCard from '../highlighted-card';
import Image from '../image';

<SnapCard title="title" />;
Expand All @@ -18,8 +17,14 @@ import Image from '../image';
// @ts-expect-error trackingEvent can't be used if the card is not touchable
<SnapCard title="title" trackingEvent={{name: 'do-something'}} />;

<DisplayMediaCard title="title" backgroundImage={{src: 'test', srcSet: 'test'}} />;
<DisplayMediaCard title="title" backgroundImage={{src: undefined, srcSet: 'test'}} />;
<DisplayMediaCard title="title" backgroundImage={{src: 'test', srcSet: undefined}} />;

// @ts-expect-error backgroundImage and backgroundVideo can't be used together
<DisplayMediaCard title="title" backgroundImage="" backgroundVideo="" />;
// @ts-expect-error backgroundImage should have src or srcSet
<DisplayMediaCard title="title" backgroundImage={{src: undefined, srcSet: undefined}} />;

<PosterCard backgroundImage="background.png" title="title" />;
<PosterCard backgroundVideo="background.mp4" title="title" />;
Expand All @@ -38,6 +43,9 @@ import Image from '../image';
<PosterCard title="title" variant="alternative" />;
<PosterCard title="title" isInverse backgroundColor="red" />;
<PosterCard title="title" variant="inverse" backgroundColor="red" />;
<PosterCard title="title" backgroundImage={{src: 'test', srcSet: 'test'}} />;
<PosterCard title="title" backgroundImage={{src: undefined, srcSet: 'test'}} />;
<PosterCard title="title" backgroundImage={{src: 'test', srcSet: undefined}} />;

// @ts-expect-error onPress and href can't be used together
<PosterCard title="title" onPress={() => {}} href="/" />;
Expand All @@ -57,6 +65,8 @@ import Image from '../image';
<PosterCard title="title" />;
// @ts-expect-error if you set a custom backgroundColor, you should specify the variant
<PosterCard title="title" backgroundColor="red" />;
// @ts-expect-error backgroundImage should have src or srcSet
<PosterCard title="title" backgroundImage={{src: undefined, srcSet: undefined}} />;

(isTouchable: boolean) => <SnapCard title="title" href={isTouchable ? '/' : undefined} />;
(isTouchable: boolean) => <SnapCard title="title" to={isTouchable ? '/' : undefined} />;
Expand Down
10 changes: 6 additions & 4 deletions src/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ const CardContainer = React.forwardRef<HTMLDivElement, CardContainerProps>(
}
);

const renderBackgroundImage = (src?: string) => {
return <Image width="100%" height="100%" src={src || ''} />;
const renderBackgroundImage = (backgroundImageProps?: string | {src?: string; srcSet?: string}) => {
const src = typeof backgroundImageProps === 'string' ? backgroundImageProps : backgroundImageProps?.src;
const srcSet = typeof backgroundImageProps === 'string' ? undefined : backgroundImageProps?.srcSet;
return <Image width="100%" height="100%" src={src || ''} srcSet={srcSet} />;
};

type VideoState = 'loading' | 'loadingTimeout' | 'playing' | 'paused' | 'error';
Expand Down Expand Up @@ -1186,7 +1188,7 @@ type DisplayMediaCardBaseProps = {
};

type DisplayMediaCardWithImageProps = CommonDisplayCardProps & {
backgroundImage: string;
backgroundImage: string | {src: string; srcSet?: string} | {src?: string; srcSet: string};
};

type DisplayMediaCardWithVideoProps = Omit<CommonDisplayCardProps, 'actions' | 'onClose'> & {
Expand Down Expand Up @@ -1476,7 +1478,7 @@ interface PosterCardBaseProps {
}

interface PosterCardWithImageProps extends PosterCardBaseProps {
backgroundImage: string;
backgroundImage: string | {src: string; srcSet?: string} | {src?: string; srcSet: string};
}

type PosterCardWithVideoProps = Omit<PosterCardBaseProps, 'actions' | 'onClose'> & {
Expand Down
Loading