Skip to content

Commit e7f7737

Browse files
committed
Allow link clicks on carousels
1 parent 77a4a14 commit e7f7737

File tree

4 files changed

+71
-48
lines changed

4 files changed

+71
-48
lines changed

dotcom-rendering/src/components/Card/Card.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -904,25 +904,21 @@ export const Card = ({
904904
isSmallCard={isSmallCard}
905905
>
906906
{media.type === 'slideshow' && (
907-
<div
908-
css={css`
909-
position: relative;
910-
z-index: ${getZIndex('card-nested-link')};
911-
`}
907+
<Island
908+
priority="feature"
909+
defer={{ until: 'visible' }}
912910
>
913-
<Island
914-
priority="feature"
915-
defer={{ until: 'visible' }}
916-
>
917-
<SlideshowCarousel
918-
images={media.slideshowImages}
919-
imageSize={mediaSize}
920-
hasNavigationBackgroundColour={
921-
!!hasSublinks
922-
}
923-
/>
924-
</Island>
925-
</div>
911+
<SlideshowCarousel
912+
images={media.slideshowImages}
913+
imageSize={mediaSize}
914+
hasNavigationBackgroundColour={
915+
!!hasSublinks
916+
}
917+
linkTo={linkTo}
918+
linkAriaLabel={headlineText}
919+
dataLinkName={resolvedDataLinkName}
920+
/>
921+
</Island>
926922
)}
927923
{media.type === 'avatar' && (
928924
<AvatarContainer

dotcom-rendering/src/components/Card/components/CardWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const hoverStyles = css`
6060
:has(
6161
ul.sublinks:hover,
6262
.loop-video-container:hover,
63-
.slideshow-carousel:hover,
63+
.slideshow-carousel-footer:hover,
6464
.branding-logo:hover
6565
) {
6666
.card-headline .show-underline {

dotcom-rendering/src/components/SlideshowCarousel.importable.tsx

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SvgChevronRightSingle,
1414
} from '@guardian/source/react-components';
1515
import { useEffect, useRef, useState } from 'react';
16+
import { getZIndex } from '../lib/getZIndex';
1617
import { takeFirst } from '../lib/tuple';
1718
import { palette } from '../palette';
1819
import type { DCRSlideshowImage } from '../types/front';
@@ -55,6 +56,11 @@ const carouselItemStyles = css`
5556
scroll-snap-align: start;
5657
`;
5758

59+
const carouselLinkStyles = css`
60+
position: relative;
61+
z-index: ${getZIndex('card-nested-link')};
62+
`;
63+
5864
const captionStyles = css`
5965
position: absolute;
6066
bottom: 0;
@@ -74,6 +80,8 @@ const navigationStyles = (hasBackgroundColour: boolean) => css`
7480
display: flex;
7581
align-items: center;
7682
padding-top: ${space[2]}px;
83+
position: relative;
84+
z-index: ${getZIndex('card-nested-link')};
7785
7886
${until.tablet} {
7987
background-color: ${hasBackgroundColour
@@ -108,12 +116,18 @@ type Props = {
108116
images: readonly DCRSlideshowImage[];
109117
imageSize: MediaSizeType;
110118
hasNavigationBackgroundColour: boolean;
119+
linkTo: string;
120+
linkAriaLabel: string;
121+
dataLinkName?: string;
111122
};
112123

113124
export const SlideshowCarousel = ({
114125
images,
115126
imageSize,
116127
hasNavigationBackgroundColour,
128+
linkTo,
129+
linkAriaLabel,
130+
dataLinkName,
117131
}: Props) => {
118132
const carouselRef = useRef<HTMLUListElement | null>(null);
119133
const [previousButtonEnabled, setPreviousButtonEnabled] = useState(false);
@@ -192,37 +206,47 @@ export const SlideshowCarousel = ({
192206
const slideshowImageCount = slideshowImages.length;
193207

194208
return (
195-
<div className="slideshow-carousel">
196-
<ul
197-
ref={carouselRef}
198-
css={carouselStyles}
199-
data-heatphan-type="carousel"
209+
<>
210+
<a
211+
href={linkTo}
212+
css={carouselLinkStyles}
213+
aria-label={linkAriaLabel}
214+
data-link-name={dataLinkName}
200215
>
201-
{slideshowImages.map((image, index) => {
202-
const loading = index > 0 ? 'lazy' : 'eager';
203-
return (
204-
<li css={carouselItemStyles} key={image.imageSrc}>
205-
<figure>
206-
<CardPicture
207-
mainImage={image.imageSrc}
208-
imageSize={imageSize}
209-
aspectRatio="5:4"
210-
alt={image.imageCaption}
211-
loading={loading}
212-
/>
213-
{!!image.imageCaption && (
214-
<figcaption css={captionStyles}>
215-
{image.imageCaption}
216-
</figcaption>
217-
)}
218-
</figure>
219-
</li>
220-
);
221-
})}
222-
</ul>
216+
<ul
217+
ref={carouselRef}
218+
css={carouselStyles}
219+
data-heatphan-type="carousel"
220+
>
221+
{slideshowImages.map((image, index) => {
222+
const loading = index > 0 ? 'lazy' : 'eager';
223+
return (
224+
<li css={carouselItemStyles} key={image.imageSrc}>
225+
<figure>
226+
<CardPicture
227+
mainImage={image.imageSrc}
228+
imageSize={imageSize}
229+
aspectRatio="5:4"
230+
alt={image.imageCaption}
231+
loading={loading}
232+
/>
233+
{!!image.imageCaption && (
234+
<figcaption css={captionStyles}>
235+
{image.imageCaption}
236+
</figcaption>
237+
)}
238+
</figure>
239+
</li>
240+
);
241+
})}
242+
</ul>
243+
</a>
223244

224245
{slideshowImageCount > 1 && (
225-
<div css={navigationStyles(hasNavigationBackgroundColour)}>
246+
<div
247+
className="slideshow-carousel-footer"
248+
css={navigationStyles(hasNavigationBackgroundColour)}
249+
>
226250
<div css={scrollingDotStyles}>
227251
<SlideshowCarouselScrollingDots
228252
total={slideshowImageCount}
@@ -266,6 +290,6 @@ export const SlideshowCarousel = ({
266290
</div>
267291
</div>
268292
)}
269-
</div>
293+
</>
270294
);
271295
};

dotcom-rendering/src/components/SlideshowCarousel.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ const meta = {
9191
images,
9292
imageSize: 'medium',
9393
hasNavigationBackgroundColour: false,
94+
linkTo: 'https://www.theguardian.com',
95+
linkAriaLabel: 'A picture of a thing',
96+
dataLinkName: 'slideshow-carousel',
9497
},
9598
render: (args) => (
9699
<Wrapper>

0 commit comments

Comments
 (0)