drupal-canvas 0.5.2 → 0.7.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/dist/react.js ADDED
@@ -0,0 +1,158 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useEffect } from "react";
3
+ //#region src/data-inspection.ts
4
+ /**
5
+ * @file
6
+ * Editor data inspection: reports the values a Code Component reads through
7
+ * the context hooks to the Canvas code editor's "Component data" panel, using
8
+ * the same `_canvas_useswr_data_fetch` message the legacy getters emit. The
9
+ * reports never fetch data or refresh previews; they are deduplicated per
10
+ * hook and value so repeated renders do not repeat entries.
11
+ */
12
+ const lastReported = /* @__PURE__ */ new Map();
13
+ /**
14
+ * Posts one deduplicated data report to the embedding document, when there
15
+ * is one. Standalone documents (no parent frame) and server rendering are
16
+ * no-ops.
17
+ */
18
+ function reportCanvasData(id, data) {
19
+ if (typeof window === "undefined") return;
20
+ const parent = window.parent;
21
+ if (!parent || parent === window) return;
22
+ let serialized;
23
+ try {
24
+ serialized = JSON.stringify(data) ?? "undefined";
25
+ } catch {
26
+ return;
27
+ }
28
+ if (lastReported.get(id) === serialized) return;
29
+ lastReported.set(id, serialized);
30
+ try {
31
+ parent.postMessage({
32
+ type: "_canvas_useswr_data_fetch",
33
+ id,
34
+ data
35
+ });
36
+ } catch {}
37
+ }
38
+ //#endregion
39
+ //#region src/warnings.ts
40
+ /**
41
+ * @file
42
+ * Deduplicated developer warnings for the context hooks. Each distinct
43
+ * message is emitted once per JavaScript realm so a missing provider does not
44
+ * flood the console on every render.
45
+ */
46
+ const emitted = /* @__PURE__ */ new Set();
47
+ /** Emits `message` through `console.warn` once. */
48
+ function warnOnce(message) {
49
+ if (emitted.has(message)) return;
50
+ emitted.add(message);
51
+ console.warn(`[drupal-canvas] ${message}`);
52
+ }
53
+ //#endregion
54
+ //#region src/context.tsx
55
+ /**
56
+ * @file
57
+ * The shared page and site context for React Code Components. Rendering
58
+ * integrations (Drupal islands, the headless React renderer, Workbench
59
+ * previews) establish the provider; components read it with the hooks.
60
+ *
61
+ * @see docs/adr/0021-code-component-runtime-compatibility-across-frontend-modes.md
62
+ */
63
+ /**
64
+ * `undefined` means no provider is mounted; an explicit `null` page or site
65
+ * value inside a provider means the integration has no such data.
66
+ */
67
+ const Context$1 = createContext(void 0);
68
+ Context$1.displayName = "CanvasContext";
69
+ /**
70
+ * Makes page and site context available to every React descendant. Adds no
71
+ * DOM wrapper. Rendering integrations use it internally; applications can use
72
+ * it to reach components outside the Canvas tree, such as a site header.
73
+ */
74
+ function CanvasContextProvider({ context, children }) {
75
+ return /* @__PURE__ */ jsx(Context$1.Provider, {
76
+ value: context,
77
+ children
78
+ });
79
+ }
80
+ const MISSING_PROVIDER_WARNING = "No CanvasContextProvider is mounted above this component. In headless React applications pass `context={page.context}` to CanvasComponentTree or wrap the tree in CanvasContextProvider; in Drupal and Workbench the rendering integration provides it.";
81
+ /**
82
+ * Returns the current page context, or `null` when no provider supplies one.
83
+ * Emits a deduplicated developer warning in that case; a provider that
84
+ * carries valid empty page data is not a missing-context condition.
85
+ */
86
+ function usePageContext() {
87
+ const context = useContext(Context$1);
88
+ const page = context === void 0 ? null : context.page;
89
+ if (context === void 0) warnOnce(`usePageContext(): ${MISSING_PROVIDER_WARNING}`);
90
+ else if (page === null) warnOnce("usePageContext(): the rendering integration supplied no page context (`context.page` is null), so the hook returns null.");
91
+ useEffect(() => {
92
+ if (context !== void 0) reportCanvasData("usePageContext()", page);
93
+ }, [context, page]);
94
+ return page;
95
+ }
96
+ /**
97
+ * Returns the current site context, or `null` when no provider supplies one.
98
+ * Emits a deduplicated developer warning in that case.
99
+ */
100
+ function useSiteContext() {
101
+ const context = useContext(Context$1);
102
+ const site = context === void 0 ? null : context.site;
103
+ if (context === void 0) warnOnce(`useSiteContext(): ${MISSING_PROVIDER_WARNING}`);
104
+ else if (site === null) warnOnce("useSiteContext(): the rendering integration supplied no site context (`context.site` is null), so the hook returns null.");
105
+ useEffect(() => {
106
+ if (context !== void 0) reportCanvasData("useSiteContext()", site);
107
+ }, [context, site]);
108
+ return site;
109
+ }
110
+ /**
111
+ * Whether a `CanvasContextProvider` is mounted above the calling component.
112
+ * Rendering integrations use it to decide whether to inherit an outer
113
+ * provider instead of adding their own.
114
+ */
115
+ function useHasCanvasContext() {
116
+ return useContext(Context$1) !== void 0;
117
+ }
118
+ //#endregion
119
+ //#region src/jsonapi-client-context.tsx
120
+ /**
121
+ * @file
122
+ * The JSON:API client context for React Code Components. Rendering
123
+ * integrations own client creation and reuse; the provider receives an
124
+ * already configured client and the hook reads it.
125
+ */
126
+ const Context = createContext(void 0);
127
+ Context.displayName = "CanvasJsonApiClientContext";
128
+ /**
129
+ * Makes a configured JSON:API client available to every React descendant.
130
+ * It does not construct the client or manage authentication.
131
+ */
132
+ function JsonApiClientProvider({ client, children }) {
133
+ return /* @__PURE__ */ jsx(Context.Provider, {
134
+ value: client,
135
+ children
136
+ });
137
+ }
138
+ /**
139
+ * Returns the nearest provider's client, or `null` with a deduplicated
140
+ * warning when no provider exists. The hook never fetches data or creates a
141
+ * client on render.
142
+ */
143
+ function useJsonApiClient() {
144
+ const client = useContext(Context);
145
+ if (client === void 0) {
146
+ warnOnce("useJsonApiClient(): no JsonApiClientProvider is mounted above this component. Headless React renderers provide one when they receive the JSON:API runtime configuration from the framework adapter; applications can also wrap components in JsonApiClientProvider. In Drupal and Workbench the rendering integration provides it.");
147
+ return null;
148
+ }
149
+ return client;
150
+ }
151
+ /**
152
+ * Whether a `JsonApiClientProvider` is mounted above the calling component.
153
+ */
154
+ function useHasJsonApiClient() {
155
+ return useContext(Context) !== void 0;
156
+ }
157
+ //#endregion
158
+ export { CanvasContextProvider, JsonApiClientProvider, useHasCanvasContext, useHasJsonApiClient, useJsonApiClient, usePageContext, useSiteContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drupal-canvas",
3
- "version": "0.5.2",
3
+ "version": "0.7.0",
4
4
  "engines": {
5
5
  "node": ">=22.19.0 <23 || >=24.5.0"
6
6
  },
@@ -19,6 +19,11 @@
19
19
  "types": "./dist/index.d.ts",
20
20
  "default": "./dist/index.js"
21
21
  },
