@veroxm/react-sdk 1.1.0 → 1.2.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/CHANGELOG.md +8 -0
- package/dist/form.d.ts +79 -0
- package/dist/form.js +107 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
- Forms: `<VeroxmForm baseUrl form mode />` shows a VeroXM form inline, as a popup (its children open it), full page
|
|
6
|
+
or as a toast in a corner, with the same options as `@veroxm/sdk`'s `embedForm` (hidden fields, size, position,
|
|
7
|
+
`openOn`, `getToken` for forms that need signing in, `onSubmit` and the other callbacks). `useVeroxmForm` gives
|
|
8
|
+
`open`/`close` for your own triggers. Both load `<VeroXM address>/f/embed.js` once, on the client; works in Next.js
|
|
9
|
+
client components.
|
|
10
|
+
|
|
3
11
|
## 1.1.0
|
|
4
12
|
|
|
5
13
|
- `VeroxmTrackerLike.identifyAccount?()`: optional, satisfied by `@veroxm/sdk` 1.1's tracker.
|
package/dist/form.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type CSSProperties, type ReactNode } from 'react';
|
|
2
|
+
export type VeroxmFormMode = 'inline' | 'popup' | 'fullpage' | 'toast';
|
|
3
|
+
/** What @veroxm/sdk's embedForm returns (a structural type: this package doesn't depend on @veroxm/sdk). */
|
|
4
|
+
export interface VeroxmFormHandle {
|
|
5
|
+
readonly iframe: HTMLIFrameElement;
|
|
6
|
+
open(): void;
|
|
7
|
+
close(): void;
|
|
8
|
+
destroy(): void;
|
|
9
|
+
}
|
|
10
|
+
export interface VeroxmFormOptions {
|
|
11
|
+
/** Your VeroXM address, e.g. "https://cms.example.com". */
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
/** The form id (f_...). */
|
|
14
|
+
form: string;
|
|
15
|
+
mode?: VeroxmFormMode;
|
|
16
|
+
hidden?: Record<string, string>;
|
|
17
|
+
/** inline: height in pixels (default 520). */
|
|
18
|
+
height?: number;
|
|
19
|
+
/** inline: grow and shrink with the form. */
|
|
20
|
+
autoHeight?: boolean;
|
|
21
|
+
/** popup: 480, 720 (default) or 960 wide. */
|
|
22
|
+
size?: 'small' | 'medium' | 'large';
|
|
23
|
+
/** toast: which corner. */
|
|
24
|
+
position?: 'bottom-right' | 'bottom-left';
|
|
25
|
+
/** popup and toast: open by themselves. */
|
|
26
|
+
openOn?: {
|
|
27
|
+
on?: 'load' | 'delay' | 'scroll' | 'manual';
|
|
28
|
+
delaySeconds?: number;
|
|
29
|
+
scrollPercent?: number;
|
|
30
|
+
};
|
|
31
|
+
/** toast: once closed, wait this many days before opening by itself again (default 7). */
|
|
32
|
+
rememberDismissDays?: number;
|
|
33
|
+
autoCloseSeconds?: number;
|
|
34
|
+
title?: string;
|
|
35
|
+
/** Forms that need signing in: the person's access token (e.g. `() => app.auth.getAccessToken()`). */
|
|
36
|
+
getToken?: () => string | null | undefined | Promise<string | null | undefined>;
|
|
37
|
+
onReady?: () => void;
|
|
38
|
+
onStarted?: () => void;
|
|
39
|
+
onSubmit?: (result: {
|
|
40
|
+
responseId: string;
|
|
41
|
+
endingId: string | null;
|
|
42
|
+
}) => void;
|
|
43
|
+
onClose?: () => void;
|
|
44
|
+
}
|
|
45
|
+
type EmbedApi = {
|
|
46
|
+
embed(options: Record<string, unknown>): VeroxmFormHandle;
|
|
47
|
+
};
|
|
48
|
+
/** Loads <baseUrl>/f/embed.js once per page. @internal */
|
|
49
|
+
export declare function loadFormEmbed(baseUrl: string): Promise<EmbedApi>;
|
|
50
|
+
/**
|
|
51
|
+
* Embeds a form and returns `open`/`close` (popup, toast) and whether it is ready. `container` is where an inline or
|
|
52
|
+
* fullpage form goes; `trigger` the element that opens a popup or toast.
|
|
53
|
+
*/
|
|
54
|
+
export declare function useVeroxmForm(options: VeroxmFormOptions, refs?: {
|
|
55
|
+
container?: HTMLElement | null;
|
|
56
|
+
trigger?: HTMLElement | null;
|
|
57
|
+
}): {
|
|
58
|
+
open: () => void | undefined;
|
|
59
|
+
close: () => void | undefined;
|
|
60
|
+
ready: boolean;
|
|
61
|
+
error: Error | null;
|
|
62
|
+
};
|
|
63
|
+
export interface VeroxmFormProps extends VeroxmFormOptions {
|
|
64
|
+
/** popup and toast: what opens the form when clicked (a button). Inline forms ignore it. */
|
|
65
|
+
children?: ReactNode;
|
|
66
|
+
className?: string;
|
|
67
|
+
style?: CSSProperties;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A VeroXM form.
|
|
71
|
+
*
|
|
72
|
+
* ```tsx
|
|
73
|
+
* <VeroxmForm baseUrl="https://cms.example.com" form="f_Ab3dEf7hJk9mNpQr2sTu" height={600} />
|
|
74
|
+
* <VeroxmForm baseUrl={url} form={id} mode="popup"><button>Give feedback</button></VeroxmForm>
|
|
75
|
+
* <VeroxmForm baseUrl={url} form={id} mode="toast" openOn={{ on: 'scroll', scrollPercent: 60 }} />
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function VeroxmForm({ children, className, style, ...options }: VeroxmFormProps): import("react").JSX.Element | null;
|
|
79
|
+
export {};
|
package/dist/form.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
|
+
const embedApi = () => window.VeroXMForms;
|
|
5
|
+
const loading = new Map();
|
|
6
|
+
/** Loads <baseUrl>/f/embed.js once per page. @internal */
|
|
7
|
+
export function loadFormEmbed(baseUrl) {
|
|
8
|
+
if (typeof window === 'undefined')
|
|
9
|
+
return Promise.reject(new Error('Forms embed in the browser'));
|
|
10
|
+
const ready = embedApi();
|
|
11
|
+
if (ready)
|
|
12
|
+
return Promise.resolve(ready);
|
|
13
|
+
const src = new URL('/f/embed.js', baseUrl).toString();
|
|
14
|
+
let p = loading.get(src);
|
|
15
|
+
if (!p) {
|
|
16
|
+
p = new Promise((resolve, reject) => {
|
|
17
|
+
const s = document.createElement('script');
|
|
18
|
+
s.src = src;
|
|
19
|
+
s.async = true;
|
|
20
|
+
s.onload = () => {
|
|
21
|
+
const api = embedApi();
|
|
22
|
+
if (api)
|
|
23
|
+
resolve(api);
|
|
24
|
+
else
|
|
25
|
+
reject(new Error('The VeroXM form embed did not load'));
|
|
26
|
+
};
|
|
27
|
+
s.onerror = () => {
|
|
28
|
+
loading.delete(src);
|
|
29
|
+
reject(new Error(`Couldn't load ${src}`));
|
|
30
|
+
};
|
|
31
|
+
document.head.appendChild(s);
|
|
32
|
+
});
|
|
33
|
+
loading.set(src, p);
|
|
34
|
+
}
|
|
35
|
+
return p;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Embeds a form and returns `open`/`close` (popup, toast) and whether it is ready. `container` is where an inline or
|
|
39
|
+
* fullpage form goes; `trigger` the element that opens a popup or toast.
|
|
40
|
+
*/
|
|
41
|
+
export function useVeroxmForm(options, refs = {}) {
|
|
42
|
+
const handle = useRef(null);
|
|
43
|
+
const [ready, setReady] = useState(false);
|
|
44
|
+
const [error, setError] = useState(null);
|
|
45
|
+
// Callbacks change on every render; the embed keeps calling the latest ones.
|
|
46
|
+
const latest = useRef(options);
|
|
47
|
+
latest.current = options;
|
|
48
|
+
const { baseUrl, form, mode = 'inline' } = options;
|
|
49
|
+
// What the embed is made from: a change re-creates it.
|
|
50
|
+
const key = JSON.stringify([baseUrl, form, mode, options.hidden, options.height, options.autoHeight, options.size, options.position, options.openOn, options.rememberDismissDays, options.autoCloseSeconds, options.title]);
|
|
51
|
+
const { container, trigger } = refs;
|
|
52
|
+
const needsContainer = mode === 'inline';
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (needsContainer && !container)
|
|
55
|
+
return;
|
|
56
|
+
let cancelled = false;
|
|
57
|
+
setReady(false);
|
|
58
|
+
loadFormEmbed(baseUrl).then((api) => {
|
|
59
|
+
if (cancelled)
|
|
60
|
+
return;
|
|
61
|
+
const o = latest.current;
|
|
62
|
+
handle.current = api.embed({
|
|
63
|
+
...o,
|
|
64
|
+
baseUrl,
|
|
65
|
+
container: container ?? undefined,
|
|
66
|
+
trigger: trigger ?? undefined,
|
|
67
|
+
getToken: () => latest.current.getToken?.() ?? null,
|
|
68
|
+
onReady: () => {
|
|
69
|
+
setReady(true);
|
|
70
|
+
latest.current.onReady?.();
|
|
71
|
+
},
|
|
72
|
+
onStarted: () => latest.current.onStarted?.(),
|
|
73
|
+
onSubmit: (r) => latest.current.onSubmit?.(r),
|
|
74
|
+
onClose: () => latest.current.onClose?.(),
|
|
75
|
+
});
|
|
76
|
+
}, (e) => !cancelled && setError(e));
|
|
77
|
+
return () => {
|
|
78
|
+
cancelled = true;
|
|
79
|
+
handle.current?.destroy();
|
|
80
|
+
handle.current = null;
|
|
81
|
+
};
|
|
82
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `key` stands for the options the embed is made from
|
|
83
|
+
}, [key, container, trigger, needsContainer]);
|
|
84
|
+
const open = useCallback(() => handle.current?.open(), []);
|
|
85
|
+
const close = useCallback(() => handle.current?.close(), []);
|
|
86
|
+
return { open, close, ready, error };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A VeroXM form.
|
|
90
|
+
*
|
|
91
|
+
* ```tsx
|
|
92
|
+
* <VeroxmForm baseUrl="https://cms.example.com" form="f_Ab3dEf7hJk9mNpQr2sTu" height={600} />
|
|
93
|
+
* <VeroxmForm baseUrl={url} form={id} mode="popup"><button>Give feedback</button></VeroxmForm>
|
|
94
|
+
* <VeroxmForm baseUrl={url} form={id} mode="toast" openOn={{ on: 'scroll', scrollPercent: 60 }} />
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export function VeroxmForm({ children, className, style, ...options }) {
|
|
98
|
+
const [box, setBox] = useState(null);
|
|
99
|
+
const mode = options.mode ?? 'inline';
|
|
100
|
+
const inline = mode === 'inline';
|
|
101
|
+
useVeroxmForm(options, inline ? { container: box } : { trigger: children ? box : null });
|
|
102
|
+
if (inline)
|
|
103
|
+
return _jsx("div", { ref: setBox, className: className, style: style, "data-veroxm-form-box": "" });
|
|
104
|
+
if (!children)
|
|
105
|
+
return null;
|
|
106
|
+
return (_jsx("span", { ref: setBox, className: className, style: { display: 'contents', ...style }, children: children }));
|
|
107
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export type { SliderProps } from './site/slider.js';
|
|
|
40
40
|
export { parseVideoUrl, videoEmbedUrl, videoThumbnailUrl } from './site/video.js';
|
|
41
41
|
export type { VideoRef, VideoProvider } from './site/video.js';
|
|
42
42
|
export type { CollectionFieldMeta, CollectionMeta, Entry, EntryConnection, EntriesQueryOptions, MediaView, MediaHotspot, WhereClause, } from './types.js';
|
|
43
|
+
export { VeroxmForm, useVeroxmForm } from './form.js';
|
|
44
|
+
export type { VeroxmFormProps, VeroxmFormOptions, VeroxmFormMode, VeroxmFormHandle } from './form.js';
|
|
43
45
|
export { VeroxmTrackerProvider, useTracker, usePageViews, useExperience, Experience, useContentView, useRecommendations, } from './tracking.js';
|
|
44
46
|
export type { VeroxmTrackerProviderProps, VeroxmTrackerLike, ExperienceDecision, ExperienceState, ExperienceProps, RecommendationsState, } from './tracking.js';
|
|
45
47
|
export { VeroxmSlot, useSlot, field, configureSlots, loadSlot, mergeSlotValues } from './slots.js';
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,8 @@ export { parseMapLink } from './site/map-math.js';
|
|
|
26
26
|
export { Chart } from './site/chart-view.js';
|
|
27
27
|
export { chartSvg, chartSummary, formatChartNumber, chartFromRows, chartDataFromRows, CHART_TEMPLATES } from './site/chart.js';
|
|
28
28
|
export { parseVideoUrl, videoEmbedUrl, videoThumbnailUrl } from './site/video.js';
|
|
29
|
+
// Forms: inline, popup, full page or toast, through the embed script of your VeroXM address.
|
|
30
|
+
export { VeroxmForm, useVeroxmForm } from './form.js';
|
|
29
31
|
// Experience OS visitor tracking -- pair with @veroxm/sdk's createTracker().
|
|
30
32
|
export { VeroxmTrackerProvider, useTracker, usePageViews, useExperience, Experience, useContentView, useRecommendations, } from './tracking.js';
|
|
31
33
|
// Visual slots (Experience Layer): named regions marketers edit from VeroXM.
|
package/package.json
CHANGED