import React, {useEffect, useCallback} from 'react'; import {useScroll} from '../../hooks/use_scroll'; import {SnapperDots} from './snapper_dots'; import {SnapperPane} from './spapper_pane'; import './snapper.scss'; interface Props { children: React.ReactNode; onChange: (index: number) => void; } interface State { cursor: number; positions: number[]; } type Action = | {type: 'SELECT'; payload: {cursor: number}} | {type: 'REPOSITION'; payload: {positions: number[]}}; const reducer = (state: State, action: Action): State => { switch (action.type) { case 'SELECT': return {...state, cursor: action.payload.cursor}; case 'REPOSITION': return {...state, positions: action.payload.positions}; } }; const closestReducer = (g: number) => (a: number, b: number): number => Math.abs(g - a) < Math.abs(g - b) ? a : b; const findClosest = (xs: number[], x: number): number => xs.reduce(closestReducer(x), 0); const calculatePositions = (parentNode: HTMLDivElement): number[] => { return Array.prototype.map.call(parentNode.children, (node: HTMLElement) => { return ( node.getBoundingClientRect().left - parentNode.getBoundingClientRect().left + parentNode.scrollLeft ); }); }; export const Snapper: React.FC = ({children, onChange}) => { const [state, dispatch] = React.useReducer(reducer, { cursor: 0, positions: [] }); const track = React.useRef(null); const length = React.Children.count(children); const {x: xPosition} = useScroll(track); const handleActive = useCallback( (index: number) => dispatch({type: 'SELECT', payload: {cursor: index}}), [] ); const handleClick = useCallback( (index: number): void => { track.current.scrollLeft = state.positions[index]; handleActive(index); }, [handleActive, state.positions] ); const handlePositions = useCallback(() => { const positions = calculatePositions(track.current); dispatch({type: 'REPOSITION', payload: {positions}}); }, []); const handlePersist = useCallback(() => { track.current.scrollLeft = state.positions[state.cursor]; }, [state.cursor, state.positions]); // Calculates positions of slides useEffect(() => { handlePositions(); window.addEventListener('resize', handlePositions, {passive: true}); return (): void => window.removeEventListener('resize', handlePositions); }, [handlePositions]); // Sticks to current slide when resizing window useEffect(() => { window.addEventListener('resize', handlePersist, {passive: true}); window.addEventListener('orientationchange', handlePersist); return (): void => { window.removeEventListener('resize', handlePersist); window.removeEventListener('orientationchange', handlePersist); }; }, [handlePersist]); // Updates cursor to nearest slide when xPosition changes useEffect(() => { if (state.positions.length === 0) return; const cursor = state.positions.indexOf( findClosest(state.positions, xPosition) ); dispatch({type: 'SELECT', payload: {cursor}}); }, [state.positions, xPosition]); // Handles passed in onActive callback when cursor changes useEffect(() => onChange(state.cursor), [onChange, state.cursor]); return (
{React.Children.map(children, (child, index) => ( {child} ))}
); };