@formriff/onboarding-react 0.0.0-stage → 0.1.2
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 +22 -0
- package/README.md +154 -2
- package/dist/index.cjs +85 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Onboarding Kit contributors
|
|
4
|
+
Copyright (c) 2026 Zero
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,155 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @formriff/onboarding-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React 18 and 19 bindings for Onboarding Kit. MIT.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @formriff/onboarding-react @formriff/onboarding react react-dom
|
|
9
|
+
# or
|
|
10
|
+
npm install @formriff/onboarding-react @formriff/onboarding react react-dom
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Import the **core CSS separately** once in your app entry or root layout:
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import '@formriff/onboarding/styles.css';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Minimal usage
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { OnboardingProvider, OnboardingStep, useOnboarding } from '@formriff/onboarding-react';
|
|
23
|
+
import { localStorageAdapter, type OnboardingOptions } from '@formriff/onboarding';
|
|
24
|
+
|
|
25
|
+
const options: OnboardingOptions = {
|
|
26
|
+
storage: localStorageAdapter('my-app'),
|
|
27
|
+
tours: [
|
|
28
|
+
{
|
|
29
|
+
id: 'onboarding',
|
|
30
|
+
steps: [
|
|
31
|
+
{
|
|
32
|
+
id: 'welcome',
|
|
33
|
+
target: '[data-onboarding="welcome"]',
|
|
34
|
+
title: 'Welcome',
|
|
35
|
+
content: 'Everything starts here.',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function Screen() {
|
|
43
|
+
const { kit, state } = useOnboarding();
|
|
44
|
+
return (
|
|
45
|
+
<>
|
|
46
|
+
<OnboardingStep name="welcome">
|
|
47
|
+
<button>Your workspace</button>
|
|
48
|
+
</OnboardingStep>
|
|
49
|
+
<button onClick={() => void kit.start('onboarding')}>Start onboarding</button>
|
|
50
|
+
<output>{state.status}</output>
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default function App() {
|
|
56
|
+
return (
|
|
57
|
+
<OnboardingProvider options={options}>
|
|
58
|
+
<Screen />
|
|
59
|
+
</OnboardingProvider>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`useOnboarding()` returns `{ kit, state }`; state is a reactive snapshot via
|
|
65
|
+
`useSyncExternalStore`. The hook must be inside a provider.
|
|
66
|
+
`OnboardingStep` renders a `span` with `data-onboarding={name}` and preserves its
|
|
67
|
+
children, HTML attributes and events. For block-level targets, put
|
|
68
|
+
`data-onboarding` directly on your element instead.
|
|
69
|
+
|
|
70
|
+
## Rich rendering / headless
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
<OnboardingProvider
|
|
74
|
+
options={options}
|
|
75
|
+
renderStep={({ state, kit, labels }) => (
|
|
76
|
+
<aside aria-label={state.step?.title}>
|
|
77
|
+
<h2>{state.step?.title}</h2>
|
|
78
|
+
<p>
|
|
79
|
+
<strong>{state.step?.content}</strong>
|
|
80
|
+
</p>
|
|
81
|
+
<button onClick={() => kit.skip()}>{labels.skip}</button>
|
|
82
|
+
<button onClick={() => void kit.prev()} disabled={state.stepIndex === 0}>
|
|
83
|
+
{labels.back}
|
|
84
|
+
</button>
|
|
85
|
+
<button onClick={() => void kit.next()}>
|
|
86
|
+
{state.stepIndex === state.totalSteps - 1 ? labels.done : labels.next}
|
|
87
|
+
</button>
|
|
88
|
+
</aside>
|
|
89
|
+
)}
|
|
90
|
+
>
|
|
91
|
+
<Screen />
|
|
92
|
+
</OnboardingProvider>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`renderStep` receives the core `RendererContext`: `{ state, kit, labels }`.
|
|
96
|
+
It is called only while `state.status === 'running'` and automatically chooses
|
|
97
|
+
`renderer: 'none'` for a provider-owned kit. React owns the returned UI; you can
|
|
98
|
+
return a portal. Labels come from the initial options merged with English defaults.
|
|
99
|
+
You own positioning, focus management and any dialog semantics/focus trap.
|
|
100
|
+
Core supplies headless keyboard shortcuts. Omit the callback and pass
|
|
101
|
+
`renderer: 'none'` to use the hook to render entirely elsewhere.
|
|
102
|
+
|
|
103
|
+
You can instead supply `<OnboardingProvider kit={existingKit}>`. Configure that
|
|
104
|
+
external kit with `renderer: 'none'` yourself when rendering custom UI; the
|
|
105
|
+
provider cannot reconfigure it. Callback labels for external kits use defaults.
|
|
106
|
+
Default rendering needs the separate core CSS; headless-only UI does not.
|
|
107
|
+
|
|
108
|
+
## Routers
|
|
109
|
+
|
|
110
|
+
No router is a peer dependency. Provide the core `RouterAdapter`:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
// React Router data router, created outside the component:
|
|
114
|
+
const adapter = {
|
|
115
|
+
getPath: () => router.state.location.pathname,
|
|
116
|
+
navigate: (path: string) => router.navigate(path),
|
|
117
|
+
subscribe: (listener: () => void) => router.subscribe(() => listener()),
|
|
118
|
+
};
|
|
119
|
+
// Supply { ...options, router: adapter } to the provider.
|
|
120
|
+
// Add route: '/' or route: '/settings' to each step.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Subscribe to actual route commits (including Back/Forward), not just clicks or
|
|
124
|
+
calls to `navigate`. Keep the provider above the routed outlet so navigation
|
|
125
|
+
doesn't unmount the tour. See `examples/react`.
|
|
126
|
+
|
|
127
|
+
### Next.js App Router / hydration
|
|
128
|
+
|
|
129
|
+
- Put a `'use client'` provider wrapper in the root layout; Server Components
|
|
130
|
+
can remain its children. The published ESM **and CJS** entrypoints preserve
|
|
131
|
+
the client directive.
|
|
132
|
+
- Create options once with `useState`; never create a browser/router singleton
|
|
133
|
+
in an SSR module. `createOnboarding` and `localStorageAdapter` are SSR-safe.
|
|
134
|
+
- Use `useRouter().push` for navigation and `usePathname()` in an effect to
|
|
135
|
+
update a stable path ref and notify adapter subscribers after route commits.
|
|
136
|
+
App Router has no `router.events`; a no-op subscription is not sufficient.
|
|
137
|
+
- Render the same initial markup on server and client. Mounting happens in an
|
|
138
|
+
effect, with a cached initial server snapshot. Start tours after hydration,
|
|
139
|
+
e.g. from a button, not during render.
|
|
140
|
+
- Import `@formriff/onboarding/styles.css` in `app/layout.tsx`.
|
|
141
|
+
|
|
142
|
+
`examples/nextjs/app/providers.tsx` is a complete implementation with a stable
|
|
143
|
+
adapter and real pathname notifications.
|
|
144
|
+
|
|
145
|
+
## Lifecycle
|
|
146
|
+
|
|
147
|
+
Options, labels and kit are captured on the first render. Change the provider's
|
|
148
|
+
React `key` to replace configuration. Providers call `kit.mount()` in an effect
|
|
149
|
+
and its reversible cleanup on unmount. They **do not call `destroy()`**, allowing
|
|
150
|
+
StrictMode effect replay and reuse of externally owned kits. Core unmount
|
|
151
|
+
returns the state to idle while retaining persisted progress. One provider
|
|
152
|
+
should own the mount lifecycle of each kit; hooks can be used by many children.
|
|
153
|
+
For an external kit, call `destroy()` only when you permanently retire it,
|
|
154
|
+
after its provider unmounts. Events can be subscribed with `kit.on`; unsubscribe
|
|
155
|
+
your own listeners in effect cleanup.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
"use client";
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// src/index.tsx
|
|
23
|
+
var index_exports = {};
|
|
24
|
+
__export(index_exports, {
|
|
25
|
+
OnboardingProvider: () => OnboardingProvider,
|
|
26
|
+
OnboardingStep: () => OnboardingStep,
|
|
27
|
+
useOnboarding: () => useOnboarding
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
var import_react = require("react");
|
|
31
|
+
var import_onboarding = require("@formriff/onboarding");
|
|
32
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
33
|
+
var OnboardingContext = (0, import_react.createContext)(null);
|
|
34
|
+
var serverSnapshot = {
|
|
35
|
+
status: "idle",
|
|
36
|
+
tourId: null,
|
|
37
|
+
stepIndex: 0,
|
|
38
|
+
step: null,
|
|
39
|
+
totalSteps: 0,
|
|
40
|
+
target: null
|
|
41
|
+
};
|
|
42
|
+
var getServerSnapshot = () => serverSnapshot;
|
|
43
|
+
var defaultLabels = {
|
|
44
|
+
next: "Next",
|
|
45
|
+
back: "Back",
|
|
46
|
+
skip: "Skip",
|
|
47
|
+
done: "Done",
|
|
48
|
+
stepOf: (current, total) => `Step ${current} of ${total}`
|
|
49
|
+
};
|
|
50
|
+
function OnboardingProvider({ children, renderStep, ...props }) {
|
|
51
|
+
const [instance] = (0, import_react.useState)(() => {
|
|
52
|
+
const kit2 = props.kit ?? (0, import_onboarding.createOnboarding)({
|
|
53
|
+
...props.options,
|
|
54
|
+
...renderStep ? { renderer: "none" } : {}
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
kit: kit2,
|
|
58
|
+
labels: { ...defaultLabels, ...props.options?.labels }
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
const { kit } = instance;
|
|
62
|
+
const subscribe = (0, import_react.useCallback)((notify) => kit.subscribe(notify), [kit]);
|
|
63
|
+
const state = (0, import_react.useSyncExternalStore)(subscribe, kit.getState, getServerSnapshot);
|
|
64
|
+
const value = (0, import_react.useMemo)(() => ({ kit, state }), [kit, state]);
|
|
65
|
+
(0, import_react.useEffect)(() => kit.mount(), [kit]);
|
|
66
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(OnboardingContext.Provider, { value, children: [
|
|
67
|
+
children,
|
|
68
|
+
state.status === "running" && renderStep?.({ kit, state, labels: instance.labels })
|
|
69
|
+
] });
|
|
70
|
+
}
|
|
71
|
+
function useOnboarding() {
|
|
72
|
+
const context = (0, import_react.useContext)(OnboardingContext);
|
|
73
|
+
if (!context) throw new Error("useOnboarding must be used within a OnboardingProvider.");
|
|
74
|
+
return context;
|
|
75
|
+
}
|
|
76
|
+
function OnboardingStep({ name, children, ...props }) {
|
|
77
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { ...props, "data-onboarding": name, children });
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
OnboardingProvider,
|
|
82
|
+
OnboardingStep,
|
|
83
|
+
useOnboarding
|
|
84
|
+
});
|
|
85
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"sourcesContent":["'use client';\n\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n type HTMLAttributes,\n type ReactNode,\n} from 'react';\nimport {\n createOnboarding,\n type Labels,\n type RendererContext,\n type Onboarding,\n type OnboardingOptions,\n type OnboardingState,\n} from '@formriff/onboarding';\n\nexport interface OnboardingContextValue {\n kit: Onboarding;\n state: OnboardingState;\n}\n\nexport type OnboardingProviderProps = (\n { options: OnboardingOptions; kit?: never } | { kit: Onboarding; options?: never }\n) & {\n children?: ReactNode;\n /** Replaces the default renderer for an owned kit. Only called while running. */\n renderStep?: (context: RendererContext) => ReactNode;\n};\n\nconst OnboardingContext = createContext<OnboardingContextValue | null>(null);\n\n// Hydration always starts idle, even if an external store changed before hydration.\nconst serverSnapshot: OnboardingState = {\n status: 'idle',\n tourId: null,\n stepIndex: 0,\n step: null,\n totalSteps: 0,\n target: null,\n};\nconst getServerSnapshot = () => serverSnapshot;\n\nconst defaultLabels: Labels = {\n next: 'Next',\n back: 'Back',\n skip: 'Skip',\n done: 'Done',\n stepOf: (current, total) => `Step ${current} of ${total}`,\n};\n\n/** Options/kit are captured once. Use a React key to deliberately replace a kit. */\nexport function OnboardingProvider({ children, renderStep, ...props }: OnboardingProviderProps) {\n const [instance] = useState(() => {\n const kit =\n props.kit ??\n createOnboarding({\n ...props.options!,\n ...(renderStep ? { renderer: 'none' as const } : {}),\n });\n return {\n kit,\n labels: { ...defaultLabels, ...props.options?.labels },\n };\n });\n const { kit } = instance;\n const subscribe = useCallback((notify: () => void) => kit.subscribe(notify), [kit]);\n const state = useSyncExternalStore(subscribe, kit.getState, getServerSnapshot);\n const value = useMemo(() => ({ kit, state }), [kit, state]);\n\n // mount cleanup is reversible, unlike destroy(). This supports StrictMode replay.\n useEffect(() => kit.mount(), [kit]);\n\n return (\n <OnboardingContext.Provider value={value}>\n {children}\n {state.status === 'running' && renderStep?.({ kit, state, labels: instance.labels })}\n </OnboardingContext.Provider>\n );\n}\n\nexport function useOnboarding(): OnboardingContextValue {\n const context = useContext(OnboardingContext);\n if (!context) throw new Error('useOnboarding must be used within a OnboardingProvider.');\n return context;\n}\n\nexport interface OnboardingStepProps extends HTMLAttributes<HTMLSpanElement> {\n name: string;\n}\n\n/** An inline anchor. Its children are preserved, not cloned or replaced. */\nexport function OnboardingStep({ name, children, ...props }: OnboardingStepProps) {\n return (\n <span {...props} data-onboarding={name}>\n {children}\n </span>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA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;AAEO,SAAS,gBAAwC;AACtD,QAAM,cAAU,yBAAW,iBAAiB;AAC5C,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,yDAAyD;AACvF,SAAO;AACT;AAOO,SAAS,eAAe,EAAE,MAAM,UAAU,GAAG,MAAM,GAAwB;AAChF,SACE,4CAAC,UAAM,GAAG,OAAO,mBAAiB,MAC/B,UACH;AAEJ;","names":["kit"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, HTMLAttributes } from 'react';
|
|
3
|
+
import { Onboarding, OnboardingState, OnboardingOptions, RendererContext } from '@formriff/onboarding';
|
|
4
|
+
|
|
5
|
+
interface OnboardingContextValue {
|
|
6
|
+
kit: Onboarding;
|
|
7
|
+
state: OnboardingState;
|
|
8
|
+
}
|
|
9
|
+
type OnboardingProviderProps = ({
|
|
10
|
+
options: OnboardingOptions;
|
|
11
|
+
kit?: never;
|
|
12
|
+
} | {
|
|
13
|
+
kit: Onboarding;
|
|
14
|
+
options?: never;
|
|
15
|
+
}) & {
|
|
16
|
+
children?: ReactNode;
|
|
17
|
+
/** Replaces the default renderer for an owned kit. Only called while running. */
|
|
18
|
+
renderStep?: (context: RendererContext) => ReactNode;
|
|
19
|
+
};
|
|
20
|
+
/** Options/kit are captured once. Use a React key to deliberately replace a kit. */
|
|
21
|
+
declare function OnboardingProvider({ children, renderStep, ...props }: OnboardingProviderProps): react.JSX.Element;
|
|
22
|
+
declare function useOnboarding(): OnboardingContextValue;
|
|
23
|
+
interface OnboardingStepProps extends HTMLAttributes<HTMLSpanElement> {
|
|
24
|
+
name: string;
|
|
25
|
+
}
|
|
26
|
+
/** An inline anchor. Its children are preserved, not cloned or replaced. */
|
|
27
|
+
declare function OnboardingStep({ name, children, ...props }: OnboardingStepProps): react.JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { OnboardingProvider, OnboardingStep, useOnboarding };
|
|
30
|
+
export type { OnboardingContextValue, OnboardingProviderProps, OnboardingStepProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, HTMLAttributes } from 'react';
|
|
3
|
+
import { Onboarding, OnboardingState, OnboardingOptions, RendererContext } from '@formriff/onboarding';
|
|
4
|
+
|
|
5
|
+
interface OnboardingContextValue {
|
|
6
|
+
kit: Onboarding;
|
|
7
|
+
state: OnboardingState;
|
|
8
|
+
}
|
|
9
|
+
type OnboardingProviderProps = ({
|
|
10
|
+
options: OnboardingOptions;
|
|
11
|
+
kit?: never;
|
|
12
|
+
} | {
|
|
13
|
+
kit: Onboarding;
|
|
14
|
+
options?: never;
|
|
15
|
+
}) & {
|
|
16
|
+
children?: ReactNode;
|
|
17
|
+
/** Replaces the default renderer for an owned kit. Only called while running. */
|
|
18
|
+
renderStep?: (context: RendererContext) => ReactNode;
|
|
19
|
+
};
|
|
20
|
+
/** Options/kit are captured once. Use a React key to deliberately replace a kit. */
|
|
21
|
+
declare function OnboardingProvider({ children, renderStep, ...props }: OnboardingProviderProps): react.JSX.Element;
|
|
22
|
+
declare function useOnboarding(): OnboardingContextValue;
|
|
23
|
+
interface OnboardingStepProps extends HTMLAttributes<HTMLSpanElement> {
|
|
24
|
+
name: string;
|
|
25
|
+
}
|
|
26
|
+
/** An inline anchor. Its children are preserved, not cloned or replaced. */
|
|
27
|
+
declare function OnboardingStep({ name, children, ...props }: OnboardingStepProps): react.JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { OnboardingProvider, OnboardingStep, useOnboarding };
|
|
30
|
+
export type { OnboardingContextValue, OnboardingProviderProps, OnboardingStepProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
// src/index.tsx
|
|
5
|
+
import {
|
|
6
|
+
createContext,
|
|
7
|
+
useCallback,
|
|
8
|
+
useContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useState,
|
|
12
|
+
useSyncExternalStore
|
|
13
|
+
} from "react";
|
|
14
|
+
import {
|
|
15
|
+
createOnboarding
|
|
16
|
+
} from "@formriff/onboarding";
|
|
17
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
18
|
+
var OnboardingContext = createContext(null);
|
|
19
|
+
var serverSnapshot = {
|
|
20
|
+
status: "idle",
|
|
21
|
+
tourId: null,
|
|
22
|
+
stepIndex: 0,
|
|
23
|
+
step: null,
|
|
24
|
+
totalSteps: 0,
|
|
25
|
+
target: null
|
|
26
|
+
};
|
|
27
|
+
var getServerSnapshot = () => serverSnapshot;
|
|
28
|
+
var defaultLabels = {
|
|
29
|
+
next: "Next",
|
|
30
|
+
back: "Back",
|
|
31
|
+
skip: "Skip",
|
|
32
|
+
done: "Done",
|
|
33
|
+
stepOf: (current, total) => `Step ${current} of ${total}`
|
|
34
|
+
};
|
|
35
|
+
function OnboardingProvider({ children, renderStep, ...props }) {
|
|
36
|
+
const [instance] = useState(() => {
|
|
37
|
+
const kit2 = props.kit ?? createOnboarding({
|
|
38
|
+
...props.options,
|
|
39
|
+
...renderStep ? { renderer: "none" } : {}
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
kit: kit2,
|
|
43
|
+
labels: { ...defaultLabels, ...props.options?.labels }
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
const { kit } = instance;
|
|
47
|
+
const subscribe = useCallback((notify) => kit.subscribe(notify), [kit]);
|
|
48
|
+
const state = useSyncExternalStore(subscribe, kit.getState, getServerSnapshot);
|
|
49
|
+
const value = useMemo(() => ({ kit, state }), [kit, state]);
|
|
50
|
+
useEffect(() => kit.mount(), [kit]);
|
|
51
|
+
return /* @__PURE__ */ jsxs(OnboardingContext.Provider, { value, children: [
|
|
52
|
+
children,
|
|
53
|
+
state.status === "running" && renderStep?.({ kit, state, labels: instance.labels })
|
|
54
|
+
] });
|
|
55
|
+
}
|
|
56
|
+
function useOnboarding() {
|
|
57
|
+
const context = useContext(OnboardingContext);
|
|
58
|
+
if (!context) throw new Error("useOnboarding must be used within a OnboardingProvider.");
|
|
59
|
+
return context;
|
|
60
|
+
}
|
|
61
|
+
function OnboardingStep({ name, children, ...props }) {
|
|
62
|
+
return /* @__PURE__ */ jsx("span", { ...props, "data-onboarding": name, children });
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
OnboardingProvider,
|
|
66
|
+
OnboardingStep,
|
|
67
|
+
useOnboarding
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"sourcesContent":["'use client';\n\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n type HTMLAttributes,\n type ReactNode,\n} from 'react';\nimport {\n createOnboarding,\n type Labels,\n type RendererContext,\n type Onboarding,\n type OnboardingOptions,\n type OnboardingState,\n} from '@formriff/onboarding';\n\nexport interface OnboardingContextValue {\n kit: Onboarding;\n state: OnboardingState;\n}\n\nexport type OnboardingProviderProps = (\n { options: OnboardingOptions; kit?: never } | { kit: Onboarding; options?: never }\n) & {\n children?: ReactNode;\n /** Replaces the default renderer for an owned kit. Only called while running. */\n renderStep?: (context: RendererContext) => ReactNode;\n};\n\nconst OnboardingContext = createContext<OnboardingContextValue | null>(null);\n\n// Hydration always starts idle, even if an external store changed before hydration.\nconst serverSnapshot: OnboardingState = {\n status: 'idle',\n tourId: null,\n stepIndex: 0,\n step: null,\n totalSteps: 0,\n target: null,\n};\nconst getServerSnapshot = () => serverSnapshot;\n\nconst defaultLabels: Labels = {\n next: 'Next',\n back: 'Back',\n skip: 'Skip',\n done: 'Done',\n stepOf: (current, total) => `Step ${current} of ${total}`,\n};\n\n/** Options/kit are captured once. Use a React key to deliberately replace a kit. */\nexport function OnboardingProvider({ children, renderStep, ...props }: OnboardingProviderProps) {\n const [instance] = useState(() => {\n const kit =\n props.kit ??\n createOnboarding({\n ...props.options!,\n ...(renderStep ? { renderer: 'none' as const } : {}),\n });\n return {\n kit,\n labels: { ...defaultLabels, ...props.options?.labels },\n };\n });\n const { kit } = instance;\n const subscribe = useCallback((notify: () => void) => kit.subscribe(notify), [kit]);\n const state = useSyncExternalStore(subscribe, kit.getState, getServerSnapshot);\n const value = useMemo(() => ({ kit, state }), [kit, state]);\n\n // mount cleanup is reversible, unlike destroy(). This supports StrictMode replay.\n useEffect(() => kit.mount(), [kit]);\n\n return (\n <OnboardingContext.Provider value={value}>\n {children}\n {state.status === 'running' && renderStep?.({ kit, state, labels: instance.labels })}\n </OnboardingContext.Provider>\n );\n}\n\nexport function useOnboarding(): OnboardingContextValue {\n const context = useContext(OnboardingContext);\n if (!context) throw new Error('useOnboarding must be used within a OnboardingProvider.');\n return context;\n}\n\nexport interface OnboardingStepProps extends HTMLAttributes<HTMLSpanElement> {\n name: string;\n}\n\n/** An inline anchor. Its children are preserved, not cloned or replaced. */\nexport function OnboardingStep({ name, children, ...props }: OnboardingStepProps) {\n return (\n <span {...props} data-onboarding={name}>\n {children}\n </span>\n );\n}\n"],"mappings":";;;;AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,OAMK;AA2DH,SAoBA,KApBA;AA5CJ,IAAM,oBAAoB,cAA6C,IAAI;AAG3E,IAAM,iBAAkC;AAAA,EACtC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,QAAQ;AACV;AACA,IAAM,oBAAoB,MAAM;AAEhC,IAAM,gBAAwB;AAAA,EAC5B,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AAAA,EACN,QAAQ,CAAC,SAAS,UAAU,QAAQ,OAAO,OAAO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|
|
4
|
+
"description": "React bindings for Onboarding Kit",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/FormRiff/onboarding-kit.git",
|
|
9
|
+
"directory": "packages/react"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/FormRiff/onboarding-kit#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/FormRiff/onboarding-kit/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.cjs",
|
|
17
|
+
"module": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/index.d.cts",
|
|
27
|
+
"default": "./dist/index.cjs"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@formriff/onboarding": "^0.1.2"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": ">=18 <20"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup && node ../../scripts/copy-assets.mjs react",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"check:package": "publint && attw --pack"
|
|
50
|
+
}
|
|
6
51
|
}
|