# Web widget

> Install @mostlovedproduct/web, theme the widget with CSS variables, attach user ids, A/B flags and page state, or go headless.

Source: https://mostlovedproduct.com/docs/web

`@mostlovedproduct/web` is a small feedback widget for web apps: a floating button, a short form, and at most two follow-up questions when they make a report more useful. It renders in a shadow DOM, so your styles don't leak in and its styles don't leak out.

Nothing is sent until someone files a report. No cookies, no session replay, no background events.

## Install

With a bundler:

```bash
npm i @mostlovedproduct/web
```

```ts
import { init } from "@mostlovedproduct/web";

const widget = init({ key: "mlp_pk_..." });
```

With a script tag:

```html
<script>
  window.mlp = window.mlp || function () { (mlp.q = mlp.q || []).push(arguments) };
</script>
<script async src="https://cdn.mostlovedproduct.com/mlp.js" data-key="mlp_pk_..."></script>
<script>
  mlp("init", {});
</script>
```

Calls made before the script loads are queued and replayed.

## Configuration

| Option | Default | What it does |
|--------|---------|--------------|
| `key` | required | Publishable key, `mlp_pk_…` |
| `user`, `flags`, `app`, `context` | none | Context attached to every report (see below) |
| `getContext` | none | Function evaluated at submit time for current state |
| `page` | `true` | Attach the page path and title. Query string and fragment are dropped unless you opt in |
| `followUps` | `1` | Follow-up questions this UI may show: `0`, `1` or `2` |
| `kinds` | all five | Which kinds to offer: `broken`, `confusing`, `missing`, `idea`, `praise` |
| `trigger` | `true` | Floating button options, or `false` to open it from your own button |
| `theme` | auto | Colours, radius and font (or use CSS variables) |
| `beforeSend` | none | Edit or drop a report before it is sent. Return `null` to drop |
| `ui` | `true` | `false` for headless use |

## Theming

The widget reads CSS custom properties from your page, so it can inherit your design tokens:

```css
:root {
  --mlp-font-family: var(--font-sans);
  --mlp-accent: #0f766e;
  --mlp-radius: 10px;
  --mlp-surface: #ffffff;
  --mlp-text: #111827;
}
[data-theme="dark"] {
  --mlp-surface: #111418;
  --mlp-text: #f3f4f6;
}
```

Other properties include `--mlp-muted`, `--mlp-border`, `--mlp-shadow`, `--mlp-space` and `--mlp-z-index`. The same values can be passed as `theme: { accent, surface, text, radius, fontFamily }`.

## Context: user ids, flags, versions and page state

Attach the context your team needs to act on a report. All fields are optional and opaque to us.

```ts
init({
  key: "mlp_pk_...",
  user: { id: account.opaqueId, tier: account.plan },   // your own id, not an email
  flags: { search_v2: "B", invoice_editor_v3: true },    // experiments and feature flags
  app: { version: BUILD_VERSION, env: "production" },
  context: { region: "eu" },
  getContext: () => ({ selected_invoices: selection.size, view: router.current }),
});
```

- `user.id` should be your own opaque identifier. Don't send emails or names.
- `flags` lets you split any issue by A/B variant: "every report came from variant B" is a strong signal.
- `getContext()` runs when someone files, never before, and its result is capped in size and time.
- Limits: up to 32 context keys and 50 flags; string values up to 500 characters.

Update context later with `setUser`, `setFlags` and `setContext` on the widget instance (or the same commands through `mlp(...)`). Later sources win.

## Follow-up questions

After a report is saved, the server may suggest one follow-up question when it would make the report clearly more actionable, for example "What did you expect to happen?" for a short bug report. Rules:

- The report is already saved, so skipping never loses it.
- Never asks what the SDK already sent.
- One question by default, two at most. Choices where possible, and always a skip.
- Praise never gets a follow-up.

## Open it from your own UI

```ts
widget.open({ kind: "idea", subject: "invoice editor", context: { invoice_id: id } });
```

Or set `trigger: false` and add `data-mlp-feedback` to any button.

## Headless

Set `ui: false` to build your own interface on the same controller: state, submission, follow-ups and redaction are handled for you.

## Privacy

- Secrets, tokens and card-like numbers are redacted in the browser before sending, and again at intake.
- Emails in free text are redacted when you turn that on.
- `page.url` is sent without query string or fragment unless you opt in.
