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(tmc-328): Fix article cards images #4044

Merged
merged 6 commits into from
Jan 29, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import { tealiumTrackingHandler, truncateText } from '../utils';
const Card = (props: ArticleCardProps) => {
const { article, numOfArticles, isLeadingArticle, isLastCard } = props;

// Function to append the resize parameter to the image URL
const appendResizeToUrl = (url: string): string => {
if (!url) {
return '';
}
return `${url}${url.includes('?') ? '&' : '?'}resize=750`;
};

return (
<StyledCard $numOfArticles={numOfArticles}>
{isLeadingArticle ? (
Expand All @@ -27,7 +35,11 @@ const Card = (props: ArticleCardProps) => {
>
<StyledMedia>
<StyledPicture>
<StyledImg src={article.image.url} />
<StyledImg
loading="lazy"
src={appendResizeToUrl(article.image.url)}
$numOfArticles={numOfArticles}
/>
</StyledPicture>
</StyledMedia>
</a>
Expand All @@ -41,7 +53,11 @@ const Card = (props: ArticleCardProps) => {
>
<StyledMedia>
<StyledPicture>
<StyledImg src={article.image.url} />
<StyledImg
loading="lazy"
src={appendResizeToUrl(article.image.url)}
$numOfArticles={numOfArticles}
/>
</StyledPicture>
</StyledMedia>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export const Hidden = styled.div<HiddenProps>`
interface CardProps {
$numOfArticles: number;
}

interface ImageProps extends React.ImgHTMLAttributes<HTMLImageElement> {
loading?: 'lazy' | 'eager' | 'auto';
$numOfArticles: number;
}

export const StyledCard = styled.div<CardProps>`
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -96,11 +102,74 @@ export const StyledPicture = styled.picture`
height: auto;
`;

export const StyledImg = styled.img`
export const StyledImg = styled.img<ImageProps>`
width: 100%;
height: 100%;
object-fit: cover;
display: inline-block;

@media (min-width: 1440px) {
${({ $numOfArticles }) =>
$numOfArticles === 4 &&
`
min-height: 147px;
`};
${({ $numOfArticles }) =>
$numOfArticles === 3 &&
`
min-height: 202px;
`};
${({ $numOfArticles }) =>
$numOfArticles <= 2 &&
`
min-height: 312px;
`};
}

@media (min-width: 1024px) and (max-width: 1439px) {
${({ $numOfArticles }) =>
$numOfArticles === 4 &&
`
min-height: 124px;
`};
${({ $numOfArticles }) =>
$numOfArticles === 3 &&
`
min-height: 172px;
`};
${({ $numOfArticles }) =>
$numOfArticles <= 2 &&
`
min-height: 267px;
`};
}

@media (min-width: 768px) and (max-width: 1023px) {
${({ $numOfArticles }) =>
$numOfArticles === 4 &&
`
min-height: 87px;
`};
${({ $numOfArticles }) =>
$numOfArticles === 3 &&
`
min-height: 123px;
`};
${({ $numOfArticles }) =>
$numOfArticles === 1 &&
`
min-height: 405px;
`};
${({ $numOfArticles }) =>
$numOfArticles === 2 &&
`
min-height: 193px;
`};
}

@media (max-width: 375px) {
min-height: 199px;
}
`;
export const StyledContent = styled.div<CardProps>`
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ describe('Card Component', () => {
render(<Card {...mockProps} />);

const image = screen.getByRole('img');
expect(image).toHaveAttribute('src', 'https://example.com/image.jpg');
expect(image).toHaveAttribute(
'src',
'https://example.com/image.jpg?resize=750'
);
});

it('renders the link with correct href', () => {
Expand Down