|
1 | 1 | # react-native-interactive-tutorial |
2 | 2 |
|
3 | | -Interactive tutorial with step by step guide |
| 3 | +Interactive tutorial with step-by-step guide |
| 4 | + |
| 5 | +<img src="./media/usage_gif.gif" width="600" alt='usage' /> |
| 6 | + |
4 | 7 |
|
5 | 8 | ## Installation |
6 | 9 |
|
| 10 | +1. Install the main library: |
7 | 11 | ```sh |
8 | | -npm install react-native-interactive-tutorial |
| 12 | +yarn add react-native-interactive-tutorial |
| 13 | +``` |
| 14 | + |
| 15 | +2. The library has peer dependencies, you should install them to your project: <br /> |
| 16 | +⚠️ ! Make sure that you have followed all the steps to install these libraries |
| 17 | +```sh |
| 18 | +yarn add react-native-reanimated react-native-gesture-handler react-native-safe-area-context react-native-svg |
| 19 | +``` |
| 20 | + |
| 21 | +3. (Optional) You should use some storage library. |
| 22 | +It can be any library, I use @react-native-async-storage/async-storage for an example. |
| 23 | +```sh |
| 24 | +yarn add @react-native-async-storage/async-storage |
9 | 25 | ``` |
10 | 26 |
|
11 | 27 | ## Usage |
| 28 | +Full example you [can find here](./example) |
| 29 | + |
| 30 | +We can divide the usage into 4 parts: |
| 31 | +1. Creating the root component in your project: |
| 32 | + |
| 33 | +InteractiveTutorial.tsx: |
| 34 | +```tsx |
| 35 | +import { type PropsWithChildren, useCallback, useMemo } from 'react'; |
| 36 | + |
| 37 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 38 | +import { Button } from 'react-native'; |
| 39 | + |
| 40 | +import { |
| 41 | + type DescriptionCardProps, |
| 42 | + type SharedDescriptionCardButtonProps, |
| 43 | + InteractiveTutorialContainer, |
| 44 | + SharedDescriptionCard, |
| 45 | +} from 'react-native-interactive-tutorial'; |
| 46 | + |
12 | 47 |
|
| 48 | +export enum TARGETS { |
| 49 | + Target1, |
| 50 | + Target2, |
| 51 | + Target3, |
| 52 | +} |
13 | 53 |
|
14 | | -```js |
15 | | -import { multiply } from 'react-native-interactive-tutorial'; |
| 54 | +export default function InteractiveTutorial({ children }: PropsWithChildren) { |
| 55 | + // !! Here you can use different library |
| 56 | + const storage = useMemo( |
| 57 | + () => ({ |
| 58 | + set: (_: boolean) => AsyncStorage.setItem('tutorial', String(_)), |
| 59 | + get: () => AsyncStorage.getItem('tutorial').then((_) => !!_), |
| 60 | + }), |
| 61 | + [] |
| 62 | + ); |
16 | 63 |
|
17 | | -// ... |
| 64 | + // !! Here are description be key dictionary |
| 65 | + const stack = useMemo( |
| 66 | + () => |
| 67 | + new Map([ |
| 68 | + [TARGETS.Target1, 'Target 1'], |
| 69 | + [TARGETS.Target2, 'Target 2'], |
| 70 | + [TARGETS.Target3, 'Target 3'], |
| 71 | + ]), |
| 72 | + [] |
| 73 | + ); |
18 | 74 |
|
19 | | -const result = await multiply(3, 7); |
| 75 | + // !! Translations (for description card) |
| 76 | + const translations = useMemo( |
| 77 | + () => ({ |
| 78 | + prevButton: 'Prev', |
| 79 | + nextButton: 'Next', |
| 80 | + finishButton: 'Finish', |
| 81 | + }), |
| 82 | + [] |
| 83 | + ); |
| 84 | + |
| 85 | + return ( |
| 86 | + <InteractiveTutorialContainer |
| 87 | + translations={translations} |
| 88 | + stack={stack} |
| 89 | + initialTarget={TARGETS.Target1} |
| 90 | + Card={DescriptionCard} |
| 91 | + storage={storage} |
| 92 | + > |
| 93 | + {children} |
| 94 | + </InteractiveTutorialContainer> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +// !! Here you can override description card with your own |
| 99 | +const DescriptionCard = (props: DescriptionCardProps) => { |
| 100 | + const DescriptionButton = useCallback( |
| 101 | + ({ type, ...rest }: SharedDescriptionCardButtonProps) => ( |
| 102 | + <Button {...rest} color={type === 'prev' ? 'darkblue' : 'blue'} /> |
| 103 | + ), |
| 104 | + [] |
| 105 | + ); |
| 106 | + return <SharedDescriptionCard Button={DescriptionButton} {...props} />; |
| 107 | +}; |
| 108 | + |
| 109 | +``` |
| 110 | + |
| 111 | +2. Wrapping your accented components |
| 112 | +```tsx |
| 113 | +// any places in your app |
| 114 | +const target1 = useUiElement(TARGETS.Target1, (_) => addBorderRadius(_, 10)); |
| 115 | +<View |
| 116 | + style={[styles.column, styles.card]} |
| 117 | + ref={target1.ref} // !! necessary prop |
| 118 | + onLayout={target1.onLayout} // !! necessary prop |
| 119 | +> |
| 120 | + <Text>Target 1</Text> |
| 121 | +</View> |
| 122 | +``` |
| 123 | + |
| 124 | +3. Creating hook to run the tutorial |
| 125 | +```tsx |
| 126 | +import { useEffect } from 'react'; |
| 127 | +import { useInteractiveTutorial } from 'react-native-interactive-tutorial'; |
| 128 | + |
| 129 | +export default function useTutorialRunner() { |
| 130 | + const tutorial = useInteractiveTutorial(); |
| 131 | + |
| 132 | + useEffect(() => { |
| 133 | + if (tutorial.finished === false) { |
| 134 | + setTimeout(() => tutorial.show()); |
| 135 | + } |
| 136 | + }, [tutorial]); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +4. Wrapping your screen or app in the component from step1 and call the hook from step 3: |
| 141 | +```tsx |
| 142 | +function Root() { |
| 143 | + return ( |
| 144 | + <SafeAreaProvider> // !! it's also necessary |
| 145 | + <InteractiveTutorial> // !! created component from step 1 |
| 146 | + <App /> |
| 147 | + </InteractiveTutorial> |
| 148 | + </SafeAreaProvider> |
| 149 | + ); |
| 150 | +} |
| 151 | +``` |
| 152 | +Call the hook **inside** the App: |
| 153 | +```tsx |
| 154 | +function App() { |
| 155 | + useTutorialRunner(); |
| 156 | + ... |
| 157 | +} |
20 | 158 | ``` |
21 | 159 |
|
22 | 160 |
|
|
0 commit comments