@sitecore-content-sdk/react 0.2.0 → 0.3.0-canary.2

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.
Files changed (34) hide show
  1. package/dist/cjs/components/EditingScripts.js +2 -2
  2. package/dist/cjs/components/ErrorBoundary.js +4 -4
  3. package/dist/cjs/components/Form.js +3 -3
  4. package/dist/cjs/components/Placeholder.js +4 -6
  5. package/dist/cjs/components/PlaceholderCommon.js +2 -2
  6. package/dist/cjs/components/{SitecoreContext.js → SitecoreProvider.js} +13 -13
  7. package/dist/cjs/enhancers/withComponentMap.js +2 -2
  8. package/dist/cjs/enhancers/withDatasourceCheck.js +4 -4
  9. package/dist/cjs/enhancers/withPlaceholder.js +4 -4
  10. package/dist/cjs/enhancers/withSitecore.js +48 -0
  11. package/dist/cjs/index.js +7 -7
  12. package/dist/esm/components/EditingScripts.js +2 -2
  13. package/dist/esm/components/ErrorBoundary.js +4 -4
  14. package/dist/esm/components/Form.js +3 -3
  15. package/dist/esm/components/Placeholder.js +4 -6
  16. package/dist/esm/components/PlaceholderCommon.js +2 -2
  17. package/dist/esm/components/{SitecoreContext.js → SitecoreProvider.js} +11 -11
  18. package/dist/esm/enhancers/withComponentMap.js +1 -1
  19. package/dist/esm/enhancers/withDatasourceCheck.js +4 -4
  20. package/dist/esm/enhancers/withPlaceholder.js +4 -4
  21. package/dist/esm/enhancers/withSitecore.js +41 -0
  22. package/dist/esm/index.js +2 -2
  23. package/package.json +3 -3
  24. package/types/components/ErrorBoundary.d.ts +3 -3
  25. package/types/components/Placeholder.d.ts +1 -1
  26. package/types/components/PlaceholderCommon.d.ts +5 -4
  27. package/types/components/SitecoreProvider.d.ts +57 -0
  28. package/types/enhancers/withPlaceholder.d.ts +1 -1
  29. package/types/enhancers/withSitecore.d.ts +51 -0
  30. package/types/index.d.ts +2 -2
  31. package/dist/cjs/enhancers/withSitecoreContext.js +0 -48
  32. package/dist/esm/enhancers/withSitecoreContext.js +0 -41
  33. package/types/components/SitecoreContext.d.ts +0 -34
  34. package/types/enhancers/withSitecoreContext.d.ts +0 -37
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.EditingScripts = void 0;
7
7
  const react_1 = __importDefault(require("react"));
8
8
  const layout_1 = require("@sitecore-content-sdk/core/layout");
9
- const withSitecoreContext_1 = require("../enhancers/withSitecoreContext");
9
+ const withSitecore_1 = require("../enhancers/withSitecore");
10
10
  const editing_1 = require("@sitecore-content-sdk/core/editing");
11
11
  const editing_2 = require("@sitecore-content-sdk/core/editing");
12
12
  /**
@@ -17,7 +17,7 @@ const editing_2 = require("@sitecore-content-sdk/core/editing");
17
17
  * @returns A JSX element containing the editing scripts or an empty fragment if not in editing/preview mode.
18
18
  */
