@colixsystems/widget-sdk 0.309.0 → 0.314.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/src/contract.js CHANGED
@@ -2509,7 +2509,7 @@ const HOOKS = [
2509
2509
  currency:
2510
2510
  "string // the ISO code this workspace charges its app users in, e.g. \"SEK\"",
2511
2511
  formatMoney:
2512
- "(minorUnits) => string // 45000 -> \"450,00 kr\"; renders identically on web and native. NEVER write a currency symbol or code yourself",
2512
+ "(minorUnits, { decimals: \"always\" }?) => string // 79500 -> \"795 kr\", 9950 -> \"99,50 kr\": a whole amount shows no decimals; pass { decimals: \"always\" } for a receipt or invoice total (\"795,00 kr\"). Renders identically on web and native. NEVER write a currency symbol or code yourself",
2513
2513
  },
2514
2514
  requiredContextSlice: ["workspace.currency"],
2515
2515
  scopes: null,
@@ -4973,8 +4973,8 @@ const VETTED_IMPORTS = [
4973
4973
  description:
4974
4974
  "RevenueCat in-app purchase (StoreKit / Play Billing). PREFER usePurchases() from the SDK, which already runs on the " +
4975
4975
  "app's configured, signed-in customer. Import this directly only for what the hook does not cover, and NEVER call " +
4976
- "Purchases.configure, logIn or logOut — the app owns them, and it configures Purchases only when the workspace " +
4977
- "connected RevenueCat. Native-only because no browser offers store billing: author it in widget.native.jsx and give " +
4976
+ "Purchases.configure, logIn or logOut — the app owns them. The package is in the app only when the workspace " +
4977
+ "connected RevenueCat: a store build of a workspace that has not is refused while a widget imports it. Native-only because no browser offers store billing: author it in widget.native.jsx and give " +
4978
4978
  "widget.web.jsx a visible 'available in the app' state.",
4979
4979
  },
