Skip to content

Commit

Permalink
Merge pull request #12836 from guardian/fix-carousel-in-android
Browse files Browse the repository at this point in the history
Add touch start and end events for key events carousel
  • Loading branch information
marjisound authored Dec 9, 2024
2 parents d8a1d75 + 31bca78 commit 2a1bd37
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions dotcom-rendering/.storybook/mocks/bridgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export const getDiscussionClient: BridgetApi<'getDiscussionClient'> = () => ({
recommend: async () => discussionErrorResponse,
});

export const getInteractionClient: BridgetApi<
'getInteractionClient'
> = () => ({});

export const ensure_all_exports_are_present = {
getUserClient,
getAcquisitionsClient,
Expand All @@ -100,6 +104,7 @@ export const ensure_all_exports_are_present = {
getNewslettersClient,
getDiscussionClient,
getTagClient,
getInteractionClient,
} satisfies {
[Method in keyof BridgeModule]: BridgetApi<Method>;
};
2 changes: 1 addition & 1 deletion dotcom-rendering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@emotion/server": "11.11.0",
"@guardian/ab-core": "8.0.0",
"@guardian/braze-components": "21.0.0",
"@guardian/bridget": "8.0.0",
"@guardian/bridget": "8.1.0",
"@guardian/browserslist-config": "6.1.0",
"@guardian/cdk": "50.13.0",
"@guardian/commercial": "23.7.4",
Expand Down
13 changes: 13 additions & 0 deletions dotcom-rendering/src/components/KeyEventsCarousel.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SvgChevronRightSingle,
} from '@guardian/source/react-components';
import { useRef } from 'react';
import { getInteractionClient } from '../lib/bridgetApi';
import { palette } from '../palette';
import type { Block } from '../types/blocks';
import type { RenderingTarget } from '../types/renderingTarget';
Expand Down Expand Up @@ -114,13 +115,23 @@ export const KeyEventsCarousel = ({
}: Props) => {
const carousel = useRef<HTMLDivElement | null>(null);
const cardWidth = 200;
const isApps = renderingTarget === 'Apps';

const goPrevious = () => {
if (carousel.current) carousel.current.scrollLeft -= cardWidth;
};
const goNext = () => {
if (carousel.current) carousel.current.scrollLeft += cardWidth;
};

const onTouchStart = async () => {
await getInteractionClient().disableArticleSwipe(true);
};

const onTouchEnd = async () => {
await getInteractionClient().disableArticleSwipe(false);
};

const filteredKeyEvents = keyEvents.filter(isValidKeyEvent);
const carouselLength = filteredKeyEvents.length;
const shortCarousel = carouselLength <= 4;
Expand All @@ -132,6 +143,8 @@ export const KeyEventsCarousel = ({
<div css={titleStyles}>Key events</div>
</Hide>
<div
onTouchStart={isApps ? onTouchStart : undefined}
onTouchEnd={isApps ? onTouchEnd : undefined}
ref={carousel}
id="key-events-carousel"
css={[carouselStyles, shortCarousel && leftMarginStyles]}
Expand Down
13 changes: 13 additions & 0 deletions dotcom-rendering/src/lib/bridgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Commercial from '@guardian/bridget/Commercial';
import * as Discussion from '@guardian/bridget/Discussion';
import * as Environment from '@guardian/bridget/Environment';
import * as Gallery from '@guardian/bridget/Gallery';
import * as Interaction from '@guardian/bridget/Interaction';
import * as Metrics from '@guardian/bridget/Metrics';
import * as Navigation from '@guardian/bridget/Navigation';
import * as Newsletters from '@guardian/bridget/Newsletters';
Expand Down Expand Up @@ -165,3 +166,15 @@ export const getDiscussionClient = (): Discussion.Client<void> => {
}
return discussionClient;
};

let interactionClient: Interaction.Client<void> | undefined = undefined;
export const getInteractionClient = (): Interaction.Client<void> => {
if (!interactionClient) {
interactionClient = createAppClient<Interaction.Client<void>>(
Interaction.Client,
'buffered',
'compact',
);
}
return interactionClient;
};
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a1bd37

Please sign in to comment.