@perspective-ai/sdk 1.1.0 → 1.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/dist/index.d.ts CHANGED
@@ -8,6 +8,18 @@ export { BRAND_KEYS, BrandKey, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_T
8
8
  */
9
9
 
10
10
  type EmbedType = "widget" | "popup" | "slider" | "float" | "fullpage" | "chat";
11
+ type TriggerConfig = {
12
+ type: "timeout";
13
+ delay: number;
14
+ } | {
15
+ type: "exit-intent";
16
+ };
17
+ type TriggerType = TriggerConfig["type"];
18
+ type ShowOnce = "session" | "visitor" | false;
19
+ interface AutoOpenConfig {
20
+ trigger: TriggerConfig;
21
+ showOnce?: ShowOnce;
22
+ }
11
23
  /** Brand colors that can be passed via embed code */
12
24
  interface BrandColors {
13
25
  /** Primary accent color (buttons, links, focus states) */
@@ -35,6 +47,8 @@ interface EmbedConfig {
35
47
  theme?: ThemeValue;
36
48
  /** Override the default host (defaults to https://getperspective.ai) */
37
49
  host?: string;
50
+ /** Auto-open trigger configuration (popup only) */
51
+ autoOpen?: AutoOpenConfig;
38
52
  /** Callback when embed is ready */
39
53
  onReady?: () => void;
40
54
  /** Callback when interview is submitted/completed */
@@ -137,6 +151,51 @@ declare const createChatBubble: typeof createFloatBubble;
137
151
 
138
152
  declare function createFullpage(config: EmbedConfig): EmbedHandle;
139
153
 
154
+ /**
155
+ * Auto-open trigger system for popup embeds.
156
+ *
157
+ * Supports:
158
+ * - Timeout: Open after a delay (ms)
159
+ * - Exit intent: Open when user moves cursor above viewport
160
+ *
161
+ * Show-once dedup:
162
+ * - "session" -> sessionStorage
163
+ * - "visitor" -> localStorage
164
+ * - false -> always show
165
+ */
166
+
167
+ /**
168
+ * Parse a trigger attribute value into a TriggerConfig.
169
+ *
170
+ * Formats:
171
+ * - "timeout:5000" -> { type: "timeout", delay: 5000 }
172
+ * - "exit-intent" -> { type: "exit-intent" }
173
+ */
174
+ declare function parseTriggerAttr(value: string): TriggerConfig;
175
+ /**
176
+ * Set up a trigger that calls `callback` when fired.
177
+ * Returns a cleanup function to teardown the trigger.
178
+ */
179
+ declare function setupTrigger(config: TriggerConfig, callback: () => void): () => void;
180
+ /**
181
+ * Parse a show-once attribute value into a ShowOnce.
182
+ *
183
+ * Formats:
184
+ * - "session" -> "session"
185
+ * - "visitor" -> "visitor"
186
+ * - "false" -> false
187
+ * - anything else -> defaults to "session"
188
+ */
189
+ declare function parseShowOnceAttr(value: string | null): ShowOnce;
190
+ /**
191
+ * Check if the popup should be shown based on show-once dedup.
192
+ */
193
+ declare function shouldShow(researchId: string, showOnce: ShowOnce): boolean;
194
+ /**
195
+ * Mark the popup as shown for dedup purposes.
196
+ */
197
+ declare function markShown(researchId: string, showOnce: ShowOnce): void;
198
+
140
199
  /**
141
200
  * Embed SDK configuration
142
201
  * SSR-safe - DOM access is guarded and lazy
@@ -152,4 +211,4 @@ declare function configure(config: SDKConfig): void;
152
211
  */
153
212
  declare function getConfig(): SDKConfig;
154
213
 
155
- export { type BrandColors, type EmbedConfig, type EmbedError, type EmbedHandle, type EmbedInstance, type EmbedType, type FloatHandle, type ModalHandle, type SDKConfig, type ThemeConfig, ThemeValue, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, openPopup, openSlider };
214
+ export { type AutoOpenConfig, type BrandColors, type EmbedConfig, type EmbedError, type EmbedHandle, type EmbedInstance, type EmbedType, type FloatHandle, type ModalHandle, type SDKConfig, type ShowOnce, type ThemeConfig, ThemeValue, type TriggerConfig, type TriggerType, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, markShown, openPopup, openSlider, parseShowOnceAttr, parseTriggerAttr, setupTrigger, shouldShow };
package/dist/index.js CHANGED
@@ -56,7 +56,7 @@ if (hasDom()) {
56
56
  }
57
57
 
58
58
  // src/constants.ts
59
- var SDK_VERSION = "1.1.0";
59
+ var SDK_VERSION = "1.1.2";
60
60
  var FEATURES = {
61
61
  RESIZE: 1 << 0,
62
62
  // 0b0001
@@ -102,7 +102,9 @@ var DATA_ATTRS = {
102
102
  brand: "data-perspective-brand",
103
103
  brandDark: "data-perspective-brand-dark",
104
104
  theme: "data-perspective-theme",
105
- noStyle: "data-perspective-no-style"
105
+ noStyle: "data-perspective-no-style",
106
+ autoOpen: "data-perspective-auto-open",
107
+ showOnce: "data-perspective-show-once"
106
108
  };
107
109
  var MESSAGE_TYPES = {
108
110
  // SDK -> Iframe (initialization)
@@ -131,7 +133,8 @@ var THEME_VALUES = {
131
133
  system: "system"
132
134
  };
133
135
  var STORAGE_KEYS = {
134
- anonId: "perspective-anon-id"
136
+ anonId: "perspective-anon-id",
137
+ triggerShown: "perspective-trigger-shown"
135
138
  };
136
139
 
137
140
  // src/utils.ts
@@ -1552,6 +1555,74 @@ function createFullpage(config) {
1552
1555
  };
1553
1556
  }
1554
1557
 
1555
- export { BRAND_KEYS, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_TYPES, PARAM_KEYS, SDK_VERSION, THEME_VALUES, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, openPopup, openSlider };
1558
+ // src/triggers.ts
1559
+ function parseTriggerAttr(value) {
1560
+ const trimmed = value.trim();
1561
+ if (trimmed.startsWith("timeout:")) {
1562
+ const delay = parseInt(trimmed.slice("timeout:".length), 10);
1563
+ if (isNaN(delay) || delay < 0) {
1564
+ throw new Error(`Invalid timeout delay: "${value}"`);
1565
+ }
1566
+ return { type: "timeout", delay };
1567
+ }
1568
+ if (trimmed === "timeout") {
1569
+ return { type: "timeout", delay: 5e3 };
1570
+ }
1571
+ if (trimmed === "exit-intent") {
1572
+ return { type: "exit-intent" };
1573
+ }
1574
+ throw new Error(
1575
+ `Unknown trigger type: "${value}". Expected "timeout:<ms>" or "exit-intent".`
1576
+ );
1577
+ }
1578
+ function setupTrigger(config, callback) {
1579
+ if (config.type === "timeout") {
1580
+ const timer = setTimeout(callback, config.delay);
1581
+ return () => clearTimeout(timer);
1582
+ }
1583
+ if (config.type === "exit-intent") {
1584
+ const handler = (e) => {
1585
+ if (e.clientY <= 0) {
1586
+ callback();
1587
+ document.removeEventListener("mouseleave", handler);
1588
+ }
1589
+ };
1590
+ document.addEventListener("mouseleave", handler);
1591
+ return () => document.removeEventListener("mouseleave", handler);
1592
+ }
1593
+ const _exhaustive = config;
1594
+ throw new Error(
1595
+ `Unknown trigger type: ${_exhaustive.type}`
1596
+ );
1597
+ }
1598
+ function storageKey(researchId) {
1599
+ return `${STORAGE_KEYS.triggerShown}:${researchId}`;
1600
+ }
1601
+ function parseShowOnceAttr(value) {
1602
+ if (!value) return "session";
1603
+ const trimmed = value.trim();
1604
+ if (trimmed === "visitor") return "visitor";
1605
+ if (trimmed === "false") return false;
1606
+ return "session";
1607
+ }
1608
+ function shouldShow(researchId, showOnce) {
1609
+ if (showOnce === false) return true;
1610
+ try {
1611
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
1612
+ return storage.getItem(storageKey(researchId)) === null;
1613
+ } catch {
1614
+ return true;
1615
+ }
1616
+ }
1617
+ function markShown(researchId, showOnce) {
1618
+ if (showOnce === false) return;
1619
+ try {
1620
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
1621
+ storage.setItem(storageKey(researchId), "1");
1622
+ } catch {
1623
+ }
1624
+ }
1625
+
1626
+ export { BRAND_KEYS, CURRENT_FEATURES, DATA_ATTRS, FEATURES, MESSAGE_TYPES, PARAM_KEYS, SDK_VERSION, THEME_VALUES, configure, createChatBubble, createFloatBubble, createFullpage, createWidget, getConfig, markShown, openPopup, openSlider, parseShowOnceAttr, parseTriggerAttr, setupTrigger, shouldShow };
1556
1627
  //# sourceMappingURL=index.js.map
1557
1628
  //# sourceMappingURL=index.js.map