4980
4980
  {
@@ -5336,18 +5336,6 @@ function normaliseCurrencyCode(value) {
5336
5336
  return code || CHARGE_CURRENCY;
5337
5337
  }
5338
5338
 
5339
- /**
5340
- * `formatMoneyIn("SEK", 45000)` → `"450,00 kr"`.
5341
- *
5342
- * Takes MINOR units — the same unit `requestPayment` charges in — so there is no
5343
- * conversion step for a widget to get wrong. Renders from `CURRENCY_FORMATS`
5344
- * rather than `Intl.NumberFormat`, which does not produce identical output in
5345
- * the exported Expo app and react-native-web; this string sits beside the amount
5346
- * the buyer is actually charged, so the two hosts must agree exactly.
5347
- *
5348
- * A non-finite amount renders as zero rather than "NaN": a price whose data has
5349
- * not loaded should read as an amount, never as a broken string in a checkout.
5350
- */
5351
5339
  /**
5352
5340
  * sc-5717 — value equality for a stored widget-route value: is this key still
5353
5341
  * at the default its widget declared?
@@ -5363,7 +5351,22 @@ function sameWidgetRouteValue(a, b) {
5363
5351
  }
5364
5352
  return Object.is(a, b);
5365
5353
  }
5366
- function formatMoneyIn(currency, minorUnits) {
5354
+ /**
5355
+ * `formatMoneyIn("SEK", 79500)` → `"795 kr"`; `formatMoneyIn("SEK", 9950)` → `"99,50 kr"`.
5356
+ *
5357
+ * Takes MINOR units — the same unit `requestPayment` charges in — so there is no
5358
+ * conversion step for a widget to get wrong. Renders from `CURRENCY_FORMATS`
5359
+ * rather than `Intl.NumberFormat`, which does not produce identical output in
5360
+ * the exported Expo app and react-native-web; this string sits beside the amount
5361
+ * the buyer is actually charged, so the two hosts must agree exactly.
5362
+ *
5363
+ * A whole amount drops its decimals, as shops and menus write prices.
5364
+ * `{ decimals: "always" }` keeps them, for a receipt or invoice total.
5365
+ *
5366
+ * A non-finite amount renders as zero rather than "NaN": a price whose data has
5367
+ * not loaded should read as an amount, never as a broken string in a checkout.
5368
+ */
5369
+ function formatMoneyIn(currency, minorUnits, options) {
5367
5370
  const code = normaliseCurrencyCode(currency);
5368
5371
  const fmt = CURRENCY_FORMATS[code] || {
5369
5372
  ...CURRENCY_FORMAT_FALLBACK,
@@ -5376,7 +5379,9 @@ function formatMoneyIn(currency, minorUnits) {
5376
5379
  const whole = fmt.decimals > 0 ? digits.slice(0, -fmt.decimals) : digits;
5377
5380
  const cents = fmt.decimals > 0 ? digits.slice(-fmt.decimals) : "";
5378
5381
  const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, fmt.groupSeparator);
5379
- const body = cents ? `${grouped}${fmt.decimalSeparator}${cents}` : grouped;
5382
+ const alwaysDecimals = Boolean(options && options.decimals === "always");
5383
+ const showCents = cents !== "" && (alwaysDecimals || /[1-9]/.test(cents));
5384
+ const body = showCents ? `${grouped}${fmt.decimalSeparator}${cents}` : grouped;
5380
5385
  const gap = fmt.space ? " " : "";
5381
5386
  const withSymbol =
5382
5387
  fmt.position === "before"
@@ -6945,7 +6950,9 @@ const CONTRACT = deepFreeze({
6945
6950
  // holding any of the values. At most 100; no `valueMode` applies.
6946
6951
  // 1.253.0: fix (sc-16577) — `react-native-safe-area-context` is host-resolved,
6947
6952
  // so `useSafeAreaInsets()` returns zeros on web as documented instead of throwing.
6948
- version: "1.253.0",
6953
+ // 1.254.0: `formatMoney` shows a whole amount without decimals ("795 kr");
6954
+ // `formatMoney(minor, { decimals: "always" })` keeps them ("795,00 kr").
6955
+ version: "1.254.0",
6949
6956
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
6950
6957
  hooks: HOOKS,
6951
6958
  primitives: PRIMITIVES,
@@ -7297,6 +7304,82 @@ function deriveSurfaceTokens(backgroundColor) {
7297
7304
  };
7298
7305
  }
7299
7306
 
7307
+ // The theme_config keys that state a light page's own surface set, by token.
7308
+ const THEME_SURFACE_COLOR_KEYS = Object.freeze({
7309
+ surfaceMuted: "surfaceMutedColor",
7310
+ border: "surfaceBorderColor",
7311
+ onSurfaceMuted: "mutedTextColor",
7312
+ });
7313
+ // How far a look's muted panel and border step from its page toward its ink.
7314
+ const LIGHT_SURFACE_MIX = Object.freeze({ surfaceMuted: 0.04, border: 0.12 });
7315
+ // Slate's own saturation, so a tinted set is as quiet as the default one.
7316
+ const LIGHT_SURFACE_MAX_SATURATION = 0.2;
7317
+ const LIGHT_SURFACE_TEXT_STEP = 0.01;
7318
+
7319
+ function isLightBackground(backgroundColor) {
7320
+ return readableTextColor(backgroundColor, "dark", "light") === "dark";
7321
+ }
7322
+
7323
+ /**
7324
+ * The page's surface set for a stored theme: `deriveSurfaceTokens` of its
7325
+ * background, with the theme's own `THEME_SURFACE_COLOR_KEYS` over it while
7326
+ * that background is light. A dark page keeps the derivation, so a light set
7327
+ * left on the theme never paints it. `null` when the background is unusable.
7328
+ */
7329
+ function themeSurfaceTokens(themeConfig) {
7330
+ const cfg = themeConfig && typeof themeConfig === "object" ? themeConfig : {};
7331
+ const derived = deriveSurfaceTokens(cfg.backgroundColor);
7332
+ if (!derived || !isLightBackground(cfg.backgroundColor)) return derived;
7333
+ const out = { ...derived };
7334
+ for (const [token, key] of Object.entries(THEME_SURFACE_COLOR_KEYS)) {
7335
+ if (isHexColor(cfg[key])) out[token] = cfg[key];
7336
+ }
7337
+ return out;
7338
+ }
7339
+
7340
+ /**
7341
+ * The surface set a look's LIGHT background wants, as theme_config keys: the
7342
+ * muted panel and border step toward the ink in the background's own hue, and
7343
+ * the muted text keeps the default's weight in that hue, darkened until it
7344
+ * reads at WCAG AA on the surface, the muted panel and the page. `null` for a
7345
+ * dark or unusable background, which `deriveSurfaceTokens` already covers.
7346
+ */
7347
+ /** The hues of the contract's own slate: a page tinted within them already matches it. */
7348
+ const SLATE_HUES = Object.freeze({ from: 195, to: 225 });
7349
+
7350
+ /** Whether a page has a hue of its own; white, grey and slate-blue keep the contract's colours. */
7351
+ function hasOwnTint(backgroundColor) {
7352
+ const channels = hexChannels(backgroundColor);
7353
+ if (Math.max(...channels) - Math.min(...channels) <= 2) return false;
7354
+ const [hue] = hexToHsl(backgroundColor);
7355
+ return hue < SLATE_HUES.from || hue > SLATE_HUES.to;
7356
+ }
7357
+
7358
+ function deriveLightSurfaceColors(backgroundColor, inkColor) {
7359
+ if (!isHexColor(backgroundColor) || !isLightBackground(backgroundColor) || !hasOwnTint(backgroundColor)) return null;
7360
+ const base = DEFAULT_THEME_TOKENS.colors;
7361
+ const [hue, saturation] = hexToHsl(backgroundColor);
7362
+ const tint = Math.min(saturation, LIGHT_SURFACE_MAX_SATURATION);
7363
+ const ink = isHexColor(inkColor) ? inkColor : base.onSurface;
7364
+ const tone = hslToHex(hue, tint, hexToHsl(ink)[2]);
7365
+ const surfaceMuted = mixHex(backgroundColor, tone, LIGHT_SURFACE_MIX.surfaceMuted);
7366
+ const backdrops = [base.surface, surfaceMuted, backgroundColor];
7367
+ let lightness = hexToHsl(base.onSurfaceMuted)[2];
7368
+ let text = hslToHex(hue, tint, lightness);
7369
+ while (
7370
+ lightness > 0 &&
7371
+ backdrops.some((backdrop) => contrastRatio(text, backdrop) < ACCENT_TEXT_MIN_CONTRAST)
7372
+ ) {
7373
+ lightness = Math.max(0, lightness - LIGHT_SURFACE_TEXT_STEP);
7374
+ text = hslToHex(hue, tint, lightness);
7375
+ }
7376
+ return {
7377
+ [THEME_SURFACE_COLOR_KEYS.surfaceMuted]: surfaceMuted,
7378
+ [THEME_SURFACE_COLOR_KEYS.border]: mixHex(backgroundColor, tone, LIGHT_SURFACE_MIX.border),
7379
+ [THEME_SURFACE_COLOR_KEYS.onSurfaceMuted]: text,
7380
+ };
7381
+ }
7382
+
7300
7383
  // sc-8370: WCAG 1.4.3 for text, 1.4.11 for a control's boundary.
7301
7384
  const INK_CONTRAST = Object.freeze({ text: 4.5, boundary: 3 });
7302
7385
 
@@ -7450,6 +7533,9 @@ export {
7450
7533
  deriveAccentTints,
7451
7534
  deriveStatusColors,
7452
7535
  deriveSurfaceTokens,
7536
+ themeSurfaceTokens,
7537
+ deriveLightSurfaceColors,
7538
+ THEME_SURFACE_COLOR_KEYS,
7453
7539
  inkForBackdrop,
7454
7540
  INK_CONTRAST,
7455
7541
  gradientAngleToVector,
package/src/webbundle.js CHANGED
@@ -40,7 +40,7 @@ export async function loadEsbuild() {
40
40
  return await import("esbuild");
41
41
  } catch {
42
42
  throw new Error(
43
- "Bundling a widget's web entry needs the optional `esbuild` dependency.\n" +
43
+ "Bundling a widget's web entry needs `esbuild`, an optional peer of the SDK.\n" +
44
44
  "Install it in your widget project:\n" +
45
45
  " npm install --save-dev esbuild",
46
46
  );