rn-smart-tour 2.0.0 → 2.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -7
- package/lib/commonjs/core/TourEngine.js +198 -32
- package/lib/commonjs/core/TourEngine.js.map +1 -1
- package/lib/commonjs/core/controller.js +2 -0
- package/lib/commonjs/core/controller.js.map +1 -1
- package/lib/commonjs/core/measure.js +4 -3
- package/lib/commonjs/core/measure.js.map +1 -1
- package/lib/commonjs/index.js +14 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/react/TourOverlay.js +106 -59
- package/lib/commonjs/react/TourOverlay.js.map +1 -1
- package/lib/commonjs/react/TourOverlayHost.js +4 -1
- package/lib/commonjs/react/TourOverlayHost.js.map +1 -1
- package/lib/commonjs/react/TourProvider.js +31 -5
- package/lib/commonjs/react/TourProvider.js.map +1 -1
- package/lib/commonjs/react/TourScrollable.js +50 -4
- package/lib/commonjs/react/TourScrollable.js.map +1 -1
- package/lib/commonjs/react/accessibility.js +39 -0
- package/lib/commonjs/react/accessibility.js.map +1 -0
- package/lib/commonjs/react/context.js +7 -1
- package/lib/commonjs/react/context.js.map +1 -1
- package/lib/commonjs/react/hooks.js +1 -0
- package/lib/commonjs/react/hooks.js.map +1 -1
- package/lib/commonjs/react-navigation.js +122 -2
- package/lib/commonjs/react-navigation.js.map +1 -1
- package/lib/module/core/TourEngine.js +197 -32
- package/lib/module/core/TourEngine.js.map +1 -1
- package/lib/module/core/controller.js +2 -0
- package/lib/module/core/controller.js.map +1 -1
- package/lib/module/core/measure.js +4 -3
- package/lib/module/core/measure.js.map +1 -1
- package/lib/module/index.js +2 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/react/TourOverlay.js +106 -59
- package/lib/module/react/TourOverlay.js.map +1 -1
- package/lib/module/react/TourOverlayHost.js +5 -2
- package/lib/module/react/TourOverlayHost.js.map +1 -1
- package/lib/module/react/TourProvider.js +32 -6
- package/lib/module/react/TourProvider.js.map +1 -1
- package/lib/module/react/TourScrollable.js +51 -5
- package/lib/module/react/TourScrollable.js.map +1 -1
- package/lib/module/react/accessibility.js +33 -0
- package/lib/module/react/accessibility.js.map +1 -0
- package/lib/module/react/context.js +6 -0
- package/lib/module/react/context.js.map +1 -1
- package/lib/module/react/hooks.js +1 -0
- package/lib/module/react/hooks.js.map +1 -1
- package/lib/module/react-navigation.js +119 -3
- package/lib/module/react-navigation.js.map +1 -1
- package/lib/typescript/commonjs/src/core/TourEngine.d.ts +47 -2
- package/lib/typescript/commonjs/src/core/TourEngine.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/core/controller.d.ts +8 -1
- package/lib/typescript/commonjs/src/core/controller.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/core/measure.d.ts +2 -1
- package/lib/typescript/commonjs/src/core/measure.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/index.d.ts +3 -3
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/TourOverlay.d.ts +9 -1
- package/lib/typescript/commonjs/src/react/TourOverlay.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/TourOverlayHost.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/TourProvider.d.ts +9 -0
- package/lib/typescript/commonjs/src/react/TourProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/TourScrollable.d.ts +24 -1
- package/lib/typescript/commonjs/src/react/TourScrollable.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/accessibility.d.ts +5 -0
- package/lib/typescript/commonjs/src/react/accessibility.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/react/context.d.ts +5 -0
- package/lib/typescript/commonjs/src/react/context.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react/hooks.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/react-navigation.d.ts +63 -0
- package/lib/typescript/commonjs/src/react-navigation.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types.d.ts +27 -0
- package/lib/typescript/commonjs/src/types.d.ts.map +1 -1
- package/lib/typescript/module/src/core/TourEngine.d.ts +47 -2
- package/lib/typescript/module/src/core/TourEngine.d.ts.map +1 -1
- package/lib/typescript/module/src/core/controller.d.ts +8 -1
- package/lib/typescript/module/src/core/controller.d.ts.map +1 -1
- package/lib/typescript/module/src/core/measure.d.ts +2 -1
- package/lib/typescript/module/src/core/measure.d.ts.map +1 -1
- package/lib/typescript/module/src/index.d.ts +3 -3
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/react/TourOverlay.d.ts +9 -1
- package/lib/typescript/module/src/react/TourOverlay.d.ts.map +1 -1
- package/lib/typescript/module/src/react/TourOverlayHost.d.ts.map +1 -1
- package/lib/typescript/module/src/react/TourProvider.d.ts +9 -0
- package/lib/typescript/module/src/react/TourProvider.d.ts.map +1 -1
- package/lib/typescript/module/src/react/TourScrollable.d.ts +24 -1
- package/lib/typescript/module/src/react/TourScrollable.d.ts.map +1 -1
- package/lib/typescript/module/src/react/accessibility.d.ts +5 -0
- package/lib/typescript/module/src/react/accessibility.d.ts.map +1 -0
- package/lib/typescript/module/src/react/context.d.ts +5 -0
- package/lib/typescript/module/src/react/context.d.ts.map +1 -1
- package/lib/typescript/module/src/react/hooks.d.ts.map +1 -1
- package/lib/typescript/module/src/react-navigation.d.ts +63 -0
- package/lib/typescript/module/src/react-navigation.d.ts.map +1 -1
- package/lib/typescript/module/src/types.d.ts +27 -0
- package/lib/typescript/module/src/types.d.ts.map +1 -1
- package/package.json +18 -4
- package/src/core/TourEngine.ts +217 -31
- package/src/core/controller.ts +9 -0
- package/src/core/measure.ts +6 -2
- package/src/index.ts +3 -3
- package/src/react/TourOverlay.tsx +99 -49
- package/src/react/TourOverlayHost.tsx +4 -2
- package/src/react/TourProvider.tsx +36 -4
- package/src/react/TourScrollable.tsx +61 -4
- package/src/react/accessibility.ts +39 -0
- package/src/react/context.ts +9 -0
- package/src/react/hooks.ts +1 -0
- package/src/react-navigation.tsx +156 -1
- package/src/types.ts +24 -0
package/README.md
CHANGED
|
@@ -106,6 +106,27 @@ const OrdersScreen = () => (
|
|
|
106
106
|
|
|
107
107
|
Not using React Navigation? `TourScope` does the same with an `active` prop you control: `<TourScope active={isVisible}>`.
|
|
108
108
|
|
|
109
|
+
**Drawer.** Drawer content stays mounted, off-screen, while the drawer is closed. Wrap it in `DrawerTourScope` so its targets only count once the drawer has finished sliding open (React Navigation drawer v6/v7; no extra dependencies). For a drawer that is always shown (`drawerType: 'permanent'`), pass `permanent`.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
import { DrawerTourScope, useStartTourAfterDrawerClose } from 'rn-smart-tour/react-navigation';
|
|
113
|
+
|
|
114
|
+
const DrawerContent = (props: DrawerContentComponentProps) => {
|
|
115
|
+
// "App Tour" in the menu: start once the drawer has slid away, so the tour never dims it mid-slide.
|
|
116
|
+
const startAfterClose = useStartTourAfterDrawerClose(props.navigation);
|
|
117
|
+
const replay = () => {
|
|
118
|
+
startAfterClose('dashboard', { force: true });
|
|
119
|
+
props.navigation.closeDrawer();
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<DrawerTourScope navigation={props.navigation} permanent={isWideTablet}>
|
|
124
|
+
<DrawerContentScrollView {...props}>{/* items with TourTargets */}</DrawerContentScrollView>
|
|
125
|
+
</DrawerTourScope>
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
109
130
|
### 5. Lists (Optional)
|
|
110
131
|
To let tours scroll a target into view, use `TourScrollView` instead of `ScrollView`, or wrap a `FlatList` / `SectionList` in `TourScrollable`:
|
|
111
132
|
|
|
@@ -127,7 +148,32 @@ const listRef = useRef<FlatList>(null);
|
|
|
127
148
|
</TourScrollable>
|
|
128
149
|
```
|
|
129
150
|
|
|
130
|
-
|
|
151
|
+
**Lists inside lists.** Wrap each scroll container (e.g. a horizontal row of cards inside a vertical page). The tour scrolls the innermost one first, then each one around it, so the target ends up on screen:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
<TourScrollView>
|
|
155
|
+
<TourScrollable scrollRef={cardsRef} horizontal>
|
|
156
|
+
<FlatList ref={cardsRef} horizontal … />
|
|
157
|
+
</TourScrollable>
|
|
158
|
+
</TourScrollView>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Items that aren't rendered yet.** A `FlatList` only renders the items near the screen. To point a step at an item further down, name the list and give the step the item's index: if the item isn't rendered when the step starts, the list is scrolled to it first.
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
<TourScrollable scrollRef={listRef} name="customers">
|
|
165
|
+
<FlatList
|
|
166
|
+
ref={listRef}
|
|
167
|
+
onScrollToIndexFailed={scrollToIndexFailedHandler(listRef)} // only without getItemLayout
|
|
168
|
+
…
|
|
169
|
+
/>
|
|
170
|
+
</TourScrollable>
|
|
171
|
+
|
|
172
|
+
// in the tour
|
|
173
|
+
{ targetId: 'customer-40', title: '…', description: '…', scrollToIndex: { list: 'customers', index: 40 } }
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
A `FlatList` without `getItemLayout` can't jump straight to an item it hasn't measured; `scrollToIndexFailedHandler` (exported by the library) jumps close using the average item size and tries again. FlashList needs neither.
|
|
131
177
|
|
|
132
178
|
### 6. Tours Across Screens (Optional)
|
|
133
179
|
`beforeEnter` runs before a step looks for its target, e.g. to open the screen it's on. The step then waits for the target to appear on the focused screen.
|
|
@@ -176,6 +222,16 @@ const mmkvAdapter = {
|
|
|
176
222
|
|
|
177
223
|
Storage is read once on mount. Calls to `startTour`, `resetTour` and `resetAll` made before it has loaded are queued and run once it has, so an already-seen tour is never shown by mistake.
|
|
178
224
|
|
|
225
|
+
**One set per user.** By default the device keeps one set of seen tours, so on a shared or handed-over device the next person never sees them. Pass the signed-in user's id:
|
|
226
|
+
|
|
227
|
+
```tsx
|
|
228
|
+
<TourProvider tours={TOURS} storageAdapter={mmkvAdapter} userId={employee?.id ?? null}>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Each user's seen tours are stored separately. When `userId` changes (sign-in, sign-out, another user), a running tour ends without being marked seen and the new user's seen tours are loaded; calls made meanwhile are queued as above. The first user on a device that already has seen tours stored without a user takes those over, so an existing user isn't shown every tour again after the upgrade; later users start fresh. Outside React: `createTourController({ userId })` and `controller.setUserId(id)`.
|
|
232
|
+
|
|
233
|
+
**Show a changed tour again.** Give a tour a `version` and raise it when its content changes enough to show again. A tour is seen only if the version the user saw is at least the current one. A tour without `version` counts as version 1, so adding `version: 1` changes nothing and `version: 2` shows it once more.
|
|
234
|
+
|
|
179
235
|
### 8. Your Own Tooltip Card (Optional)
|
|
180
236
|
Render the card with your design system and theme. It is placed beside the target for you (below by default, flipping above when there's no room), using its measured height.
|
|
181
237
|
|
|
@@ -286,7 +342,7 @@ Pass `onEvent` to see how tours perform: how many users finish each walkthrough,
|
|
|
286
342
|
<TourProvider tours={TOURS} onEvent={event => analyticsQueue.push(event)}>
|
|
287
343
|
```
|
|
288
344
|
|
|
289
|
-
Every event has `type`, `runId` (the same for every event of one run of a tour, start to end), `tourId`, `tourKind` and `timestamp`, plus:
|
|
345
|
+
Every event has `type`, `eventId` (unique: drop duplicates when a batch is uploaded again after a failed request), `runId` (the same for every event of one run of a tour, start to end), `tourId`, `tourKind` and `timestamp`, plus:
|
|
290
346
|
|
|
291
347
|
| `type` | When | Extra fields |
|
|
292
348
|
|:---|:---|:---|
|
|
@@ -303,6 +359,23 @@ Every event has `type`, `runId` (the same for every event of one run of a tour,
|
|
|
303
359
|
|
|
304
360
|
---
|
|
305
361
|
|
|
362
|
+
### 13. Accessibility
|
|
363
|
+
Works with VoiceOver and TalkBack and the system "reduce motion" setting, with nothing to set up:
|
|
364
|
+
|
|
365
|
+
- **Each step is announced** when it appears (once per step, not again when it is re-measured): title, description, what to do on an action step, and "Step 2 of 5". This also covers custom cards. To translate or change it, pass `getStepAnnouncement`; return `null` to say nothing for a step:
|
|
366
|
+
|
|
367
|
+
```tsx
|
|
368
|
+
<TourProvider
|
|
369
|
+
tours={TOURS}
|
|
370
|
+
getStepAnnouncement={({ step, stepIndex, stepCount }) =>
|
|
371
|
+
t('tour.announcement', { title: step.title, text: step.description, n: stepIndex + 1, total: stepCount })
|
|
372
|
+
}>
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
`defaultStepAnnouncement(props)` is the built-in text, if you only want to add to it.
|
|
376
|
+
- On iOS, VoiceOver stays inside the overlay while a step is shown. The built-in card marks its title as a heading and its buttons as buttons, and reads the position as "Step 2 of 5".
|
|
377
|
+
- **Reduce motion** (iOS Reduce Motion, Android "Remove animations"): the overlay appears without fading in, and targets are scrolled into view without animating.
|
|
378
|
+
|
|
306
379
|
## 🧠 Technical Architecture
|
|
307
380
|
|
|
308
381
|
<details>
|
|
@@ -331,7 +404,7 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
331
404
|
<details>
|
|
332
405
|
<summary><b>Tour Lifecycle Rules</b></summary>
|
|
333
406
|
|
|
334
|
-
- **Skip**, **Finish** and the **Android back button** end the tour and mark it as seen.
|
|
407
|
+
- **Skip**, **Finish** and the **Android back button** end the tour and mark it as seen. The tour takes the back button only while a step is shown (otherwise it goes to the app as usual), ahead of the app's own back handlers, including ones a screen added during the tour. For a step inside an app popup, back goes to the popup (its `onRequestClose`): the popup closes, and the step's target is then missing as usual: a walkthrough pauses with the Resume chip, a tour ends unseen.
|
|
335
408
|
- If a step's target is not on screen within `missingTargetTimeoutMs` (default **3000ms**, or the step's `targetTimeoutMs`), the tour ends **without** being marked as seen, moves to the next step with `onMissingTarget: 'skip'`, or pauses with a Resume chip with `onMissingTarget: 'pause'` (the default for walkthroughs). A warning is logged in development.
|
|
336
409
|
- If the target of the step being shown disappears (unmounts, or its screen loses focus), the step is hidden until it comes back, within the same timeout.
|
|
337
410
|
- While moving to the next step, the current one stays on screen until the next target is measured, and extra taps on **Next** are ignored.
|
|
@@ -347,6 +420,7 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
347
420
|
|:---|:---|:---|:---|
|
|
348
421
|
| `tours` | `Record<string, Tour>` | — | All tours, keyed by id. |
|
|
349
422
|
| `storageAdapter` | `StorageAdapter` | — | Persists seen tours (AsyncStorage, MMKV, …). Read once on mount. |
|
|
423
|
+
| `userId` | `string \| null` | — | The signed-in user: each user has their own seen tours. Changing it ends a running tour (unseen) and loads the new user's. See [Remember Seen Tours](#7-remember-seen-tours-optional). |
|
|
350
424
|
| `missingTargetTimeoutMs` | `number` | `3000` | How long a step waits for its target. |
|
|
351
425
|
| `controller` | `TourController` | — | From `createTourController`, instead of `tours` / `storageAdapter`. |
|
|
352
426
|
| `renderTooltip` | `(props: TourTooltipProps) => ReactNode` | built-in card | Draws the tooltip card. Receives the step, its index and count, and `next` / `prev` / `skip`. |
|
|
@@ -359,12 +433,14 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
359
433
|
| `onEvent` | `(event: TourEvent) => void` | — | Analytics: every start, step shown, completion, exit (and why), block, pause, resume and missing target. See [Analytics](#12-analytics-optional). |
|
|
360
434
|
| `renderResumeChip` | `(props: ResumeChipProps) => ReactNode` | built-in chip | Draws the "Resume" chip shown while a tour is paused (`tour`, `resume`, `dismiss`). |
|
|
361
435
|
| `highlightColor` | `string` | `'#FFFFFF'` | Ring around an element the user should tap. |
|
|
436
|
+
| `getStepAnnouncement` | `(props: TourTooltipProps) => string \| null` | `defaultStepAnnouncement` | What screen readers say when a step appears. See [Accessibility](#13-accessibility). |
|
|
362
437
|
|
|
363
438
|
### Tour Configuration
|
|
364
439
|
| Property | Type | Description |
|
|
365
440
|
|:---|:---|:---|
|
|
366
441
|
| `id` | `string` | Unique identifier for caching. |
|
|
367
442
|
| `autoStart` | `boolean` | Start the first time the first step with a target appears. |
|
|
443
|
+
| `version` | `number` | Content version (from 1). Raise it to show a changed tour again to users who saw an older one. |
|
|
368
444
|
| `steps` | `TourStep[]` | Sequence of highlight steps. |
|
|
369
445
|
| `kind` | `'tour' \| 'walkthrough'` | Walkthroughs pause on a missing target and say "Exit". Default `'tour'`. |
|
|
370
446
|
| `title` / `description` / `keywords` | `string` / `string` / `string[]` | Shown and searched by `useTourCatalog`. |
|
|
@@ -383,6 +459,7 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
383
459
|
| `when` | `(context) => boolean` | — | Show the step only when this returns true. |
|
|
384
460
|
| `onMissingTarget` | `'end' \| 'skip' \| 'pause'` | `'end'` (`'pause'` for walkthroughs) | What happens if the target isn't found in time. |
|
|
385
461
|
| `targetTimeoutMs` | `number` | provider's | Per-step override of `missingTargetTimeoutMs`. |
|
|
462
|
+
| `scrollToIndex` | `{ list, index }` | — | The target is item `index` of the list named `list` (`TourScrollable name`): if it isn't rendered, the list is scrolled to it first. |
|
|
386
463
|
|
|
387
464
|
### TourTarget
|
|
388
465
|
| Property | Type | Description |
|
|
@@ -401,11 +478,14 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
401
478
|
|:---|:---|:---|
|
|
402
479
|
| `TourScope` | `active?: boolean` (default `true`) | Targets inside only count while `active` (and every parent scope is active). |
|
|
403
480
|
| `NavigationTourScope` | — | From `rn-smart-tour/react-navigation`. Active while the screen is focused and not animating. |
|
|
481
|
+
| `DrawerTourScope` | `navigation`, `permanent?` | From `rn-smart-tour/react-navigation`. For drawer content: active once the drawer has finished opening, inactive as it starts to close; always active when `permanent`. |
|
|
482
|
+
|
|
483
|
+
`useStartTourAfterDrawerClose(navigation, { fallbackMs? })` (also from `rn-smart-tour/react-navigation`) returns `(tourId, options?) => void`: it starts the tour once the drawer has finished closing, or after `fallbackMs` (default 600) if the drawer reports no close. Call it, then close the drawer or navigate.
|
|
404
484
|
|
|
405
485
|
### TourScrollable / TourScrollView
|
|
406
486
|
| Component | Props | Description |
|
|
407
487
|
|:---|:---|:---|
|
|
408
|
-
| `TourScrollable` | `scrollRef`, `horizontal?` | Lets tours scroll targets inside the referenced `ScrollView` / `FlatList` / `SectionList`. |
|
|
488
|
+
| `TourScrollable` | `scrollRef`, `horizontal?`, `name?` | Lets tours scroll targets inside the referenced `ScrollView` / `FlatList` / `SectionList`. Nested ones are scrolled innermost first. `name` is for steps with `scrollToIndex`. |
|
|
409
489
|
| `TourScrollView` | `ScrollViewProps` | A `ScrollView` already wrapped in `TourScrollable`. A `ref` is forwarded. |
|
|
410
490
|
|
|
411
491
|
### `useTour()` Hook
|
|
@@ -422,6 +502,7 @@ A tour that is stopped (finished, skipped, or ended because a target was missing
|
|
|
422
502
|
| `seenTours` | `Record<string, boolean>` of tours already seen. |
|
|
423
503
|
| `emit(event)` | Report an app event, for steps with `advanceOn: { event }`. |
|
|
424
504
|
| `pauseTour()` / `resumeTour()` | Put the running tour on hold / continue it. |
|
|
505
|
+
| `setUserId(id)` | Switch whose seen tours are used (same as changing the provider's `userId`). |
|
|
425
506
|
|
|
426
507
|
`useTourState(selector)` subscribes to any slice of the tour state and re-renders only when it changes.
|
|
427
508
|
|
|
@@ -442,9 +523,9 @@ Behaviour changes:
|
|
|
442
523
|
|
|
443
524
|
## ⚠️ Known Limitations
|
|
444
525
|
|
|
445
|
-
- **
|
|
446
|
-
- **Sliding elements**: a target moved by a transform animation (a
|
|
447
|
-
- **
|
|
526
|
+
- **Scroll containers must be wrapped**: only containers wrapped in `TourScrollable` / `TourScrollView` are scrolled.
|
|
527
|
+
- **Sliding elements**: a target moved by a transform animation (a sheet sliding in) is measured once it holds still for 3 frames, or after 600 ms. Put such targets in a `TourScope` that becomes active once the animation has finished. For a React Navigation drawer, use `DrawerTourScope`.
|
|
528
|
+
- **Items that aren't rendered yet**: reachable with a step's `scrollToIndex` when you know the item's index; otherwise only once the item has rendered.
|
|
448
529
|
|
|
449
530
|
---
|
|
450
531
|
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.TourEngine = exports.STORAGE_KEY = exports.PRESS_SLOP = void 0;
|
|
6
|
+
exports.userStorageKey = exports.TourEngine = exports.STORAGE_KEY = exports.PRESS_SLOP = exports.MIGRATED_KEY = void 0;
|
|
7
7
|
var _store = require("./store.js");
|
|
8
8
|
var _measure = require("./measure.js");
|
|
9
9
|
/** A `TourScope`: its targets only count while it (and every parent scope) is ready. */
|
|
10
10
|
|
|
11
|
-
/**
|
|
11
|
+
/** A scroll container registered with `TourScrollable`, used to scroll targets into view. */
|
|
12
12
|
|
|
13
13
|
/** A `TourOverlayHost` (e.g. inside an app popup): steps on its targets are drawn there. */
|
|
14
14
|
|
|
@@ -20,12 +20,37 @@ var _measure = require("./measure.js");
|
|
|
20
20
|
/** A `TourEvent` without the fields the engine fills in. */
|
|
21
21
|
|
|
22
22
|
let runCounter = 0;
|
|
23
|
-
/** Unique enough
|
|
24
|
-
const
|
|
23
|
+
/** Unique enough for run and event ids on a backend (not a secure id). */
|
|
24
|
+
const createId = () => `${Date.now().toString(36)}-${(++runCounter).toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
25
25
|
|
|
26
26
|
/** A press on a target only counts if the finger moved less than this (not a scroll). */
|
|
27
27
|
const PRESS_SLOP = exports.PRESS_SLOP = 10;
|
|
28
|
+
|
|
29
|
+
/** Seen tours of the device (no `userId`). Kept from 1.x: existing installs depend on it. */
|
|
28
30
|
const STORAGE_KEY = exports.STORAGE_KEY = '@rn-dap:seen_tours';
|
|
31
|
+
/** Seen tours of one user. */
|
|
32
|
+
const userStorageKey = userId => `${STORAGE_KEY}:user:${userId}`;
|
|
33
|
+
/** Set once the device's seen tours have been copied to a user, so only the first user inherits them. */
|
|
34
|
+
exports.userStorageKey = userStorageKey;
|
|
35
|
+
const MIGRATED_KEY = exports.MIGRATED_KEY = `${STORAGE_KEY}:migrated`;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What is stored per tour: `true` (seen; written before tours had versions, and for
|
|
39
|
+
* tours without one) or the version that was seen.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
const parseStoredSeen = raw => {
|
|
43
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
44
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
|
|
45
|
+
const stored = {};
|
|
46
|
+
for (const [id, value] of Object.entries(parsed)) {
|
|
47
|
+
if (value === true || typeof value === 'number' && Number.isFinite(value)) stored[id] = value;
|
|
48
|
+
}
|
|
49
|
+
return stored;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Seen if the version seen (`true` = 1) is at least the tour's (none = 1). */
|
|
53
|
+
const isSeenVersion = (value, tour) => value !== undefined && (value === true ? 1 : value) >= (tour?.version ?? 1);
|
|
29
54
|
const DEFAULT_MISSING_TARGET_TIMEOUT_MS = 3000;
|
|
30
55
|
const DEFAULT_AUTO_START_DELAY_MS = 300;
|
|
31
56
|
/** Let an animated scroll run this long before waiting for the target to stop moving. */
|
|
@@ -45,16 +70,25 @@ const isScopeReady = scope => !scope || scope.ready && isScopeReady(scope.parent
|
|
|
45
70
|
* moving), and again when something may have moved it while it is shown.
|
|
46
71
|
*/
|
|
47
72
|
class TourEngine {
|
|
73
|
+
reduceMotion = false;
|
|
48
74
|
/** The running tour's run, for analytics. */
|
|
49
75
|
run = null;
|
|
50
76
|
/** The step token whose step_shown event was sent (re-presenting the same step sends none). */
|
|
51
77
|
shownToken = -1;
|
|
52
78
|
targets = new Map();
|
|
79
|
+
/** Lists registered with `TourScrollable name`, for steps with `scrollToIndex`. */
|
|
80
|
+
lists = new Map();
|
|
81
|
+
/** The step token whose `scrollToIndex` was done (once per step). */
|
|
82
|
+
indexScrolledToken = -1;
|
|
53
83
|
/** Not auto-started again until the next launch (or resetTour); startTour still works. */
|
|
54
84
|
stoppedThisSession = new Set();
|
|
55
85
|
/** Work requested before storage finished loading; run in order once it has. */
|
|
56
86
|
pendingOps = [];
|
|
57
87
|
storageLoadStarted = false;
|
|
88
|
+
/** Changes with every load, so a load for a previous user is thrown away. */
|
|
89
|
+
loadGeneration = 0;
|
|
90
|
+
/** As stored (with versions); `state.seen` is derived from it and the current tours. */
|
|
91
|
+
storedSeen = {};
|
|
58
92
|
warnedDuplicates = new Set();
|
|
59
93
|
|
|
60
94
|
/** Changes whenever the current step changes, cancelling async work for the old one. */
|
|
@@ -66,9 +100,11 @@ class TourEngine {
|
|
|
66
100
|
constructor(options) {
|
|
67
101
|
this.tours = options.tours;
|
|
68
102
|
this.storage = options.storage;
|
|
103
|
+
this.userId = options.userId ?? null;
|
|
69
104
|
this.missingTargetTimeoutMs = options.missingTargetTimeoutMs ?? DEFAULT_MISSING_TARGET_TIMEOUT_MS;
|
|
70
105
|
this.autoStartDelayMs = options.autoStartDelayMs ?? DEFAULT_AUTO_START_DELAY_MS;
|
|
71
106
|
this.context = options.context ?? {};
|
|
107
|
+
this.reduceMotion = options.reduceMotion ?? false;
|
|
72
108
|
this.onStartBlocked = options.onStartBlocked;
|
|
73
109
|
this.onEvent = options.onEvent;
|
|
74
110
|
this.store = (0, _store.createStore)({
|
|
@@ -95,13 +131,16 @@ class TourEngine {
|
|
|
95
131
|
setOptions(options) {
|
|
96
132
|
if (options.missingTargetTimeoutMs !== undefined) this.missingTargetTimeoutMs = options.missingTargetTimeoutMs;
|
|
97
133
|
if (options.autoStartDelayMs !== undefined) this.autoStartDelayMs = options.autoStartDelayMs;
|
|
134
|
+
if (options.reduceMotion !== undefined) this.reduceMotion = options.reduceMotion;
|
|
98
135
|
if (options.context !== undefined) this.context = options.context;
|
|
99
136
|
if ('onStartBlocked' in options) this.onStartBlocked = options.onStartBlocked;
|
|
100
137
|
if ('onEvent' in options) this.onEvent = options.onEvent;
|
|
101
138
|
if (options.tours && options.tours !== this.tours) {
|
|
102
139
|
this.tours = options.tours;
|
|
103
140
|
this.store.setState({
|
|
104
|
-
toursRevision: this.getState().toursRevision + 1
|
|
141
|
+
toursRevision: this.getState().toursRevision + 1,
|
|
142
|
+
// A tour's version may have changed.
|
|
143
|
+
seen: this.deriveSeen()
|
|
105
144
|
});
|
|
106
145
|
const {
|
|
107
146
|
tourId
|
|
@@ -116,23 +155,57 @@ class TourEngine {
|
|
|
116
155
|
|
|
117
156
|
// ---------------------------------------------------------------- storage
|
|
118
157
|
|
|
119
|
-
/** Reads seen tours once. Safe to call more than once. */
|
|
158
|
+
/** Reads seen tours once (for the current user). Safe to call more than once. */
|
|
120
159
|
async loadStorage() {
|
|
121
|
-
|
|
122
|
-
if (!adapter || this.storageLoadStarted) return;
|
|
160
|
+
if (!this.storage || this.storageLoadStarted) return;
|
|
123
161
|
this.storageLoadStarted = true;
|
|
162
|
+
await this.load();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Switches whose seen tours are used. A running tour ends without being marked
|
|
167
|
+
* seen; tours the new user hasn't seen can auto-start again.
|
|
168
|
+
*/
|
|
169
|
+
setUserId = userId => {
|
|
170
|
+
const next = userId ?? null;
|
|
171
|
+
if (next === this.userId) return;
|
|
172
|
+
this.userId = next;
|
|
173
|
+
if (this.getState().tourId) this.finish(false, 'app');
|
|
174
|
+
this.stoppedThisSession.clear();
|
|
175
|
+
this.storedSeen = {};
|
|
176
|
+
if (!this.storage) {
|
|
177
|
+
// Nothing is kept between users without storage.
|
|
178
|
+
this.store.setState({
|
|
179
|
+
seen: {}
|
|
180
|
+
});
|
|
181
|
+
this.scheduleAutoStart();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// Before the provider has asked for the first load, there is nothing to reload.
|
|
185
|
+
if (!this.storageLoadStarted) return;
|
|
186
|
+
cancelTimer(this.autoStartTimer);
|
|
187
|
+
this.store.setState({
|
|
188
|
+
seen: {},
|
|
189
|
+
storageReady: false
|
|
190
|
+
});
|
|
191
|
+
this.load();
|
|
192
|
+
};
|
|
193
|
+
async load() {
|
|
194
|
+
const adapter = this.storage;
|
|
195
|
+
if (!adapter) return;
|
|
196
|
+
const generation = ++this.loadGeneration;
|
|
197
|
+
const userId = this.userId;
|
|
124
198
|
let stored = {};
|
|
125
199
|
try {
|
|
126
|
-
|
|
127
|
-
const parsed = raw ? JSON.parse(raw) : null;
|
|
128
|
-
if (parsed && typeof parsed === 'object') {
|
|
129
|
-
stored = parsed;
|
|
130
|
-
}
|
|
200
|
+
stored = (await this.readStoredSeen(adapter, userId)) ?? {};
|
|
131
201
|
} catch (e) {
|
|
132
202
|
console.error('[rn-smart-tour] failed to load storage', e);
|
|
133
203
|
}
|
|
204
|
+
// The user changed while this was loading: the newer load applies instead.
|
|
205
|
+
if (generation !== this.loadGeneration) return;
|
|
206
|
+
this.storedSeen = stored;
|
|
134
207
|
this.store.setState({
|
|
135
|
-
seen:
|
|
208
|
+
seen: this.deriveSeen(),
|
|
136
209
|
storageReady: true
|
|
137
210
|
});
|
|
138
211
|
const ops = this.pendingOps;
|
|
@@ -140,6 +213,38 @@ class TourEngine {
|
|
|
140
213
|
ops.forEach(op => op());
|
|
141
214
|
this.scheduleAutoStart();
|
|
142
215
|
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The user's seen tours. The first user to sign in on a device that already has
|
|
219
|
+
* seen tours stored without a user (1.x, or 2.0 without `userId`) takes them over,
|
|
220
|
+
* so the device's owner isn't shown every tour again; later users start fresh.
|
|
221
|
+
*/
|
|
222
|
+
async readStoredSeen(adapter, userId) {
|
|
223
|
+
if (userId === null) return parseStoredSeen(await adapter.getItem(STORAGE_KEY));
|
|
224
|
+
const key = userStorageKey(userId);
|
|
225
|
+
const own = parseStoredSeen(await adapter.getItem(key));
|
|
226
|
+
if (own) return own;
|
|
227
|
+
if (await adapter.getItem(MIGRATED_KEY)) return null;
|
|
228
|
+
const device = parseStoredSeen(await adapter.getItem(STORAGE_KEY));
|
|
229
|
+
const hasDevice = !!device && Object.keys(device).length > 0;
|
|
230
|
+
// A failed write only means the copy is tried again next launch.
|
|
231
|
+
try {
|
|
232
|
+
await adapter.setItem(MIGRATED_KEY, '1');
|
|
233
|
+
if (hasDevice) await adapter.setItem(key, JSON.stringify(device));
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.error('[rn-smart-tour] failed to save storage', e);
|
|
236
|
+
}
|
|
237
|
+
return hasDevice ? device : null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** `state.seen`: tours whose seen version is at least their current one. */
|
|
241
|
+
deriveSeen() {
|
|
242
|
+
const seen = {};
|
|
243
|
+
for (const [id, value] of Object.entries(this.storedSeen)) {
|
|
244
|
+
if (isSeenVersion(value, this.tours[id])) seen[id] = true;
|
|
245
|
+
}
|
|
246
|
+
return seen;
|
|
247
|
+
}
|
|
143
248
|
whenStorageReady(op) {
|
|
144
249
|
if (this.getState().storageReady) {
|
|
145
250
|
op();
|
|
@@ -148,12 +253,14 @@ class TourEngine {
|
|
|
148
253
|
}
|
|
149
254
|
}
|
|
150
255
|
persistSeen(next) {
|
|
256
|
+
this.storedSeen = next;
|
|
151
257
|
this.store.setState({
|
|
152
|
-
seen:
|
|
258
|
+
seen: this.deriveSeen()
|
|
153
259
|
});
|
|
154
260
|
const adapter = this.storage;
|
|
155
261
|
if (adapter) {
|
|
156
|
-
|
|
262
|
+
const key = this.userId === null ? STORAGE_KEY : userStorageKey(this.userId);
|
|
263
|
+
Promise.resolve().then(() => adapter.setItem(key, JSON.stringify(next))).catch(e => {
|
|
157
264
|
console.error('[rn-smart-tour] failed to save storage', e);
|
|
158
265
|
});
|
|
159
266
|
}
|
|
@@ -177,6 +284,21 @@ class TourEngine {
|
|
|
177
284
|
};
|
|
178
285
|
}
|
|
179
286
|
|
|
287
|
+
/** Registers a named list (`TourScrollable name`); returns its unregister function. */
|
|
288
|
+
registerList(name, handle) {
|
|
289
|
+
const list = this.lists.get(name) ?? [];
|
|
290
|
+
list.push(handle);
|
|
291
|
+
this.lists.set(name, list);
|
|
292
|
+
this.scrollToIndexIfNeeded();
|
|
293
|
+
return () => {
|
|
294
|
+
const current = this.lists.get(name);
|
|
295
|
+
if (!current) return;
|
|
296
|
+
const index = current.indexOf(handle);
|
|
297
|
+
if (index >= 0) current.splice(index, 1);
|
|
298
|
+
if (current.length === 0) this.lists.delete(name);
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
180
302
|
/** A TourScope became ready or stopped being ready. */
|
|
181
303
|
notifyScopeChanged() {
|
|
182
304
|
this.onTargetsChanged(null);
|
|
@@ -235,6 +357,9 @@ class TourEngine {
|
|
|
235
357
|
presented: null
|
|
236
358
|
});
|
|
237
359
|
this.armMissingTimer(this.stepToken, step);
|
|
360
|
+
} else {
|
|
361
|
+
// e.g. the list's screen became ready: the item may need scrolling to.
|
|
362
|
+
this.scrollToIndexIfNeeded();
|
|
238
363
|
}
|
|
239
364
|
}
|
|
240
365
|
|
|
@@ -262,7 +387,7 @@ class TourEngine {
|
|
|
262
387
|
this.track(tourId, {
|
|
263
388
|
type: 'tour_blocked',
|
|
264
389
|
reason: allowed
|
|
265
|
-
},
|
|
390
|
+
}, createId());
|
|
266
391
|
this.onStartBlocked?.(tour, allowed);
|
|
267
392
|
}
|
|
268
393
|
return;
|
|
@@ -277,7 +402,7 @@ class TourEngine {
|
|
|
277
402
|
});
|
|
278
403
|
}
|
|
279
404
|
this.run = {
|
|
280
|
-
id:
|
|
405
|
+
id: createId(),
|
|
281
406
|
startedAt: Date.now()
|
|
282
407
|
};
|
|
283
408
|
this.store.setState({
|
|
@@ -431,12 +556,9 @@ class TourEngine {
|
|
|
431
556
|
resetTour = tourId => {
|
|
432
557
|
this.whenStorageReady(() => {
|
|
433
558
|
this.stoppedThisSession.delete(tourId);
|
|
434
|
-
|
|
435
|
-
seen
|
|
436
|
-
} = this.getState();
|
|
437
|
-
if (seen[tourId]) {
|
|
559
|
+
if (tourId in this.storedSeen) {
|
|
438
560
|
const next = {
|
|
439
|
-
...
|
|
561
|
+
...this.storedSeen
|
|
440
562
|
};
|
|
441
563
|
delete next[tourId];
|
|
442
564
|
this.persistSeen(next);
|
|
@@ -574,12 +696,17 @@ class TourEngine {
|
|
|
574
696
|
this.store.setState({
|
|
575
697
|
presented: null
|
|
576
698
|
});
|
|
699
|
+
this.scrollToIndexIfNeeded();
|
|
577
700
|
return;
|
|
578
701
|
}
|
|
579
702
|
const ref = handle.ref.current;
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
703
|
+
// Every scroll container the target sits in, innermost first (e.g. a horizontal row,
|
|
704
|
+
// then the vertical page around it). Each waits for the previous scroll to settle,
|
|
705
|
+
// since it decides from where the target is on screen.
|
|
706
|
+
for (let scrollable = handle.scrollable; scrollable; scrollable = scrollable.parent ?? null) {
|
|
707
|
+
const scrolled = await (0, _measure.scrollIntoView)(ref, scrollable.ref.current, {
|
|
708
|
+
horizontal: scrollable.horizontal,
|
|
709
|
+
animated: !this.reduceMotion
|
|
583
710
|
});
|
|
584
711
|
if (cancelled()) return;
|
|
585
712
|
if (scrolled) {
|
|
@@ -587,7 +714,8 @@ class TourEngine {
|
|
|
587
714
|
if (!this.isCurrentStepPresented()) this.store.setState({
|
|
588
715
|
presented: null
|
|
589
716
|
});
|
|
590
|
-
|
|
717
|
+
// An instant scroll only needs a frame to lay out; measureStable then waits for it to hold still.
|
|
718
|
+
await (this.reduceMotion ? (0, _measure.nextFrame)() : (0, _measure.delay)(SCROLL_SETTLE_MS));
|
|
591
719
|
if (cancelled()) return;
|
|
592
720
|
}
|
|
593
721
|
}
|
|
@@ -620,11 +748,48 @@ class TourEngine {
|
|
|
620
748
|
});
|
|
621
749
|
this.trackStepShown(token);
|
|
622
750
|
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* For a step with `scrollToIndex` whose target isn't rendered: scrolls the named list
|
|
754
|
+
* to the item, once per step, so it renders (its TourTarget then registers and the
|
|
755
|
+
* step reveals it). Waits for beforeEnter and for the list to be on a ready screen.
|
|
756
|
+
*/
|
|
757
|
+
scrollToIndexIfNeeded() {
|
|
758
|
+
const token = this.stepToken;
|
|
759
|
+
const step = this.currentStep();
|
|
760
|
+
const request = step?.scrollToIndex;
|
|
761
|
+
if (!request || !step?.targetId || this.getState().paused) return;
|
|
762
|
+
if (token !== this.enteredToken || token === this.indexScrolledToken) return;
|
|
763
|
+
if (this.resolveTarget(step.targetId)) return;
|
|
764
|
+
const ready = (this.lists.get(request.list) ?? []).filter(h => h.ref.current && isScopeReady(h.scope ?? null));
|
|
765
|
+
const listRef = ready[ready.length - 1]?.ref.current;
|
|
766
|
+
if (!listRef) return; // registering the list (or its screen becoming ready) calls this again
|
|
767
|
+
|
|
768
|
+
this.indexScrolledToken = token;
|
|
769
|
+
if (typeof listRef.scrollToIndex !== 'function') {
|
|
770
|
+
if (__DEV__) {
|
|
771
|
+
console.warn(`[rn-smart-tour] scrollToIndex: the list "${request.list}" has no scrollToIndex ` + '(use a FlatList, SectionList or FlashList ref).');
|
|
772
|
+
}
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
try {
|
|
776
|
+
listRef.scrollToIndex({
|
|
777
|
+
index: request.index,
|
|
778
|
+
animated: !this.reduceMotion,
|
|
779
|
+
viewPosition: 0.5
|
|
780
|
+
});
|
|
781
|
+
} catch (e) {
|
|
782
|
+
// FlatList throws for an item it hasn't measured unless it has getItemLayout or
|
|
783
|
+
// onScrollToIndexFailed (see `scrollToIndexFailedHandler`).
|
|
784
|
+
if (__DEV__) {
|
|
785
|
+
console.warn(`[rn-smart-tour] scrollToIndex to item ${request.index} of "${request.list}" failed. ` + 'Give the list getItemLayout, or onScrollToIndexFailed={scrollToIndexFailedHandler(listRef)}.', e);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
623
789
|
finish(markAsSeen, outcome) {
|
|
624
790
|
const {
|
|
625
791
|
tourId,
|
|
626
|
-
stepIndex
|
|
627
|
-
seen
|
|
792
|
+
stepIndex
|
|
628
793
|
} = this.getState();
|
|
629
794
|
if (!tourId) return;
|
|
630
795
|
if (this.run) {
|
|
@@ -651,8 +816,8 @@ class TourEngine {
|
|
|
651
816
|
});
|
|
652
817
|
if (markAsSeen) {
|
|
653
818
|
this.persistSeen({
|
|
654
|
-
...
|
|
655
|
-
[tourId]: true
|
|
819
|
+
...this.storedSeen,
|
|
820
|
+
[tourId]: this.tours[tourId]?.version ?? true
|
|
656
821
|
});
|
|
657
822
|
}
|
|
658
823
|
this.scheduleAutoStart();
|
|
@@ -666,7 +831,8 @@ class TourEngine {
|
|
|
666
831
|
try {
|
|
667
832
|
onEvent({
|
|
668
833
|
...body,
|
|
669
|
-
|
|
834
|
+
eventId: createId(),
|
|
835
|
+
runId: runId ?? createId(),
|
|
670
836
|
tourId,
|
|
671
837
|
tourKind: this.tours[tourId]?.kind ?? 'tour',
|
|
672
838
|
timestamp: Date.now()
|