MCPcopy Create free account
hub / github.com/FormidableLabs/react-swipeable / Carousel

Function Carousel

examples/app/SimpleCarousel/Carousel.tsx:31–73  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

29const getInitialState = (numItems: number): CarouselState => ({ pos: numItems - 1, sliding: false, dir: NEXT });
30
31const Carousel: FunctionComponent<{children: ReactNode}> = (props) => {
32 const numItems = React.Children.count(props.children);
33 const [state, dispatch] = React.useReducer(reducer, getInitialState(numItems));
34
35 const slide = (dir: Direction) => {
36 dispatch({ type: dir, numItems });
37 setTimeout(() => {
38 dispatch({ type: 'stopSliding' });
39 }, 50);
40 };
41
42 const handlers = useSwipeable({
43 onSwipedLeft: () => slide(NEXT),
44 onSwipedRight: () => slide(PREV),
45 swipeDuration: 500,
46 preventScrollOnSwipe: true,
47 trackMouse: true
48 });
49
50 return (
51 <div {...handlers}>
52 <Wrapper>
53 <CarouselContainer dir={state.dir} sliding={state.sliding}>
54 {React.Children.map(props.children, (child, index) => (
55 <CarouselSlot
56 order={getOrder(index, state.pos, numItems)}
57 >
58 {child}
59 </CarouselSlot>
60 ))}
61 </CarouselContainer>
62 </Wrapper>
63 <SlideButtonContainer>
64 <SlideButton onClick={() => slide(PREV)} float="left">
65 Prev
66 </SlideButton>
67 <SlideButton onClick={() => slide(NEXT)} float="right">
68 Next
69 </SlideButton>
70 </SlideButtonContainer>
71 </div>
72 );
73};
74
75function reducer(state: CarouselState, action: CarouselAction): CarouselState {
76 switch (action.type) {

Callers

nothing calls this directly

Calls 4

useSwipeableFunction · 0.85
getInitialStateFunction · 0.70
slideFunction · 0.70
getOrderFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…