Skip to content

Commit

Permalink
Apply navigation styles only when required
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmockett committed Nov 1, 2024
1 parent 8091ecd commit dd89ae6
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions dotcom-rendering/src/components/ScrollableCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,24 @@ const themeButtonDisabled: Partial<ThemeButton> = {
textTertiary: palette('--carousel-chevron-disabled'),
};

const carouselContainerStyles = css`
const containerStyles = css`
/* Extend carousel into outer margins on mobile */
margin-left: -10px;
margin-right: -10px;
${from.mobileLandscape} {
margin-left: -20px;
margin-right: -20px;
}
${from.tablet} {
margin-left: 10px;
margin-right: 10px;
}
${from.leftCol} {
margin-left: -10px;
}
`;

const containerWithNavigationStyles = css`
display: flex;
flex-direction: column-reverse;
${from.tablet} {
Expand All @@ -56,14 +73,6 @@ const carouselContainerStyles = css`
gap: ${space[1]}px;
}
/* Extend carousel into outer margins on mobile */
margin-left: -10px;
margin-right: -10px;
${from.mobileLandscape} {
margin-left: -20px;
margin-right: -20px;
}
/**
* From tablet, pull container up so navigation buttons align with title.
* The margin is calculated from the front section title font size and line
Expand All @@ -73,16 +82,13 @@ const carouselContainerStyles = css`
* into the right-hand column.
*/
${from.tablet} {
margin-left: 10px;
margin-right: 10px;
margin-top: calc(
(-${titlePreset.fontSize} * ${titlePreset.lineHeight}) -
${space[3]}px
);
}
${from.leftCol} {
margin-top: 0;
margin-left: -10px;
}
${from.wide} {
margin-right: calc(${space[2]}px - ${gridColumnWidth} - ${gridGap});
Expand Down Expand Up @@ -210,6 +216,8 @@ export const ScrollableCarousel = ({
const [previousButtonEnabled, setPreviousButtonEnabled] = useState(false);
const [nextButtonEnabled, setNextButtonEnabled] = useState(true);

const showNavigation = carouselLength > visibleCardsOnTablet;

const scrollTo = (direction: 'left' | 'right') => {
if (!carouselRef.current) return;

Expand Down Expand Up @@ -260,7 +268,12 @@ export const ScrollableCarousel = ({
}, []);

return (
<div css={carouselContainerStyles}>
<div
css={[
containerStyles,
showNavigation && containerWithNavigationStyles,
]}
>
<ol
ref={carouselRef}
css={[
Expand All @@ -275,7 +288,7 @@ export const ScrollableCarousel = ({
>
{children}
</ol>
{carouselLength > visibleCardsOnTablet && (
{showNavigation && (
<div css={buttonStyles}>
<Button
hideLabel={true}
Expand Down

0 comments on commit dd89ae6

Please sign in to comment.