Skip to content
Open
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
6 changes: 4 additions & 2 deletions dotcom-rendering/src/components/CarouselNavigationButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type ThemeButton,
} from '@guardian/source/react-components';
import { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { createPortal } from 'react-dom';
import { palette } from '../palette';

type CarouselNavigationProps = {
Expand Down Expand Up @@ -84,7 +84,7 @@ export const CarouselNavigationButtons = ({

if (!portalNode) return null;

return ReactDOM.createPortal(
return createPortal(
<div
aria-controls="carousel"
aria-label="carousel arrows"
Expand All @@ -101,6 +101,7 @@ export const CarouselNavigationButtons = ({
}
size="small"
disabled={!previousButtonEnabled}
data-testid="carousel-back-button"
aria-label="previous"
value="previous"
data-link-name={dataLinkNamePreviousButton}
Expand All @@ -109,6 +110,7 @@ export const CarouselNavigationButtons = ({
<Button
hideLabel={true}
iconSide="left"
data-testid="carousel-forward-button"
icon={<SvgChevronRightSingle />}
onClick={onClickNextButton}
priority="tertiary"
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/src/components/ScrollableCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export const ScrollableCarousel = ({
const showNavigation =
kind === CarouselKind.VisibleSlides
? carouselLength > visibleCarouselSlidesOnTablet
: false;
: true;

const scrollTo = (direction: 'left' | 'right') => {
if (!carouselRef.current) return;
Expand Down
58 changes: 43 additions & 15 deletions dotcom-rendering/src/components/ScrollableProduct.importable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { css } from '@emotion/react';
import { palette, space } from '@guardian/source/foundations';
import type { ArticleFormat } from '../lib/articleFormat';
import type { ProductBlockElement } from '../types/content';
import { ProductCarouselCard } from './ProductCarouselCard';
import type { FixedSlideWidth } from './ScrollableCarousel';
import { CarouselKind, ScrollableCarousel } from './ScrollableCarousel';
import { Subheading } from './Subheading';

const carouselHeader = css`
padding-bottom: 10px;
padding-top: ${space[6]}px;
border-bottom: 1px solid ${palette.neutral[86]};
margin-bottom: 10px;
display: flex;
justify-content: space-between;
`;

export const ScrollableProduct = ({
products,
Expand All @@ -19,21 +31,37 @@ export const ScrollableProduct = ({
],
};
return (
<ScrollableCarousel
isArticle={true}
kind={CarouselKind.FixedWidthSlides}
carouselLength={products.length}
fixedSlideWidth={fixedCardWidth}
gapSizes={{ row: 'none', column: 'large' }}
>
{products.map((product: ProductBlockElement) => (
<ScrollableCarousel.SubgridItem
key={product.productCtas[0]?.url ?? product.elementId}
subgridRows={4}
<>
<div css={carouselHeader}>
<Subheading
format={format}
id={'at-a-glance'}
topPadding={false}
>
<ProductCarouselCard product={product} format={format} />
</ScrollableCarousel.SubgridItem>
))}
</ScrollableCarousel>
At a glance
</Subheading>
<div id={'at-a-glance-carousel-navigation'}></div>
</div>
<ScrollableCarousel
isArticle={true}
kind={CarouselKind.FixedWidthSlides}
carouselLength={products.length}
fixedSlideWidth={fixedCardWidth}
gapSizes={{ row: 'none', column: 'large' }}
sectionId={'at-a-glance'}
>
{products.map((product: ProductBlockElement) => (
<ScrollableCarousel.SubgridItem
key={product.productCtas[0]?.url ?? product.elementId}
subgridRows={4}
>
<ProductCarouselCard
product={product}
format={format}
/>
</ScrollableCarousel.SubgridItem>
))}
</ScrollableCarousel>
</>
);
};
45 changes: 41 additions & 4 deletions dotcom-rendering/src/components/ScrollableProducts.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,39 @@ const meta = {
breakpoints.tablet,
breakpoints.wide,
],
// Because this component uses react.createPortal
// we delay to give the portals time to hydrate
delay: 500,
},
},
args: {
products: [
{ ...exampleProduct, h2Id: 'product' },
{
...exampleProduct,
primaryHeadingHtml: '<em>Product 0</em>',
h2Id: 'product',
},
{
...exampleProduct,
primaryHeadingHtml: '<em>Product 1</em>',
h2Id: 'product-1',
productName: 'Lorem ipsum dolor sit amet',
},
{ ...exampleProduct, h2Id: 'product-2' },
{ ...exampleProduct, h2Id: 'product-3' },
{ ...exampleProduct, h2Id: 'product-4' },
{
...exampleProduct,
primaryHeadingHtml: '<em>Product 2</em>',
h2Id: 'product-2',
},
{
...exampleProduct,
primaryHeadingHtml: '<em>Product 3</em>',
h2Id: 'product-3',
},
{
...exampleProduct,
primaryHeadingHtml: '<em>Product 4</em>',
h2Id: 'product-4',
},
],
format: {
design: ArticleDesign.Review,
Expand Down Expand Up @@ -78,3 +98,20 @@ export default meta;
type Story = StoryObj<typeof meta>;

export const With5Cards = {} satisfies Story;

export const ClickForwardArrow = {
play: async ({ canvas, userEvent }) => {
await userEvent.click(canvas.getByTestId('carousel-forward-button'));
},
} satisfies Story;

export const ClickForwardTwiceAndBackOnce = {
play: async ({ canvas, userEvent }) => {
const user = userEvent.setup({
delay: 500, // delay between each interaction
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious why we need this delay when we have one as part of the parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This delay is to leave time for the carousel to move between interactions. I thought that the delay at the top is before the first click so that the navigation buttons have time to find the element in the DOM where they are supposed to be but that may not be needed. I will do some testing to see if it works without.

});
await user.click(canvas.getByTestId('carousel-forward-button'));
await user.click(canvas.getByTestId('carousel-forward-button'));
await user.click(canvas.getByTestId('carousel-back-button'));
},
} satisfies Story;
Loading