22
+ "./react": {
23
+ "import": "./dist/react.js",
24
+ "types": "./dist/react.d.ts",
25
+ "default": "./dist/react.js"
26
+ },
22
27
  "./json-render-utils": {
23
28
  "import": "./dist/json-render-utils.js",
24
29
  "types": "./dist/json-render-utils.d.ts",
@@ -75,6 +80,8 @@
75
80
  "zod": "^4.2.0"
76
81
  },
77
82
  "devDependencies": {
83
+ "jsdom": "^26.1.0",
84
+ "react-dom": "^18.3.1",
78
85
  "tsdown": "^0.22.12",
79
86
  "typescript": "^5.9.3",
80
87
  "vitest": "^4.1.8"
@@ -84,5 +91,8 @@
84
91
  "dev": "NODE_ENV=development tsdown --watch",
85
92
  "type-check": "tsc --noEmit --pretty",
86
93
  "test": "vitest run"
94
+ },
95
+ "peerDependencies": {
96
+ "react": ">=18.0.0"
87
97
  }
88
98
  }
@@ -1,13 +0,0 @@
1
- import { JsonApiClient, JsonApiClientOptions } from "@drupal-api-client/json-api-client";
2
- //#region ../../node_modules/@drupal-api-client/api-client/dist/index.d.mts
3
- /**
4
- * The base URL for all API requests.
5
- */
6
- type BaseUrl = string;
7
- //#endregion
8
- //#region src/jsonapi-client.d.ts
9
- declare class CanvasJsonApiClient extends JsonApiClient {
10
- constructor(baseUrl?: BaseUrl, options?: JsonApiClientOptions);
11
- }
12
- //#endregion
13
- export { CanvasJsonApiClient as t };