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

fix: improve arrow container structure for hover interactions #1087

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion docs/api/autoplay.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For accessibility, the carousel will pause when the user is interacting with it.
| `autoplay` | boolean | `false` |
| `autoplayInterval` | number | `3000` |

### Example
#### Example

<Carousel autoplay={true} autoplayInterval={1000} wrapMode="wrap">
<img src="/open-source/nuka-carousel/img/pexels-01.jpg" />
Expand All @@ -31,3 +31,26 @@ For accessibility, the carousel will pause when the user is interacting with it.
<img src="pexels-03.jpg" />
</Carousel>
```

### Navigation Arrows

| Prop Name | Type | Default Value |
| :----------------- | :------ | :------------ |
| `autoplay` | boolean | `false` |
| `showArrows` | boolean \| `always` \| `hover` | `false` |

<Carousel autoplay={true} autoplayInterval={2000} wrapMode="wrap" showArrows={true}>
<img src="/open-source/nuka-carousel/img/pexels-01.jpg" />
<img src="/open-source/nuka-carousel/img/pexels-02.jpg" />
<img src="/open-source/nuka-carousel/img/pexels-03.jpg" />
</Carousel>

#### Code

```tsx
<Carousel autoplay={true} showArrows={true}>
<img src="pexels-01.jpg" />
<img src="pexels-02.jpg" />
<img src="pexels-03.jpg" />
</Carousel>
```
10 changes: 8 additions & 2 deletions packages/nuka/src/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(
const carouselRef = useRef<HTMLDivElement | null>(null);
const containerRef = useRef<HTMLDivElement | null>(null);
const previousPageRef = useRef<number>(-1);
const arrowsContainerRef = useRef<HTMLDivElement | null>(null);

// -- update page count and scroll offset based on scroll distance
const { totalPages, scrollOffset } = useMeasurement({
Expand Down Expand Up @@ -126,8 +127,13 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(

// -- autoplay
const isHovered = useHover({ element: containerRef, enabled: autoplay });
const isArrowHovered = useHover({
element: arrowsContainerRef,
enabled: autoplay && showArrows === true,
});
const prefersReducedMotion = useReducedMotion({ enabled: autoplay });
const autoplayEnabled = autoplay && !(isHovered || prefersReducedMotion);
const autoplayEnabled =
autoplay && !(isHovered || prefersReducedMotion || isArrowHovered);
useInterval(goForward, autoplayInterval, autoplayEnabled);

// -- scroll container when page index changes
Expand Down Expand Up @@ -191,7 +197,7 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(
{children}
</div>
</div>
{showArrows && arrows}
{showArrows && <div ref={arrowsContainerRef}>{arrows}</div>}
</div>
</div>
{showDots && dots}
Expand Down
Loading