Skip to content

Commit 59eb1a1

Browse files
committed
Updating docs, fixing typo
1 parent 0b3c9ad commit 59eb1a1

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

Diff for: docs/docs/examples/simple-carousel.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function reducer(state: CarouselState, action: CarouselAction): CarouselState {
8686

8787
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.
8888

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`.
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 `useSwipeable`, we set `swipeDuration` to `500ms`. We set `preventScrollOnSwipe` to `true`, and `trackMouse` to `true`.
9090

9191
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`.
9292

Diff for: docs/docs/examples/simple-pattern.mdx

+17-16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can see this full example as pure code within the [Pattern.tsx](https://gith
1616

1717
## Simple Carousel Code Explained
1818

19-
Import the hook directly from the `react-swipeable` library, along with . In our example, we built and imported a local set of UI components: you can utilize your own UI and styling, or use your favorite UI component library of choice.
19+
Import the hook directly from the `react-swipeable` library, along with the directions from the library, and the `SwipeEventData`. In our example, we built and imported a local set of UI components: you can utilize your own UI and styling, or use your favorite UI component library of choice.
2020

2121
```typescript
2222
import React, { FunctionComponent, ReactNode } from 'react';
@@ -32,7 +32,7 @@ import {
3232
} from '../components';
3333
```
3434

35-
In our example, we stylize our own `UpArrow` and `DownArrow`, but know you can use whatever UI library of your choice, or stylize your own!
35+
In our example, we utilize SVGs for our `UpArrow` and `DownArrow` to give indications of when someone is successfully activating the pattern for user feedback, but know you can use whatever UI library of your choice, or stylize your own!
3636

3737
```typescript
3838
const UpArrow = ({active}: {active: boolean}) => (
@@ -52,19 +52,7 @@ const DownArrow = ({active}: {active: boolean}) => (
5252
);
5353
```
5454

55-
```typescript
56-
type Direction = typeof PREV | typeof NEXT;
57-
58-
interface CarouselState {
59-
pos: number;
60-
sliding: boolean;
61-
dir: Direction;
62-
}
63-
64-
type CarouselAction =
65-
| { type: Direction, numItems: number }
66-
| { type: 'stopSliding' };
67-
```
55+
Next, we set up our types for the `Directions`, `CarouselState`, and `CarouselAction`.
6856

6957
```typescript
7058
type Direction = typeof PREV | typeof NEXT;
@@ -80,6 +68,8 @@ type CarouselAction =
8068
| { type: 'stopSliding' };
8169
```
8270

71+
Below, we create a function called `getOrder`, which drives the position of each item in the carousel, and what order of position each will be displayed in the context of the carousel. Then, we set a `pattern` as an array of the pattern we want the user to follow to unlock the slide action. Finally here, we then set `getInitialState`, setting the position of the initial items, the `sliding`, as false, and the direction.
72+
8373
```typescript
8474

8575
const getOrder = (index: number, pos: number, numItems: number) => {
@@ -92,6 +82,7 @@ const getInitialState = (numItems: number): CarouselState => ({ pos: numItems -
9282

9383
```
9484

85+
At the bottom of the file, we set up a reducer for controlling the action of the Carousel utilizing a switch to set the `CarouselState` logic.
9586

9687
```typescript
9788
function reducer(state: CarouselState, action: CarouselAction): CarouselState {
@@ -118,6 +109,17 @@ function reducer(state: CarouselState, action: CarouselAction): CarouselState {
118109
}
119110
```
120111

112+
Then, building upon the reducer logic, the `<Carousel>` is constructed. We hold the number of items within `numItems`, and utilize the reducer within the `React.useReducer` hook.
113+
114+
By creating the `slide`, as a `const`, we can utilize that to call within `handleSwiped` as an action that is called upon the user successfully execution of the pattern.
115+
116+
It may help to briefly look at the `handlers` for a moment, and how we utilize `useSwipeable`. Within this, with each `onSwiped`, we call `handleSwiped`. So for each swipe the user takes within the text box above the carousel, we execute `handleSwiped` and pass along the `eventData`. If the `eventData.dir` matches the pattern for this indexed (`pIdx`) item, and the direction indicated, then we `setPIdx` to a greater number.
117+
118+
What does this do? Two things: it helps us know when the user successfully got to the end of the pattern, and activate the `slide` action, and it also controls the arrows activating the color within the `<PatternBox>` to give feedback to the user that they were successful in activating the steps of the pattern!
119+
120+
Two other important items to note: we utilized `onTouchStartOrOnMouseDown` to pass through `event.preventDefault()` as a callback, and used `touchEventOptions: {passive: false}` in case certain browsers ignored the `preventDefault()` callback bubbling up.
121+
122+
From there, the rest of the UI of the component is built. The `<PatternBox>` holds where the user swipes in order to interact with the Carousel itself, along with the arrows that give the feedback to the user that the pattern was successful. The `<CarouselContainer>` holds the Carousel display and items. Our Simple Pattern is complete!
121123

122124
```typescript
123125
const Carousel: FunctionComponent<{children: ReactNode}> = (props) => {
@@ -191,6 +193,5 @@ const Carousel: FunctionComponent<{children: ReactNode}> = (props) => {
191193
</>
192194
);
193195
};
194-
195196
```
196197

Diff for: docs/src/components/examples/SimplePattern/pattern.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ const Carousel: FunctionComponent<{ children: ReactNode }> = (props) => {
102102
return (
103103
<>
104104
<PatternBox {...handlers}>
105-
Swipe the pattern below, within this box, to make the carousel go to the
106-
next slide
105+
Within this text area, swipe the pattern seen below to make the carousel
106+
navigate to the next slide.
107107
{`\n`}
108108
<p style={{ textAlign: "center", paddingTop: "15px" }}>
109109
Swipe:

Diff for: examples/app/SimplePattern/pattern.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const Carousel: FunctionComponent<{children: ReactNode}> = (props) => {
9090
return (
9191
<>
9292
<PatternBox {...handlers}>
93-
Swipe the pattern below, within this box, to make the carousel go to the next
94-
slide
93+
Within this text area, swipe the pattern seen below to make the carousel
94+
navigate to the next slide.
9595
{`\n`}
9696
<p style={{textAlign: 'center', paddingTop: '15px'}}>
9797
Swipe:

0 commit comments

Comments
 (0)