@dif.sh/react 0.2.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 ADDED
@@ -0,0 +1,84 @@
1
+ # @dif.sh/react
2
+
3
+ React adapter for [@dif.sh/sdk](https://www.npmjs.com/package/@dif.sh/sdk).
4
+ Wraps `dif.init`, `dif.track`, and the experiment-assignment call in a
5
+ provider + hook so React apps don't have to touch the module-level singleton
6
+ directly.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm install @dif.sh/sdk @dif.sh/react
12
+ ```
13
+
14
+ `@dif.sh/sdk` is a peer dependency — install it explicitly.
15
+
16
+ ## Initialize at the root of your tree
17
+
18
+ ```tsx
19
+ import { DifProvider } from "@dif.sh/react";
20
+
21
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
22
+ return (
23
+ <DifProvider
24
+ config={{
25
+ project: "acme-shop",
26
+ publishableKey: process.env.NEXT_PUBLIC_DIF_PUBLISHABLE_KEY,
27
+ userId: () => currentUser?.id ?? null,
28
+ }}
29
+ >
30
+ {children}
31
+ </DifProvider>
32
+ );
33
+ }
34
+ ```
35
+
36
+ The provider runs `dif.init(config)` exactly once on first render. The
37
+ underlying state is a module-level singleton; re-mounting the provider with
38
+ a different config will replace the previous one.
39
+
40
+ ## Track from any component
41
+
42
+ ```tsx
43
+ import { useEffect } from "react";
44
+ import { useDif } from "@dif.sh/react";
45
+
46
+ export function CheckoutSuccess() {
47
+ const { track } = useDif();
48
+ useEffect(() => {
49
+ track("completed_checkout", { value: 49, currency: "USD" });
50
+ }, []);
51
+ return <Receipt />;
52
+ }
53
+ ```
54
+
55
+ ## Experiment assignment from a hook
56
+
57
+ ```tsx
58
+ import { useDif } from "@dif.sh/react";
59
+
60
+ export function CheckoutCTA() {
61
+ const { exposure: dif } = useDif();
62
+ const cta = dif("checkout-cta-v2", {
63
+ control: () => "Place order",
64
+ variant_a: () => "Get it today",
65
+ });
66
+ return <button>{cta()}</button>;
67
+ }
68
+ ```
69
+
70
+ `exposure` has the same signature as the bare `dif()` from `@dif.sh/sdk`.
71
+ You can also keep using the bare import directly in non-component code —
72
+ both paths read the same module-level state set by `<DifProvider>`.
73
+
74
+ ## What this package does, what it doesn't
75
+
76
+ **Does:**
77
+ - Provide `<DifProvider>` to initialize the SDK at the root of your tree.
78
+ - Provide `useDif()` to read `{ track, exposure }` from anywhere.
79
+
80
+ **Does not:**
81
+ - Re-initialize on prop changes in v0 — pass a stable config.
82
+ - Maintain its own event queue. Same fire-and-forget semantics as the SDK.
83
+
84
+ See [../sdk/README.md](../sdk/README.md) for the full SDK contract.
@@ -0,0 +1,4 @@
1
+ export { DifProvider, useDif } from "./provider.js";
2
+ export type { DifProviderProps, DifContextValue } from "./provider.js";
3
+ export type { DifInitConfig, TrackProps } from "@dif.sh/sdk";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACvE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { DifProvider, useDif } from "./provider.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,27 @@
1
+ import { type ReactNode } from "react";
2
+ import type { DifInitConfig, TrackProps } from "@dif.sh/sdk";
3
+ interface ExperimentFn {
4
+ <V extends string, R>(id: string, branches: Record<V, () => R>): () => R;
5
+ }
6
+ export interface DifContextValue {
7
+ /** Fire a metric event. Matches the cloud-page snippet's `const { track } = useDif()`. */
8
+ track: (metric: string, opts?: TrackProps) => void;
9
+ /** Experiment assignment — same signature as the bare `dif(...)` function. */
10
+ exposure: ExperimentFn;
11
+ }
12
+ export interface DifProviderProps {
13
+ config: DifInitConfig;
14
+ children: ReactNode;
15
+ }
16
+ /**
17
+ * Initializes the SDK exactly once and provides `useDif()` to descendants.
18
+ *
19
+ * The SDK's underlying state is a module-level singleton, so re-mounting the
20
+ * provider with a different config will replace the previous config. v0 does
21
+ * not re-init on prop changes — pass a stable config object.
22
+ */
23
+ export declare function DifProvider({ config, children }: DifProviderProps): import("react/jsx-runtime").JSX.Element;
24
+ /** Hook returning `{ track, exposure }`. Must be called inside `<DifProvider>`. */
25
+ export declare function useDif(): DifContextValue;
26
+ export {};
27
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAqC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAE1E,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE7D,UAAU,YAAY;IACpB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;CAC1E;AAED,MAAM,WAAW,eAAe;IAC9B,0FAA0F;IAC1F,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU,KAAK,IAAI,CAAC;IACnD,8EAA8E;IAC9E,QAAQ,EAAE,YAAY,CAAC;CACxB;AAID,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,aAAa,CAAC;IACtB,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,gBAAgB,2CAajE;AAED,mFAAmF;AACnF,wBAAgB,MAAM,IAAI,eAAe,CAMxC"}
@@ -0,0 +1,33 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { createContext, useContext, useRef } from "react";
4
+ import { dif } from "@dif.sh/sdk";
5
+ const DifContext = createContext(null);
6
+ /**
7
+ * Initializes the SDK exactly once and provides `useDif()` to descendants.
8
+ *
9
+ * The SDK's underlying state is a module-level singleton, so re-mounting the
10
+ * provider with a different config will replace the previous config. v0 does
11
+ * not re-init on prop changes — pass a stable config object.
12
+ */
13
+ export function DifProvider({ config, children }) {
14
+ const initialized = useRef(false);
15
+ if (!initialized.current) {
16
+ dif.init(config);
17
+ initialized.current = true;
18
+ }
19
+ const value = {
20
+ track: (metric, opts) => dif.track(metric, opts),
21
+ exposure: ((id, branches) => dif(id, branches)),
22
+ };
23
+ return _jsx(DifContext.Provider, { value: value, children: children });
24
+ }
25
+ /** Hook returning `{ track, exposure }`. Must be called inside `<DifProvider>`. */
26
+ export function useDif() {
27
+ const ctx = useContext(DifContext);
28
+ if (!ctx) {
29
+ throw new Error("useDif must be called inside <DifProvider>");
30
+ }
31
+ return ctx;
32
+ }
33
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAclC,MAAM,UAAU,GAAG,aAAa,CAAyB,IAAI,CAAC,CAAC;AAO/D;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAoB;IAChE,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QACzB,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACjB,WAAW,CAAC,OAAO,GAAG,IAAI,CAAC;IAC7B,CAAC;IAED,MAAM,KAAK,GAAoB;QAC7B,KAAK,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC;QAChD,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAiB;KAChE,CAAC;IAEF,OAAO,KAAC,UAAU,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YAAG,QAAQ,GAAuB,CAAC;AAC7E,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,MAAM;IACpB,MAAM,GAAG,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;IACnC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAChE,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@dif.sh/react",
3
+ "version": "0.2.0",
4
+ "description": "React adapter for @dif.sh/sdk — provider + useDif hook.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "sideEffects": false,
20
+ "scripts": {
21
+ "build": "tsc -p tsconfig.json",
22
+ "lint": "tsc --noEmit",
23
+ "prepublishOnly": "npm run lint && npm run build"
24
+ },
25
+ "engines": {
26
+ "node": ">=20.6"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/dif-sh/dif.git",
34
+ "directory": "cli/packages/react"
35
+ },
36
+ "keywords": [
37
+ "experimentation",
38
+ "ab-testing",
39
+ "feature-flags",
40
+ "analytics",
41
+ "react",
42
+ "dif.sh"
43
+ ],
44
+ "peerDependencies": {
45
+ "@dif.sh/sdk": "^0.2.0",
46
+ "react": ">=18"
47
+ },
48
+ "devDependencies": {
49
+ "@dif.sh/sdk": "file:../sdk",
50
+ "@types/react": "^19.0.0",
51
+ "react": "^19.0.0",
52
+ "typescript": "^5.4.0"
53
+ }
54
+ }