react-guide-step 0.1.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/LICENSE +21 -0
- package/README.md +258 -0
- package/README.zh-CN.md +258 -0
- package/dist/components/Guide.d.ts +2 -0
- package/dist/components/GuideArrow.d.ts +11 -0
- package/dist/components/GuideOverlay.d.ts +11 -0
- package/dist/components/GuideTooltip.d.ts +17 -0
- package/dist/context/GuideContext.d.ts +6 -0
- package/dist/context/GuideProvider.d.ts +13 -0
- package/dist/engine/arrow.d.ts +15 -0
- package/dist/engine/computePosition.d.ts +13 -0
- package/dist/engine/constants.d.ts +6 -0
- package/dist/hooks/useGuide.d.ts +2 -0
- package/dist/hooks/useKeyboard.d.ts +12 -0
- package/dist/hooks/usePersistence.d.ts +8 -0
- package/dist/hooks/usePosition.d.ts +19 -0
- package/dist/hooks/usePreloadImages.d.ts +6 -0
- package/dist/hooks/useScrollIntoView.d.ts +4 -0
- package/dist/hooks/useWaitForElement.d.ts +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/react-guide-step.cjs +171 -0
- package/dist/react-guide-step.js +863 -0
- package/dist/styles/classes.d.ts +5 -0
- package/dist/styles/inject.d.ts +9 -0
- package/dist/styles/theme.d.ts +10 -0
- package/dist/types.d.ts +101 -0
- package/dist/utils/dom.d.ts +13 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 react-guide-step
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# react-guide-step
|
|
2
|
+
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A lightweight, zero-dependency React component library for building interactive product tours, onboarding walkthroughs, and step-by-step guided experiences.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Step-based tours** — Define a sequence of steps targeting any DOM element
|
|
10
|
+
- **Spotlight overlay** — Highlights target elements while dimming the rest of the page
|
|
11
|
+
- **Smart positioning** — Auto-calculates tooltip placement with 12 positions and viewport flip logic
|
|
12
|
+
- **Keyboard navigation** — Navigate with arrow keys, Enter, and Escape
|
|
13
|
+
- **Custom rendering** — Full control over step content via render props
|
|
14
|
+
- **Persistence** — Optional localStorage-based completion tracking to avoid re-showing tours
|
|
15
|
+
- **Auto-scroll** — Automatically scrolls target elements into view
|
|
16
|
+
- **Element waiting** — Waits for async/lazy DOM elements to appear before proceeding
|
|
17
|
+
- **Theming** — Customize colors, border radius, z-index, and animation duration
|
|
18
|
+
- **i18n ready** — All button labels are configurable
|
|
19
|
+
- **Lifecycle hooks** — `beforeEnter` and `afterLeave` async hooks per step
|
|
20
|
+
- **Image preloading** — Preload step images to prevent layout shifts
|
|
21
|
+
- **Tree-shakeable** — Zero runtime dependencies, ESM and CJS support
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install react-guide-step
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add react-guide-step
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add react-guide-step
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
> **Peer dependencies:** `react >= 18.0.0` and `react-dom >= 18.0.0`
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
### 1. Define your tour steps
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// tours/onboarding.ts
|
|
45
|
+
import type { GuideStep } from 'react-guide-step';
|
|
46
|
+
|
|
47
|
+
const steps: GuideStep[] = [
|
|
48
|
+
{
|
|
49
|
+
target: '#welcome-header',
|
|
50
|
+
title: 'Welcome!',
|
|
51
|
+
content: 'Let us show you around the app.',
|
|
52
|
+
placement: 'bottom',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
target: '#sidebar-nav',
|
|
56
|
+
title: 'Navigation',
|
|
57
|
+
content: 'Use the sidebar to navigate between pages.',
|
|
58
|
+
placement: 'right',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
target: '#create-btn',
|
|
62
|
+
title: 'Create',
|
|
63
|
+
content: 'Click here to create a new project.',
|
|
64
|
+
placement: 'bottom',
|
|
65
|
+
highlightPadding: 8,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 2. Set up the provider and start the tour
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import { GuideProvider, useGuide } from 'react-guide-step';
|
|
74
|
+
import type { GuideOptions } from 'react-guide-step';
|
|
75
|
+
import { steps } from './tours/onboarding';
|
|
76
|
+
|
|
77
|
+
function App() {
|
|
78
|
+
const options: GuideOptions = {
|
|
79
|
+
steps,
|
|
80
|
+
maskClosable: true,
|
|
81
|
+
keyboardNavigation: true,
|
|
82
|
+
onComplete: () => console.log('Tour completed!'),
|
|
83
|
+
onSkip: (step) => console.log(`Skipped at step ${step}`),
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const guide = useGuide(options);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<GuideProvider guide={guide} options={options}>
|
|
90
|
+
<YourApp />
|
|
91
|
+
<button onClick={() => guide.start()}>Start Tour</button>
|
|
92
|
+
</GuideProvider>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### `useGuide(options)`
|
|
100
|
+
|
|
101
|
+
The main hook that creates a guide controller.
|
|
102
|
+
|
|
103
|
+
**Returns: `GuideController`**
|
|
104
|
+
|
|
105
|
+
| Property | Type | Description |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| `start()` | `() => void` | Start the tour |
|
|
108
|
+
| `stop()` | `() => void` | Stop the tour |
|
|
109
|
+
| `next()` | `() => void` | Go to next step |
|
|
110
|
+
| `prev()` | `() => void` | Go to previous step |
|
|
111
|
+
| `goTo(index)` | `(number) => void` | Jump to a specific step |
|
|
112
|
+
| `isActive` | `boolean` | Whether the tour is currently running |
|
|
113
|
+
| `currentStep` | `number` | Current step index |
|
|
114
|
+
| `totalSteps` | `number` | Total number of steps |
|
|
115
|
+
| `isCompleted` | `boolean` | Whether the tour has been completed |
|
|
116
|
+
|
|
117
|
+
### `GuideOptions`
|
|
118
|
+
|
|
119
|
+
| Option | Type | Default | Description |
|
|
120
|
+
|---|---|---|---|
|
|
121
|
+
| `steps` | `GuideStep[]` | *required* | Array of tour steps |
|
|
122
|
+
| `initialStep` | `number` | `0` | Starting step index |
|
|
123
|
+
| `autoStart` | `boolean` | `false` | Auto-start the guide on mount |
|
|
124
|
+
| `maskClosable` | `boolean` | `false` | Close guide when clicking the overlay |
|
|
125
|
+
| `keyboardNavigation` | `boolean` | `true` | Enable keyboard navigation |
|
|
126
|
+
| `scrollBehavior` | `ScrollBehavior` | `'smooth'` | Scroll behavior when scrolling target into view |
|
|
127
|
+
| `persistKey` | `string` | — | localStorage key; completed tours won't re-show |
|
|
128
|
+
| `theme` | `GuideTheme` | — | Custom theme overrides |
|
|
129
|
+
| `labels` | `GuideLabels` | — | Custom button labels for i18n |
|
|
130
|
+
| `onComplete` | `() => void` | — | Called when the tour finishes |
|
|
131
|
+
| `onSkip` | `(stepIndex) => void` | — | Called when the tour is skipped |
|
|
132
|
+
| `onStepChange` | `(stepIndex) => void` | — | Called on each step transition |
|
|
133
|
+
|
|
134
|
+
### `GuideStep`
|
|
135
|
+
|
|
136
|
+
| Property | Type | Default | Description |
|
|
137
|
+
|---|---|---|---|
|
|
138
|
+
| `target` | `string \| HTMLElement` | *required* | CSS selector or element reference |
|
|
139
|
+
| `title` | `string` | — | Step title |
|
|
140
|
+
| `content` | `ReactNode` | — | Step description (supports text, JSX, images) |
|
|
141
|
+
| `placement` | `Placement` | `'bottom'` | Tooltip position relative to target |
|
|
142
|
+
| `customRender` | `(props: StepRenderProps) => ReactNode` | — | Custom render function |
|
|
143
|
+
| `beforeEnter` | `() => void \| Promise<void>` | — | Async hook before entering step |
|
|
144
|
+
| `afterLeave` | `() => void \| Promise<void>` | — | Async hook after leaving step |
|
|
145
|
+
| `highlightPadding` | `number` | — | Extra padding around the spotlight (px) |
|
|
146
|
+
| `waitForElement` | `boolean` | `false` | Wait for target element to appear in DOM |
|
|
147
|
+
| `showArrow` | `boolean` | `true` | Show arrow pointing to target |
|
|
148
|
+
| `allowInteraction` | `boolean` | `false` | Allow user interaction with highlighted element |
|
|
149
|
+
| `preloadImages` | `string[]` | — | Image URLs to preload before this step is displayed |
|
|
150
|
+
|
|
151
|
+
### `Placement`
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
'top' | 'top-start' | 'top-end'
|
|
155
|
+
'bottom' | 'bottom-start' | 'bottom-end'
|
|
156
|
+
'left' | 'left-start' | 'left-end'
|
|
157
|
+
'right' | 'right-start' | 'right-end'
|
|
158
|
+
'center'
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `GuideTheme`
|
|
162
|
+
|
|
163
|
+
| Property | Type | Description |
|
|
164
|
+
|---|---|---|
|
|
165
|
+
| `primaryColor` | `string` | Primary button and accent color |
|
|
166
|
+
| `textColor` | `string` | Text color |
|
|
167
|
+
| `bgColor` | `string` | Tooltip background color |
|
|
168
|
+
| `overlayColor` | `string` | Overlay/mask color |
|
|
169
|
+
| `borderRadius` | `number` | Border radius in px |
|
|
170
|
+
| `zIndex` | `number` | Base z-index for the guide layer |
|
|
171
|
+
| `animationDuration` | `number` | Animation duration in ms |
|
|
172
|
+
|
|
173
|
+
### `GuideLabels`
|
|
174
|
+
|
|
175
|
+
| Property | Type | Description |
|
|
176
|
+
|---|---|---|
|
|
177
|
+
| `next` | `string` | "Next" button text |
|
|
178
|
+
| `prev` | `string` | "Previous" button text |
|
|
179
|
+
| `skip` | `string` | "Skip" button text |
|
|
180
|
+
| `finish` | `string` | "Finish" button text |
|
|
181
|
+
| `stepOf` | `string` | Step counter template, e.g. `"{current} of {total}"` |
|
|
182
|
+
|
|
183
|
+
## Advanced Usage
|
|
184
|
+
|
|
185
|
+
### Custom Step Rendering
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
const steps: GuideStep[] = [
|
|
189
|
+
{
|
|
190
|
+
target: '#feature',
|
|
191
|
+
customRender: ({ step, stepIndex, totalSteps, next, prev, skip }) => (
|
|
192
|
+
<div className="my-custom-tooltip">
|
|
193
|
+
<h3>Step {stepIndex + 1} of {totalSteps}</h3>
|
|
194
|
+
<p>Your custom content here</p>
|
|
195
|
+
<div>
|
|
196
|
+
<button onClick={prev}>Back</button>
|
|
197
|
+
<button onClick={next}>Continue</button>
|
|
198
|
+
<button onClick={skip}>Skip Tour</button>
|
|
199
|
+
</div>
|
|
200
|
+
</div>
|
|
201
|
+
),
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Persistence
|
|
207
|
+
|
|
208
|
+
Prevent completed tours from re-appearing by setting a `persistKey`:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const options: GuideOptions = {
|
|
212
|
+
steps,
|
|
213
|
+
persistKey: 'onboarding-v1',
|
|
214
|
+
};
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Completion state is stored in `localStorage` under the key `rgs:<persistKey>`. To reset, remove the key:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
localStorage.removeItem('rgs:onboarding-v1');
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Async Step Hooks
|
|
224
|
+
|
|
225
|
+
Run async operations before entering or after leaving a step:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
const steps: GuideStep[] = [
|
|
229
|
+
{
|
|
230
|
+
target: '#dynamic-section',
|
|
231
|
+
title: 'Dynamic Content',
|
|
232
|
+
content: 'This section was loaded just for you.',
|
|
233
|
+
waitForElement: true,
|
|
234
|
+
beforeEnter: async () => {
|
|
235
|
+
await fetchAndRenderSection();
|
|
236
|
+
},
|
|
237
|
+
afterLeave: async () => {
|
|
238
|
+
await trackStepViewed();
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
];
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Development
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# Install dependencies
|
|
248
|
+
npm install
|
|
249
|
+
|
|
250
|
+
# Start demo dev server
|
|
251
|
+
npm run dev
|
|
252
|
+
|
|
253
|
+
# Build the library
|
|
254
|
+
npm run build
|
|
255
|
+
|
|
256
|
+
# Preview demo production build
|
|
257
|
+
npm run preview
|
|
258
|
+
```
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# react-guide-step
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
一个轻量级、零依赖的 React 组件库,用于构建交互式产品导览、用户引导和分步引导体验。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- **分步引导** — 定义一系列步骤,定位到任意 DOM 元素
|
|
10
|
+
- **聚光灯遮罩** — 高亮目标元素,同时遮暗页面其他区域
|
|
11
|
+
- **智能定位** — 自动计算提示框位置,支持 12 种定位方式和视口翻转逻辑
|
|
12
|
+
- **键盘导航** — 支持方向键、Enter 和 Escape 键导航
|
|
13
|
+
- **自定义渲染** — 通过 render props 完全控制步骤内容
|
|
14
|
+
- **持久化** — 可选的 localStorage 完成状态跟踪,避免重复展示
|
|
15
|
+
- **自动滚动** — 自动滚动目标元素到可视区域
|
|
16
|
+
- **元素等待** — 等待异步/懒加载 DOM 元素出现后再继续
|
|
17
|
+
- **主题定制** — 自定义颜色、圆角、层级和动画时长
|
|
18
|
+
- **国际化支持** — 所有按钮文案均可配置
|
|
19
|
+
- **生命周期钩子** — 每个步骤支持 `beforeEnter` 和 `afterLeave` 异步钩子
|
|
20
|
+
- **图片预加载** — 预加载步骤图片,防止布局抖动
|
|
21
|
+
- **Tree-shakeable** — 零运行时依赖,支持 ESM 和 CJS
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install react-guide-step
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add react-guide-step
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add react-guide-step
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
> **对等依赖:** `react >= 18.0.0` 和 `react-dom >= 18.0.0`
|
|
38
|
+
|
|
39
|
+
## 快速开始
|
|
40
|
+
|
|
41
|
+
### 1. 定义引导步骤
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// tours/onboarding.ts
|
|
45
|
+
import type { GuideStep } from 'react-guide-step';
|
|
46
|
+
|
|
47
|
+
const steps: GuideStep[] = [
|
|
48
|
+
{
|
|
49
|
+
target: '#welcome-header',
|
|
50
|
+
title: '欢迎!',
|
|
51
|
+
content: '让我们带你了解这个应用。',
|
|
52
|
+
placement: 'bottom',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
target: '#sidebar-nav',
|
|
56
|
+
title: '导航栏',
|
|
57
|
+
content: '使用侧边栏在不同页面之间切换。',
|
|
58
|
+
placement: 'right',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
target: '#create-btn',
|
|
62
|
+
title: '创建',
|
|
63
|
+
content: '点击这里创建一个新项目。',
|
|
64
|
+
placement: 'bottom',
|
|
65
|
+
highlightPadding: 8,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 2. 设置 Provider 并启动引导
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import { GuideProvider, useGuide } from 'react-guide-step';
|
|
74
|
+
import type { GuideOptions } from 'react-guide-step';
|
|
75
|
+
import { steps } from './tours/onboarding';
|
|
76
|
+
|
|
77
|
+
function App() {
|
|
78
|
+
const options: GuideOptions = {
|
|
79
|
+
steps,
|
|
80
|
+
maskClosable: true,
|
|
81
|
+
keyboardNavigation: true,
|
|
82
|
+
onComplete: () => console.log('引导完成!'),
|
|
83
|
+
onSkip: (step) => console.log(`在第 ${step} 步跳过`),
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const guide = useGuide(options);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<GuideProvider guide={guide} options={options}>
|
|
90
|
+
<YourApp />
|
|
91
|
+
<button onClick={() => guide.start()}>开始引导</button>
|
|
92
|
+
</GuideProvider>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API 参考
|
|
98
|
+
|
|
99
|
+
### `useGuide(options)`
|
|
100
|
+
|
|
101
|
+
创建引导控制器的主要 Hook。
|
|
102
|
+
|
|
103
|
+
**返回值:`GuideController`**
|
|
104
|
+
|
|
105
|
+
| 属性 | 类型 | 说明 |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| `start()` | `() => void` | 开始引导 |
|
|
108
|
+
| `stop()` | `() => void` | 停止引导 |
|
|
109
|
+
| `next()` | `() => void` | 跳转到下一步 |
|
|
110
|
+
| `prev()` | `() => void` | 跳转到上一步 |
|
|
111
|
+
| `goTo(index)` | `(number) => void` | 跳转到指定步骤 |
|
|
112
|
+
| `isActive` | `boolean` | 引导是否正在运行 |
|
|
113
|
+
| `currentStep` | `number` | 当前步骤索引 |
|
|
114
|
+
| `totalSteps` | `number` | 总步骤数 |
|
|
115
|
+
| `isCompleted` | `boolean` | 引导是否已完成 |
|
|
116
|
+
|
|
117
|
+
### `GuideOptions`
|
|
118
|
+
|
|
119
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
120
|
+
|---|---|---|---|
|
|
121
|
+
| `steps` | `GuideStep[]` | *必填* | 引导步骤数组 |
|
|
122
|
+
| `initialStep` | `number` | `0` | 起始步骤索引 |
|
|
123
|
+
| `autoStart` | `boolean` | `false` | 挂载时自动启动引导 |
|
|
124
|
+
| `maskClosable` | `boolean` | `false` | 点击遮罩层关闭引导 |
|
|
125
|
+
| `keyboardNavigation` | `boolean` | `true` | 启用键盘导航 |
|
|
126
|
+
| `scrollBehavior` | `ScrollBehavior` | `'smooth'` | 滚动目标到可视区域时的滚动行为 |
|
|
127
|
+
| `persistKey` | `string` | — | localStorage 键名;已完成的引导不会再次显示 |
|
|
128
|
+
| `theme` | `GuideTheme` | — | 自定义主题覆盖 |
|
|
129
|
+
| `labels` | `GuideLabels` | — | 自定义按钮文案(用于国际化) |
|
|
130
|
+
| `onComplete` | `() => void` | — | 引导完成时的回调 |
|
|
131
|
+
| `onSkip` | `(stepIndex) => void` | — | 引导被跳过时的回调 |
|
|
132
|
+
| `onStepChange` | `(stepIndex) => void` | — | 每次步骤切换时的回调 |
|
|
133
|
+
|
|
134
|
+
### `GuideStep`
|
|
135
|
+
|
|
136
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
137
|
+
|---|---|---|---|
|
|
138
|
+
| `target` | `string \| HTMLElement` | *必填* | CSS 选择器或元素引用 |
|
|
139
|
+
| `title` | `string` | — | 步骤标题 |
|
|
140
|
+
| `content` | `ReactNode` | — | 步骤描述(支持文本、JSX、图片) |
|
|
141
|
+
| `placement` | `Placement` | `'bottom'` | 提示框相对于目标的位置 |
|
|
142
|
+
| `customRender` | `(props: StepRenderProps) => ReactNode` | — | 自定义渲染函数 |
|
|
143
|
+
| `beforeEnter` | `() => void \| Promise<void>` | — | 进入步骤前的异步钩子 |
|
|
144
|
+
| `afterLeave` | `() => void \| Promise<void>` | — | 离开步骤后的异步钩子 |
|
|
145
|
+
| `highlightPadding` | `number` | — | 聚光灯周围的额外内边距(px) |
|
|
146
|
+
| `waitForElement` | `boolean` | `false` | 等待目标元素出现在 DOM 中 |
|
|
147
|
+
| `showArrow` | `boolean` | `true` | 显示指向目标的箭头 |
|
|
148
|
+
| `allowInteraction` | `boolean` | `false` | 允许用户与高亮元素交互 |
|
|
149
|
+
| `preloadImages` | `string[]` | — | 在显示此步骤前预加载的图片 URL |
|
|
150
|
+
|
|
151
|
+
### `Placement`
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
'top' | 'top-start' | 'top-end'
|
|
155
|
+
'bottom' | 'bottom-start' | 'bottom-end'
|
|
156
|
+
'left' | 'left-start' | 'left-end'
|
|
157
|
+
'right' | 'right-start' | 'right-end'
|
|
158
|
+
'center'
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `GuideTheme`
|
|
162
|
+
|
|
163
|
+
| 属性 | 类型 | 说明 |
|
|
164
|
+
|---|---|---|
|
|
165
|
+
| `primaryColor` | `string` | 主按钮和强调色 |
|
|
166
|
+
| `textColor` | `string` | 文字颜色 |
|
|
167
|
+
| `bgColor` | `string` | 提示框背景颜色 |
|
|
168
|
+
| `overlayColor` | `string` | 遮罩层颜色 |
|
|
169
|
+
| `borderRadius` | `number` | 圆角大小(px) |
|
|
170
|
+
| `zIndex` | `number` | 引导层的基础 z-index |
|
|
171
|
+
| `animationDuration` | `number` | 动画时长(ms) |
|
|
172
|
+
|
|
173
|
+
### `GuideLabels`
|
|
174
|
+
|
|
175
|
+
| 属性 | 类型 | 说明 |
|
|
176
|
+
|---|---|---|
|
|
177
|
+
| `next` | `string` | "下一步" 按钮文案 |
|
|
178
|
+
| `prev` | `string` | "上一步" 按钮文案 |
|
|
179
|
+
| `skip` | `string` | "跳过" 按钮文案 |
|
|
180
|
+
| `finish` | `string` | "完成" 按钮文案 |
|
|
181
|
+
| `stepOf` | `string` | 步骤计数模板,例如 `"{current} / {total}"` |
|
|
182
|
+
|
|
183
|
+
## 进阶用法
|
|
184
|
+
|
|
185
|
+
### 自定义步骤渲染
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
const steps: GuideStep[] = [
|
|
189
|
+
{
|
|
190
|
+
target: '#feature',
|
|
191
|
+
customRender: ({ step, stepIndex, totalSteps, next, prev, skip }) => (
|
|
192
|
+
<div className="my-custom-tooltip">
|
|
193
|
+
<h3>第 {stepIndex + 1} 步,共 {totalSteps} 步</h3>
|
|
194
|
+
<p>你的自定义内容</p>
|
|
195
|
+
<div>
|
|
196
|
+
<button onClick={prev}>上一步</button>
|
|
197
|
+
<button onClick={next}>继续</button>
|
|
198
|
+
<button onClick={skip}>跳过引导</button>
|
|
199
|
+
</div>
|
|
200
|
+
</div>
|
|
201
|
+
),
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### 持久化
|
|
207
|
+
|
|
208
|
+
通过设置 `persistKey` 防止已完成的引导再次出现:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const options: GuideOptions = {
|
|
212
|
+
steps,
|
|
213
|
+
persistKey: 'onboarding-v1',
|
|
214
|
+
};
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
完成状态存储在 `localStorage` 中,键名为 `rgs:<persistKey>`。要重置,移除该键即可:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
localStorage.removeItem('rgs:onboarding-v1');
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### 异步步骤钩子
|
|
224
|
+
|
|
225
|
+
在进入或离开步骤时执行异步操作:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
const steps: GuideStep[] = [
|
|
229
|
+
{
|
|
230
|
+
target: '#dynamic-section',
|
|
231
|
+
title: '动态内容',
|
|
232
|
+
content: '这个区域是专门为你加载的。',
|
|
233
|
+
waitForElement: true,
|
|
234
|
+
beforeEnter: async () => {
|
|
235
|
+
await fetchAndRenderSection();
|
|
236
|
+
},
|
|
237
|
+
afterLeave: async () => {
|
|
238
|
+
await trackStepViewed();
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
];
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## 开发
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# 安装依赖
|
|
248
|
+
npm install
|
|
249
|
+
|
|
250
|
+
# 启动 demo 开发服务器
|
|
251
|
+
npm run dev
|
|
252
|
+
|
|
253
|
+
# 构建库
|
|
254
|
+
npm run build
|
|
255
|
+
|
|
256
|
+
# 预览 demo 生产构建
|
|
257
|
+
npm run preview
|
|
258
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Placement } from '../types';
|
|
3
|
+
interface GuideOverlayProps {
|
|
4
|
+
targetElement: HTMLElement | null;
|
|
5
|
+
highlightPadding?: number;
|
|
6
|
+
allowInteraction?: boolean;
|
|
7
|
+
onMaskClick?: () => void;
|
|
8
|
+
placement?: Placement;
|
|
9
|
+
}
|
|
10
|
+
export declare const GuideOverlay: React.FC<GuideOverlayProps>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { GuideLabels, GuideStep, PositionResult } from '../types';
|
|
3
|
+
interface GuideTooltipProps {
|
|
4
|
+
step: GuideStep;
|
|
5
|
+
stepIndex: number;
|
|
6
|
+
totalSteps: number;
|
|
7
|
+
position: PositionResult | null;
|
|
8
|
+
tooltipRef: React.Ref<HTMLDivElement>;
|
|
9
|
+
labels: Required<GuideLabels>;
|
|
10
|
+
showSkip?: boolean;
|
|
11
|
+
onNext: () => void;
|
|
12
|
+
onPrev: () => void;
|
|
13
|
+
onSkip: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function resolveLabels(labels?: GuideLabels): Required<GuideLabels>;
|
|
16
|
+
export declare const GuideTooltip: React.FC<GuideTooltipProps>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { GuideController, GuideOptions } from '../types';
|
|
3
|
+
interface GuideProviderProps {
|
|
4
|
+
guide: GuideController;
|
|
5
|
+
options: GuideOptions;
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* GuideProvider wraps your app and provides guide context.
|
|
10
|
+
* It also injects the base CSS styles and mounts the Guide component.
|
|
11
|
+
*/
|
|
12
|
+
export declare const GuideProvider: React.FC<GuideProviderProps>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Placement } from '../types';
|
|
2
|
+
type ArrowSide = 'top' | 'bottom' | 'left' | 'right';
|
|
3
|
+
/**
|
|
4
|
+
* Get which side of the tooltip the arrow should appear on.
|
|
5
|
+
* The arrow points FROM the tooltip TOWARD the target.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getArrowSide(placement: Placement): ArrowSide;
|
|
8
|
+
/**
|
|
9
|
+
* Get CSS styles for the arrow element based on its position and the resolved placement.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getArrowStyle(arrowPos: {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}, placement: Placement): React.CSSProperties;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Placement, PositionResult } from '../types';
|
|
2
|
+
interface Rect {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Main positioning function.
|
|
10
|
+
* Computes tooltip position with flip logic and viewport clamping.
|
|
11
|
+
*/
|
|
12
|
+
export declare function computePosition(targetRect: Rect, tooltipRect: Rect, placement?: Placement, offset?: number): PositionResult;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Default gap between the target element and the tooltip (px) */
|
|
2
|
+
export declare const DEFAULT_OFFSET = 10;
|
|
3
|
+
/** Arrow size in px */
|
|
4
|
+
export declare const ARROW_SIZE = 8;
|
|
5
|
+
/** Opposite side mapping for flip logic */
|
|
6
|
+
export declare const OPPOSITE_SIDE: Record<string, string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface UseKeyboardOptions {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
onNext: () => void;
|
|
4
|
+
onPrev: () => void;
|
|
5
|
+
onSkip: () => void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Hook for keyboard navigation in the guide.
|
|
9
|
+
* ArrowRight / Enter = next, ArrowLeft = prev, Escape = skip
|
|
10
|
+
*/
|
|
11
|
+
export declare function useKeyboard({ enabled, onNext, onPrev, onSkip }: UseKeyboardOptions): void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Placement, PositionResult } from '../types';
|
|
2
|
+
interface UsePositionOptions {
|
|
3
|
+
targetElement: HTMLElement | null;
|
|
4
|
+
placement: Placement;
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
/** Extra padding around the highlight box — added to the tooltip offset */
|
|
7
|
+
highlightPadding?: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Hook that computes and tracks tooltip position relative to a target element.
|
|
11
|
+
* Re-computes on scroll, resize, and when the target changes.
|
|
12
|
+
* Retains the previous position during step transitions to avoid flicker.
|
|
13
|
+
*/
|
|
14
|
+
export declare function usePosition({ targetElement, placement, enabled, highlightPadding }: UsePositionOptions): {
|
|
15
|
+
position: PositionResult | null;
|
|
16
|
+
tooltipRef: import("react").RefObject<HTMLDivElement>;
|
|
17
|
+
updatePosition: () => void;
|
|
18
|
+
};
|
|
19
|
+
export {};
|