You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/examples/simple-carousel.mdx
+130-1
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,136 @@ import SimpleCarousel from '@site/src/components/examples/SimpleCarousel'
4
4
5
5
# Simple Carousel
6
6
7
-
Below is an example implementation of a simple carousel which utilizes the hooks provided by react-swipeable.
7
+
Below is an example implementation of a simple carousel which utilizes the hooks provided by `react-swipeable` within a TypeScript context.
8
+
9
+
10
+
## Simple Carousel Code Source
11
+
12
+
You can see this full example as pure code within the [Carousel.tsx](https://github.com/FormidableLabs/react-swipeable/blob/main/examples/app/SimpleCarousel/Carousel.tsx) file within the React-Swipeable repo directly.
13
+
14
+
## Simple Carousel Live Preview
8
15
9
16
<SimpleCarousel />
10
17
18
+
Note: The action of swiping must have a duration of `500ms` or lower in order to trigger the swipe action.
Below, we set up types and an interface for some of the work we'll be building. Next, we write a function called `getOrder`, which will drive the position of each item in the carousel, and what order of position each will be displayed in context of the carousel. Finally, we have a simple `getInitialState` position that sets the `CarouselState` of the carousel we'll be building.
Next, we build a reducer for controlling the action of the Carousel, using a switch to set `CarouselState` logic.
61
+
62
+
```typescript
63
+
function reducer(state:CarouselState, action:CarouselAction):CarouselState {
64
+
switch (action.type) {
65
+
casePREV:
66
+
return {
67
+
...state,
68
+
dir: PREV,
69
+
sliding: true,
70
+
pos: state.pos===0?action.numItems-1:state.pos-1
71
+
};
72
+
caseNEXT:
73
+
return {
74
+
...state,
75
+
dir: NEXT,
76
+
sliding: true,
77
+
pos: state.pos===action.numItems-1?0:state.pos+1
78
+
};
79
+
case'stopSliding':
80
+
return { ...state, sliding: false };
81
+
default:
82
+
returnstate;
83
+
}
84
+
}
85
+
```
86
+
87
+
Then, building upon the reducer logic, the `<Carousel>` is constructed. We hold the number of items within a count of `numItems`. We utilize the reducer within the `React.useReducer` hook.
88
+
89
+
By creating `slide`, as a `const`, we can utilize that later in the component, calling it within `useSwipeable`: called upon `slide(NEXT)` and `slide(PREV)`, invoking the `dispatch` and the `timeout` we built within `slide`. Within the use of `useSwippeable`, we set `swipeDuration` to `500ms`. We set `preventScrollOnSwipe` to `true`, and `trackMouse` to `true`.
90
+
91
+
At the end, we return the component itself, built with the components we've created, with `handlers` passed into the wrapping `<div>` around the surrounding container. The `<CarouselContainer>` holds the directional and sliding state, and within that container the items we want to display are mapped as `React.Children`, utilizing `getOrder`.
92
+
93
+
When we put it all together, our `<Carousel>` is complete!
Copy file name to clipboardExpand all lines: examples/app/SimpleCarousel/index.tsx
-8
Original file line number
Diff line number
Diff line change
@@ -7,21 +7,13 @@ import Carousel from './Carousel';
7
7
functionSimpleCarousel(){
8
8
return(
9
9
<div>
10
-
<h5style={{marginBottom: '20px'}}>
11
-
<strong>🖼 Image Carousel</strong>
12
-
</h5>
13
-
14
10
<Carousel>
15
11
<Itemimg="https://unsplash.it/475/205"/>
16
12
<Itemimg="https://unsplash.it/476/205"/>
17
13
<Itemimg="https://unsplash.it/477/205"/>
18
14
<Itemimg="https://unsplash.it/478/205"/>
19
15
<Itemimg="https://unsplash.it/479/205"/>
20
16
</Carousel>
21
-
<b>Note: swipe must be "faster" then 500ms to trigger.</b>
22
-
<h6>
23
-
<ahref="https://github.com/FormidableLabs/react-swipeable/blob/main/examples/app/SimpleCarousel/Carousel.tsx">See code</a> for example usage of <codestyle={{whiteSpace: "nowrap"}}>swipeDuration</code> and <code>preventScrollOnSwipe</code>.
0 commit comments