Skip to content

Commit 0b3c9ad

Browse files
committed
Starting to scaffold the documentation
1 parent 5005742 commit 0b3c9ad

File tree

2 files changed

+189
-4
lines changed

2 files changed

+189
-4
lines changed

docs/docs/examples/simple-pattern.mdx

+188
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,193 @@ import SimplePattern from '@site/src/components/examples/SimplePattern'
44

55
# Simple Pattern
66

7+
Below is an example implementation of a simple pattern which utilizes the hooks provided by `react-swipeable` within a TypeScript context.
8+
9+
## Simple Pattern Code Source
10+
11+
You can see this full example as pure code within the [Pattern.tsx](https://github.com/FormidableLabs/react-swipeable/blob/main/examples/app/SimplePattern/pattern.tsx) file within the React-Swipeable repo directly.
12+
13+
## Simple Pattern Live Preview
14+
715
<SimplePattern />
816

17+
## Simple Carousel Code Explained
18+
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.
20+
21+
```typescript
22+
import React, { FunctionComponent, ReactNode } from 'react';
23+
import { useSwipeable, UP, DOWN, SwipeEventData } from 'react-swipeable';
24+
import {
25+
Wrapper,
26+
CarouselContainer,
27+
CarouselSlot,
28+
PatternBox,
29+
PREV,
30+
NEXT,
31+
D
32+
} from '../components';
33+
```
34+
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!
36+
37+
```typescript
38+
const UpArrow = ({active}: {active: boolean}) => (
39+
<svg viewBox="0 0 16 16" version="1.1" style={{width: '15px'}}>
40+
<g transform="translate(-35.399 -582.91)">
41+
<path style={{fill: active ? '#EEEE00' : '#000000'}} d="m40.836 598.91v-6.75h-5.4375l4-4.625 4-4.625 4 4.625 4 4.625h-5.0938v6.75h-5.4688z" />
42+
</g>
43+
</svg>
44+
);
45+
46+
const DownArrow = ({active}: {active: boolean}) => (
47+
<svg viewBox="0 0 16 16" version="1.1" style={{width: '15px'}}>
48+
<g transform="translate(-35.399 -598.91)">
49+
<path style={{fill: active ? '#EEEE00' : '#000000'}} d="m40.836 598.91v6.75h-5.4375l4 4.625 4 4.625 4-4.625 4-4.625h-5.0938v-6.75h-5.4688z" />
50+
</g>
51+
</svg>
52+
);
53+
```
54+
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+
```
68+
69+
```typescript
70+
type Direction = typeof PREV | typeof NEXT;
71+
72+
interface CarouselState {
73+
pos: number;
74+
sliding: boolean;
75+
dir: Direction;
76+
}
77+
78+
type CarouselAction =
79+
| { type: Direction, numItems: number }
80+
| { type: 'stopSliding' };
81+
```
82+
83+
```typescript
84+
85+
const getOrder = (index: number, pos: number, numItems: number) => {
86+
return index - pos < 0 ? numItems - Math.abs(index - pos) : index - pos;
87+
};
88+
89+
const pattern = [UP, DOWN, UP, DOWN];
90+
91+
const getInitialState = (numItems: number): CarouselState => ({ pos: numItems - 1, sliding: false, dir: NEXT });
92+
93+
```
94+
95+
96+
```typescript
97+
function reducer(state: CarouselState, action: CarouselAction): CarouselState {
98+
switch (action.type) {
99+
case PREV:
100+
return {
101+
...state,
102+
dir: PREV,
103+
sliding: true,
104+
pos: state.pos === 0 ? action.numItems - 1 : state.pos - 1
105+
};
106+
case NEXT:
107+
return {
108+
...state,
109+
dir: NEXT,
110+
sliding: true,
111+
pos: state.pos === action.numItems - 1 ? 0 : state.pos + 1
112+
};
113+
case 'stopSliding':
114+
return { ...state, sliding: false };
115+
default:
116+
return state;
117+
}
118+
}
119+
```
120+
121+
122+
```typescript
123+
const Carousel: FunctionComponent<{children: ReactNode}> = (props) => {
124+
const numItems = React.Children.count(props.children);
125+
const [state, dispatch] = React.useReducer(reducer, getInitialState(numItems));
126+
127+
const slide = (dir: Direction) => {
128+
dispatch({ type: dir, numItems });
129+
setTimeout(() => {
130+
dispatch({ type: 'stopSliding' });
131+
}, 50);
132+
};
133+
134+
const [pIdx, setPIdx] = React.useState(0);
135+
136+
const handleSwiped = (eventData: SwipeEventData) => {
137+
if (eventData.dir === pattern[pIdx]) {
138+
// user successfully got to the end of the pattern!
139+
if (pIdx + 1 === pattern.length) {
140+
setPIdx(pattern.length);
141+
slide(NEXT);
142+
setTimeout(() => {
143+
setPIdx(0);
144+
}, 50);
145+
} else {
146+
// user is cont. with the pattern
147+
setPIdx((i) => (i += 1));
148+
}
149+
} else {
150+
// user got the next pattern step wrong, reset pattern
151+
setPIdx(0);
152+
}
153+
};
154+
155+
const handlers = useSwipeable({
156+
onSwiped: handleSwiped,
157+
onTouchStartOrOnMouseDown: (({ event }) => event.preventDefault()),
158+
touchEventOptions: { passive: false },
159+
preventScrollOnSwipe: true,
160+
trackMouse: true
161+
});
162+
163+
return (
164+
<>
165+
<PatternBox {...handlers}>
166+
Swipe the pattern below, within this box, to make the carousel go to the next
167+
slide
168+
{`\n`}
169+
<p style={{textAlign: 'center', paddingTop: '15px'}}>
170+
Swipe:
171+
<D><UpArrow active={pIdx > 0} /></D>
172+
<D><DownArrow active={pIdx > 1} /></D>
173+
<D><UpArrow active={pIdx > 2} /></D>
174+
<D><DownArrow active={pIdx > 3} /></D>
175+
</p>
176+
</PatternBox>
177+
<div style={{paddingBottom: '15px'}}>
178+
<Wrapper>
179+
<CarouselContainer dir={state.dir} sliding={state.sliding}>
180+
{React.Children.map(props.children, (child, index) => (
181+
<CarouselSlot
182+
key={index}
183+
order={getOrder(index, state.pos, numItems)}
184+
>
185+
{child}
186+
</CarouselSlot>
187+
))}
188+
</CarouselContainer>
189+
</Wrapper>
190+
</div>
191+
</>
192+
);
193+
};
194+
195+
```
196+

examples/app/SimplePattern/index.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Carousel from './pattern';
77
function SimplePattern() {
88
return (
99
<div>
10-
<h5 style={{ marginBottom: '20px' }}>
10+
<h5 style={{ marginBottom: '20px'}}>
1111
<strong>👉 Swipe pattern</strong>
1212
</h5>
1313
<Carousel>
@@ -17,9 +17,6 @@ function SimplePattern() {
1717
<Item img="https://unsplash.it/478/205" />
1818
<Item img="https://unsplash.it/479/205" />
1919
</Carousel>
20-
<h6>
21-
<a href="https://github.com/FormidableLabs/react-swipeable/blob/main/examples/app/SimplePattern/pattern.tsx">See code</a> for example usage of <code>onTouchStartOrMouseDown</code> and <code>touchEventOptions</code>
22-
</h6>
2320
</div>
2421
);
2522
}

0 commit comments

Comments
 (0)