@microsurveysai/react 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MicroSurveys
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,54 @@
1
+ # @microsurveysai/react
2
+
3
+ React SDK for [MicroSurveys](https://microsurveys.ai). Wrap your app in a provider, fire events, and
4
+ targeted in-app surveys present themselves — or embed a specific survey inline.
5
+
6
+ ```bash
7
+ npm install @microsurveysai/react
8
+ ```
9
+
10
+ ## Auto-triggered surveys
11
+
12
+ ```tsx
13
+ import { MicroSurveysProvider, useMicroSurveys } from "@microsurveysai/react";
14
+
15
+ export default function App({ children }) {
16
+ return <MicroSurveysProvider apiKey="ms_live_…">{children}</MicroSurveysProvider>;
17
+ }
18
+
19
+ function Checkout() {
20
+ const { track, identify } = useMicroSurveys();
21
+ identify("user_123", { plan: "pro" });
22
+ return <button onClick={() => track("checkout_completed")}>Buy</button>;
23
+ }
24
+ ```
25
+
26
+ Eligibility (triggers, audience, sampling, frequency caps) is configured in the dashboard and
27
+ evaluated **on-device**. An eligible survey presents as a bottom sheet or centered dialog per its
28
+ configuration.
29
+
30
+ ## Inline surveys
31
+
32
+ ```tsx
33
+ import { MicroSurvey } from "@microsurveysai/react";
34
+
35
+ <MicroSurvey surveyId="svy_abc" onComplete={(result) => console.log(result)} />;
36
+ ```
37
+
38
+ ## Props
39
+
40
+ `MicroSurveysProvider`:
41
+
42
+ | Prop | Default | Description |
43
+ | --- | --- | --- |
44
+ | `apiKey` | — | Browser-embeddable project key (`ms_live_…` / `ms_test_…`). |
45
+ | `apiBaseURL` | staging | Override for prod / self-hosted. |
46
+ | `autoStart` | `true` | Start on mount (fetch config, flush ingest). |
47
+ | `refreshOnForeground` | `true` | Refresh config when the tab regains focus. |
48
+
49
+ Next.js App Router: the package ships with `"use client"`, so you can render `<MicroSurveysProvider>`
50
+ from a Server Component without extra wrapping.
51
+
52
+ ## License
53
+
54
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,518 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.tsx
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ MicroSurvey: () => MicroSurvey,
25
+ MicroSurveysProvider: () => MicroSurveysProvider,
26
+ useMicroSurveys: () => useMicroSurveys,
27
+ useMicroSurveysClient: () => useMicroSurveysClient
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/provider.tsx
32
+ var import_react3 = require("react");
33
+ var import_web_core = require("@microsurveysai/web-core");
34
+
35
+ // src/overlayHost.tsx
36
+ var import_react2 = require("react");
37
+ var import_react_dom = require("react-dom");
38
+
39
+ // src/SurveyCard.tsx
40
+ var import_react = require("react");
41
+
42
+ // src/questions.tsx
43
+ var import_jsx_runtime = require("react/jsx-runtime");
44
+ function QuestionInput({ question, value, onChange }) {
45
+ const cfg = question.config;
46
+ switch (cfg.kind) {
47
+ case "scale":
48
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ScaleInput, { min: cfg.min, max: cfg.max, minLabel: cfg.minLabel, maxLabel: cfg.maxLabel, value: numValue(value), onPick: (n) => onChange({ value: n }) });
49
+ case "stars":
50
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(StarsInput, { count: cfg.count, value: numValue(value), onPick: (n) => onChange({ value: n }) });
51
+ case "emoji":
52
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EmojiInput, { options: cfg.options, value: strValue(value), onPick: (v) => onChange({ value: v }) });
53
+ case "thumbs":
54
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ThumbsInput, { value: strValue(value), onPick: (v) => onChange({ value: v }) });
55
+ case "singleChoice":
56
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ChoiceInput, { options: cfg.options, value: value && "choice" in value ? value.choice : void 0, onPick: (v) => onChange({ choice: v }) });
57
+ case "openText":
58
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OpenTextInput, { placeholder: cfg.placeholder, maxLength: cfg.maxLength, value: value && "text" in value ? value.text : "", onInput: (t) => onChange({ text: t }) });
59
+ }
60
+ }
61
+ function isAnswered(value) {
62
+ if (!value) return false;
63
+ if ("text" in value) return value.text.trim().length > 0;
64
+ return true;
65
+ }
66
+ function numValue(v) {
67
+ return v && "value" in v && typeof v.value === "number" ? v.value : void 0;
68
+ }
69
+ function strValue(v) {
70
+ return v && "value" in v && typeof v.value === "string" ? v.value : void 0;
71
+ }
72
+ function ScaleInput({ min, max, minLabel, maxLabel, value, onPick }) {
73
+ const points = [];
74
+ for (let i = min; i <= max; i++) points.push(i);
75
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
76
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ms-scale", role: "group", children: points.map((n) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "ms-chip", "aria-pressed": value === n, onClick: () => onPick(n), children: n }, n)) }),
77
+ (minLabel || maxLabel) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "ms-endlabels", children: [
78
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: minLabel }),
79
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: maxLabel })
80
+ ] })
81
+ ] });
82
+ }
83
+ function StarsInput({ count, value, onPick }) {
84
+ const stars = [];
85
+ for (let i = 1; i <= count; i++) stars.push(i);
86
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ms-stars", role: "group", children: stars.map((n) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: `ms-star${value !== void 0 && n <= value ? " ms-on" : ""}`, "aria-label": `${n} star${n > 1 ? "s" : ""}`, "aria-pressed": value === n, onClick: () => onPick(n), children: "\u2605" }, n)) });
87
+ }
88
+ function EmojiInput({ options, value, onPick }) {
89
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ms-emoji-row", role: "group", children: options.map((o) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", { type: "button", className: "ms-emoji", "aria-pressed": value === o.value, onClick: () => onPick(o.value), children: [
90
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "ms-emoji-glyph", children: o.emoji }),
91
+ o.label && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "ms-emoji-label", children: o.label })
92
+ ] }, o.value)) });
93
+ }
94
+ function ThumbsInput({ value, onPick }) {
95
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "ms-thumbs", role: "group", children: [
96
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "ms-thumb", "aria-label": "Thumbs up", "aria-pressed": value === "up", onClick: () => onPick("up"), children: "\u{1F44D}" }),
97
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "ms-thumb", "aria-label": "Thumbs down", "aria-pressed": value === "down", onClick: () => onPick("down"), children: "\u{1F44E}" })
98
+ ] });
99
+ }
100
+ function ChoiceInput({ options, value, onPick }) {
101
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ms-choices", role: "group", children: options.map((o) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "ms-choice", "aria-pressed": value === o.value, onClick: () => onPick(o.value), children: o.label }, o.value)) });
102
+ }
103
+ function OpenTextInput({ placeholder, maxLength, value, onInput }) {
104
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
105
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("textarea", { className: "ms-textarea", placeholder, maxLength, value, onChange: (e) => onInput(e.target.value) }),
106
+ maxLength !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "ms-counter", children: [
107
+ value.length,
108
+ " / ",
109
+ maxLength
110
+ ] })
111
+ ] });
112
+ }
113
+
114
+ // src/SurveyCard.tsx
115
+ var import_jsx_runtime2 = require("react/jsx-runtime");
116
+ var SUCCESS_MS = 1400;
117
+ function SurveyCard({ survey, onClose }) {
118
+ const questions = (0, import_react.useMemo)(() => [...survey.questions].sort((a, b) => a.order - b.order), [survey]);
119
+ const dismissible = survey.dismissible !== false;
120
+ const [step, setStep] = (0, import_react.useState)(0);
121
+ const [answers, setAnswers] = (0, import_react.useState)({});
122
+ const [done, setDone] = (0, import_react.useState)(false);
123
+ const question = questions[step];
124
+ const isLast = step >= questions.length - 1;
125
+ if (!question) {
126
+ return null;
127
+ }
128
+ const current = answers[question.id];
129
+ const canAdvance = !question.required || isAnswered(current);
130
+ function collected() {
131
+ return questions.filter((q) => answers[q.id] !== void 0).map((q) => ({ questionId: q.id, value: answers[q.id] }));
132
+ }
133
+ function setAnswer(value) {
134
+ setAnswers((prev) => ({ ...prev, [question.id]: value }));
135
+ }
136
+ function advance() {
137
+ if (!canAdvance) return;
138
+ if (isLast) {
139
+ finish();
140
+ } else {
141
+ setStep((s) => s + 1);
142
+ }
143
+ }
144
+ function finish() {
145
+ const result = { answers: collected(), completed: true, dismissed: false };
146
+ setDone(true);
147
+ setTimeout(() => onClose(result), SUCCESS_MS);
148
+ }
149
+ function dismiss() {
150
+ onClose({ answers: collected(), completed: false, dismissed: true });
151
+ }
152
+ if (done) {
153
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "ms-card", role: "dialog", "aria-live": "polite", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "ms-success", children: [
154
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "ms-success-check", children: "\u2713" }),
155
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: survey.successMessage ?? "Thanks for your feedback!" })
156
+ ] }) });
157
+ }
158
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "ms-card", role: "dialog", "aria-modal": "true", "aria-label": survey.name, children: [
159
+ dismissible && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "ms-close", "aria-label": "Close", onClick: dismiss, children: "\u2715" }),
160
+ questions.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("p", { className: "ms-progress", children: [
161
+ step + 1,
162
+ " of ",
163
+ questions.length
164
+ ] }),
165
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { className: "ms-prompt", children: question.prompt }),
166
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(QuestionInput, { question, value: current, onChange: setAnswer }),
167
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "ms-footer", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "ms-btn", disabled: !canAdvance, onClick: advance, children: isLast ? "Submit" : "Next" }) })
168
+ ] });
169
+ }
170
+
171
+ // src/theme.ts
172
+ var DEFAULTS = {
173
+ scrim: "rgba(9, 9, 11, 0.45)",
174
+ surface: "#FFFFFF",
175
+ text: "#18181B",
176
+ secondaryText: "#71717A",
177
+ accent: "#6366F1",
178
+ // indigo-500
179
+ accentText: "#FFFFFF",
180
+ border: "#E4E4E7",
181
+ track: "#F4F4F5",
182
+ cardRadius: 16,
183
+ controlRadius: 12,
184
+ controlHeight: 48,
185
+ spacing: 16,
186
+ textScale: 1,
187
+ titleSize: 20,
188
+ titleWeight: "semibold",
189
+ titleLineHeight: 1.1,
190
+ titleLetterSpacing: 0
191
+ };
192
+ var WEIGHTS = { regular: 400, medium: 500, semibold: 600, bold: 700 };
193
+ var CURATED_FONTS = /* @__PURE__ */ new Set(["Inter", "Roboto", "Open Sans", "Noto Sans", "Lato", "Montserrat", "Poppins"]);
194
+ function px(n) {
195
+ return `${n}px`;
196
+ }
197
+ function resolveTheme(theme) {
198
+ const t = theme ?? {};
199
+ const controlHeight = t.controlHeight ?? DEFAULTS.controlHeight;
200
+ const controlRadius = t.controlPill ? controlHeight / 2 : t.controlRadius ?? DEFAULTS.controlRadius;
201
+ const cardRadius = t.cornerRadius ?? DEFAULTS.cardRadius;
202
+ const titleWeight = WEIGHTS[t.titleWeight ?? DEFAULTS.titleWeight] ?? 600;
203
+ const font = t.font && t.font !== "system" && t.font.length > 0 ? t.font : null;
204
+ const googleFont = font && CURATED_FONTS.has(font) ? font : null;
205
+ const fontFamily = font ? `'${font}', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif` : "system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif";
206
+ const vars = {
207
+ "--ms-scrim": DEFAULTS.scrim,
208
+ "--ms-surface": t.surface ?? DEFAULTS.surface,
209
+ "--ms-text": t.text ?? DEFAULTS.text,
210
+ "--ms-secondary-text": t.secondaryText ?? DEFAULTS.secondaryText,
211
+ "--ms-accent": t.accent ?? DEFAULTS.accent,
212
+ "--ms-accent-text": t.accentText ?? DEFAULTS.accentText,
213
+ "--ms-border": t.border ?? DEFAULTS.border,
214
+ "--ms-track": DEFAULTS.track,
215
+ "--ms-card-radius": px(cardRadius),
216
+ "--ms-control-radius": px(controlRadius),
217
+ "--ms-control-height": px(controlHeight),
218
+ "--ms-spacing": px(t.spacing ?? DEFAULTS.spacing),
219
+ "--ms-text-scale": String(t.textScale ?? DEFAULTS.textScale),
220
+ "--ms-title-size": px(t.titleSize ?? DEFAULTS.titleSize),
221
+ "--ms-title-weight": String(titleWeight),
222
+ "--ms-title-line-height": String(t.titleLineHeight ?? DEFAULTS.titleLineHeight),
223
+ "--ms-title-letter-spacing": px(t.titleLetterSpacing ?? DEFAULTS.titleLetterSpacing),
224
+ "--ms-font": fontFamily,
225
+ "--ms-align": t.alignment === "center" ? "center" : "left"
226
+ };
227
+ return { vars, googleFont };
228
+ }
229
+ function resolvePosition(presentation, theme) {
230
+ const p = presentation ?? null;
231
+ if (p === "sheet") return "sheet";
232
+ if (p === "dialog") return "dialog";
233
+ return theme?.position === "center" ? "dialog" : "sheet";
234
+ }
235
+ function ensureGoogleFont(family) {
236
+ if (!family || typeof document === "undefined") return;
237
+ const id = `ms-font-${family.replace(/\s+/g, "-").toLowerCase()}`;
238
+ if (document.getElementById(id)) return;
239
+ const link = document.createElement("link");
240
+ link.id = id;
241
+ link.rel = "stylesheet";
242
+ link.href = `https://fonts.googleapis.com/css2?family=${encodeURIComponent(family)}:wght@400;500;600;700&display=swap`;
243
+ document.head.appendChild(link);
244
+ }
245
+
246
+ // src/styles.ts
247
+ var STYLE_ID = "microsurveys-styles";
248
+ var CSS = `
249
+ .ms-root, .ms-root * { box-sizing: border-box; }
250
+ .ms-root {
251
+ font-family: var(--ms-font);
252
+ color: var(--ms-text);
253
+ text-align: var(--ms-align);
254
+ -webkit-font-smoothing: antialiased;
255
+ }
256
+
257
+ /* \u2500\u2500 Overlay \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
258
+ .ms-scrim {
259
+ position: fixed; inset: 0; z-index: 2147483000;
260
+ display: flex; padding: var(--ms-spacing);
261
+ background: var(--ms-scrim);
262
+ animation: ms-fade 160ms ease-out;
263
+ }
264
+ .ms-scrim.ms-sheet { align-items: flex-end; justify-content: center; }
265
+ .ms-scrim.ms-dialog { align-items: center; justify-content: center; }
266
+
267
+ .ms-card {
268
+ position: relative; width: 100%; max-width: 480px;
269
+ background: var(--ms-surface);
270
+ border-radius: var(--ms-card-radius);
271
+ border: 1px solid var(--ms-border);
272
+ box-shadow: 0 12px 40px rgba(9, 9, 11, 0.18);
273
+ padding: calc(var(--ms-spacing) * 1.5);
274
+ animation: ms-rise 200ms cubic-bezier(0.16, 1, 0.3, 1);
275
+ }
276
+ .ms-inline .ms-card {
277
+ box-shadow: none; animation: none; max-width: none;
278
+ }
279
+ .ms-inline { display: block; }
280
+
281
+ @keyframes ms-fade { from { opacity: 0; } to { opacity: 1; } }
282
+ @keyframes ms-rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
283
+
284
+ /* \u2500\u2500 Header \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
285
+ .ms-prompt {
286
+ margin: 0 0 var(--ms-spacing);
287
+ font-size: var(--ms-title-size);
288
+ font-weight: var(--ms-title-weight);
289
+ line-height: var(--ms-title-line-height);
290
+ letter-spacing: var(--ms-title-letter-spacing);
291
+ color: var(--ms-text);
292
+ padding-right: 28px;
293
+ }
294
+ .ms-close {
295
+ position: absolute; top: 14px; right: 14px;
296
+ width: 28px; height: 28px; border: none; cursor: pointer;
297
+ background: transparent; color: var(--ms-secondary-text);
298
+ border-radius: 8px; font-size: 18px; line-height: 1;
299
+ display: inline-flex; align-items: center; justify-content: center;
300
+ }
301
+ .ms-close:hover { background: var(--ms-track); color: var(--ms-text); }
302
+
303
+ .ms-progress {
304
+ font-size: calc(12px * var(--ms-text-scale));
305
+ color: var(--ms-secondary-text);
306
+ margin: 0 0 8px;
307
+ }
308
+
309
+ /* \u2500\u2500 Scale (NPS / CES) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
310
+ .ms-scale { display: flex; flex-wrap: wrap; gap: 8px; }
311
+ .ms-chip {
312
+ flex: 1 1 auto; min-width: var(--ms-control-height);
313
+ height: var(--ms-control-height);
314
+ border: 1px solid var(--ms-border); background: var(--ms-surface);
315
+ color: var(--ms-text); border-radius: var(--ms-control-radius);
316
+ cursor: pointer; font-size: calc(15px * var(--ms-text-scale));
317
+ font-weight: 500; transition: background 120ms, border-color 120ms;
318
+ }
319
+ .ms-chip:hover { border-color: var(--ms-accent); }
320
+ .ms-chip[aria-pressed="true"] {
321
+ background: var(--ms-accent); border-color: var(--ms-accent); color: var(--ms-accent-text);
322
+ }
323
+ .ms-endlabels {
324
+ display: flex; justify-content: space-between; gap: 12px;
325
+ margin-top: 8px; font-size: calc(12px * var(--ms-text-scale));
326
+ color: var(--ms-secondary-text);
327
+ }
328
+
329
+ /* \u2500\u2500 Stars \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
330
+ .ms-stars { display: flex; gap: 6px; }
331
+ .ms-star {
332
+ background: none; border: none; cursor: pointer; padding: 2px;
333
+ font-size: calc(30px * var(--ms-text-scale)); line-height: 1;
334
+ color: var(--ms-border); transition: color 100ms;
335
+ }
336
+ .ms-star.ms-on { color: var(--ms-accent); }
337
+
338
+ /* \u2500\u2500 Emoji \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
339
+ .ms-emoji-row { display: flex; flex-wrap: wrap; gap: 8px; }
340
+ .ms-emoji {
341
+ flex: 1 1 auto; display: flex; flex-direction: column; align-items: center; gap: 4px;
342
+ padding: 10px 6px; border: 1px solid var(--ms-border); background: var(--ms-surface);
343
+ border-radius: var(--ms-control-radius); cursor: pointer;
344
+ }
345
+ .ms-emoji:hover { border-color: var(--ms-accent); }
346
+ .ms-emoji[aria-pressed="true"] { border-color: var(--ms-accent); background: color-mix(in srgb, var(--ms-accent) 10%, var(--ms-surface)); }
347
+ .ms-emoji-glyph { font-size: calc(26px * var(--ms-text-scale)); line-height: 1; }
348
+ .ms-emoji-label { font-size: calc(12px * var(--ms-text-scale)); color: var(--ms-secondary-text); }
349
+
350
+ /* \u2500\u2500 Thumbs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
351
+ .ms-thumbs { display: flex; gap: 12px; }
352
+ .ms-thumb {
353
+ flex: 1; height: calc(var(--ms-control-height) + 8px);
354
+ border: 1px solid var(--ms-border); background: var(--ms-surface);
355
+ border-radius: var(--ms-control-radius); cursor: pointer;
356
+ font-size: calc(22px * var(--ms-text-scale));
357
+ }
358
+ .ms-thumb:hover { border-color: var(--ms-accent); }
359
+ .ms-thumb[aria-pressed="true"] { border-color: var(--ms-accent); background: color-mix(in srgb, var(--ms-accent) 12%, var(--ms-surface)); }
360
+
361
+ /* \u2500\u2500 Single choice \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
362
+ .ms-choices { display: flex; flex-direction: column; gap: 8px; }
363
+ .ms-choice {
364
+ min-height: var(--ms-control-height); padding: 12px 14px; text-align: left;
365
+ border: 1px solid var(--ms-border); background: var(--ms-surface);
366
+ color: var(--ms-text); border-radius: var(--ms-control-radius);
367
+ cursor: pointer; font-size: calc(15px * var(--ms-text-scale)); font-weight: 500;
368
+ }
369
+ .ms-choice:hover { border-color: var(--ms-accent); }
370
+ .ms-choice[aria-pressed="true"] { border-color: var(--ms-accent); background: color-mix(in srgb, var(--ms-accent) 10%, var(--ms-surface)); }
371
+
372
+ /* \u2500\u2500 Open text \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
373
+ .ms-textarea {
374
+ width: 100%; min-height: 96px; resize: vertical; padding: 12px 14px;
375
+ border: 1px solid var(--ms-border); border-radius: var(--ms-control-radius);
376
+ background: var(--ms-surface); color: var(--ms-text);
377
+ font: inherit; font-size: calc(15px * var(--ms-text-scale));
378
+ }
379
+ .ms-textarea:focus { outline: none; border-color: var(--ms-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ms-accent) 20%, transparent); }
380
+ .ms-counter { margin-top: 6px; font-size: calc(12px * var(--ms-text-scale)); color: var(--ms-secondary-text); text-align: right; }
381
+
382
+ /* \u2500\u2500 Footer / button \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
383
+ .ms-footer { margin-top: calc(var(--ms-spacing) * 1.25); }
384
+ .ms-btn {
385
+ width: 100%; height: var(--ms-control-height);
386
+ border: none; border-radius: var(--ms-control-radius);
387
+ background: var(--ms-accent); color: var(--ms-accent-text);
388
+ font-size: calc(15px * var(--ms-text-scale)); font-weight: 600; cursor: pointer;
389
+ transition: opacity 120ms;
390
+ }
391
+ .ms-btn:hover { opacity: 0.92; }
392
+ .ms-btn:disabled { opacity: 0.45; cursor: not-allowed; }
393
+
394
+ /* \u2500\u2500 Success \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
395
+ .ms-success {
396
+ display: flex; flex-direction: column; align-items: center; gap: 10px;
397
+ padding: calc(var(--ms-spacing) * 1.5) var(--ms-spacing);
398
+ text-align: center;
399
+ }
400
+ .ms-success-check {
401
+ width: 44px; height: 44px; border-radius: 50%;
402
+ background: var(--ms-accent); color: var(--ms-accent-text);
403
+ display: flex; align-items: center; justify-content: center; font-size: 24px;
404
+ }
405
+ `;
406
+ function ensureStyles() {
407
+ if (typeof document === "undefined") return;
408
+ if (document.getElementById(STYLE_ID)) return;
409
+ const el = document.createElement("style");
410
+ el.id = STYLE_ID;
411
+ el.textContent = CSS;
412
+ document.head.appendChild(el);
413
+ }
414
+
415
+ // src/overlayHost.tsx
416
+ var import_jsx_runtime3 = require("react/jsx-runtime");
417
+ function OverlayHost({ client }) {
418
+ const [active, setActive] = (0, import_react2.useState)(null);
419
+ (0, import_react2.useEffect)(() => {
420
+ if (!client) return;
421
+ return client.onPresent((a) => setActive(a));
422
+ }, [client]);
423
+ (0, import_react2.useEffect)(() => {
424
+ if (!active) return;
425
+ const onKey = (e) => {
426
+ if (e.key === "Escape" && active.survey.dismissible !== false) close({ answers: [], completed: false, dismissed: true });
427
+ };
428
+ document.addEventListener("keydown", onKey);
429
+ return () => document.removeEventListener("keydown", onKey);
430
+ }, [active]);
431
+ if (!active || !client || typeof document === "undefined") return null;
432
+ ensureStyles();
433
+ const theme = client.getTheme();
434
+ const { vars, googleFont } = resolveTheme(theme);
435
+ ensureGoogleFont(googleFont);
436
+ const position = resolvePosition(active.survey.presentation, theme);
437
+ function close(result) {
438
+ active.close(result);
439
+ setActive(null);
440
+ }
441
+ function onScrimClick(e) {
442
+ if (e.target === e.currentTarget && active.survey.dismissible !== false) {
443
+ close({ answers: [], completed: false, dismissed: true });
444
+ }
445
+ }
446
+ return (0, import_react_dom.createPortal)(
447
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "ms-root", style: vars, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: `ms-scrim ms-${position}`, onClick: onScrimClick, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SurveyCard, { survey: active.survey, onClose: close }) }) }),
448
+ document.body
449
+ );
450
+ }
451
+
452
+ // src/provider.tsx
453
+ var import_jsx_runtime4 = require("react/jsx-runtime");
454
+ var ClientContext = (0, import_react3.createContext)(null);
455
+ function MicroSurveysProvider({ apiKey, apiBaseURL, refreshOnForeground, autoStart = true, children }) {
456
+ const [client] = (0, import_react3.useState)(
457
+ () => typeof window === "undefined" ? null : (0, import_web_core.createClient)({ apiKey, apiBaseURL, refreshOnForeground })
458
+ );
459
+ (0, import_react3.useEffect)(() => {
460
+ if (!client) return;
461
+ ensureStyles();
462
+ if (autoStart) client.start();
463
+ return () => client.stop();
464
+ }, [client, autoStart]);
465
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ClientContext.Provider, { value: client, children: [
466
+ children,
467
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(OverlayHost, { client })
468
+ ] });
469
+ }
470
+ function useMicroSurveysClient() {
471
+ return (0, import_react3.useContext)(ClientContext);
472
+ }
473
+ function useMicroSurveys() {
474
+ const client = (0, import_react3.useContext)(ClientContext);
475
+ return (0, import_react3.useMemo)(
476
+ () => ({
477
+ client,
478
+ track: (name, properties) => client?.track(name, properties),
479
+ identify: (userId, properties) => client?.identify(userId, properties),
480
+ refresh: () => client?.refreshConfig()
481
+ }),
482
+ [client]
483
+ );
484
+ }
485
+
486
+ // src/MicroSurvey.tsx
487
+ var import_react4 = require("react");
488
+ var import_jsx_runtime5 = require("react/jsx-runtime");
489
+ function MicroSurvey({ surveyId, onComplete }) {
490
+ const client = useMicroSurveysClient();
491
+ const [survey, setSurvey] = (0, import_react4.useState)(null);
492
+ const [closed, setClosed] = (0, import_react4.useState)(false);
493
+ const shownAt = (0, import_react4.useRef)(null);
494
+ (0, import_react4.useEffect)(() => {
495
+ if (!client) return;
496
+ setSurvey(client.getSurvey(surveyId) ?? null);
497
+ return client.onConfigChange(() => setSurvey(client.getSurvey(surveyId) ?? null));
498
+ }, [client, surveyId]);
499
+ if (!client || !survey || closed) return null;
500
+ ensureStyles();
501
+ if (!shownAt.current) shownAt.current = /* @__PURE__ */ new Date();
502
+ const { vars, googleFont } = resolveTheme(client.getTheme());
503
+ ensureGoogleFont(googleFont);
504
+ function close(result) {
505
+ client.recordInline(survey, result, shownAt.current ?? /* @__PURE__ */ new Date());
506
+ setClosed(true);
507
+ onComplete?.(result);
508
+ }
509
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "ms-root ms-inline", style: vars, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SurveyCard, { survey, onClose: close }) });
510
+ }
511
+ // Annotate the CommonJS export names for ESM import in node:
512
+ 0 && (module.exports = {
513
+ MicroSurvey,
514
+ MicroSurveysProvider,
515
+ useMicroSurveys,
516
+ useMicroSurveysClient
517
+ });
518
+ //# sourceMappingURL=index.cjs.map