@meterbox/react 0.1.4 → 0.1.6

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/README.md CHANGED
@@ -45,7 +45,7 @@ import {
45
45
  } from "@meterbox/react";
46
46
  ```
47
47
 
48
- ### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? />` or `<PricingTable baseUrl publishableKey … />`
48
+ ### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? branding? />` or `<PricingTable baseUrl publishableKey … />`
49
49
 
50
50
  On a public page with no backend of its own, pass `baseUrl` and a
51
51
  publishable key (`mb_pub_...`, from Settings > API keys) instead of `plans`.
@@ -74,6 +74,31 @@ pay for usage: the amounts included each month, what happens past them
74
74
  (billed, stopped until next month, or an upgrade), and every model's input
75
75
  and output rates per 1M tokens, tier ladders included.
76
76
 
77
+ ### Branding: `<BrandingProvider branding>`
78
+
79
+ Every component styles itself from `--mbx-*` CSS variables (`--mbx-accent`,
80
+ `--mbx-text`, `--mbx-card`, `--mbx-font`, `--mbx-radius` and the button
81
+ variables), with Meterbox's defaults as fallbacks. Wrap them in
82
+ `<BrandingProvider branding={branding}>` to apply your branding from
83
+ Settings → Branding (`GET /v1/settings/branding`): logo, colors, font and
84
+ button style. `<PricingTable baseUrl publishableKey>` applies it from the
85
+ pricing snapshot by itself, and any component's surroundings can set the
86
+ variables in CSS. `useBranding()` reads the values (e.g. the logo), and
87
+ `theme` exports the variable references for styling your own elements to
88
+ match.
89
+
90
+ ### `<BillingDetailsButton baseUrl customerId customerToken returnUrl? label? onSession? onError? />`
91
+
92
+ Sends the customer to Stripe's own Billing Portal to update their payment
93
+ method and billing details (address, tax IDs, email, name, phone) and see
94
+ their invoices, so payment details never pass through your app or Meterbox.
95
+ POSTs to `/v1/customer-portal/:id/billing-details` and redirects; Stripe
96
+ sends them back to `returnUrl` (default: the current page). Cancelling and
97
+ plan changes aren't offered there: they stay in Meterbox's own flows. It
98
+ works once the customer has a Stripe account and the tenant has enabled
99
+ `checkout_via_meterbox`, which `GET /v1/customer-portal/:id/me` reports as
100
+ `billing_details_available`.
101
+
77
102
  ### `<PlanComparison from to onSwitch? />`
78
103
 
