@sitecore-content-sdk/react 2.5.0 → 2.5.1-canary.20261102062040

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.
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ 'use client';
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.DatasourceCheckFallback = void 0;
8
+ const react_1 = __importDefault(require("react"));
9
+ const SitecoreProvider_1 = require("../components/SitecoreProvider");
10
+ /**
11
+ * Resolves the missing-datasource outcome from `SitecoreProvider` context when `page` is not available in props.
12
+ * Accepts only React elements so it can be rendered from a Server Component.
13
+ * @deprecated Temporary fallback for callers that omit `page`. Remove this component in version 3.0.0
14
+ * and make the `page` prop required on `withDatasourceCheck`.
15
+ * @param {DatasourceCheckFallbackProps} props component props
16
+ * @returns {JSX.Element | null} the wrapped component in Design Library, the editing error in editing mode, otherwise null
17
+ */
18
+ const DatasourceCheckFallback = ({ editingError, children, }) => {
19
+ var _a, _b;
20
+ const { page } = (0, SitecoreProvider_1.useSitecore)();
21
+ const isDesignLibrary = (_a = page === null || page === void 0 ? void 0 : page.mode) === null || _a === void 0 ? void 0 : _a.isDesignLibrary;
22
+ if (isDesignLibrary) {
23
+ return react_1.default.createElement(react_1.default.Fragment, null, children);
24
+ }
25
+ return ((_b = page === null || page === void 0 ? void 0 : page.mode) === null || _b === void 0 ? void 0 : _b.isEditing) ? react_1.default.createElement(react_1.default.Fragment, null, editingError) : null;
26
+ };
27
+ exports.DatasourceCheckFallback = DatasourceCheckFallback;
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- 'use client';
3
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
4
  };
@@ -7,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
6
  exports.DefaultEditingError = void 0;
8
7
  exports.withDatasourceCheck = withDatasourceCheck;
9
8
  const react_1 = __importDefault(require("react"));
10
- const SitecoreProvider_1 = require("./../components/SitecoreProvider");
9
+ const DatasourceCheckFallback_1 = require("./DatasourceCheckFallback");
11
10
  const DefaultEditingError = () => (react_1.default.createElement("div", { className: "sc-jss-editing-error", role: "alert" }, "Datasource is required. Please choose a content item for this component."));
12
11
  exports.DefaultEditingError = DefaultEditingError;
13
12
  /**
@@ -24,6 +23,10 @@ function hasValidDatasource(rendering) {
24
23
  /**
25
24
  * Checks whether a Sitecore datasource is present and valid, then renders appropriately depending on page mode (normal vs editing).
26
25
  * `isContentResolved: false` is treated the same as a missing datasource. If the property is omitted, the original presence check is used.
26
+ *
27
+ * Works in both Server and Client Components. Apply it in the same module as the wrapped component.
28
+ * When `page` is not passed (e.g. outside a `Placeholder`), the page mode is resolved on the client
29
+ * from `SitecoreProvider` context. In Server Components, pass `page` to keep the check on the server.
27
30
  * @param {WithDatasourceCheckOptions} [options]
28
31
  * @returns
29
32
  * The wrapped component, if a datasource is present and valid.
@@ -40,13 +43,24 @@ function hasValidDatasource(rendering) {
40
43
  */
