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(Carousel): Auto hide navigation buttons if total slides are less than visible items #2400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 40 additions & 34 deletions packages/blade/src/components/Carousel/Carousel.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ControlsProp = Required<
onIndicatorButtonClick: (index: number) => void;
onNextButtonClick: () => void;
onPreviousButtonClick: () => void;
showControls: boolean;
};

const Controls = ({
Expand All @@ -51,43 +52,46 @@ const Controls = ({
onPreviousButtonClick,
indicatorVariant,
navigationButtonVariant,
showControls,
}: ControlsProp): React.ReactElement => {
if (navigationButtonPosition === 'bottom') {
return (
<Box marginTop="spacing.7" display="flex" alignItems="center" gap="spacing.4">
<NavigationButton
type="previous"
variant={navigationButtonVariant}
onClick={onPreviousButtonClick}
/>
{showIndicators ? (
if (showControls) {
if (navigationButtonPosition === 'bottom') {
return (
<Box marginTop="spacing.7" display="flex" alignItems="center" gap="spacing.4">
<NavigationButton
type="previous"
variant={navigationButtonVariant}
onClick={onPreviousButtonClick}
/>
{showIndicators ? (
<Indicators
onClick={onIndicatorButtonClick}
activeIndex={activeIndicator}
totalItems={totalSlides}
variant={indicatorVariant}
/>
) : null}
<NavigationButton
onClick={onNextButtonClick}
type="next"
variant={navigationButtonVariant}
/>
</Box>
);
}

if (showIndicators && navigationButtonPosition === 'side') {
return (
<Box marginTop="spacing.7">
<Indicators
onClick={onIndicatorButtonClick}
activeIndex={activeIndicator}
totalItems={totalSlides}
variant={indicatorVariant}
/>
) : null}
<NavigationButton
onClick={onNextButtonClick}
type="next"
variant={navigationButtonVariant}
/>
</Box>
);
}

if (showIndicators && navigationButtonPosition === 'side') {
return (
<Box marginTop="spacing.7">
<Indicators
onClick={onIndicatorButtonClick}
activeIndex={activeIndicator}
totalItems={totalSlides}
variant={indicatorVariant}
/>
</Box>
);
</Box>
);
}
}

return <></>;
Expand Down Expand Up @@ -308,6 +312,7 @@ const Carousel = ({
const shouldNavButtonsFloat = isResponsive && navigationButtonPosition === 'side';
const totalNumberOfSlides = React.Children.count(children);
const numberOfIndicators = Math.ceil(totalNumberOfSlides / _visibleItems);
const shouldShowControls = totalNumberOfSlides > _visibleItems;

// hide next/prev button on reaching start/end when carousel is responsive
// in non-responsive carousel we always show the next/prev buttons to allow looping
Expand Down Expand Up @@ -529,7 +534,7 @@ const Carousel = ({
flexDirection="row"
height="100%"
>
{shouldShowPrevButton && shouldNavButtonsFloat ? (
{shouldShowPrevButton && shouldNavButtonsFloat && shouldShowControls ? (
<BaseBox zIndex={2} position="absolute" left="spacing.11">
<NavigationButton
type="previous"
Expand All @@ -538,7 +543,7 @@ const Carousel = ({
/>
</BaseBox>
) : null}
{isNavButtonsOnSide ? (
{isNavButtonsOnSide && shouldShowControls ? (
<NavigationButton
type="previous"
variant={navigationButtonVariant}
Expand All @@ -559,7 +564,7 @@ const Carousel = ({
>
{children}
</CarouselBody>
{shouldShowNextButton && shouldNavButtonsFloat ? (
{shouldShowNextButton && shouldNavButtonsFloat && shouldShowControls ? (
<BaseBox zIndex={2} position="absolute" right="spacing.11">
<NavigationButton
onClick={goToNextSlide}
Expand All @@ -568,7 +573,7 @@ const Carousel = ({
/>
</BaseBox>
) : null}
{isNavButtonsOnSide ? (
{isNavButtonsOnSide && shouldShowControls ? (
<NavigationButton
onClick={goToNextSlide}
type="next"
Expand All @@ -586,6 +591,7 @@ const Carousel = ({
onPreviousButtonClick={goToPreviousSlide}
indicatorVariant={indicatorVariant}
navigationButtonVariant={navigationButtonVariant}
showControls={shouldShowControls}
/>
</BaseBox>
</CarouselContext.Provider>
Expand Down