Skip to content

useSnapNavigation

Edit on GitHub
@volvo-cars/react-headless v0.24.7

Returns prev/next button props for scroll-snap containers like carousels and reels.

import { useRef } from 'react';
import { useSnapNavigation } from '@volvo-cars/react-headless';
function Carousel() {
const ref = useRef<HTMLDivElement>(null);
const { previousButtonProps, nextButtonProps } = useSnapNavigation(ref);
return (
<div>
<div ref={ref} className="reel">
<div className="reel-frame">Slide 1</div>
<div className="reel-frame">Slide 2</div>
<div className="reel-frame">Slide 3</div>
</div>
<button {...previousButtonProps}>Previous</button>
<button {...nextButtonProps}>Next</button>
</div>
);
}

The returned props include:

  • onClick – scrolls to the previous/next snap point
  • disabled – true when at the start/end of the container
const { previousButtonProps, nextButtonProps } = useSnapNavigation(ref);
// previousButtonProps.disabled is true when scrolled to the start
// nextButtonProps.disabled is true when scrolled to the end

Pair with useSnapIndicators for a complete carousel:

import { useRef } from 'react';
import {
useSnapIndicators,
useSnapNavigation,
} from '@volvo-cars/react-headless';
function FullCarousel() {
const ref = useRef<HTMLDivElement>(null);
const { indicators, activeIndex } = useSnapIndicators(ref);
const { previousButtonProps, nextButtonProps } = useSnapNavigation(ref);
return (
<div>
<div ref={ref} className="reel">
{/* slides */}
</div>
<div className="flex items-center gap-8">
<button {...previousButtonProps}>←</button>
<span>
{activeIndex + 1} / {indicators.length}
</span>
<button {...nextButtonProps}>→</button>
</div>
</div>
);
}

useSnapNavigation accepts a ref to the scroll-snap container.

Returns:

  • previousButtonProps – props to spread on the previous button (onClick, disabled)
  • nextButtonProps – props to spread on the next button (onClick, disabled)