41
44
  function withDatasourceCheck(options) {
42
45
  return function withDatasourceCheckHoc(Component) {
43
- return (props) => {
44
- var _a;
45
- const { page } = (0, SitecoreProvider_1.useSitecore)();
46
+ return function WithDatasourceCheck(props) {
47
+ var _a, _b, _c;
46
48
  const EditingError = (_a = options === null || options === void 0 ? void 0 : options.editingErrorComponent) !== null && _a !== void 0 ? _a : exports.DefaultEditingError;
49
+ if (hasValidDatasource(props.rendering)) {
50
+ return react_1.default.createElement(Component, Object.assign({}, props));
51
+ }
52
+ if (!props.page) {
53
+ console.warn('withDatasourceCheck: The `page` prop was not provided. Pass the `page` prop to components wrapped by `withDatasourceCheck`, especially when using Server Components.');
54
+ return (react_1.default.createElement(DatasourceCheckFallback_1.DatasourceCheckFallback, { editingError: react_1.default.createElement(EditingError, null) },
55
+ react_1.default.createElement(Component, Object.assign({}, props))));
56
+ }
57
+ const isDesignLibrary = (_b = props.page.mode) === null || _b === void 0 ? void 0 : _b.isDesignLibrary;
58
+ const isEditing = (_c = props.page.mode) === null || _c === void 0 ? void 0 : _c.isEditing;
47
59
  // If the component is rendered in DesignLibrary, we don't need to check for datasource
48
- const isDesignLibrary = page.mode.isDesignLibrary;
49
- return isDesignLibrary || hasValidDatasource(props.rendering) ? (react_1.default.createElement(Component, Object.assign({}, props))) : page.mode.isEditing ? (react_1.default.createElement(EditingError, null)) : null;
60
+ if (isDesignLibrary) {
61
+ return react_1.default.createElement(Component, Object.assign({}, props));
62
+ }
63
+ return isEditing ? react_1.default.createElement(EditingError, null) : null;
50
64
  };
51
65
  };
52
66
  }
@@ -0,0 +1,20 @@
1
+ 'use client';
2
+ import React from 'react';
3
+ import { useSitecore } from '../components/SitecoreProvider';
4
+ /**
5
+ * Resolves the missing-datasource outcome from `SitecoreProvider` context when `page` is not available in props.
6
+ * Accepts only React elements so it can be rendered from a Server Component.
7
+ * @deprecated Temporary fallback for callers that omit `page`. Remove this component in version 3.0.0
8
+ * and make the `page` prop required on `withDatasourceCheck`.
9
+ * @param {DatasourceCheckFallbackProps} props component props
10
+ * @returns {JSX.Element | null} the wrapped component in Design Library, the editing error in editing mode, otherwise null
11
+ */
12
+ export const DatasourceCheckFallback = ({ editingError, children, }) => {
13
+ var _a, _b;
14
+ const { page } = useSitecore();
15
+ const isDesignLibrary = (_a = page === null || page === void 0 ? void 0 : page.mode) === null || _a === void 0 ? void 0 : _a.isDesignLibrary;
16
+ if (isDesignLibrary) {
17
+ return React.createElement(React.Fragment, null, children);
18
+ }
19
+ return ((_b = page === null || page === void 0 ? void 0 : page.mode) === null || _b === void 0 ? void 0 : _b.isEditing) ? React.createElement(React.Fragment, null, editingError) : null;
20
+ };
@@ -1,6 +1,5 @@
1
- 'use client';
2
1
  import React from 'react';
3
- import { useSitecore } from './../components/SitecoreProvider';
2
+ import { DatasourceCheckFallback } from './DatasourceCheckFallback';
4
3
  export const DefaultEditingError = () => (React.createElement("div", { className: "sc-jss-editing-error", role: "alert" }, "Datasource is required. Please choose a content item for this component."));
5
4
  /**
6
5
  * Returns true when the rendering has a datasource and Layout Service did not report unresolved content.
@@ -16,6 +15,10 @@ function hasValidDatasource(rendering) {
16
15
  /**
17
16
  * Checks whether a Sitecore datasource is present and valid, then renders appropriately depending on page mode (normal vs editing).
18
17
  * `isContentResolved: false` is treated the same as a missing datasource. If the property is omitted, the original presence check is used.
18
+ *
19
+ * Works in both Server and Client Components. Apply it in the same module as the wrapped component.
20
+ * When `page` is not passed (e.g. outside a `Placeholder`), the page mode is resolved on the client
21
+ * from `SitecoreProvider` context. In Server Components, pass `page` to keep the check on the server.
19
22
  * @param {WithDatasourceCheckOptions} [options]
20
23
  * @returns
21
24
  * The wrapped component, if a datasource is present and valid.
@@ -32,13 +35,24 @@ function hasValidDatasource(rendering) {
32
35
  */
33
36
  export function withDatasourceCheck(options) {
34
37
  return function withDatasourceCheckHoc(Component) {
35
- return (props) => {
36
- var _a;
37
- const { page } = useSitecore();
38
+ return function WithDatasourceCheck(props) {
39
+ var _a, _b, _c;
38
40
  const EditingError = (_a = options === null || options === void 0 ? void 0 : options.editingErrorComponent) !== null && _a !== void 0 ? _a : DefaultEditingError;
41
+ if (hasValidDatasource(props.rendering)) {
42
+ return React.createElement(Component, Object.assign({}, props));
43
+ }
44
+ if (!props.page) {
45
+ console.warn('withDatasourceCheck: The `page` prop was not provided. Pass the `page` prop to components wrapped by `withDatasourceCheck`, especially when using Server Components.');
46
+ return (React.createElement(DatasourceCheckFallback, { editingError: React.createElement(EditingError, null) },
47
+ React.createElement(Component, Object.assign({}, props))));
48
+ }
49
+ const isDesignLibrary = (_b = props.page.mode) === null || _b === void 0 ? void 0 : _b.isDesignLibrary;
50
+ const isEditing = (_c = props.page.mode) === null || _c === void 0 ? void 0 : _c.isEditing;
39
51
  // If the component is rendered in DesignLibrary, we don't need to check for datasource
40
- const isDesignLibrary = page.mode.isDesignLibrary;
41
- return isDesignLibrary || hasValidDatasource(props.rendering) ? (React.createElement(Component, Object.assign({}, props))) : page.mode.isEditing ? (React.createElement(EditingError, null)) : null;
52
+ if (isDesignLibrary) {
53
+ return React.createElement(Component, Object.assign({}, props));
54
+ }
55
+ return isEditing ? React.createElement(EditingError, null) : null;
42
56
  };
43
57
  };
44
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-content-sdk/react",
3
- "version": "2.5.0",
3
+ "version": "2.5.1-canary.20261102062040",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -0,0 +1,21 @@
1
+ import React, { JSX } from 'react';
2
+ export interface DatasourceCheckFallbackProps {
3
+ /**
4
+ * Element rendered in editing mode.
5
+ */
6
+ editingError: React.ReactNode;
7
+ /**
8
+ * Wrapped component element, rendered in Design Library mode.
9
+ */
10
+ children: React.ReactNode;
11
+ }
12
+ /**
13
+ * Resolves the missing-datasource outcome from `SitecoreProvider` context when `page` is not available in props.
14
+ * Accepts only React elements so it can be rendered from a Server Component.
15
+ * @deprecated Temporary fallback for callers that omit `page`. Remove this component in version 3.0.0
16
+ * and make the `page` prop required on `withDatasourceCheck`.
17
+ * @param {DatasourceCheckFallbackProps} props component props
18
+ * @returns {JSX.Element | null} the wrapped component in Design Library, the editing error in editing mode, otherwise null
19
+ */
20
+ export declare const DatasourceCheckFallback: ({ editingError, children, }: DatasourceCheckFallbackProps) => JSX.Element | null;
21
+ //# sourceMappingURL=DatasourceCheckFallback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DatasourceCheckFallback.d.ts","sourceRoot":"","sources":["../../src/enhancers/DatasourceCheckFallback.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAGnC,MAAM,WAAW,4BAA4B;IAC3C;;OAEG;IACH,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC;IAC9B;;OAEG;IACH,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,uBAAuB,GAAI,6BAGrC,4BAA4B,KAAG,GAAG,CAAC,OAAO,GAAG,IAS/C,CAAC"}
@@ -1,8 +1,15 @@
1
1
  import React, { JSX } from 'react';
2
+ import { Page } from '@sitecore-content-sdk/content/client';
2
3
  import { ComponentRendering } from '@sitecore-content-sdk/content/layout';
3
4
  export declare const DefaultEditingError: () => JSX.Element;
4
5
  export interface WithDatasourceCheckProps {
5
6
  rendering: ComponentRendering;
7
+ /**
8
+ * Page data, supplied by `Placeholder`. Used to detect editing and Design Library modes.
9
+ * When omitted, the page mode is read from `SitecoreProvider` context on the client.
10
+ * This prop will be required in version 3.0.0, when `DatasourceCheckFallback` is removed.
11
+ */
12
+ page?: Page;
6
13
  }
7
14
  export interface WithDatasourceCheckOptions {
8
15
  /**
@@ -14,6 +21,10 @@ export interface WithDatasourceCheckOptions {
14
21
  /**
15
22
  * Checks whether a Sitecore datasource is present and valid, then renders appropriately depending on page mode (normal vs editing).
16
23
  * `isContentResolved: false` is treated the same as a missing datasource. If the property is omitted, the original presence check is used.
24
+ *
25
+ * Works in both Server and Client Components. Apply it in the same module as the wrapped component.
26
+ * When `page` is not passed (e.g. outside a `Placeholder`), the page mode is resolved on the client
27
+ * from `SitecoreProvider` context. In Server Components, pass `page` to keep the check on the server.
17
28
  * @param {WithDatasourceCheckOptions} [options]
18
29
  * @returns
19
30
  * The wrapped component, if a datasource is present and valid.
@@ -1 +1 @@
1
- {"version":3,"file":"withDatasourceCheck.d.ts","sourceRoot":"","sources":["../../src/enhancers/withDatasourceCheck.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AAG1E,eAAO,MAAM,mBAAmB,QAAO,GAAG,CAAC,OA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