@colixsystems/widget-sdk 0.128.0 → 0.129.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/README.md CHANGED
@@ -70,7 +70,31 @@ See the design reference for the full architecture: [`docs/architecture/widget-m
70
70
 
71
71
  ## Status
72
72
 
73
- `v0.128.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
73
+ `v0.129.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
74
+
75
+ ### What's new in 0.129.0 (contract 1.100.0)
76
+
77
+ **Widgets can READ a barcode now — `useBarcodeScanner()` (sc-7228).** A widget could already *generate* a QR code (`react-native-qrcode-svg`) but never read one, so the whole class of apps that starts by pointing a phone at a label — inventory counts, asset check-in/out, warehouse picking, ticket scanning, scan-on-delivery — had no way in.
78
+
79
+ ```jsx
80
+ import { useBarcodeScanner, BarcodeError } from "@colixsystems/widget-sdk";
81
+
82
+ const { result, scanning, supported, scan, reset } = useBarcodeScanner();
83
+ const hit = await scan(); // { value, format } | null
84
+ ```
85
+
86
+ `scan()` is **imperative** — call it from a tap, because both hosts gate the camera permission prompt on a gesture. It resolves the first code decoded, resolves `null` when the user dismisses (a dismissal is NOT an error), and rejects a `BarcodeError` with `.code` of `PERMISSION_DENIED | UNSUPPORTED | INTERNAL`. `format` is a lowercase symbology name (`qr_code`, `code_128`, `ean_13`, …) and is a **hint**: the two hosts detect different sets, so never branch on it for correctness.
87
+
88
+ It is deliberately **one-shot** rather than a start/stop subscription — to read several codes, call `scan()` again. There is no loop to leave running and no camera to forget to release.
89
+
90
+ **Gate the button on `supported`, and always keep a manual-entry path.** `BarcodeDetector` is Chromium-only today, so the web Player reports `supported: false` in Safari and Firefox. That is a genuine browser gap, not a missing feature, so a scanner must never be the only way to enter a code:
91
+
92
+ ```jsx
93
+ <TextInput value={code} onChangeText={setCode} />
94
+ {supported ? <Pressable onPress={scan}><Text>Scan</Text></Pressable> : null}
95
+ ```
96
+
97
+ **Host-brokered, not a vetted import.** Like the camera and speech-to-text, your widget never imports the native module: `expo-camera` is pinned by the Expo export only, so a widget that does not scan gains no native dependency and nothing new enters widget bundles.
74
98
 
75
99
  ### What's new in 0.128.0 (contract 1.99.0 — unchanged)
76
100
 
package/dist/contract.cjs CHANGED
@@ -1670,6 +1670,38 @@ const HOOKS = [
1670
1670
  requiredContextSlice: [],
1671
1671
  scopes: null,
1672
1672
  },
1673
+ // sc-7228 — host-brokered barcode / QR scanning. Optional slice; the hook
1674
+ // reports supported:false rather than throwing at render.
1675
+ {
1676
+ name: "useBarcodeScanner",
1677
+ signature: "useBarcodeScanner(options?)",
1678
+ description:
1679
+ "Read a barcode or QR code with the device camera. Returns { result, scanning, error, supported, scan, reset }. " +
1680
+ "Scanning is IMPERATIVE — call scan() from a user gesture (a tap); the browser and the mobile OS gate the camera " +
1681
+ "permission prompt on a gesture, so it NEVER opens on mount. It resolves { value, format } for the FIRST code " +
1682
+ "decoded, or NULL when the user dismisses the scanner — dismissal is the common case and is deliberately NOT an " +
1683
+ "error, so no try/catch is needed on the happy path. It rejects with a BarcodeError whose .code is one of " +
1684
+ "PERMISSION_DENIED | UNSUPPORTED | INTERNAL. `value` is the decoded text; `format` is a lowercase symbology name " +
1685
+ "(qr_code, code_128, ean_13, …) — treat it as a hint, not a promise, because the two hosts detect different sets. " +
1686
+ "It is deliberately ONE-SHOT rather than a start/stop subscription: to scan several codes, call scan() again. " +
1687
+ "reset() clears the last result and error. ALWAYS check `supported` before rendering a scan button and give the " +
1688
+ "widget a manual-entry path — the web Player reports false wherever the browser has no BarcodeDetector (Safari and " +
1689
+ "Firefox today), which is a real browser gap, not a missing feature. options: { formats } is a HINT narrowing the " +
1690
+ "symbologies to look for. The Expo export scans via expo-camera; the web Player via BarcodeDetector over the same " +
1691
+ "getUserMedia preview useCamera() uses. The camera is released on every exit path, including a dismissal.",
1692
+ returnShape: {
1693
+ result: "{ value, format } | null",
1694
+ scanning: "boolean",
1695
+ error: "BarcodeError | null",
1696
+ supported:
1697
+ "boolean // GATE THE BUTTON ON THIS: false where the browser has no BarcodeDetector",
1698
+ scan:
1699
+ "() => Promise<{ value, format } | null> // null if dismissed; rejects with BarcodeError",
1700
+ reset: "() => void // clear the last result + error",
1701
+ },
1702
+ requiredContextSlice: [],
1703
+ scopes: null,
1704
+ },
1673
1705
  // sc-7193 — host-brokered image editing. Optional slice; the hook reports
1674
1706
  // supported:false rather than throwing at render.
1675
1707
  {
@@ -2343,10 +2375,11 @@ const WIDGET_CONTEXT_SHAPE = {
2343
2375
  "subscribeBackgroundWatchState(cb) -> unsubscribe }, " +
2344
2376
  "speech: { isSupported() -> boolean, start(options, { onResult, onError, onEnd }) -> Promise<{ stop(), abort() }> }, " +
2345
2377
  "camera: { isSupported() -> boolean, capture(options?) -> Promise<asset | null>, pick(options?) -> Promise<asset | null> }, " +
2346
- "imageEditor: { isSupported() -> boolean, edit(uri, actions, options?) -> Promise<asset> } }. " +
2347
- "Backs useGeolocation(), useSpeechToText(), useCamera() and useImageEditor(). The web Player brokers them via " +
2348
- "navigator.geolocation, window.SpeechRecognition, a getUserMedia camera preview and a host-side canvas; the Expo export via " +
2349
- "expo-location, expo-speech-recognition, expo-image-picker and expo-image-manipulator. " +
2378
+ "imageEditor: { isSupported() -> boolean, edit(uri, actions, options?) -> Promise<asset> }, " +
2379
+ "barcode: { isSupported() -> boolean, scan(options?) -> Promise<{ value, format } | null> } }. " +
2380
+ "Backs useGeolocation(), useSpeechToText(), useCamera(), useImageEditor() and useBarcodeScanner(). The web Player brokers them via " +
2381
+ "navigator.geolocation, window.SpeechRecognition, a getUserMedia camera preview, a host-side canvas and BarcodeDetector; the Expo export via " +
2382
+ "expo-location, expo-speech-recognition, expo-image-picker, expo-image-manipulator and expo-camera. " +
2350
2383
  "imageEditor.edit applies resize / crop / rotate / flip in order and rejects with an ImageEditorError " +
2351
2384
  "(.code UNSUPPORTED | INVALID_ACTION | DECODE_FAILED | ENCODE_FAILED | INTERNAL). " +
2352
2385
  "getCurrentPosition rejects with a GeolocationError (.code PERMISSION_DENIED | UNAVAILABLE | TIMEOUT | UNSUPPORTED | INTERNAL). " +
@@ -2354,6 +2387,11 @@ const WIDGET_CONTEXT_SHAPE = {
2354
2387
  "its onError carries the Web Speech error vocabulary (not-allowed | no-speech | language-not-supported | network | aborted). " +
2355
2388
  "camera.capture/pick resolve a normalised { uri, name, mimeType, width, height, size, file, release? } or NULL when the " +
2356
2389
  "user dismisses the picker, and reject with a CameraError (.code PERMISSION_DENIED | UNSUPPORTED | INTERNAL). " +
2390
+ "barcode.scan resolves { value, format } for the first code decoded or NULL when the user dismisses, and rejects with a " +
2391
+ "BarcodeError (.code PERMISSION_DENIED | UNSUPPORTED | INTERNAL). Its formats hint and its format output BOTH speak " +
2392
+ "BarcodeDetector names (qr_code, code_128, ean_13, ...) on both hosts; the Expo export maps them to expo-camera own " +
2393
+ "vocabulary internally and DROPS a name it cannot map. isSupported() is false wherever the browser ships no " +
2394
+ "BarcodeDetector (Safari, Firefox), so a widget must always offer manual entry beside a scan button. " +
2357
2395
  "sc-6450 — the geolocation background-watch members are NATIVE-ONLY and opt-in per app: the web Player and an export that " +
2358
2396
  "did not opt in both report isBackgroundSupported() false, and the Expo export backs it with expo-location + " +
2359
2397
  "expo-task-manager. subscribeBackgroundPositions and subscribeBackgroundWatchState are plain subscriptions — they start no " +
@@ -2367,6 +2405,7 @@ const WIDGET_CONTEXT_SHAPE = {
2367
2405
  speech: "object",
2368
2406
  camera: "object",
2369
2407
  imageEditor: "object",
2408
+ barcode: "object",
2370
2409
  },
2371
2410
  },
2372
2411
  };
@@ -3768,7 +3807,20 @@ const CONTRACT = deepFreeze({
3768
3807
  // reason date-fns is: an AI-agent widget is transpiled, never bundled, so
3769
3808
  // its bare import must resolve at runtime on both hosts. No existing entry
3770
3809
  // changed shape — minor bump on the pre-1.0 channel.
3771
- version: "1.99.0",
3810
+ // 1.100.0: additive (sc-7228) — new `useBarcodeScanner()` hook + a `barcode`
3811
+ // member on the optional `device` host slice. Widgets could GENERATE a QR
3812
+ // code (react-native-qrcode-svg) but never READ one, so inventory counts,
3813
+ // asset check-in/out, ticket scanning and scan-on-delivery were
3814
+ // unbuildable. Host-brokered rather than a vetted import, the same call as
3815
+ // `camera` and `speech`: widgets never import the native module, so
3816
+ // `expo-camera` stays out of widget bundles and is pinned in the export
3817
+ // only. One-shot scan() modelled on useCamera().capture() rather than a
3818
+ // start/stop subscription — no second streaming lifecycle to keep in step.
3819
+ // NOT native-only: the web half is BarcodeDetector over the getUserMedia
3820
+ // preview useCamera already owns, so this is full parity wherever the
3821
+ // browser ships the API and a declared `supported:false` where it does not
3822
+ // (Safari, Firefox) — a genuine browser gap per CLAUDE.md §8.
3823
+ version: "1.100.0",
3772
3824
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
3773
3825
  hooks: HOOKS,
3774
3826
  primitives: PRIMITIVES,
package/dist/contract.js CHANGED
@@ -1670,6 +1670,38 @@ const HOOKS = [
1670
1670
  requiredContextSlice: [],
1671
1671
  scopes: null,
1672
1672
  },
1673
+ // sc-7228 — host-brokered barcode / QR scanning. Optional slice; the hook
1674
+ // reports supported:false rather than throwing at render.
1675
+ {
1676
+ name: "useBarcodeScanner",
1677
+ signature: "useBarcodeScanner(options?)",
1678
+ description:
1679
+ "Read a barcode or QR code with the device camera. Returns { result, scanning, error, supported, scan, reset }. " +
1680
+ "Scanning is IMPERATIVE — call scan() from a user gesture (a tap); the browser and the mobile OS gate the camera " +
1681
+ "permission prompt on a gesture, so it NEVER opens on mount. It resolves { value, format } for the FIRST code " +
1682
+ "decoded, or NULL when the user dismisses the scanner — dismissal is the common case and is deliberately NOT an " +
1683
+ "error, so no try/catch is needed on the happy path. It rejects with a BarcodeError whose .code is one of " +
1684
+ "PERMISSION_DENIED | UNSUPPORTED | INTERNAL. `value` is the decoded text; `format` is a lowercase symbology name " +
1685
+ "(qr_code, code_128, ean_13, …) — treat it as a hint, not a promise, because the two hosts detect different sets. " +
1686
+ "It is deliberately ONE-SHOT rather than a start/stop subscription: to scan several codes, call scan() again. " +
1687
+ "reset() clears the last result and error. ALWAYS check `supported` before rendering a scan button and give the " +
1688
+ "widget a manual-entry path — the web Player reports false wherever the browser has no BarcodeDetector (Safari and " +
1689
+ "Firefox today), which is a real browser gap, not a missing feature. options: { formats } is a HINT narrowing the " +
1690
+ "symbologies to look for. The Expo export scans via expo-camera; the web Player via BarcodeDetector over the same " +
1691
+ "getUserMedia preview useCamera() uses. The camera is released on every exit path, including a dismissal.",
1692
+ returnShape: {
1693
+ result: "{ value, format } | null",
1694
+ scanning: "boolean",
1695
+ error: "BarcodeError | null",
1696
+ supported:
1697
+ "boolean // GATE THE BUTTON ON THIS: false where the browser has no BarcodeDetector",
1698
+ scan:
1699
+ "() => Promise<{ value, format } | null> // null if dismissed; rejects with BarcodeError",
1700
+ reset: "() => void // clear the last result + error",
1701
+ },
1702
+ requiredContextSlice: [],
1703
+ scopes: null,
1704
+ },
1673
1705
  // sc-7193 — host-brokered image editing. Optional slice; the hook reports
1674
1706
  // supported:false rather than throwing at render.
1675
1707
  {
@@ -2343,10 +2375,11 @@ const WIDGET_CONTEXT_SHAPE = {
2343
2375
  "subscribeBackgroundWatchState(cb) -> unsubscribe }, " +
2344
2376
  "speech: { isSupported() -> boolean, start(options, { onResult, onError, onEnd }) -> Promise<{ stop(), abort() }> }, " +
2345
2377
  "camera: { isSupported() -> boolean, capture(options?) -> Promise<asset | null>, pick(options?) -> Promise<asset | null> }, " +
2346
- "imageEditor: { isSupported() -> boolean, edit(uri, actions, options?) -> Promise<asset> } }. " +
2347
- "Backs useGeolocation(), useSpeechToText(), useCamera() and useImageEditor(). The web Player brokers them via " +
2348
- "navigator.geolocation, window.SpeechRecognition, a getUserMedia camera preview and a host-side canvas; the Expo export via " +
2349
- "expo-location, expo-speech-recognition, expo-image-picker and expo-image-manipulator. " +
2378
+ "imageEditor: { isSupported() -> boolean, edit(uri, actions, options?) -> Promise<asset> }, " +
2379
+ "barcode: { isSupported() -> boolean, scan(options?) -> Promise<{ value, format } | null> } }. " +
2380
+ "Backs useGeolocation(), useSpeechToText(), useCamera(), useImageEditor() and useBarcodeScanner(). The web Player brokers them via " +
2381
+ "navigator.geolocation, window.SpeechRecognition, a getUserMedia camera preview, a host-side canvas and BarcodeDetector; the Expo export via " +
2382
+ "expo-location, expo-speech-recognition, expo-image-picker, expo-image-manipulator and expo-camera. " +
2350
2383
  "imageEditor.edit applies resize / crop / rotate / flip in order and rejects with an ImageEditorError " +
2351
2384
  "(.code UNSUPPORTED | INVALID_ACTION | DECODE_FAILED | ENCODE_FAILED | INTERNAL). " +
2352
2385
  "getCurrentPosition rejects with a GeolocationError (.code PERMISSION_DENIED | UNAVAILABLE | TIMEOUT | UNSUPPORTED | INTERNAL). " +
@@ -2354,6 +2387,11 @@ const WIDGET_CONTEXT_SHAPE = {
2354
2387
  "its onError carries the Web Speech error vocabulary (not-allowed | no-speech | language-not-supported | network | aborted). " +
2355
2388
  "camera.capture/pick resolve a normalised { uri, name, mimeType, width, height, size, file, release? } or NULL when the " +
2356
2389
  "user dismisses the picker, and reject with a CameraError (.code PERMISSION_DENIED | UNSUPPORTED | INTERNAL). " +
2390
+ "barcode.scan resolves { value, format } for the first code decoded or NULL when the user dismisses, and rejects with a " +
2391
+ "BarcodeError (.code PERMISSION_DENIED | UNSUPPORTED | INTERNAL). Its formats hint and its format output BOTH speak " +
2392
+ "BarcodeDetector names (qr_code, code_128, ean_13, ...) on both hosts; the Expo export maps them to expo-camera own " +
2393
+ "vocabulary internally and DROPS a name it cannot map. isSupported() is false wherever the browser ships no " +
2394
+ "BarcodeDetector (Safari, Firefox), so a widget must always offer manual entry beside a scan button. " +
2357
2395
  "sc-6450 — the geolocation background-watch members are NATIVE-ONLY and opt-in per app: the web Player and an export that " +
2358
2396
  "did not opt in both report isBackgroundSupported() false, and the Expo export backs it with expo-location + " +
2359
2397
  "expo-task-manager. subscribeBackgroundPositions and subscribeBackgroundWatchState are plain subscriptions — they start no " +
@@ -2367,6 +2405,7 @@ const WIDGET_CONTEXT_SHAPE = {
2367
2405
  speech: "object",
2368
2406
  camera: "object",
2369
2407
  imageEditor: "object",
2408
+ barcode: "object",
2370
2409
  },
2371
2410
  },
2372
2411
  };
@@ -3768,7 +3807,20 @@ const CONTRACT = deepFreeze({
3768
3807
  // reason date-fns is: an AI-agent widget is transpiled, never bundled, so
3769
3808
  // its bare import must resolve at runtime on both hosts. No existing entry
3770
3809
  // changed shape — minor bump on the pre-1.0 channel.
3771
- version: "1.99.0",
3810
+ // 1.100.0: additive (sc-7228) — new `useBarcodeScanner()` hook + a `barcode`
3811
+ // member on the optional `device` host slice. Widgets could GENERATE a QR
3812
+ // code (react-native-qrcode-svg) but never READ one, so inventory counts,
3813
+ // asset check-in/out, ticket scanning and scan-on-delivery were
3814
+ // unbuildable. Host-brokered rather than a vetted import, the same call as
3815
+ // `camera` and `speech`: widgets never import the native module, so
3816
+ // `expo-camera` stays out of widget bundles and is pinned in the export
3817
+ // only. One-shot scan() modelled on useCamera().capture() rather than a
3818
+ // start/stop subscription — no second streaming lifecycle to keep in step.
3819
+ // NOT native-only: the web half is BarcodeDetector over the getUserMedia
3820
+ // preview useCamera already owns, so this is full parity wherever the
3821
+ // browser ships the API and a declared `supported:false` where it does not
3822
+ // (Safari, Firefox) — a genuine browser gap per CLAUDE.md §8.
3823
+ version: "1.100.0",
3772
3824
  sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
3773
3825
  hooks: HOOKS,
3774
3826
  primitives: PRIMITIVES,
package/dist/hooks.js CHANGED
@@ -1589,6 +1589,163 @@ export function useCamera(options) {
1589
1589
  return { asset, loading, error, supported, capture, pick, reset };
1590
1590
  }
1591
1591
 
1592
+ /**
1593
+ * The symbology names the SDK speaks on BOTH hosts — BarcodeDetector's
1594
+ * vocabulary. Normalising here is what stops the hosts diverging on bad
1595
+ * input: the web detector THROWS on a name outside its enum while the native
1596
+ * scanner silently ignores one, so an unknown name would reject on web and
1597
+ * quietly scan everything on native.
1598
+ */
1599
+ const BARCODE_FORMATS = Object.freeze([
1600
+ "aztec",
1601
+ "codabar",
1602
+ "code_128",
1603
+ "code_39",
1604
+ "code_93",
1605
+ "data_matrix",
1606
+ "ean_13",
1607
+ "ean_8",
1608
+ "itf",
1609
+ "pdf417",
1610
+ "qr_code",
1611
+ "upc_a",
1612
+ "upc_e",
1613
+ ]);
1614
+
1615
+ /** Drop anything the hosts cannot both honour, so neither has to guess. */
1616
+ function normaliseBarcodeOptions(options) {
1617
+ const opts = options || {};
1618
+ if (!Array.isArray(opts.formats)) return opts;
1619
+ const formats = opts.formats
1620
+ .map((name) => String(name).toLowerCase())
1621
+ .filter((name) => BARCODE_FORMATS.includes(name));
1622
+ return { ...opts, formats };
1623
+ }
1624
+
1625
+ /**
1626
+ * Structured error thrown by `useBarcodeScanner` callbacks.
1627
+ *
1628
+ * `code` is one of:
1629
+ * - "PERMISSION_DENIED" — the user (or OS) refused camera access.
1630
+ * - "UNSUPPORTED" — this host brokers no scanner (no BarcodeDetector).
1631
+ * - "INTERNAL" — anything else.
1632
+ *
1633
+ * A user dismissing the scanner is NOT an error — the promise resolves `null`.
1634
+ */
1635
+ export class BarcodeError extends Error {
1636
+ constructor(code, message, opts) {
1637
+ super(message);
1638
+ this.name = "BarcodeError";
1639
+ this.code = code;
1640
+ if (opts && opts.cause) this.cause = opts.cause;
1641
+ }
1642
+ }
1643
+
1644
+ /** Coerce a thrown value into a BarcodeError with a stable code. */
1645
+ function toBarcodeError(err) {
1646
+ if (err instanceof BarcodeError) return err;
1647
+ const raw = err && err.code !== undefined ? err.code : null;
1648
+ let code = "INTERNAL";
1649
+ if (raw === "PERMISSION_DENIED") code = "PERMISSION_DENIED";
1650
+ else if (raw === "UNSUPPORTED") code = "UNSUPPORTED";
1651
+ const message =
1652
+ (err && typeof err.message === "string" && err.message) ||
1653
+ "Barcode scan failed";
1654
+ return new BarcodeError(code, message, { cause: err });
1655
+ }
1656
+
1657
+ /**
1658
+ * Read a barcode or QR code with the device camera. Returns
1659
+ * `{ result, scanning, error, supported, scan, reset }`.
1660
+ *
1661
+ * Scanning is IMPERATIVE — call `scan()` from a user gesture. The OS and the
1662
+ * browser gate the camera permission prompt on a gesture, so the hook never
1663
+ * opens the camera on mount.
1664
+ *
1665
+ * `scan()` resolves `{ value, format }` for the first code decoded, or `null`
1666
+ * when the user dismisses the scanner — dismissal is the most common outcome
1667
+ * and is deliberately not an error, so widgets need no try/catch on the happy
1668
+ * path. It rejects with a `BarcodeError` for a genuine failure.
1669
+ *
1670
+ * One-shot by design: to read several codes, call `scan()` again. There is no
1671
+ * start/stop subscription to leave running.
1672
+ *
1673
+ * ALWAYS check `supported` before rendering a scan button, and give the widget
1674
+ * a manual-entry path: the web Player reports false wherever the browser ships
1675
+ * no `BarcodeDetector` (Safari and Firefox today). That is a real browser gap,
1676
+ * so a scanner must never be the only way to enter a code.
1677
+ */
1678
+ export function useBarcodeScanner(options) {
1679
+ const ctx = useWidgetContextOrThrow("useBarcodeScanner");
1680
+ const [result, setResult] = useState(null);
1681
+ const [scanning, setScanning] = useState(false);
1682
+ const [error, setError] = useState(null);
1683
+ // `ctx` is a fresh identity every host render — hold the live client and
1684
+ // options in refs so the returned callbacks stay stable.
1685
+ const clientRef = useRef(ctx.device && ctx.device.barcode);
1686
+ clientRef.current = ctx.device && ctx.device.barcode;
1687
+ const optionsRef = useRef(options);
1688
+ optionsRef.current = options;
1689
+ const runRef = useRef(0);
1690
+
1691
+ const supported = Boolean(
1692
+ clientRef.current &&
1693
+ typeof clientRef.current.scan === "function" &&
1694
+ (typeof clientRef.current.isSupported !== "function" ||
1695
+ clientRef.current.isSupported()),
1696
+ );
1697
+
1698
+ // A scan open at unmount still resolves; bumping the run id discards it
1699
+ // instead of setting state on a widget that is gone.
1700
+ useEffect(
1701
+ () => () => {
1702
+ runRef.current += 1;
1703
+ },
1704
+ [],
1705
+ );
1706
+
1707
+ const reset = useCallback(() => {
1708
+ runRef.current += 1;
1709
+ setResult(null);
1710
+ setError(null);
1711
+ }, []);
1712
+
1713
+ const scan = useCallback(async () => {
1714
+ const client = clientRef.current;
1715
+ if (
1716
+ !client ||
1717
+ typeof client.scan !== "function" ||
1718
+ (typeof client.isSupported === "function" && !client.isSupported())
1719
+ ) {
1720
+ const e = new BarcodeError(
1721
+ "UNSUPPORTED",
1722
+ "This host does not provide barcode scanning.",
1723
+ );
1724
+ setError(e);
1725
+ throw e;
1726
+ }
1727
+ const run = (runRef.current += 1);
1728
+ setScanning(true);
1729
+ setError(null);
1730
+ try {
1731
+ const next = await client.scan(normaliseBarcodeOptions(optionsRef.current));
1732
+ // A reset(), a newer scan, or an unmount landed while this one was open.
1733
+ if (run !== runRef.current) return null;
1734
+ if (!next) return null;
1735
+ setResult(next);
1736
+ return next;
1737
+ } catch (err) {
1738
+ const be = toBarcodeError(err);
1739
+ if (run === runRef.current) setError(be);
1740
+ throw be;
1741
+ } finally {
1742
+ if (run === runRef.current) setScanning(false);
1743
+ }
1744
+ }, []);
1745
+
1746
+ return { result, scanning, error, supported, scan, reset };
1747
+ }
1748
+
1592
1749
  /**
1593
1750
  * Structured error thrown by `useImageEditor` callbacks.
1594
1751
  *
package/dist/index.d.ts CHANGED
@@ -1746,6 +1746,68 @@ export class ImageEditorError extends Error {
1746
1746
  );
1747
1747
  }
1748
1748
 
1749
+ /** A code decoded by `useBarcodeScanner().scan()`. */
1750
+ export interface BarcodeScan {
1751
+ /** The decoded text. */
1752
+ value: string;
1753
+ /**
1754
+ * Lowercase symbology name (`qr_code`, `code_128`, `ean_13`, …). A HINT, not
1755
+ * a promise: the two hosts detect different sets, so never branch on it for
1756
+ * correctness.
1757
+ */
1758
+ format: string;
1759
+ }
1760
+
1761
+ /** Options for `useBarcodeScanner(...)`. */
1762
+ export interface BarcodeScannerOptions {
1763
+ /** Narrow which symbologies to look for. A hint the host honours where it can. */
1764
+ formats?: string[];
1765
+ }
1766
+
1767
+ export interface BarcodeScannerResult {
1768
+ /** The most recent scan, or null before the first call / after reset. */
1769
+ result: BarcodeScan | null;
1770
+ scanning: boolean;
1771
+ error: BarcodeError | null;
1772
+ /**
1773
+ * False where the host brokers no scanner — notably any browser without
1774
+ * `BarcodeDetector` (Safari, Firefox). GATE THE SCAN BUTTON ON THIS.
1775
+ */
1776
+ supported: boolean;
1777
+ /** Resolves the first code decoded, or null if the user dismisses. */
1778
+ scan(): Promise<BarcodeScan | null>;
1779
+ /** Clear `result` and `error`. */
1780
+ reset(): void;
1781
+ }
1782
+
1783
+ /**
1784
+ * Read a barcode or QR code with the device camera. Imperative — call `scan()`
1785
+ * from a user gesture; the OS and the browser gate the camera prompt on one, so
1786
+ * it never opens on mount. One-shot by design: to read several codes, call
1787
+ * `scan()` again rather than leaving a subscription running.
1788
+ *
1789
+ * The Expo export scans via `expo-camera`; the web Player via `BarcodeDetector`
1790
+ * over the same `getUserMedia` preview `useCamera()` uses. Where the browser
1791
+ * ships no `BarcodeDetector` this is a genuine platform gap (CLAUDE.md §8), so
1792
+ * `supported` is false and the widget must offer manual entry instead.
1793
+ */
1794
+ export function useBarcodeScanner(
1795
+ options?: BarcodeScannerOptions,
1796
+ ): BarcodeScannerResult;
1797
+
1798
+ /**
1799
+ * Error surfaced by `useBarcodeScanner()` — thrown by `scan()` and stored in
1800
+ * the hook's `error` slot. A dismissal is not an error; `scan()` resolves null.
1801
+ */
1802
+ export class BarcodeError extends Error {
1803
+ code: "PERMISSION_DENIED" | "UNSUPPORTED" | "INTERNAL";
1804
+ constructor(
1805
+ code: BarcodeError["code"],
1806
+ message: string,
1807
+ opts?: { cause?: unknown },
1808
+ );
1809
+ }
1810
+
1749
1811
  /**
1750
1812
  * Error class thrown by useDatastoreMutation callbacks (and surfaced by
1751
1813
  * useDatastoreQuery in its `error` slot). The `code` is a stable
package/dist/index.js CHANGED
@@ -84,6 +84,8 @@ export {
84
84
  CameraError,
85
85
  useImageEditor,
86
86
  ImageEditorError,
87
+ useBarcodeScanner,
88
+ BarcodeError,
87
89
  WidgetTree,
88
90
  } from "./hooks.js";
89
91
  export { isNarrowWidth, NARROW_WIDTH_PX } from "./container-width.js";
@@ -84,6 +84,8 @@ export {
84
84
  CameraError,
85
85
  useImageEditor,
86
86
  ImageEditorError,
87
+ useBarcodeScanner,
88
+ BarcodeError,
87
89
  WidgetTree,
88
90
  } from "./hooks.js";
89
91
  export { isNarrowWidth, NARROW_WIDTH_PX } from "./container-width.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colixsystems/widget-sdk",
3
- "version": "0.128.0",
3
+ "version": "0.129.0",
4
4
  "description": "Common widget interface for AppStudio. Implements WidgetManifest, WidgetContext, property schema, and helper hooks.",
5
5
  "homepage": "https://github.com/Colix-AB/AppStudio",
6
6
  "type": "module",
@@ -49,7 +49,7 @@
49
49
  ],
50
50
  "scripts": {
51
51
  "build": "node scripts/build.js",
52
- "test": "node --test src/__tests__/contract.test.js src/__tests__/vetted-imports-audit.test.js src/__tests__/hooks-users.test.js src/__tests__/hooks-groups.test.js src/__tests__/hooks-invites.test.js src/__tests__/hooks-schema.test.js src/__tests__/hooks-assets-by-tag.test.js src/__tests__/hooks-filestore-upload.test.js src/__tests__/hooks-filestore-file.test.js src/__tests__/hooks-mutation.test.js src/__tests__/hooks-payments.test.js src/__tests__/hooks-record-permissions.test.js src/__tests__/hooks-geolocation.test.js src/__tests__/hooks-section-empty.test.js src/__tests__/hooks-widget-event.test.js src/__tests__/hooks-widget-input.test.js src/__tests__/hooks-identification.test.js src/__tests__/hooks-subscription.test.js src/__tests__/hooks-volatile-query-key.test.js src/__tests__/linter-users-scope.test.js src/__tests__/linter-comments.test.js src/__tests__/linter-translation-api.test.js src/__tests__/linter-page-url.test.js src/__tests__/linter-image-height.test.js src/__tests__/linter-flex-basis-percent.test.js src/__tests__/linter-hardcoded-design.test.js src/__tests__/linter-measured-padding.test.js src/__tests__/linter-payment-error.test.js src/__tests__/linter-platform.test.js src/__tests__/linter-react-import.test.js src/__tests__/lucide-icon-names.test.js src/__tests__/lucideIconName.test.js src/__tests__/manifest-actions.test.js src/__tests__/widget-translations.test.js src/__tests__/hooks-translate.test.js src/__tests__/devserver.test.js src/__tests__/flatten-entry.test.js src/__tests__/host-externals.test.js src/__tests__/datetimepicker.test.js src/__tests__/property-schema-resolve.test.js src/__tests__/corner-radius.test.js src/__tests__/theme-components-parity.test.js src/__tests__/navigation-parity.test.js src/__tests__/theme-depth-tokens.test.js src/__tests__/interaction-lift.test.js src/__tests__/toast-host.test.js src/__tests__/overlay-tokens.test.js src/__tests__/hooks-domain-error-mapping.test.js src/__tests__/linter-datastore-error.test.js src/__tests__/linter-write-gating.test.js src/__tests__/hooks-speech-to-text.test.js src/__tests__/hooks-camera.test.js src/__tests__/hooks-image-editor.test.js src/__tests__/hooks-bound-columns.test.js src/__tests__/hooks-stable-query.test.js src/__tests__/hooks-can-write.test.js src/__tests__/widget-route.test.js src/__tests__/linter-html-in-content.test.js src/__tests__/markdown.test.js src/__tests__/markdown-edit.test.js src/__tests__/richtext-tokens.test.js"
52
+ "test": "node --test src/__tests__/contract.test.js src/__tests__/vetted-imports-audit.test.js src/__tests__/hooks-users.test.js src/__tests__/hooks-groups.test.js src/__tests__/hooks-invites.test.js src/__tests__/hooks-schema.test.js src/__tests__/hooks-assets-by-tag.test.js src/__tests__/hooks-filestore-upload.test.js src/__tests__/hooks-filestore-file.test.js src/__tests__/hooks-mutation.test.js src/__tests__/hooks-payments.test.js src/__tests__/hooks-record-permissions.test.js src/__tests__/hooks-geolocation.test.js src/__tests__/hooks-section-empty.test.js src/__tests__/hooks-widget-event.test.js src/__tests__/hooks-widget-input.test.js src/__tests__/hooks-identification.test.js src/__tests__/hooks-subscription.test.js src/__tests__/hooks-volatile-query-key.test.js src/__tests__/linter-users-scope.test.js src/__tests__/linter-comments.test.js src/__tests__/linter-translation-api.test.js src/__tests__/linter-page-url.test.js src/__tests__/linter-image-height.test.js src/__tests__/linter-flex-basis-percent.test.js src/__tests__/linter-hardcoded-design.test.js src/__tests__/linter-measured-padding.test.js src/__tests__/linter-payment-error.test.js src/__tests__/linter-platform.test.js src/__tests__/linter-react-import.test.js src/__tests__/lucide-icon-names.test.js src/__tests__/lucideIconName.test.js src/__tests__/manifest-actions.test.js src/__tests__/widget-translations.test.js src/__tests__/hooks-translate.test.js src/__tests__/devserver.test.js src/__tests__/flatten-entry.test.js src/__tests__/host-externals.test.js src/__tests__/datetimepicker.test.js src/__tests__/property-schema-resolve.test.js src/__tests__/corner-radius.test.js src/__tests__/theme-components-parity.test.js src/__tests__/navigation-parity.test.js src/__tests__/theme-depth-tokens.test.js src/__tests__/interaction-lift.test.js src/__tests__/toast-host.test.js src/__tests__/overlay-tokens.test.js src/__tests__/hooks-domain-error-mapping.test.js src/__tests__/linter-datastore-error.test.js src/__tests__/linter-write-gating.test.js src/__tests__/hooks-speech-to-text.test.js src/__tests__/hooks-camera.test.js src/__tests__/hooks-barcode.test.js src/__tests__/hooks-image-editor.test.js src/__tests__/hooks-bound-columns.test.js src/__tests__/hooks-stable-query.test.js src/__tests__/hooks-can-write.test.js src/__tests__/widget-route.test.js src/__tests__/linter-html-in-content.test.js src/__tests__/markdown.test.js src/__tests__/markdown-edit.test.js src/__tests__/richtext-tokens.test.js"
53
53
  },
54
54
  "engines": {
55
55
  "node": ">=18"