19
19
  const EditingScripts = (props) => {
20
- const { sitecoreContext: { pageState, clientData, clientScripts, renderingType }, } = (0, withSitecoreContext_1.useSitecoreContext)();
20
+ const { pageContext: { pageState, clientData, clientScripts, renderingType }, } = (0, withSitecore_1.useSitecore)();
21
21
  // Don't render anything if not in editing/preview mode and rendering type is not component
22
22
  if (renderingType !== layout_1.RenderingType.Component &&
23
23
  (pageState === layout_1.LayoutServicePageState.Normal ||
@@ -35,7 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  const react_1 = __importStar(require("react"));
37
37
  const layout_1 = require("@sitecore-content-sdk/core/layout");
38
- const withSitecoreContext_1 = require("../enhancers/withSitecoreContext");
38
+ const withSitecore_1 = require("../enhancers/withSitecore");
39
39
  class ErrorBoundary extends react_1.default.Component {
40
40
  constructor(props) {
41
41
  super(props);
@@ -59,8 +59,8 @@ class ErrorBoundary extends react_1.default.Component {
59
59
  showErrorDetails() {
60
60
  var _a, _b;
61
61
  return (this.isInDevMode() ||
62
- ((_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageState) === layout_1.LayoutServicePageState.Edit ||
63
- ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageState) === layout_1.LayoutServicePageState.Preview);
62
+ ((_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageState) === layout_1.LayoutServicePageState.Edit ||
63
+ ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageState) === layout_1.LayoutServicePageState.Preview);
64
64
  }
65
65
  render() {
66
66
  var _a;
@@ -92,4 +92,4 @@ class ErrorBoundary extends react_1.default.Component {
92
92
  return (react_1.default.createElement(react_1.Suspense, { fallback: react_1.default.createElement("h4", null, this.props.componentLoadingMessage || this.defaultLoadingMessage) }, this.props.children));
93
93
  }
94
94
  }
95
- exports.default = (0, withSitecoreContext_1.withSitecoreContext)()(ErrorBoundary);
95
+ exports.default = (0, withSitecore_1.withSitecore)()(ErrorBoundary);
@@ -36,7 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Form = exports.mockFormModule = void 0;
37
37
  const react_1 = __importStar(require("react"));
38
38
  const core_1 = require("@sitecore-content-sdk/core");
39
- const withSitecoreContext_1 = require("../enhancers/withSitecoreContext");
39
+ const withSitecore_1 = require("../enhancers/withSitecore");
40
40
  let { executeScriptElements, loadForm, subscribeToFormSubmitEvent } = core_1.form;
41
41
  /**
42
42
  * Mock function to replace the form module functions for `testing` purposes.
@@ -53,9 +53,9 @@ const Form = ({ params, rendering }) => {
53
53
  const id = params === null || params === void 0 ? void 0 : params.RenderingIdentifier;
54
54
  const [error, setError] = (0, react_1.useState)(false);
55
55
  const [content, setContent] = (0, react_1.useState)('');
56
- const context = (0, withSitecoreContext_1.useSitecoreContext)();
56
+ const context = (0, withSitecore_1.useSitecore)();
57
57
  const formRef = (0, react_1.useRef)(null);
58
- const isEditing = context.sitecoreContext.pageEditing;
58
+ const isEditing = context.pageContext.pageEditing;
59
59
  (0, react_1.useEffect)(() => {
60
60
  var _a, _b, _c, _d;
61
61
  if (!content) {
@@ -8,7 +8,7 @@ const react_1 = __importDefault(require("react"));
8
8
  const PlaceholderCommon_1 = require("./PlaceholderCommon");
9
9
  const withComponentMap_1 = require("../enhancers/withComponentMap");
10
10
  const editing_1 = require("@sitecore-content-sdk/core/editing");
11
- const withSitecoreContext_1 = require("../enhancers/withSitecoreContext");
11
+ const withSitecore_1 = require("../enhancers/withSitecore");
12
12
  class PlaceholderComponent extends PlaceholderCommon_1.PlaceholderCommon {
13
13
  constructor(props) {
14
14
  super(props);
@@ -41,14 +41,12 @@ class PlaceholderComponent extends PlaceholderCommon_1.PlaceholderCommon {
41
41
  "."));
42
42
  }
43
43
  const renderingData = childProps.rendering;
44
- const placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name, (_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageEditing);
44
+ const placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name, (_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageEditing);
45
45
  this.isEmpty = !placeholderData.length;
46
46
  const components = this.getComponentsForRenderingData(placeholderData);
47
47
  if (this.isEmpty) {
48
48
  const rendered = this.props.renderEmpty ? this.props.renderEmpty(components) : components;
49
- return ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageEditing)
50
- ? this.renderEmptyPlaceholder(rendered)
51
- : rendered;
49
+ return ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageEditing) ? this.renderEmptyPlaceholder(rendered) : rendered;
52
50
  }
53
51
  else if (this.props.render) {
54
52
  return this.props.render(components, placeholderData, childProps);
@@ -68,4 +66,4 @@ class PlaceholderComponent extends PlaceholderCommon_1.PlaceholderCommon {
68
66
  }
69
67
  }
70
68
  const PlaceholderWithComponentMap = (0, withComponentMap_1.withComponentMap)(PlaceholderComponent);
71
- exports.Placeholder = (0, withSitecoreContext_1.withSitecoreContext)()(PlaceholderWithComponentMap);
69
+ exports.Placeholder = (0, withSitecore_1.withSitecore)()(PlaceholderWithComponentMap);
@@ -136,13 +136,13 @@ class PlaceholderCommon extends react_1.default.Component {
136
136
  rendered = (react_1.default.createElement(ErrorBoundary_1.default, Object.assign({ key: rendered.type + '-' + index, errorComponent: this.props.errorComponent, componentLoadingMessage: this.props.componentLoadingMessage, type: type, isDynamic: ((_a = component.render) === null || _a === void 0 ? void 0 : _a.preload) ? true : false }, rendered.props), rendered));
137
137
  }
138
138
  // if in edit mode then emit shallow chromes for hydration in Pages
139
- if ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageEditing) {
139
+ if ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageEditing) {
140
140
  return (react_1.default.createElement(PlaceholderMetadata_1.PlaceholderMetadata, { key: key, rendering: rendering }, rendered));
141
141
  }
142
142
  return rendered;
143
143
  })
144
144
  .filter((element) => element); // remove nulls
145
- if ((_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageEditing) {
145
+ if ((_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageEditing) {
146
146
  return [
147
147
  react_1.default.createElement(PlaceholderMetadata_1.PlaceholderMetadata, { key: this.props.rendering.uid, placeholderName: name, rendering: this.props.rendering }, transformedComponents),
148
148
  ];
@@ -3,36 +3,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SitecoreContext = exports.ComponentMapReactContext = exports.SitecoreContextReactContext = void 0;
6
+ exports.SitecoreProvider = exports.ComponentMapReactContext = exports.SitecoreProviderReactContext = void 0;
7
7
  /* eslint-disable @typescript-eslint/no-explicit-any */
8
8
  const react_1 = __importDefault(require("react"));
9
9
  const react_2 = __importDefault(require("fast-deep-equal/es6/react"));
10
10
  const core_1 = require("@sitecore-content-sdk/core");
11
- exports.SitecoreContextReactContext = react_1.default.createContext({});
11
+ exports.SitecoreProviderReactContext = react_1.default.createContext({});
12
12
  exports.ComponentMapReactContext = react_1.default.createContext(new Map());
13
- class SitecoreContext extends react_1.default.Component {
13
+ class SitecoreProvider extends react_1.default.Component {
14
14
  constructor(props) {
15
15
  var _a, _b, _c, _d;
16
16
  super(props);
17
17
  /**
18
18
  * Update context state. Value can be @type {LayoutServiceData} which will be automatically transformed
19
- * or you can provide exact @type {SitecoreContextValue}
20
- * @param {SitecoreContextValue | LayoutServiceData} value New context value
19
+ * or you can provide exact @type {SitecoreProviderPageContext}
20
+ * @param {SitecoreProviderPageContext | LayoutServiceData} value New context value
21
21
  */
22
22
  this.setContext = (value) => {
23
23
  this.setState({
24
- context: value.sitecore
24
+ pageContext: value.sitecore
25
25
  ? this.constructContext(value)
26
26
  : Object.assign({}, value),
27
27
  });
28
28
  };
29
- const context = this.constructContext(props.layoutData);
29
+ const pageContext = this.constructContext(props.layoutData);
30
30
  let api = props.api;
31
31
  if (((_b = (_a = props.api) === null || _a === void 0 ? void 0 : _a.edge) === null || _b === void 0 ? void 0 : _b.contextId) && !((_d = (_c = props.api) === null || _c === void 0 ? void 0 : _c.edge) === null || _d === void 0 ? void 0 : _d.edgeUrl)) {
32
32
  api = Object.assign(Object.assign({}, props.api), { edge: Object.assign(Object.assign({}, props.api.edge), { edgeUrl: core_1.constants.SITECORE_EDGE_URL_DEFAULT }) });
33
33
  }
34
34
  this.state = {
35
- context,
35
+ pageContext,
36
36
  setContext: this.setContext,
37
37
  api,
38
38
  };
@@ -47,8 +47,8 @@ class SitecoreContext extends react_1.default.Component {
47
47
  return Object.assign({ route: layoutData.sitecore.route, itemId: (_a = layoutData.sitecore.route) === null || _a === void 0 ? void 0 : _a.itemId }, layoutData.sitecore.context);
48
48
  }
49
49
  componentDidUpdate(prevProps) {
50
- // In case if somebody will manage SitecoreContext state by passing fresh `layoutData` prop
51
- // instead of using `updateSitecoreContext`
50
+ // In case if somebody will manage SitecoreProvider state by passing fresh `layoutData` prop
51
+ // instead of using `updateContext`
52
52
  if (!(0, react_2.default)(prevProps.layoutData, this.props.layoutData)) {
53
53
  this.setContext(this.props.layoutData);
54
54
  return;
@@ -56,8 +56,8 @@ class SitecoreContext extends react_1.default.Component {
56
56
  }
57
57
  render() {
58
58
  return (react_1.default.createElement(exports.ComponentMapReactContext.Provider, { value: this.props.componentMap },
59
- react_1.default.createElement(exports.SitecoreContextReactContext.Provider, { value: this.state }, this.props.children)));
59
+ react_1.default.createElement(exports.SitecoreProviderReactContext.Provider, { value: this.state }, this.props.children)));
60
60
  }
61
61
  }
62
- exports.SitecoreContext = SitecoreContext;
63
- SitecoreContext.displayName = 'SitecoreContext';
62
+ exports.SitecoreProvider = SitecoreProvider;
63
+ SitecoreProvider.displayName = 'SitecoreProvider';
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.withComponentMap = withComponentMap;
7
7
  const react_1 = __importDefault(require("react"));
8
- const SitecoreContext_1 = require("../components/SitecoreContext");
8
+ const SitecoreProvider_1 = require("../components/SitecoreProvider");
9
9
  const react_2 = require("react");
10
10
  /**
11
11
  * @param {React.ComponentClass<T> | React.FC<T>} Component
@@ -16,7 +16,7 @@ function withComponentMap(Component) {
16
16
  * @returns {JSX.Element} - the rendered component
17
17
  */
18
18
  function WithComponentMap(props) {
19
- const context = (0, react_2.useContext)(SitecoreContext_1.ComponentMapReactContext);
19
+ const context = (0, react_2.useContext)(SitecoreProvider_1.ComponentMapReactContext);
20
20
  return react_1.default.createElement(Component, Object.assign({}, props, { componentMap: props.componentMap || context }));
21
21
  }
22
22
  WithComponentMap.displayName = `withComponentMap(${Component.displayName ||
@@ -7,7 +7,7 @@ exports.DefaultEditingError = void 0;
7
7
  exports.withDatasourceCheck = withDatasourceCheck;
8
8
  const react_1 = __importDefault(require("react"));
9
9
  const layout_1 = require("@sitecore-content-sdk/core/layout");
10
- const withSitecoreContext_1 = require("./withSitecoreContext");
10
+ const withSitecore_1 = require("./withSitecore");
11
11
  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
12
  exports.DefaultEditingError = DefaultEditingError;
13
13
  /**
@@ -21,11 +21,11 @@ function withDatasourceCheck(options) {
21
21
  return function withDatasourceCheckHoc(Component) {
22
22
  return function WithDatasourceCheck(props) {
23
23
  var _a, _b;
24
- const { sitecoreContext } = (0, withSitecoreContext_1.useSitecoreContext)();
24
+ const { pageContext } = (0, withSitecore_1.useSitecore)();
25
25
  const EditingError = (_a = options === null || options === void 0 ? void 0 : options.editingErrorComponent) !== null && _a !== void 0 ? _a : exports.DefaultEditingError;
26
26
  // If the component is rendered in DesignLibrary, we don't need to check for datasource
27
- const isDesignLibrary = (sitecoreContext === null || sitecoreContext === void 0 ? void 0 : sitecoreContext.renderingType) === layout_1.RenderingType.Component;
28
- return isDesignLibrary || ((_b = props.rendering) === null || _b === void 0 ? void 0 : _b.dataSource) ? (react_1.default.createElement(Component, Object.assign({}, props))) : sitecoreContext.pageEditing ? (react_1.default.createElement(EditingError, null)) : null;
27
+ const isDesignLibrary = (pageContext === null || pageContext === void 0 ? void 0 : pageContext.renderingType) === layout_1.RenderingType.Component;
28
+ return isDesignLibrary || ((_b = props.rendering) === null || _b === void 0 ? void 0 : _b.dataSource) ? (react_1.default.createElement(Component, Object.assign({}, props))) : pageContext.pageEditing ? (react_1.default.createElement(EditingError, null)) : null;
29
29
  };
30
30
  };
31
31
  }
@@ -7,7 +7,7 @@ exports.withPlaceholder = withPlaceholder;
7
7
  const react_1 = __importDefault(require("react"));
8
8
  const PlaceholderCommon_1 = require("../components/PlaceholderCommon");
9
9
  const withComponentMap_1 = require("./withComponentMap");
10
- const withSitecoreContext_1 = require("./withSitecoreContext");
10
+ const withSitecore_1 = require("./withSitecore");
11
11
  /**
12
12
  * @param {WithPlaceholderSpec} placeholders
13
13
  * @param {WithPlaceholderOptions} [options]
@@ -42,13 +42,13 @@ function withPlaceholder(placeholders, options) {
42
42
  definitelyArrayPlacholders.forEach((placeholder) => {
43
43
  let placeholderData;
44
44
  if (typeof placeholder !== 'string' && placeholder.placeholder && placeholder.prop) {
45
- placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder.placeholder, childProps.sitecoreContext.pageEditing);
45
+ placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder.placeholder, childProps.pageContext.pageEditing);
46
46
  if (placeholderData) {
47
47
  childProps[placeholder.prop] = this.getComponentsForRenderingData(placeholderData);
48
48
  }
49
49
  }
50
50
  else {
51
- placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder, childProps.sitecoreContext.pageEditing);
51
+ placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder, childProps.pageContext.pageEditing);
52
52
  if (placeholderData) {
53
53
  childProps[placeholder] = this.getComponentsForRenderingData(placeholderData);
54
54
  }
@@ -57,6 +57,6 @@ function withPlaceholder(placeholders, options) {
57
57
  return react_1.default.createElement(WrappedComponent, Object.assign({}, childProps));
58
58
  }
59
59
  }
60
- return (0, withSitecoreContext_1.withSitecoreContext)()((0, withComponentMap_1.withComponentMap)(WithPlaceholder));
60
+ return (0, withSitecore_1.withSitecore)()((0, withComponentMap_1.withComponentMap)(WithPlaceholder));
61
61
  };
62
62
  }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.withSitecore = withSitecore;
7
+ exports.useSitecore = useSitecore;
8
+ const react_1 = __importDefault(require("react"));
9
+ const SitecoreProvider_1 = require("../components/SitecoreProvider");
10
+ /**
11
+ * @param {WithSitecoreProviderOptions} [options]
12
+ */
13
+ function withSitecore(options) {
14
+ return function withSitecoreProviderHoc(Component) {
15
+ return function WithSitecoreProvider(props) {
16
+ return (react_1.default.createElement(SitecoreProvider_1.SitecoreProviderReactContext.Consumer, null, (value) => (react_1.default.createElement(Component, Object.assign({}, props, { pageContext: value.pageContext, api: value.api, updateContext: options && options.updatable && value.setContext })))));
17
+ };
18
+ };
19
+ }
20
+ /**
21
+ * This hook grants acсess to the current Sitecore page context and api.
22
+ * by default JSS includes the following properties in this context:
23
+ * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
24
+ * - pageState - Like pageEditing, but a string: normal, preview or edit.
25
+ * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
26
+ * @param {WithSitecoreOptions} [options] hook options
27
+ * @example
28
+ * const EditMode = () => {
29
+ * const { pageContext } = useSitecore();
30
+ * return <span>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
31
+ * }
32
+ * @example
33
+ * const EditMode = () => {
34
+ * const { pageContext, updateContext } = useSitecore({ updatable: true });
35
+ * const onClick = () => updateContext({ pageEditing: true });
36
+ * return <span onClick={onClick}>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
37
+ * }
38
+ * @returns {object} { api, pageContext, updateContext }
39
+ */
40
+ function useSitecore(options) {
41
+ const reactContext = react_1.default.useContext(SitecoreProvider_1.SitecoreProviderReactContext);
42
+ const updatable = options === null || options === void 0 ? void 0 : options.updatable;
43
+ return {
44
+ api: reactContext.api,
45
+ pageContext: reactContext.pageContext,
46
+ updateContext: updatable ? reactContext.setContext : undefined,
47
+ };
48
+ }
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GraphQLSitePathService = exports.DefaultEmptyFieldEditingComponentImage = exports.DefaultEmptyFieldEditingComponentText = exports.EditingScripts = exports.withEmptyFieldEditingComponent = exports.withFieldMetadata = exports.withDatasourceCheck = exports.withPlaceholder = exports.withEditorChromes = exports.useSitecoreContext = exports.withSitecoreContext = exports.SitecoreContextReactContext = exports.SitecoreContext = exports.File = exports.Link = exports.BYOCWrapper = exports.fetchBYOCComponentServerProps = exports.BYOCComponent = exports.DesignLibrary = exports.FEaaSWrapper = exports.fetchFEaaSComponentServerProps = exports.FEaaSComponent = exports.DateField = exports.Text = exports.RichText = exports.Image = exports.Placeholder = exports.Form = exports.mediaApi = exports.GraphQLRequestClient = exports.DefaultRetryStrategy = exports.GraphQLDictionaryService = exports.EditMode = exports.getFieldValue = exports.getChildPlaceholder = exports.GraphQLLayoutService = exports.LayoutServicePageState = exports.getDesignLibraryStylesheetLinks = exports.getContentStylesheetLink = exports.resetEditorChromes = exports.isEditorActive = exports.NativeDataFetcher = exports.MemoryCacheClient = exports.ClientError = exports.enableDebug = exports.constants = void 0;
3
+ exports.GraphQLSitePathService = exports.DefaultEmptyFieldEditingComponentImage = exports.DefaultEmptyFieldEditingComponentText = exports.EditingScripts = exports.withEmptyFieldEditingComponent = exports.withFieldMetadata = exports.withDatasourceCheck = exports.withPlaceholder = exports.withEditorChromes = exports.useSitecore = exports.withSitecore = exports.SitecoreProviderReactContext = exports.SitecoreProvider = exports.File = exports.Link = exports.BYOCWrapper = exports.fetchBYOCComponentServerProps = exports.BYOCComponent = exports.DesignLibrary = exports.FEaaSWrapper = exports.fetchFEaaSComponentServerProps = exports.FEaaSComponent = exports.DateField = exports.Text = exports.RichText = exports.Image = exports.Placeholder = exports.Form = exports.mediaApi = exports.GraphQLRequestClient = exports.DefaultRetryStrategy = exports.GraphQLDictionaryService = exports.EditMode = exports.getFieldValue = exports.getChildPlaceholder = exports.GraphQLLayoutService = exports.LayoutServicePageState = exports.getDesignLibraryStylesheetLinks = exports.getContentStylesheetLink = exports.resetEditorChromes = exports.isEditorActive = exports.NativeDataFetcher = exports.MemoryCacheClient = exports.ClientError = exports.enableDebug = exports.constants = void 0;
4
4
  var core_1 = require("@sitecore-content-sdk/core");
5
5
  Object.defineProperty(exports, "constants", { enumerable: true, get: function () { return core_1.constants; } });
6
6
  Object.defineProperty(exports, "enableDebug", { enumerable: true, get: function () { return core_1.enableDebug; } });
@@ -53,12 +53,12 @@ var Link_1 = require("./components/Link");
53
53
  Object.defineProperty(exports, "Link", { enumerable: true, get: function () { return Link_1.Link; } });
54
54
  var File_1 = require("./components/File");
55
55
  Object.defineProperty(exports, "File", { enumerable: true, get: function () { return File_1.File; } });
56
- var SitecoreContext_1 = require("./components/SitecoreContext");
57
- Object.defineProperty(exports, "SitecoreContext", { enumerable: true, get: function () { return SitecoreContext_1.SitecoreContext; } });
58
- Object.defineProperty(exports, "SitecoreContextReactContext", { enumerable: true, get: function () { return SitecoreContext_1.SitecoreContextReactContext; } });
59
- var withSitecoreContext_1 = require("./enhancers/withSitecoreContext");
60
- Object.defineProperty(exports, "withSitecoreContext", { enumerable: true, get: function () { return withSitecoreContext_1.withSitecoreContext; } });
61
- Object.defineProperty(exports, "useSitecoreContext", { enumerable: true, get: function () { return withSitecoreContext_1.useSitecoreContext; } });
56
+ var SitecoreProvider_1 = require("./components/SitecoreProvider");
57
+ Object.defineProperty(exports, "SitecoreProvider", { enumerable: true, get: function () { return SitecoreProvider_1.SitecoreProvider; } });
58
+ Object.defineProperty(exports, "SitecoreProviderReactContext", { enumerable: true, get: function () { return SitecoreProvider_1.SitecoreProviderReactContext; } });
59
+ var withSitecore_1 = require("./enhancers/withSitecore");
60
+ Object.defineProperty(exports, "withSitecore", { enumerable: true, get: function () { return withSitecore_1.withSitecore; } });
61
+ Object.defineProperty(exports, "useSitecore", { enumerable: true, get: function () { return withSitecore_1.useSitecore; } });
62
62
  var withEditorChromes_1 = require("./enhancers/withEditorChromes");
63
63
  Object.defineProperty(exports, "withEditorChromes", { enumerable: true, get: function () { return withEditorChromes_1.withEditorChromes; } });
64
64
  var withPlaceholder_1 = require("./enhancers/withPlaceholder");
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { LayoutServicePageState, RenderingType } from '@sitecore-content-sdk/core/layout';
3
- import { useSitecoreContext } from '../enhancers/withSitecoreContext';
3
+ import { useSitecore } from '../enhancers/withSitecore';
4
4
  import { getJssPagesClientData } from '@sitecore-content-sdk/core/editing';
5
5
  import { getDesignLibraryScriptLink } from '@sitecore-content-sdk/core/editing';
6
6
  /**
@@ -11,7 +11,7 @@ import { getDesignLibraryScriptLink } from '@sitecore-content-sdk/core/editing';
11
11
  * @returns A JSX element containing the editing scripts or an empty fragment if not in editing/preview mode.
12
12
  */
13
13
  export const EditingScripts = (props) => {
14
- const { sitecoreContext: { pageState, clientData, clientScripts, renderingType }, } = useSitecoreContext();
14
+ const { pageContext: { pageState, clientData, clientScripts, renderingType }, } = useSitecore();
15
15
  // Don't render anything if not in editing/preview mode and rendering type is not component
16
16
  if (renderingType !== RenderingType.Component &&
17
17
  (pageState === LayoutServicePageState.Normal ||
@@ -1,6 +1,6 @@
1
1
  import React, { Suspense } from 'react';
2
2
  import { LayoutServicePageState } from '@sitecore-content-sdk/core/layout';
3
- import { withSitecoreContext } from '../enhancers/withSitecoreContext';
3
+ import { withSitecore } from '../enhancers/withSitecore';
4
4
  class ErrorBoundary extends React.Component {
5
5
  constructor(props) {
6
6
  super(props);
@@ -24,8 +24,8 @@ class ErrorBoundary extends React.Component {
24
24
  showErrorDetails() {
25
25
  var _a, _b;
26
26
  return (this.isInDevMode() ||
27
- ((_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageState) === LayoutServicePageState.Edit ||
28
- ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageState) === LayoutServicePageState.Preview);
27
+ ((_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageState) === LayoutServicePageState.Edit ||
28
+ ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageState) === LayoutServicePageState.Preview);
29
29
  }
30
30
  render() {
31
31
  var _a;
@@ -57,4 +57,4 @@ class ErrorBoundary extends React.Component {
57
57
  return (React.createElement(Suspense, { fallback: React.createElement("h4", null, this.props.componentLoadingMessage || this.defaultLoadingMessage) }, this.props.children));
58
58
  }
59
59
  }
60
- export default withSitecoreContext()(ErrorBoundary);
60
+ export default withSitecore()(ErrorBoundary);
@@ -1,6 +1,6 @@
1
1
  import React, { useEffect, useRef, useState } from 'react';
2
2
  import { form } from '@sitecore-content-sdk/core';
3
- import { useSitecoreContext } from '../enhancers/withSitecoreContext';
3
+ import { useSitecore } from '../enhancers/withSitecore';
4
4
  let { executeScriptElements, loadForm, subscribeToFormSubmitEvent } = form;
5
5
  /**
6
6
  * Mock function to replace the form module functions for `testing` purposes.
@@ -16,9 +16,9 @@ export const Form = ({ params, rendering }) => {
16
16
  const id = params === null || params === void 0 ? void 0 : params.RenderingIdentifier;
17
17
  const [error, setError] = useState(false);
18
18
  const [content, setContent] = useState('');
19
- const context = useSitecoreContext();
19
+ const context = useSitecore();
20
20
  const formRef = useRef(null);
21
- const isEditing = context.sitecoreContext.pageEditing;
21
+ const isEditing = context.pageContext.pageEditing;
22
22
  useEffect(() => {
23
23
  var _a, _b, _c, _d;
24
24
  if (!content) {
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { PlaceholderCommon } from './PlaceholderCommon';
3
3
  import { withComponentMap } from '../enhancers/withComponentMap';
4
4
  import { PagesEditor } from '@sitecore-content-sdk/core/editing';
5
- import { withSitecoreContext } from '../enhancers/withSitecoreContext';
5
+ import { withSitecore } from '../enhancers/withSitecore';
6
6
  class PlaceholderComponent extends PlaceholderCommon {
7
7
  constructor(props) {
8
8
  super(props);
@@ -35,14 +35,12 @@ class PlaceholderComponent extends PlaceholderCommon {
35
35
  "."));
36
36
  }
37
37
  const renderingData = childProps.rendering;
38
- const placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name, (_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageEditing);
38
+ const placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name, (_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageEditing);
39
39
  this.isEmpty = !placeholderData.length;
40
40
  const components = this.getComponentsForRenderingData(placeholderData);
41
41
  if (this.isEmpty) {
42
42
  const rendered = this.props.renderEmpty ? this.props.renderEmpty(components) : components;
43
- return ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageEditing)
44
- ? this.renderEmptyPlaceholder(rendered)
45
- : rendered;
43
+ return ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageEditing) ? this.renderEmptyPlaceholder(rendered) : rendered;
46
44
  }
47
45
  else if (this.props.render) {
48
46
  return this.props.render(components, placeholderData, childProps);
@@ -62,4 +60,4 @@ class PlaceholderComponent extends PlaceholderCommon {
62
60
  }
63
61
  }
64
62
  const PlaceholderWithComponentMap = withComponentMap(PlaceholderComponent);
65
- export const Placeholder = withSitecoreContext()(PlaceholderWithComponentMap);
63
+ export const Placeholder = withSitecore()(PlaceholderWithComponentMap);
@@ -130,13 +130,13 @@ export class PlaceholderCommon extends React.Component {
130
130
  rendered = (React.createElement(ErrorBoundary, Object.assign({ key: rendered.type + '-' + index, errorComponent: this.props.errorComponent, componentLoadingMessage: this.props.componentLoadingMessage, type: type, isDynamic: ((_a = component.render) === null || _a === void 0 ? void 0 : _a.preload) ? true : false }, rendered.props), rendered));
131
131
  }
132
132
  // if in edit mode then emit shallow chromes for hydration in Pages
133
- if ((_b = this.props.sitecoreContext) === null || _b === void 0 ? void 0 : _b.pageEditing) {
133
+ if ((_b = this.props.pageContext) === null || _b === void 0 ? void 0 : _b.pageEditing) {
134
134
  return (React.createElement(PlaceholderMetadata, { key: key, rendering: rendering }, rendered));
135
135
  }
136
136
  return rendered;
137
137
  })
138
138
  .filter((element) => element); // remove nulls
139
- if ((_a = this.props.sitecoreContext) === null || _a === void 0 ? void 0 : _a.pageEditing) {
139
+ if ((_a = this.props.pageContext) === null || _a === void 0 ? void 0 : _a.pageEditing) {
140
140
  return [
141
141
  React.createElement(PlaceholderMetadata, { key: this.props.rendering.uid, placeholderName: name, rendering: this.props.rendering }, transformedComponents),
142
142
  ];
@@ -2,31 +2,31 @@
2
2
  import React from 'react';
3
3
  import fastDeepEqual from 'fast-deep-equal/es6/react';
4
4
  import { constants } from '@sitecore-content-sdk/core';
5
- export const SitecoreContextReactContext = React.createContext({});
5
+ export const SitecoreProviderReactContext = React.createContext({});
6
6
  export const ComponentMapReactContext = React.createContext(new Map());
7
- export class SitecoreContext extends React.Component {
7
+ export class SitecoreProvider extends React.Component {
8
8
  constructor(props) {
9
9
  var _a, _b, _c, _d;
10
10
  super(props);
11
11
  /**
12
12
  * Update context state. Value can be @type {LayoutServiceData} which will be automatically transformed
13
- * or you can provide exact @type {SitecoreContextValue}
14
- * @param {SitecoreContextValue | LayoutServiceData} value New context value
13
+ * or you can provide exact @type {SitecoreProviderPageContext}
14
+ * @param {SitecoreProviderPageContext | LayoutServiceData} value New context value
15
15
  */
16
16
  this.setContext = (value) => {
17
17
  this.setState({
18
- context: value.sitecore
18
+ pageContext: value.sitecore
19
19
  ? this.constructContext(value)
20
20
  : Object.assign({}, value),
21
21
  });
22
22
  };
23
- const context = this.constructContext(props.layoutData);
23
+ const pageContext = this.constructContext(props.layoutData);
24
24
  let api = props.api;
25
25
  if (((_b = (_a = props.api) === null || _a === void 0 ? void 0 : _a.edge) === null || _b === void 0 ? void 0 : _b.contextId) && !((_d = (_c = props.api) === null || _c === void 0 ? void 0 : _c.edge) === null || _d === void 0 ? void 0 : _d.edgeUrl)) {
26
26
  api = Object.assign(Object.assign({}, props.api), { edge: Object.assign(Object.assign({}, props.api.edge), { edgeUrl: constants.SITECORE_EDGE_URL_DEFAULT }) });
27
27
  }
28
28
  this.state = {
29
- context,
29
+ pageContext,
30
30
  setContext: this.setContext,
31
31
  api,
32
32
  };
@@ -41,8 +41,8 @@ export class SitecoreContext extends React.Component {
41
41
  return Object.assign({ route: layoutData.sitecore.route, itemId: (_a = layoutData.sitecore.route) === null || _a === void 0 ? void 0 : _a.itemId }, layoutData.sitecore.context);
42
42
  }
43
43
  componentDidUpdate(prevProps) {
44
- // In case if somebody will manage SitecoreContext state by passing fresh `layoutData` prop
45
- // instead of using `updateSitecoreContext`
44
+ // In case if somebody will manage SitecoreProvider state by passing fresh `layoutData` prop
45
+ // instead of using `updateContext`
46
46
  if (!fastDeepEqual(prevProps.layoutData, this.props.layoutData)) {
47
47
  this.setContext(this.props.layoutData);
48
48
  return;
@@ -50,7 +50,7 @@ export class SitecoreContext extends React.Component {
50
50
  }
51
51
  render() {
52
52
  return (React.createElement(ComponentMapReactContext.Provider, { value: this.props.componentMap },
53
- React.createElement(SitecoreContextReactContext.Provider, { value: this.state }, this.props.children)));
53
+ React.createElement(SitecoreProviderReactContext.Provider, { value: this.state }, this.props.children)));
54
54
  }
55
55
  }
56
- SitecoreContext.displayName = 'SitecoreContext';
56
+ SitecoreProvider.displayName = 'SitecoreProvider';
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { ComponentMapReactContext } from '../components/SitecoreContext';
2
+ import { ComponentMapReactContext } from '../components/SitecoreProvider';
3
3
  import { useContext } from 'react';
4
4
  /**
5
5
  * @param {React.ComponentClass<T> | React.FC<T>} Component
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { RenderingType } from '@sitecore-content-sdk/core/layout';
3
- import { useSitecoreContext } from './withSitecoreContext';
3
+ import { useSitecore } from './withSitecore';
4
4
  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
5
  /**
6
6
  * Checks whether a Sitecore datasource is present and renders appropriately depending on page mode (normal vs editing).
@@ -13,11 +13,11 @@ export function withDatasourceCheck(options) {
13
13
  return function withDatasourceCheckHoc(Component) {
14
14
  return function WithDatasourceCheck(props) {
15
15
  var _a, _b;
16
- const { sitecoreContext } = useSitecoreContext();
16
+ const { pageContext } = useSitecore();
17
17
  const EditingError = (_a = options === null || options === void 0 ? void 0 : options.editingErrorComponent) !== null && _a !== void 0 ? _a : DefaultEditingError;
18
18
  // If the component is rendered in DesignLibrary, we don't need to check for datasource
19
- const isDesignLibrary = (sitecoreContext === null || sitecoreContext === void 0 ? void 0 : sitecoreContext.renderingType) === RenderingType.Component;
20
- return isDesignLibrary || ((_b = props.rendering) === null || _b === void 0 ? void 0 : _b.dataSource) ? (React.createElement(Component, Object.assign({}, props))) : sitecoreContext.pageEditing ? (React.createElement(EditingError, null)) : null;
19
+ const isDesignLibrary = (pageContext === null || pageContext === void 0 ? void 0 : pageContext.renderingType) === RenderingType.Component;
20
+ return isDesignLibrary || ((_b = props.rendering) === null || _b === void 0 ? void 0 : _b.dataSource) ? (React.createElement(Component, Object.assign({}, props))) : pageContext.pageEditing ? (React.createElement(EditingError, null)) : null;
21
21
  };
22
22
  };
23
23
  }
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { PlaceholderCommon } from '../components/PlaceholderCommon';
3
3
  import { withComponentMap } from './withComponentMap';
4
- import { withSitecoreContext } from './withSitecoreContext';
4
+ import { withSitecore } from './withSitecore';
5
5
  /**
6
6
  * @param {WithPlaceholderSpec} placeholders
7
7
  * @param {WithPlaceholderOptions} [options]
@@ -36,13 +36,13 @@ export function withPlaceholder(placeholders, options) {
36
36
  definitelyArrayPlacholders.forEach((placeholder) => {
37
37
  let placeholderData;
38
38
  if (typeof placeholder !== 'string' && placeholder.placeholder && placeholder.prop) {
39
- placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder.placeholder, childProps.sitecoreContext.pageEditing);
39
+ placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder.placeholder, childProps.pageContext.pageEditing);
40
40
  if (placeholderData) {
41
41
  childProps[placeholder.prop] = this.getComponentsForRenderingData(placeholderData);
42
42
  }
43
43
  }
44
44
  else {
45
- placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder, childProps.sitecoreContext.pageEditing);
45
+ placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, placeholder, childProps.pageContext.pageEditing);
46
46
  if (placeholderData) {
47
47
  childProps[placeholder] = this.getComponentsForRenderingData(placeholderData);
48
48
  }
@@ -51,6 +51,6 @@ export function withPlaceholder(placeholders, options) {
51
51
  return React.createElement(WrappedComponent, Object.assign({}, childProps));
52
52
  }
53
53
  }
54
- return withSitecoreContext()(withComponentMap(WithPlaceholder));
54
+ return withSitecore()(withComponentMap(WithPlaceholder));
55
55
  };
56
56
  }
@@ -0,0 +1,41 @@
1
+ import React from 'react';
2
+ import { SitecoreProviderReactContext, } from '../components/SitecoreProvider';
3
+ /**
4
+ * @param {WithSitecoreProviderOptions} [options]
5
+ */
6
+ export function withSitecore(options) {
7
+ return function withSitecoreProviderHoc(Component) {
8
+ return function WithSitecoreProvider(props) {
9
+ return (React.createElement(SitecoreProviderReactContext.Consumer, null, (value) => (React.createElement(Component, Object.assign({}, props, { pageContext: value.pageContext, api: value.api, updateContext: options && options.updatable && value.setContext })))));
10
+ };
11
+ };
12
+ }
13
+ /**
14
+ * This hook grants acсess to the current Sitecore page context and api.
15
+ * by default JSS includes the following properties in this context:
16
+ * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
17
+ * - pageState - Like pageEditing, but a string: normal, preview or edit.
18
+ * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
19
+ * @param {WithSitecoreOptions} [options] hook options
20
+ * @example
21
+ * const EditMode = () => {
22
+ * const { pageContext } = useSitecore();
23
+ * return <span>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
24
+ * }
25
+ * @example
26
+ * const EditMode = () => {
27
+ * const { pageContext, updateContext } = useSitecore({ updatable: true });
28
+ * const onClick = () => updateContext({ pageEditing: true });
29
+ * return <span onClick={onClick}>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
30
+ * }
31
+ * @returns {object} { api, pageContext, updateContext }
32
+ */
33
+ export function useSitecore(options) {
34
+ const reactContext = React.useContext(SitecoreProviderReactContext);
35
+ const updatable = options === null || options === void 0 ? void 0 : options.updatable;
36
+ return {
37
+ api: reactContext.api,
38
+ pageContext: reactContext.pageContext,
39
+ updateContext: updatable ? reactContext.setContext : undefined,
40
+ };
41
+ }
package/dist/esm/index.js CHANGED
@@ -17,8 +17,8 @@ export { BYOCComponent, fetchBYOCComponentServerProps, } from './components/BYOC
17
17
  export { BYOCWrapper } from './components/BYOCWrapper';
18
18
  export { Link } from './components/Link';
19
19
  export { File } from './components/File';
20
- export { SitecoreContext, SitecoreContextReactContext, } from './components/SitecoreContext';
21
- export { withSitecoreContext, useSitecoreContext, } from './enhancers/withSitecoreContext';
20
+ export { SitecoreProvider, SitecoreProviderReactContext, } from './components/SitecoreProvider';
21
+ export { withSitecore, useSitecore, } from './enhancers/withSitecore';
22
22
  export { withEditorChromes } from './enhancers/withEditorChromes';
23
23
  export { withPlaceholder } from './enhancers/withPlaceholder';
24
24
  export { withDatasourceCheck } from './enhancers/withDatasourceCheck';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-content-sdk/react",
3
- "version": "0.2.0",
3
+ "version": "0.3.0-canary.2",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -63,12 +63,12 @@
63
63
  "react-dom": "^19.1.0"
64
64
  },
65
65
  "dependencies": {
66
- "@sitecore-content-sdk/core": "0.2.0",
66
+ "@sitecore-content-sdk/core": "0.3.0-canary.2",
67
67
  "fast-deep-equal": "^3.1.3"
68
68
  },
69
69
  "description": "",
70
70
  "types": "types/index.d.ts",
71
- "gitHead": "0ea2d0d026c3be308ae018629020aa00442d6b42",
71
+ "gitHead": "8243c78fad6aa9456ac8cb726ac0a90c72589506",
72
72
  "files": [
73
73
  "dist",
74
74
  "types"
@@ -1,17 +1,17 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  import { ComponentRendering } from '@sitecore-content-sdk/core/layout';
3
- import { SitecoreContextValue } from './SitecoreContext';
3
+ import { SitecoreProviderPageContext } from './SitecoreProvider';
4
4
  type ErrorComponentProps = {
5
5
  [prop: string]: unknown;
6
6
  };
7
7
  export type ErrorBoundaryProps = {
8
8
  children: ReactNode;
9
- sitecoreContext: SitecoreContextValue;
9
+ pageContext: SitecoreProviderPageContext;
10
10
  type: string;
11
11
  isDynamic?: boolean;
12
12
  errorComponent?: React.ComponentClass<ErrorComponentProps> | React.FC<ErrorComponentProps>;
13
13
  rendering?: ComponentRendering;
14
14
  componentLoadingMessage?: string;
15
15
  };
16
- declare const _default: (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<ErrorBoundaryProps, keyof import("../enhancers/withSitecoreContext").WithSitecoreContextProps>) => React.JSX.Element;
16
+ declare const _default: (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<ErrorBoundaryProps, keyof import("../enhancers/withSitecore").WithSitecoreProps>) => React.JSX.Element;
17
17
  export default _default;
@@ -17,4 +17,4 @@ export interface PlaceholderComponentProps extends PlaceholderProps {
17
17
  */
18
18
  renderEach?: (component: React.ReactNode, index: number) => React.ReactNode;
19
19
  }
20
- export declare const Placeholder: (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<PlaceholderComponentProps, keyof import("../enhancers/withSitecoreContext").WithSitecoreContextProps>) => React.JSX.Element;
20
+ export declare const Placeholder: (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<PlaceholderComponentProps, keyof import("../enhancers/withSitecore").WithSitecoreProps>) => React.JSX.Element;
@@ -1,7 +1,7 @@
1
1
  import React, { ComponentType } from 'react';
2
2
  import { ComponentMap } from './sharedTypes';
3
3
  import { ComponentRendering, RouteData, Field, Item } from '@sitecore-content-sdk/core/layout';
4
- import { SitecoreContextValue } from './SitecoreContext';
4
+ import { SitecoreProviderPageContext } from './SitecoreProvider';
5
5
  type ErrorComponentProps = {
6
6
  [prop: string]: unknown;
7
7
  };
@@ -18,7 +18,7 @@ export interface PlaceholderProps {
18
18
  rendering: ComponentRendering | RouteData;
19
19
  /**
20
20
  * Component Map will be used to map Sitecore component names to app implementation
21
- * When rendered within a <SitecoreContext> component, defaults to the context componentMap.
21
+ * When rendered within a <SitecoreProvider> component, defaults to the context componentMap.
22
22
  */
23
23
  componentMap?: ComponentMap;
24
24
  /**
@@ -57,9 +57,10 @@ export interface PlaceholderProps {
57
57
  */
58
58
  errorComponent?: React.ComponentClass<ErrorComponentProps> | React.FC<ErrorComponentProps>;
59
59
  /**
60
- * Context data from the Sitecore Layout Service
60
+ * Page context data.
61
+ * This data is passed by the SitecoreProvider.
61
62
  */
62
- sitecoreContext: SitecoreContextValue;
63
+ pageContext: SitecoreProviderPageContext;
63
64
  /**
64
65
  * The message that gets displayed while component is loading
65
66
  */
@@ -0,0 +1,57 @@
1
+ import React from 'react';
2
+ import { SitecoreConfig } from '@sitecore-content-sdk/core/config';
3
+ import { LayoutServiceContext, LayoutServiceData, RouteData } from '../index';
4
+ import { ComponentMap } from './sharedTypes';
5
+ export interface SitecoreProviderProps {
6
+ /**
7
+ * The API configuration defined in the `SitecoreConfig`.
8
+ */
9
+ api: SitecoreConfig['api'];
10
+ /**
11
+ * The component map to use for rendering components.
12
+ */
13
+ componentMap: ComponentMap;
14
+ /**
15
+ * The Sitecore Layout data.
16
+ */
17
+ layoutData?: LayoutServiceData;
18
+ children: React.ReactNode;
19
+ }
20
+ export interface SitecoreProviderState {
21
+ /**
22
+ * Method to set the page context.
23
+ * @param {SitecoreProviderPageContext | LayoutServiceData} value New page context value.
24
+ * @returns {void}
25
+ */
26
+ setContext: (value: SitecoreProviderPageContext | LayoutServiceData) => void;
27
+ /**
28
+ * The current page context.
29
+ */
30
+ pageContext: SitecoreProviderPageContext;
31
+ /**
32
+ * The API configuration defined in the `SitecoreConfig`.
33
+ */
34
+ api?: SitecoreProviderProps['api'];
35
+ }
36
+ export declare const SitecoreProviderReactContext: React.Context<SitecoreProviderState>;
37
+ export declare const ComponentMapReactContext: React.Context<ComponentMap>;
38
+ /**
39
+ * The page context provided by the SitecoreProvider.
40
+ */
41
+ export type SitecoreProviderPageContext = LayoutServiceContext & {
42
+ itemId?: string;
43
+ route?: RouteData;
44
+ };
45
+ export declare class SitecoreProvider extends React.Component<SitecoreProviderProps, SitecoreProviderState> {
46
+ static displayName: string;
47
+ constructor(props: SitecoreProviderProps);
48
+ constructContext(layoutData?: LayoutServiceData): SitecoreProviderPageContext;
49
+ componentDidUpdate(prevProps: SitecoreProviderProps): void;
50
+ /**
51
+ * Update context state. Value can be @type {LayoutServiceData} which will be automatically transformed
52
+ * or you can provide exact @type {SitecoreProviderPageContext}
53
+ * @param {SitecoreProviderPageContext | LayoutServiceData} value New context value
54
+ */
55
+ setContext: (value: SitecoreProviderPageContext | LayoutServiceData) => void;
56
+ render(): React.JSX.Element;
57
+ }
@@ -31,4 +31,4 @@ export type WithPlaceholderSpec = (string | PlaceholderToPropMapping) | (string
31
31
  * @param {WithPlaceholderSpec} placeholders
32
32
  * @param {WithPlaceholderOptions} [options]
33
33
  */
34
- export declare function withPlaceholder(placeholders: WithPlaceholderSpec, options?: WithPlaceholderOptions): (WrappedComponent: React.ComponentClass<PlaceholderProps> | React.FunctionComponent<PlaceholderProps>) => (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<PlaceholderProps, keyof import("./withSitecoreContext").WithSitecoreContextProps>) => React.JSX.Element;
34
+ export declare function withPlaceholder(placeholders: WithPlaceholderSpec, options?: WithPlaceholderOptions): (WrappedComponent: React.ComponentClass<PlaceholderProps> | React.FunctionComponent<PlaceholderProps>) => (props: import("@sitecore-content-sdk/core/utils").EnhancedOmit<PlaceholderProps, keyof import("./withSitecore").WithSitecoreProps>) => React.JSX.Element;
@@ -0,0 +1,51 @@
1
+ import React from 'react';
2
+ import { EnhancedOmit } from '@sitecore-content-sdk/core/utils';
3
+ import { SitecoreProviderState, SitecoreProviderPageContext } from '../components/SitecoreProvider';
4
+ export interface WithSitecoreOptions {
5
+ /**
6
+ * If set to true, the `updateContext` method will be injected into the component props.
7
+ */
8
+ updatable?: boolean;
9
+ }
10
+ export interface WithSitecoreProps {
11
+ /**
12
+ * The current page context.
13
+ */
14
+ pageContext: SitecoreProviderPageContext;
15
+ /**
16
+ * The API configuration defined in the `SitecoreConfig`.
17
+ */
18
+ api?: SitecoreProviderState['api'];
19
+ /**
20
+ * Method to update the page context. This is only available if `updatable` is set to true.
21
+ * @param {SitecoreProviderPageContext} value New page context value.
22
+ * @returns {void}
23
+ */
24
+ updateContext?: ((value: SitecoreProviderPageContext) => void) | false;
25
+ }
26
+ export type WithSitecoreHocProps<ComponentProps> = EnhancedOmit<ComponentProps, keyof WithSitecoreProps>;
27
+ /**
28
+ * @param {WithSitecoreProviderOptions} [options]
29
+ */
30
+ export declare function withSitecore(options?: WithSitecoreOptions): <ComponentProps extends WithSitecoreProps>(Component: React.ComponentType<ComponentProps>) => (props: WithSitecoreHocProps<ComponentProps>) => React.JSX.Element;
31
+ /**
32
+ * This hook grants acсess to the current Sitecore page context and api.
33
+ * by default JSS includes the following properties in this context:
34
+ * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
35
+ * - pageState - Like pageEditing, but a string: normal, preview or edit.
36
+ * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
37
+ * @param {WithSitecoreOptions} [options] hook options
38
+ * @example
39
+ * const EditMode = () => {
40
+ * const { pageContext } = useSitecore();
41
+ * return <span>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
42
+ * }
43
+ * @example
44
+ * const EditMode = () => {
45
+ * const { pageContext, updateContext } = useSitecore({ updatable: true });
46
+ * const onClick = () => updateContext({ pageEditing: true });
47
+ * return <span onClick={onClick}>Edit Mode is {pageContext.pageEditing ? 'active' : 'inactive'}</span>
48
+ * }
49
+ * @returns {object} { api, pageContext, updateContext }
50
+ */
51
+ export declare function useSitecore(options?: WithSitecoreOptions): WithSitecoreProps;
package/types/index.d.ts CHANGED
@@ -19,8 +19,8 @@ export { BYOCComponent, BYOCComponentParams, BYOCComponentProps, fetchBYOCCompon
19
19
  export { BYOCWrapper } from './components/BYOCWrapper';
20
20
  export { Link, LinkField, LinkFieldValue, LinkProps } from './components/Link';
21
21
  export { File, FileField } from './components/File';
22
- export { SitecoreContext, SitecoreContextState, SitecoreContextValue, SitecoreContextReactContext, } from './components/SitecoreContext';
23
- export { withSitecoreContext, useSitecoreContext, WithSitecoreContextOptions, WithSitecoreContextProps, WithSitecoreContextHocProps, } from './enhancers/withSitecoreContext';
22
+ export { SitecoreProvider, SitecoreProviderState, SitecoreProviderPageContext, SitecoreProviderReactContext, } from './components/SitecoreProvider';
23
+ export { withSitecore, useSitecore, WithSitecoreOptions, WithSitecoreProps, WithSitecoreHocProps, } from './enhancers/withSitecore';
24
24
  export { withEditorChromes } from './enhancers/withEditorChromes';
25
25
  export { withPlaceholder } from './enhancers/withPlaceholder';
26
26
  export { withDatasourceCheck } from './enhancers/withDatasourceCheck';
@@ -1,48 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.withSitecoreContext = withSitecoreContext;
7
- exports.useSitecoreContext = useSitecoreContext;
8
- const react_1 = __importDefault(require("react"));
9
- const SitecoreContext_1 = require("../components/SitecoreContext");
10
- /**
11
- * @param {WithSitecoreContextOptions} [options]
12
- */
13
- function withSitecoreContext(options) {
14
- return function withSitecoreContextHoc(Component) {
15
- return function WithSitecoreContext(props) {
16
- return (react_1.default.createElement(SitecoreContext_1.SitecoreContextReactContext.Consumer, null, (context) => (react_1.default.createElement(Component, Object.assign({}, props, { sitecoreContext: context.context, api: context.api, updateSitecoreContext: options && options.updatable && context.setContext })))));
17
- };
18
- };
19
- }
20
- /**
21
- * This hook grants acсess to the current Sitecore page context
22
- * by default JSS includes the following properties in this context:
23
- * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
24
- * - pageState - Like pageEditing, but a string: normal, preview or edit.
25
- * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
26
- * @param {WithSitecoreContextOptions} [options] hook options
27
- * @example
28
- * const EditMode = () => {
29
- * const { sitecoreContext } = useSitecoreContext();
30
- * return <span>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
31
- * }
32
- * @example
33
- * const EditMode = () => {
34
- * const { sitecoreContext, updateSitecoreContext } = useSitecoreContext({ updatable: true });
35
- * const onClick = () => updateSitecoreContext({ pageEditing: true });
36
- * return <span onClick={onClick}>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
37
- * }
38
- * @returns {object} { sitecoreContext, updateSitecoreContext }
39
- */
40
- function useSitecoreContext(options) {
41
- const reactContext = react_1.default.useContext(SitecoreContext_1.SitecoreContextReactContext);
42
- const updatable = options === null || options === void 0 ? void 0 : options.updatable;
43
- return {
44
- api: reactContext.api,
45
- sitecoreContext: reactContext.context,
46
- updateSitecoreContext: updatable ? reactContext.setContext : undefined,
47
- };
48
- }
@@ -1,41 +0,0 @@
1
- import React from 'react';
2
- import { SitecoreContextReactContext, } from '../components/SitecoreContext';
3
- /**
4
- * @param {WithSitecoreContextOptions} [options]
5
- */
6
- export function withSitecoreContext(options) {
7
- return function withSitecoreContextHoc(Component) {
8
- return function WithSitecoreContext(props) {
9
- return (React.createElement(SitecoreContextReactContext.Consumer, null, (context) => (React.createElement(Component, Object.assign({}, props, { sitecoreContext: context.context, api: context.api, updateSitecoreContext: options && options.updatable && context.setContext })))));
10
- };
11
- };
12
- }
13
- /**
14
- * This hook grants acсess to the current Sitecore page context
15
- * by default JSS includes the following properties in this context:
16
- * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
17
- * - pageState - Like pageEditing, but a string: normal, preview or edit.
18
- * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
19
- * @param {WithSitecoreContextOptions} [options] hook options
20
- * @example
21
- * const EditMode = () => {
22
- * const { sitecoreContext } = useSitecoreContext();
23
- * return <span>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
24
- * }
25
- * @example
26
- * const EditMode = () => {
27
- * const { sitecoreContext, updateSitecoreContext } = useSitecoreContext({ updatable: true });
28
- * const onClick = () => updateSitecoreContext({ pageEditing: true });
29
- * return <span onClick={onClick}>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
30
- * }
31
- * @returns {object} { sitecoreContext, updateSitecoreContext }
32
- */
33
- export function useSitecoreContext(options) {
34
- const reactContext = React.useContext(SitecoreContextReactContext);
35
- const updatable = options === null || options === void 0 ? void 0 : options.updatable;
36
- return {
37
- api: reactContext.api,
38
- sitecoreContext: reactContext.context,
39
- updateSitecoreContext: updatable ? reactContext.setContext : undefined,
40
- };
41
- }
@@ -1,34 +0,0 @@
1
- import React from 'react';
2
- import { SitecoreConfig } from '@sitecore-content-sdk/core/config';
3
- import { LayoutServiceContext, LayoutServiceData, RouteData } from '../index';
4
- import { ComponentMap } from './sharedTypes';
5
- export interface SitecoreContextProps {
6
- api: SitecoreConfig['api'];
7
- componentMap: ComponentMap;
8
- layoutData?: LayoutServiceData;
9
- children: React.ReactNode;
10
- }
11
- export interface SitecoreContextState {
12
- setContext: (value: SitecoreContextValue | LayoutServiceData) => void;
13
- context: SitecoreContextValue;
14
- api?: SitecoreContextProps['api'];
15
- }
16
- export declare const SitecoreContextReactContext: React.Context<SitecoreContextState>;
17
- export declare const ComponentMapReactContext: React.Context<ComponentMap>;
18
- export type SitecoreContextValue = LayoutServiceContext & {
19
- itemId?: string;
20
- route?: RouteData;
21
- };
22
- export declare class SitecoreContext extends React.Component<SitecoreContextProps, SitecoreContextState> {
23
- static displayName: string;
24
- constructor(props: SitecoreContextProps);
25
- constructContext(layoutData?: LayoutServiceData): SitecoreContextValue;
26
- componentDidUpdate(prevProps: SitecoreContextProps): void;
27
- /**
28
- * Update context state. Value can be @type {LayoutServiceData} which will be automatically transformed
29
- * or you can provide exact @type {SitecoreContextValue}
30
- * @param {SitecoreContextValue | LayoutServiceData} value New context value
31
- */
32
- setContext: (value: SitecoreContextValue | LayoutServiceData) => void;
33
- render(): React.JSX.Element;
34
- }
@@ -1,37 +0,0 @@
1
- import React from 'react';
2
- import { EnhancedOmit } from '@sitecore-content-sdk/core/utils';
3
- import { SitecoreContextState, SitecoreContextValue } from '../components/SitecoreContext';
4
- export interface WithSitecoreContextOptions {
5
- updatable?: boolean;
6
- }
7
- export interface WithSitecoreContextProps {
8
- sitecoreContext: SitecoreContextValue;
9
- api?: SitecoreContextState['api'];
10
- updateSitecoreContext?: ((value: SitecoreContextValue) => void) | false;
11
- }
12
- export type WithSitecoreContextHocProps<ComponentProps> = EnhancedOmit<ComponentProps, keyof WithSitecoreContextProps>;
13
- /**
14
- * @param {WithSitecoreContextOptions} [options]
15
- */
16
- export declare function withSitecoreContext(options?: WithSitecoreContextOptions): <ComponentProps extends WithSitecoreContextProps>(Component: React.ComponentType<ComponentProps>) => (props: WithSitecoreContextHocProps<ComponentProps>) => React.JSX.Element;
17
- /**
18
- * This hook grants acсess to the current Sitecore page context
19
- * by default JSS includes the following properties in this context:
20
- * - pageEditing - Provided by Layout Service, a boolean indicating whether the route is being accessed via the Sitecore Editor.
21
- * - pageState - Like pageEditing, but a string: normal, preview or edit.
22
- * - site - Provided by Layout Service, an object containing the name of the current Sitecore site context.
23
- * @param {WithSitecoreContextOptions} [options] hook options
24
- * @example
25
- * const EditMode = () => {
26
- * const { sitecoreContext } = useSitecoreContext();
27
- * return <span>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
28
- * }
29
- * @example
30
- * const EditMode = () => {
31
- * const { sitecoreContext, updateSitecoreContext } = useSitecoreContext({ updatable: true });
32
- * const onClick = () => updateSitecoreContext({ pageEditing: true });
33
- * return <span onClick={onClick}>Edit Mode is {sitecoreContext.pageEditing ? 'active' : 'inactive'}</span>
34
- * }
35
- * @returns {object} { sitecoreContext, updateSitecoreContext }
36
- */
37
- export declare function useSitecoreContext(options?: WithSitecoreContextOptions): WithSitecoreContextProps;