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

Add hasPrevPage and hasNextPage booleans #29

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `hasPrevPage` and `hasNextPage` booleans to `SnapCarouselResult` to indicate if there's a previous or next page to scroll to.

## [0.3.1] - 2023-02-14

### Added
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export const Carousel = <T extends any>({
scrollRef,
pages,
activePageIndex,
hasPrevPage,
hasNextPage,
prev,
next,
goTo,
Expand All @@ -127,9 +129,10 @@ export const Carousel = <T extends any>({
<button
style={{
...styles.nextPrevButton,
...(activePageIndex <= 0 ? styles.nextPrevButtonDisabled : {})
...(!hasPrevPage ? styles.nextPrevButtonDisabled : {})
}}
onClick={() => prev()}
disabled={!hasPrevPage}
Copy link
Owner

Choose a reason for hiding this comment

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

👍

>
Prev
</button>
Expand All @@ -148,11 +151,10 @@ export const Carousel = <T extends any>({
<button
style={{
...styles.nextPrevButton,
...(activePageIndex === pages.length - 1
? styles.nextPrevButtonDisabled
: {})
...(!hasNextPage ? styles.nextPrevButtonDisabled : {})
}}
onClick={() => next()}
disabled={!hasNextPage}
>
Next
</button>
Expand Down Expand Up @@ -228,6 +230,8 @@ export interface SnapCarouselResult {
readonly pages: number[][];
readonly activePageIndex: number;
readonly snapPointIndexes: Set<number>;
readonly hasPrevPage: boolean;
readonly hasNextPage: boolean;
readonly prev: (opts?: SnapCarouselGoToOptions) => void;
readonly next: (opts?: SnapCarouselGoToOptions) => void;
readonly goTo: (pageIndex: number, opts?: SnapCarouselGoToOptions) => void;
Expand Down
7 changes: 7 additions & 0 deletions src/use-snap-carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface SnapCarouselResult {
readonly pages: number[][];
readonly activePageIndex: number;
readonly snapPointIndexes: Set<number>;
readonly hasPrevPage: boolean;
readonly hasNextPage: boolean;
readonly prev: (opts?: SnapCarouselGoToOptions) => void;
readonly next: (opts?: SnapCarouselGoToOptions) => void;
readonly goTo: (pageIndex: number, opts?: SnapCarouselGoToOptions) => void;
Expand Down Expand Up @@ -201,7 +203,12 @@ export const useSnapCarousel = ({
[pages]
);

const hasPrevPage = activePageIndex > 0;
const hasNextPage = activePageIndex < pages.length - 1;

return {
hasPrevPage,
hasNextPage,
prev: handlePrev,
next: handleNext,
goTo: handleGoTo,
Expand Down
6 changes: 4 additions & 2 deletions stories/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const Carousel = React.forwardRef<CarouselRef, CarouselProps<unknown>>(
({ axis, items, renderItem, scrollMargin = false, scrollBehavior }, ref) => {
const {
scrollRef,
hasNextPage,
hasPrevPage,
next,
prev,
goTo,
Expand Down Expand Up @@ -65,7 +67,7 @@ export const Carousel = React.forwardRef<CarouselRef, CarouselProps<unknown>>(
</div>
<div className={styles.controls}>
<button
disabled={activePageIndex <= 0}
disabled={!hasPrevPage}
onClick={() => prev({ behavior: scrollBehavior })}
className={styles.prevButton}
>
Expand All @@ -89,7 +91,7 @@ export const Carousel = React.forwardRef<CarouselRef, CarouselProps<unknown>>(
))}
</ol>
<button
disabled={activePageIndex === pages.length - 1}
disabled={!hasNextPage}
onClick={() => next({ behavior: scrollBehavior })}
className={styles.nextButton}
>
Expand Down
6 changes: 4 additions & 2 deletions stories/infinite-carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const InfiniteCarousel = React.forwardRef<
({ axis, items, renderItem, scrollMargin = false, scrollBehavior }, ref) => {
const {
scrollRef,
hasNextPage,
hasPrevPage,
next,
prev,
goTo,
Expand Down Expand Up @@ -106,7 +108,7 @@ export const InfiniteCarousel = React.forwardRef<
</div>
<div className={styles.controls}>
<button
disabled={activePageIndex <= 0}
disabled={!hasPrevPage}
onClick={() => prev({ behavior: scrollBehavior })}
className={styles.prevButton}
>
Expand Down Expand Up @@ -135,7 +137,7 @@ export const InfiniteCarousel = React.forwardRef<
))}
</ol>
<button
disabled={activePageIndex === pages.length - 1}
disabled={!hasNextPage}
onClick={() => next({ behavior: scrollBehavior })}
className={styles.nextButton}
>
Expand Down
6 changes: 4 additions & 2 deletions stories/slideshow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const SlideShow = <T extends any>({
}: SlideShowProps<T>) => {
const {
scrollRef,
hasPrevPage,
hasNextPage,
next,
prev,
goTo,
Expand Down Expand Up @@ -79,7 +81,7 @@ export const SlideShow = <T extends any>({
</div>
<div className={styles.controls}>
<button
disabled={activePageIndex === 0}
disabled={!hasPrevPage}
onClick={() => prev()}
className={styles.prevButton}
>
Expand All @@ -103,7 +105,7 @@ export const SlideShow = <T extends any>({
))}
</ol>
<button
disabled={activePageIndex === pages.length - 1}
disabled={!hasNextPage}
onClick={() => next()}
className={styles.nextButton}
>
Expand Down
Loading