79
104
  Side-by-side comparison of two plan records with added / removed feature
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterbox/react",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Embeddable React components for Meterbox: pricing tables, subscription preview, credit balance and usage charts. Presentational by default, with data fetched by your backend via @meterbox/node.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://meterbox.ai",
@@ -0,0 +1,96 @@
1
+ /**
2
+ * <BillingDetailsButton baseUrl customerId customerToken returnUrl? label? />
3
+ *
4
+ * Sends the customer to Stripe's own Billing Portal to update their payment
5
+ * method and billing details (address, tax IDs, email, name, phone) and see
6
+ * their invoices, so payment details never pass through your app or
7
+ * Meterbox. POSTs to `/v1/customer-portal/:id/billing-details` and redirects
8
+ * to the returned URL; Stripe sends them back to `returnUrl` (default: this
9
+ * page). Cancelling and plan changes aren't offered there: they stay in
10
+ * Meterbox's own flows.
11
+ *
12
+ * Needs the customer to have a Stripe account and the tenant to have enabled
13
+ * `checkout_via_meterbox`; `GET /v1/customer-portal/:id/me` says whether it
14
+ * will work (`billing_details_available`). Otherwise `onError` receives the
15
+ * 409. Pass `onSession({ url })` to intercept the redirect.
16
+ */
17
+
18
+ const React = require("react");
19
+ const theme = require("./theme");
20
+
21
+ const styles = {
22
+ btn: {
23
+ padding: "8px 14px",
24
+ ...theme.button,
25
+ borderRadius: theme.radius(8),
26
+ fontWeight: 600,
27
+ cursor: "pointer",
28
+ fontFamily: theme.font,
29
+ },
30
+ disabled: { opacity: 0.6, cursor: "default" },
31
+ };
32
+
33
+ async function openBillingDetails({ baseUrl, customerId, customerToken, returnUrl, fetchImpl }) {
34
+ const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
35
+ customerId,
36
+ )}/billing-details`;
37
+ const res = await fetchImpl(url, {
38
+ method: "POST",
39
+ headers: { Authorization: `Bearer ${customerToken}`, "Content-Type": "application/json" },
40
+ body: JSON.stringify({ return_url: returnUrl }),
41
+ });
42
+ const body = await res.json().catch(() => ({}));
43
+ if (!res.ok) {
44
+ const err = new Error(body.error || `billing-details: ${res.status}`);
45
+ err.status = res.status;
46
+ throw err;
47
+ }
48
+ return body;
49
+ }
50
+
51
+ function BillingDetailsButton({
52
+ baseUrl,
53
+ customerId,
54
+ customerToken,
55
+ returnUrl,
56
+ label = "Update payment method and billing details",
57
+ fetch: fetchImpl,
58
+ onSession,
59
+ onError,
60
+ }) {
61
+ const [busy, setBusy] = React.useState(false);
62
+ const ready = baseUrl && customerId && customerToken;
63
+
64
+ async function onClick() {
65
+ setBusy(true);
66
+ try {
67
+ const session = await openBillingDetails({
68
+ baseUrl,
69
+ customerId,
70
+ customerToken,
71
+ returnUrl: returnUrl || globalThis.window?.location?.href,
72
+ fetchImpl: fetchImpl || globalThis.fetch,
73
+ });
74
+ if (onSession) onSession(session);
75
+ else if (session.url && globalThis.window) globalThis.window.location.href = session.url;
76
+ } catch (err) {
77
+ if (onError) onError(err);
78
+ } finally {
79
+ setBusy(false);
80
+ }
81
+ }
82
+
83
+ return React.createElement(
84
+ "button",
85
+ {
86
+ type: "button",
87
+ className: "meterbox-billing-details-button",
88
+ style: !ready || busy ? { ...styles.btn, ...styles.disabled } : styles.btn,
89
+ disabled: !ready || busy,
90
+ onClick,
91
+ },
92
+ busy ? "Redirecting…" : label,
93
+ );
94
+ }
95
+
96
+ module.exports = BillingDetailsButton;
@@ -19,17 +19,16 @@
19
19
  */
20
20
 
21
21
  const React = require("react");
22
+ const theme = require("./theme");
22
23
 
23
24
  const styles = {
24
25
  btn: {
25
26
  padding: "10px 18px",
26
- border: "none",
27
- borderRadius: 8,
28
- background: "#5045e8",
29
- color: "#fff",
27
+ ...theme.button,
28
+ borderRadius: theme.radius(8),
30
29
  fontWeight: 600,
31
30
  cursor: "pointer",
32
- fontFamily: "system-ui, -apple-system, sans-serif",
31
+ fontFamily: theme.font,
33
32
  },
34
33
  disabled: { opacity: 0.6, cursor: "default" },
35
34
  };
@@ -20,6 +20,7 @@
20
20
  */
21
21
 
22
22
  const React = require("react");
23
+ const theme = require("./theme");
23
24
 
24
25
  async function fetchBalance({ baseUrl, customerId, customerToken, fetchImpl }) {
25
26
  const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
@@ -40,12 +41,12 @@ async function fetchBalance({ baseUrl, customerId, customerToken, fetchImpl }) {
40
41
 
41
42
  const styles = {
42
43
  card: {
43
- fontFamily: "system-ui, -apple-system, sans-serif",
44
- color: "#111",
44
+ fontFamily: theme.font,
45
+ color: theme.text,
45
46
  border: "1px solid #e5e7eb",
46
47
  borderRadius: 12,
47
48
  padding: 16,
48
- background: "#fff",
49
+ background: theme.card,
49
50
  display: "grid",
50
51
  gap: 8,
51
52
  maxWidth: 320,
@@ -56,10 +57,10 @@ const styles = {
56
57
  hint: { fontSize: 12, opacity: 0.65 },
57
58
  topUp: {
58
59
  padding: "8px 12px",
59
- border: "1px solid #5045e8",
60
- borderRadius: 8,
61
- background: "#fff",
62
- color: "#5045e8",
60
+ border: `1px solid ${theme.accent}`,
61
+ borderRadius: theme.radius(8),
62
+ background: theme.card,
63
+ color: theme.accent,
63
64
  fontWeight: 600,
64
65
  cursor: "pointer",
65
66
  width: "fit-content",
@@ -21,15 +21,16 @@
21
21
  */
22
22
 
23
23
  const React = require("react");
24
+ const theme = require("./theme");
24
25
 
25
26
  const styles = {
26
27
  card: {
27
- fontFamily: "system-ui, -apple-system, sans-serif",
28
- color: "#111",
28
+ fontFamily: theme.font,
29
+ color: theme.text,
29
30
  border: "1px solid #e5e7eb",
30
31
  borderRadius: 12,
31
32
  padding: 16,
32
- background: "#fff",
33
+ background: theme.card,
33
34
  display: "grid",
34
35
  gap: 12,
35
36
  maxWidth: 720,
@@ -46,8 +47,8 @@ const styles = {
46
47
  borderRadius: 999,
47
48
  fontSize: 11,
48
49
  fontWeight: 600,
49
- background: "#eef2ff",
50
- color: "#5045e8",
50
+ background: theme.accentTint(),
51
+ color: theme.accent,
51
52
  },
52
53
  };
53
54
 
@@ -18,23 +18,24 @@
18
18
  */
19
19
 
20
20
  const React = require("react");
21
+ const theme = require("./theme");
21
22
 
22
23
  const styles = {
23
24
  wrap: {
24
- fontFamily: "system-ui, -apple-system, sans-serif",
25
- color: "#111",
25
+ fontFamily: theme.font,
26
+ color: theme.text,
26
27
  border: "1px solid #e5e7eb",
27
28
  borderRadius: 12,
28
29
  padding: 16,
29
- background: "#fff",
30
+ background: theme.card,
30
31
  display: "grid",
31
32
  gap: 8,
32
33
  maxWidth: 480,
33
34
  },
34
35
  label: { fontSize: 12, opacity: 0.6, textTransform: "uppercase", letterSpacing: 0.4 },
35
36
  current: { fontSize: 24, fontWeight: 700 },
36
- area: { fill: "rgba(67, 56, 202, 0.15)" },
37
- line: { fill: "none", stroke: "#5045e8", strokeWidth: 1.5 },
37
+ area: { fill: theme.accentTint(15) },
38
+ line: { fill: "none", stroke: theme.accent, strokeWidth: 1.5 },
38
39
  };
39
40
 
40
41
  function dollars(n) {
@@ -14,15 +14,16 @@
14
14
  */
15
15
 
16
16
  const React = require("react");
17
+ const theme = require("./theme");
17
18
 
18
19
  const styles = {
19
20
  card: {
20
- fontFamily: "system-ui, -apple-system, sans-serif",
21
- color: "#111",
21
+ fontFamily: theme.font,
22
+ color: theme.text,
22
23
  border: "1px solid #e5e7eb",
23
24
  borderRadius: 12,
24
25
  padding: 16,
25
- background: "#fff",
26
+ background: theme.card,
26
27
  display: "grid",
27
28
  gap: 12,
28
29
  maxWidth: 720,
@@ -38,7 +39,7 @@ const styles = {
38
39
  },
39
40
  td: { padding: "6px 8px", borderBottom: "1px solid #f3f4f6" },
40
41
  links: { display: "flex", gap: 10 },
41
- link: { color: "#5045e8", textDecoration: "none", fontWeight: 600 },
42
+ link: { color: theme.accent, textDecoration: "none", fontWeight: 600 },
42
43
  muted: { opacity: 0.6 },
43
44
  };
44
45
 
@@ -25,15 +25,16 @@
25
25
  */
26
26
 
27
27
  const React = require("react");
28
+ const theme = require("./theme");
28
29
 
29
30
  const styles = {
30
31
  card: {
31
- fontFamily: "system-ui, -apple-system, sans-serif",
32
- color: "#111",
32
+ fontFamily: theme.font,
33
+ color: theme.text,
33
34
  border: "1px solid #e5e7eb",
34
35
  borderRadius: 12,
35
36
  padding: 16,
36
- background: "#fff",
37
+ background: theme.card,
37
38
  display: "grid",
38
39
  gap: 12,
39
40
  maxWidth: 720,
package/src/Paywall.js CHANGED
@@ -22,6 +22,7 @@
22
22
  */
23
23
 
24
24
  const React = require("react");
25
+ const theme = require("./theme");
25
26
 
26
27
  const defaultStyles = {
27
28
  fallback: {
@@ -29,8 +30,8 @@ const defaultStyles = {
29
30
  borderRadius: 10,
30
31
  border: "1px solid #e5e7eb",
31
32
  background: "#fafafa",
32
- fontFamily: "system-ui, -apple-system, sans-serif",
33
- color: "#111",
33
+ fontFamily: theme.font,
34
+ color: theme.text,
34
35
  display: "flex",
35
36
  flexDirection: "column",
36
37
  gap: 8,
@@ -40,10 +41,8 @@ const defaultStyles = {
40
41
  cta: {
41
42
  marginTop: 4,
42
43
  padding: "8px 14px",
43
- border: "none",
44
- borderRadius: 8,
45
- background: "#5045e8",
46
- color: "#fff",
44
+ ...theme.button,
45
+ borderRadius: theme.radius(8),
47
46
  fontWeight: 600,
48
47
  cursor: "pointer",
49
48
  alignSelf: "flex-start",
@@ -37,11 +37,12 @@
37
37
  */
38
38
 
39
39
  const React = require("react");
40
+ const theme = require("./theme");
40
41
  const SubscriptionPreview = require("./SubscriptionPreview");
41
42
 
42
43
  const styles = {
43
44
  wrap: {
44
- fontFamily: "system-ui, -apple-system, sans-serif",
45
+ fontFamily: theme.font,
45
46
  display: "grid",
46
47
  gap: 12,
47
48
  maxWidth: 480,
@@ -61,19 +62,15 @@ const styles = {
61
62
  btn: {
62
63
  padding: "10px 16px",
63
64
  border: "1px solid #d1d5db",
64
- borderRadius: 8,
65
- background: "#fff",
66
- color: "#111",
65
+ borderRadius: theme.radius(8),
66
+ background: theme.card,
67
+ color: theme.text,
67
68
  fontWeight: 600,
68
69
  cursor: "pointer",
69
- fontFamily: "system-ui, -apple-system, sans-serif",
70
+ fontFamily: theme.font,
70
71
  fontSize: 14,
71
72
  },
72
- btnPrimary: {
73
- border: "none",
74
- background: "#5045e8",
75
- color: "#fff",
76
- },
73
+ btnPrimary: theme.button,
77
74
  disabled: { opacity: 0.6, cursor: "default" },
78
75
  message: { fontSize: 13 },
79
76
  success: { color: "#16a34a" },
@@ -11,24 +11,25 @@
11
11
  */
12
12
 
13
13
  const React = require("react");
14
+ const theme = require("./theme");
14
15
 
15
16
  const styles = {
16
17
  grid: {
17
18
  display: "grid",
18
19
  gridTemplateColumns: "1fr 1fr",
19
20
  gap: 24,
20
- fontFamily: "system-ui, -apple-system, sans-serif",
21
- color: "#111",
21
+ fontFamily: theme.font,
22
+ color: theme.text,
22
23
  },
23
24
  col: {
24
25
  border: "1px solid #e5e7eb",
25
26
  borderRadius: 12,
26
27
  padding: 16,
27
- background: "#fff",
28
+ background: theme.card,
28
29
  display: "grid",
29
30
  gap: 10,
30
31
  },
31
- colTo: { borderColor: "#5045e8" },
32
+ colTo: { borderColor: theme.accent },
32
33
  heading: { fontSize: 14, fontWeight: 600, opacity: 0.7, margin: 0 },
33
34
  planName: { fontSize: 20, fontWeight: 700, margin: 0 },
34
35
  features: { margin: 0, padding: 0, listStyle: "none", fontSize: 13, display: "grid", gap: 4 },
@@ -37,10 +38,8 @@ const styles = {
37
38
  switchBtn: {
38
39
  marginTop: 8,
39
40
  padding: "10px 14px",
40
- border: "none",
41
- borderRadius: 8,
42
- background: "#5045e8",
43
- color: "#fff",
41
+ ...theme.button,
42
+ borderRadius: theme.radius(8),
44
43
  fontWeight: 600,
45
44
  cursor: "pointer",
46
45
  },
@@ -26,6 +26,10 @@
26
26
  * and its models' input and output rates per 1M tokens, tier ladders
27
27
  * included.
28
28
  *
29
+ * Self-fetching, it takes the tenant's branding (Settings → Branding) from
30
+ * the snapshot; `branding` overrides it, and a <BrandingProvider> around it
31
+ * applies when neither is set.
32
+ *
29
33
  * From the snapshot, the featured plan is highlighted with its `badge`, a
30
34
  * plan's `trial_days` shows as "14-day free trial", and without `onSelect`
31
35
  * each plan's button is its call-to-action link (`customerEmail` fills
@@ -36,6 +40,8 @@
36
40
  */
37
41
 
38
42
  const React = require("react");
43
+ const theme = require("./theme");
44
+ const { brandingVars } = require("./branding");
39
45
 
40
46
  /**
41
47
  * Total charged for one year prepaid upfront, or null when the plan
@@ -68,27 +74,27 @@ const styles = {
68
74
  color: "#4b5563",
69
75
  fontSize: 13,
70
76
  fontWeight: 600,
71
- fontFamily: "system-ui, -apple-system, sans-serif",
77
+ fontFamily: theme.font,
72
78
  cursor: "pointer",
73
79
  },
74
- toggleBtnActive: { background: "#5045e8", color: "#fff" },
80
+ toggleBtnActive: { background: theme.accent, color: "#fff" },
75
81
  grid: {
76
82
  display: "grid",
77
83
  gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
78
84
  gap: 16,
79
- fontFamily: "system-ui, -apple-system, sans-serif",
80
- color: "#111",
85
+ fontFamily: theme.font,
86
+ color: theme.text,
81
87
  },
82
88
  card: {
83
89
  border: "1px solid #e5e7eb",
84
90
  borderRadius: 12,
85
91
  padding: 20,
86
- background: "#fff",
92
+ background: theme.card,
87
93
  display: "flex",
88
94
  flexDirection: "column",
89
95
  gap: 12,
90
96
  },
91
- cardCurrent: { borderColor: "#5045e8", boxShadow: "0 0 0 3px #5045e822" },
97
+ cardCurrent: { borderColor: theme.accent, boxShadow: `0 0 0 3px ${theme.accentTint(13)}` },
92
98
  name: { fontSize: 18, fontWeight: 600, margin: 0 },
93
99
  price: { fontSize: 28, fontWeight: 700 },
94
100
  perMonth: { fontSize: 12, opacity: 0.6, marginLeft: 4 },
@@ -96,14 +102,17 @@ const styles = {
96
102
  cta: {
97
103
  marginTop: "auto",
98
104
  padding: "10px 14px",
99
- border: "none",
100
- borderRadius: 8,
101
- background: "#5045e8",
102
- color: "#fff",
105
+ ...theme.button,
106
+ borderRadius: theme.radius(8),
103
107
  fontWeight: 600,
104
108
  cursor: "pointer",
105
109
  },
106
- ctaCurrent: { background: "#e5e7eb", color: "#111", cursor: "default" },
110
+ ctaCurrent: {
111
+ background: "#e5e7eb",
112
+ color: theme.text,
113
+ border: "1px solid transparent",
114
+ cursor: "default",
115
+ },
107
116
  trial: { fontSize: 13, fontWeight: 600 },
108
117
  usage: {
109
118
  display: "grid",
@@ -115,7 +124,7 @@ const styles = {
115
124
  badge: {
116
125
  fontSize: 11,
117
126
  fontWeight: 600,
118
- background: "#5045e8",
127
+ background: theme.accent,
119
128
  color: "#fff",
120
129
  padding: "2px 8px",
121
130
  borderRadius: 999,
@@ -232,7 +241,7 @@ async function fetchCatalog({ baseUrl, publishableKey, fetchImpl }) {
232
241
  const res = await fetchImpl(url, { method: "GET" });
233
242
  if (!res.ok) throw new Error(`pricing-table: ${res.status}`);
234
243
  const body = await res.json();
235
- return (body.plans || []).map(fromSnapshot);
244
+ return { plans: (body.plans || []).map(fromSnapshot), branding: body.branding || null };
236
245
  }
237
246
 
238
247
  function PricingTable({
@@ -241,6 +250,7 @@ function PricingTable({
241
250
  currency = "USD",
242
251
  onSelect,
243
252
  customerEmail,
253
+ branding,
244
254
  baseUrl,
245
255
  publishableKey,
246
256
  fetch: fetchImpl,
@@ -257,14 +267,17 @@ function PricingTable({
257
267
  if (alive) setFetched(d);
258
268
  })
259
269
  .catch(() => {
260
- if (alive) setFetched([]);
270
+ if (alive) setFetched({ plans: [], branding: null });
261
271
  });
262
272
  return () => {
263
273
  alive = false;
264
274
  };
265
275
  }, [selfFetch, baseUrl, publishableKey, fetchImpl]);
266
276
 
267
- const list = Array.isArray(plans) ? plans : fetched;
277
+ const list = Array.isArray(plans) ? plans : fetched?.plans;
278
+ // Its own `branding`, else the tenant's from the snapshot; inside a
279
+ // <BrandingProvider>, neither overrides the provider unless given.
280
+ const vars = brandingVars(branding || fetched?.branding);
268
281
  if (!Array.isArray(list) || list.length === 0) {
269
282
  return React.createElement(
270
283
  "div",
@@ -277,7 +290,7 @@ function PricingTable({
277
290
 
278
291
  return React.createElement(
279
292
  "div",
280
- { className: "meterbox-pricing-table", style: styles.wrap },
293
+ { className: "meterbox-pricing-table", style: { ...styles.wrap, ...vars } },
281
294
  hasAnnualOption &&
282
295
  React.createElement(
283
296
  "div",
@@ -21,15 +21,16 @@
21
21
  */
22
22
 
23
23
  const React = require("react");
24
+ const theme = require("./theme");
24
25
 
25
26
  const styles = {
26
27
  panel: {
27
- fontFamily: "system-ui, -apple-system, sans-serif",
28
- color: "#111",
28
+ fontFamily: theme.font,
29
+ color: theme.text,
29
30
  border: "1px solid #e5e7eb",
30
31
  borderRadius: 12,
31
32
  padding: 20,
32
- background: "#fff",
33
+ background: theme.card,
33
34
  display: "grid",
34
35
  gap: 12,
35
36
  maxWidth: 480,
@@ -10,6 +10,7 @@
10
10
  */
11
11
 
12
12
  const React = require("react");
13
+ const theme = require("./theme");
13
14
 
14
15
  const styles = {
15
16
  banner: {
@@ -19,18 +20,16 @@ const styles = {
19
20
  gap: 12,
20
21
  padding: "10px 14px",
21
22
  borderRadius: 10,
22
- background: "#eef2ff",
23
- color: "#3730a3",
24
- fontFamily: "system-ui, -apple-system, sans-serif",
23
+ background: theme.accentTint(),
24
+ color: theme.accent,
25
+ fontFamily: theme.font,
25
26
  fontSize: 14,
26
27
  },
27
28
  expired: { background: "#fee2e2", color: "#991b1b" },
28
29
  cta: {
29
30
  padding: "6px 12px",
30
- border: "none",
31
- borderRadius: 6,
32
- background: "#5045e8",
33
- color: "#fff",
31
+ ...theme.button,
32
+ borderRadius: theme.radius(6),
34
33
  fontWeight: 600,
35
34
  cursor: "pointer",
36
35
  },
package/src/UsageChart.js CHANGED
@@ -12,21 +12,22 @@
12
12
  */
13
13
 
14
14
  const React = require("react");
15
+ const theme = require("./theme");
15
16
 
16
17
  const styles = {
17
18
  wrap: {
18
- fontFamily: "system-ui, -apple-system, sans-serif",
19
- color: "#111",
19
+ fontFamily: theme.font,
20
+ color: theme.text,
20
21
  border: "1px solid #e5e7eb",
21
22
  borderRadius: 12,
22
23
  padding: 16,
23
- background: "#fff",
24
+ background: theme.card,
24
25
  display: "grid",
25
26
  gap: 8,
26
27
  },
27
28
  label: { fontSize: 12, opacity: 0.6, textTransform: "uppercase", letterSpacing: 0.4 },
28
29
  total: { fontSize: 24, fontWeight: 700 },
29
- bar: { fill: "#5045e8" },
30
+ bar: { fill: theme.accent },
30
31
  };
31
32
 
32
33
  const VALUE_LABELS = {
@@ -16,19 +16,20 @@
16
16
  */
17
17
 
18
18
  const React = require("react");
19
+ const theme = require("./theme");
19
20
 
20
21
  const styles = {
21
22
  wrap: {
22
23
  display: "grid",
23
24
  gap: 6,
24
- fontFamily: "system-ui, -apple-system, sans-serif",
25
- color: "#111",
25
+ fontFamily: theme.font,
26
+ color: theme.text,
26
27
  },
27
28
  header: { display: "flex", justifyContent: "space-between", fontSize: 13 },
28
29
  label: { fontWeight: 600 },
29
30
  value: { opacity: 0.75, fontFamily: "monospace" },
30
31
  track: { background: "#e5e7eb", borderRadius: 6, height: 8, overflow: "hidden" },
31
- fill: { height: "100%", background: "#5045e8", transition: "width 200ms ease" },
32
+ fill: { height: "100%", background: theme.accent, transition: "width 200ms ease" },
32
33
  warn: { background: "#d97706" },
33
34
  danger: { background: "#dc2626" },
34
35
  stale: { fontSize: 11, opacity: 0.55, alignSelf: "flex-end" },
@@ -0,0 +1,66 @@
1
+ /**
2
+ * branding.js — apply the tenant's branding (GET /v1/settings/branding, as
3
+ * the pricing snapshot and GET /v1/customer-portal/:id/branding return it) to
4
+ * the components: it becomes the --mbx-* CSS variables theme.js reads.
5
+ *
6
+ * <BrandingProvider branding={branding}>…</BrandingProvider>
7
+ *
8
+ * themes everything inside it; `useBranding()` reads the raw values (e.g. the
9
+ * logo). Values are checked again here (hex colors, a font list, an https
10
+ * logo), so a bad one is dropped rather than reaching a style.
11
+ */
12
+
13
+ const React = require("react");
14
+
15
+ const COLOR = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
16
+ const FONT = /^[A-Za-z0-9 ,'"-]{1,100}$/;
17
+ const LOGO = /^https:\/\/[^\s"'<>()\\]+$/;
18
+
19
+ const BrandingContext = React.createContext(null);
20
+
21
+ /** The --mbx-* CSS variables for `branding`; anything invalid is left out. */
22
+ function brandingVars(branding) {
23
+ const b = branding || {};
24
+ const vars = {};
25
+ if (COLOR.test(b.accent_color || "")) vars["--mbx-accent"] = b.accent_color;
26
+ if (COLOR.test(b.text_color || "")) vars["--mbx-text"] = b.text_color;
27
+ if (COLOR.test(b.background_color || "")) vars["--mbx-card"] = b.background_color;
28
+ if (FONT.test(b.font_family || "")) vars["--mbx-font"] = b.font_family;
29
+ const radius = Number(b.button_radius_px);
30
+ if (b.button_radius_px != null && radius >= 0 && radius <= 32) {
31
+ vars["--mbx-radius"] = `${Math.round(radius)}px`;
32
+ }
33
+ if (b.button_style === "outline") {
34
+ vars["--mbx-button-bg"] = "transparent";
35
+ vars["--mbx-button-text"] = "var(--mbx-accent, #5045e8)";
36
+ vars["--mbx-button-border"] = "var(--mbx-accent, #5045e8)";
37
+ }
38
+ return vars;
39
+ }
40
+
41
+ /** The logo URL, when it's a valid https URL. */
42
+ function logoUrl(branding) {
43
+ return LOGO.test(branding?.logo_url || "") ? branding.logo_url : null;
44
+ }
45
+
46
+ /**
47
+ * Themes its children with `branding`. The wrapper is `display: contents`, so
48
+ * it adds no box to the layout; CSS variables still inherit through it.
49
+ */
50
+ function BrandingProvider({ branding, children }) {
51
+ return React.createElement(
52
+ BrandingContext.Provider,
53
+ { value: branding || null },
54
+ React.createElement(
55
+ "div",
56
+ { className: "meterbox-branding", style: { display: "contents", ...brandingVars(branding) } },
57
+ children,
58
+ ),
59
+ );
60
+ }
61
+
62
+ function useBranding() {
63
+ return React.useContext(BrandingContext);
64
+ }
65
+
66
+ module.exports = { BrandingProvider, useBranding, brandingVars, logoUrl };
package/src/index.js CHANGED
@@ -11,6 +11,10 @@
11
11
  * `/v1/customer-portal/*` endpoints using a short-lived customer-scoped
12
12
  * JWT minted server-side (`POST /v1/tokens/:id`).
13
13
  *
14
+ * Branding: `<BrandingProvider branding>` themes every component inside it
15
+ * with the tenant's branding (GET /v1/settings/branding); `theme` is the
16
+ * --mbx-* CSS variables they read, for matching your own elements.
17
+ *
14
18
  * Re-exports `@meterbox/node` for convenience: `import { plans } from
15
19
  * '@meterbox/react'` works the same as importing the SDK directly.
16
20
  */
