Skip to content

Commit

Permalink
feat: provide page info on beforeSlide and afterSlide callbacks (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot authored Oct 7, 2024
2 parents 723f6ec + fc30468 commit de9cfbb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-berries-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nuka-carousel": minor
---

feat: provide page info on beforeSlide and afterSlide callbacks
16 changes: 6 additions & 10 deletions docs/api/callbacks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ Functions that are invoked when the progression methods (goBack()/goForward()) a

### Details

| Prop Name | Type | Default Value |
| :------------ | :--------- | :------------ |
| `beforeSlide` | () => void | `undefined` |
| `afterSlide` | () => void | `undefined` |
| Prop Name | Type | Default Value |
| :------------ | :--------------------------------------------------------- | :------------ |
| `beforeSlide` | (currentSlideIndex: number, endSlideIndex: number) => void | `undefined` |
| `afterSlide` | (endSlideIndex: number) => void | `undefined` |

- `beforeSlide` - Runs a given function before scrolling when a progression method is called. It will also run right before the carousel registers that it has been scrolled on if manually scrolled.
- `afterSlide` - Runs a given function after scrolling when a progression method is called or after manually scrolling.

### Example

```tsx
<Carousel beforeSlide={() => myCustomBeforeFunction()}>
{/* Cards */}
</Carousel>
<Carousel beforeSlide={() => myCustomBeforeFunction()}>{/* Cards */}</Carousel>
```

```tsx
<Carousel afterSlide={() => myCustomAfterFunction()}>
{/* Cards */}
</Carousel>
<Carousel afterSlide={() => myCustomAfterFunction()}>{/* Cards */}</Carousel>
```
11 changes: 8 additions & 3 deletions packages/nuka/src/Carousel/Carousel.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ export const GoToPage: Story = {

export const BeforeSlide: Story = {
args: {
beforeSlide: () =>
console.log('Function was called before scroll occurred '),
beforeSlide: (currentSlideIndex, endSlideIndex) =>
console.log(
'Function was called before scroll occurred ',
currentSlideIndex,
endSlideIndex,
),
children: (
<>
{[...Array(10)].map((_, index) => (
Expand All @@ -185,7 +189,8 @@ export const BeforeSlide: Story = {

export const AfterSlide: Story = {
args: {
afterSlide: () => console.log('Function was called after scroll occurred '),
afterSlide: (endSlideIndex) =>
console.log('Function was called after scroll occurred ', endSlideIndex),
children: (
<>
{[...Array(10)].map((_, index) => (
Expand Down
8 changes: 6 additions & 2 deletions packages/nuka/src/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(

const carouselRef = useRef<HTMLDivElement | null>(null);
const containerRef = useRef<HTMLDivElement | null>(null);
const previousPageRef = useRef<number>(-1);

// -- update page count and scroll offset based on scroll distance
const { totalPages, scrollOffset } = useMeasurement({
Expand Down Expand Up @@ -103,9 +104,12 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(
// -- scroll container when page index changes
useEffect(() => {
if (containerRef.current) {
beforeSlide && beforeSlide();
const currentSlideIndex = previousPageRef.current;
const endSlideIndex = currentPage;
beforeSlide && beforeSlide(currentSlideIndex, endSlideIndex);
containerRef.current.scrollLeft = scrollOffset[currentPage];
afterSlide && setTimeout(() => afterSlide(), 0);
afterSlide && setTimeout(() => afterSlide(endSlideIndex), 0);
previousPageRef.current = currentPage;
}
}, [currentPage, scrollOffset, beforeSlide, afterSlide]);

Expand Down
4 changes: 2 additions & 2 deletions packages/nuka/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type ShowArrowsOption = boolean | 'always' | 'hover';
export type ScrollDistanceType = number | 'slide' | 'screen';

export type CarouselCallbacks = {
beforeSlide?: () => void;
afterSlide?: () => void;
beforeSlide?: (currentSlideIndex: number, endSlideIndex: number) => void;
afterSlide?: (endSlideIndex: number) => void;
};

export type CarouselProps = CarouselCallbacks & {
Expand Down

0 comments on commit de9cfbb

Please sign in to comment.