@@ -32,6 +36,9 @@ const Paywall = require("./Paywall");
32
36
  const UsageProgress = require("./UsageProgress");
33
37
  const TrialBanner = require("./TrialBanner");
34
38
  const CheckoutButton = require("./CheckoutButton");
39
+ const BillingDetailsButton = require("./BillingDetailsButton");
40
+ const { BrandingProvider, useBranding, brandingVars, logoUrl } = require("./branding");
41
+ const theme = require("./theme");
35
42
 
36
43
  module.exports = {
37
44
  PricingTable,
@@ -48,6 +55,12 @@ module.exports = {
48
55
  UsageProgress,
49
56
  TrialBanner,
50
57
  CheckoutButton,
58
+ BillingDetailsButton,
59
+ BrandingProvider,
60
+ useBranding,
61
+ brandingVars,
62
+ logoUrl,
63
+ theme,
51
64
  sdk,
52
65
  MeterboxClient: sdk.MeterboxClient,
53
66
  };
package/src/theme.js ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * theme.js — the style values every component uses: --mbx-* CSS variables,
3
+ * with Meterbox's defaults as fallbacks. The tenant's branding (Settings →
4
+ * Branding) sets the variables through <BrandingProvider> (branding.js) or a
5
+ * component's own `branding`; a page can also set them in its CSS.
6
+ */
7
+
8
+ const accent = "var(--mbx-accent, #5045e8)";
9
+
10
+ module.exports = {
11
+ accent,
12
+ // A light wash of the accent (badges, banners, the current plan's glow).
13
+ accentTint: (pct = 12) => `color-mix(in srgb, ${accent} ${pct}%, transparent)`,
14
+ text: "var(--mbx-text, #111)",
15
+ card: "var(--mbx-card, #fff)",
16
+ font: "var(--mbx-font, system-ui, -apple-system, sans-serif)",
17
+ // Primary buttons: filled with the accent, or outlined (branding's button_style).
18
+ button: {
19
+ background: `var(--mbx-button-bg, ${accent})`,
20
+ color: "var(--mbx-button-text, #fff)",
21
+ border: "1px solid var(--mbx-button-border, transparent)",
22
+ },
23
+ radius: (px) => `var(--mbx-radius, ${px}px)`,
24
+ };