@contentful/experiences-sdk-react 0.0.1-alpha.10

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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +113 -0
  3. package/dist/VisualEditorInjectScript-6a92528d.js +38 -0
  4. package/dist/VisualEditorInjectScript-6a92528d.js.map +1 -0
  5. package/dist/VisualEditorLoader-255ce544.js +28 -0
  6. package/dist/VisualEditorLoader-255ce544.js.map +1 -0
  7. package/dist/index.d.ts +51 -0
  8. package/dist/index.js +5014 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/src/ExperienceRoot.d.ts +10 -0
  11. package/dist/src/blocks/editor/VisualEditorInjectScript.d.ts +4 -0
  12. package/dist/src/blocks/editor/VisualEditorLoader.d.ts +7 -0
  13. package/dist/src/blocks/editor/VisualEditorRoot.d.ts +9 -0
  14. package/dist/src/blocks/preview/CompositionBlock.d.ts +10 -0
  15. package/dist/src/blocks/preview/CompositionBlock.spec.d.ts +1 -0
  16. package/dist/src/blocks/preview/PreviewDeliveryRoot.d.ts +8 -0
  17. package/dist/src/blocks/preview/PreviewDeliveryRoot.test.d.ts +1 -0
  18. package/dist/src/components/Assembly.d.ts +3 -0
  19. package/dist/src/components/ErrorBoundary.d.ts +25 -0
  20. package/dist/src/components/Flex.d.ts +64 -0
  21. package/dist/src/constants.d.ts +3 -0
  22. package/dist/src/core/componentRegistry.d.ts +24 -0
  23. package/dist/src/core/componentRegistry.test.d.ts +1 -0
  24. package/dist/src/core/index.d.ts +1 -0
  25. package/dist/src/core/preview/assemblyUtils.d.ts +10 -0
  26. package/dist/src/core/preview/assemblyUtils.spec.d.ts +1 -0
  27. package/dist/src/hooks/index.d.ts +3 -0
  28. package/dist/src/hooks/useBreakpoints.d.ts +4 -0
  29. package/dist/src/hooks/useBreakpoints.spec.d.ts +1 -0
  30. package/dist/src/hooks/useDetectEditorMode.d.ts +7 -0
  31. package/dist/src/hooks/useFetchByBase.d.ts +8 -0
  32. package/dist/src/hooks/useFetchById.d.ts +13 -0
  33. package/dist/src/hooks/useFetchById.spec.d.ts +1 -0
  34. package/dist/src/hooks/useFetchBySlug.d.ts +13 -0
  35. package/dist/src/hooks/useFetchBySlug.spec.d.ts +1 -0
  36. package/dist/src/hooks/useInitializeVisualEditor.d.ts +7 -0
  37. package/dist/src/hooks/usePrevious.d.ts +6 -0
  38. package/dist/src/hooks/useStyleTag.d.ts +16 -0
  39. package/dist/src/index.d.ts +6 -0
  40. package/dist/src/sdkVersion.d.ts +1 -0
  41. package/dist/src/utils/withComponentWrapper.d.ts +36 -0
  42. package/dist/src/utils/withComponentWrapper.spec.d.ts +1 -0
  43. package/dist/test/__fixtures__/assembly.d.ts +96 -0
  44. package/dist/test/__fixtures__/composition.d.ts +7 -0
  45. package/dist/test/__fixtures__/entities.d.ts +9 -0
  46. package/dist/test/components/Test.d.ts +1 -0
  47. package/dist/test/components/Test.spec.d.ts +1 -0
  48. package/dist/vite.config.d.ts +2 -0
  49. package/package.json +92 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Contentful
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # @contentful/experiences-sdk-react
2
+
3
+ Experience builder SDK by [Contentful](https://www.contentful.com/).
4
+
5
+ ## Documentation
6
+
7
+ Please find more information about this sdk [on our Wiki page](https://github.com/contentful/experience-builder/wiki)
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ npm install @contentful/experiences-sdk-react
13
+ ```
14
+
15
+ ## Example Component:
16
+
17
+ ```tsx
18
+ function MyButton({ buttonTitle, buttonUrl, ...props }) {
19
+ // WARNING:
20
+ // - you must spread the props as last argument to enable canvas interactions and proper rendering on canvas
21
+ // - be sure to ensure that onClick handlers are above the {...props} spreading so that they are stubbed
22
+ // during EDITOR mode
23
+ return (
24
+ <button onClick={() => (window.location.href = buttonUrl)} {...props}>
25
+ {buttonTitle}
26
+ </button>
27
+ );
28
+ }
29
+ ```
30
+
31
+ ## Setup example
32
+
33
+ Please find more setup examples [on a dedicated Wiki page](https://github.com/contentful/experience-builder/wiki/Setup-examples)
34
+
35
+ ```tsx
36
+ import {
37
+ defineComponents,
38
+ defineDesignTokens,
39
+ ExperienceRoot,
40
+ useFetchBySlug,
41
+ } from '@contentful/experiences-sdk-react';
42
+
43
+ import { createClient } from 'contentful';
44
+
45
+ // component example can be found at the top of this document
46
+ import { MyButton } from './components/MyButton';
47
+
48
+ const client = createClient({
49
+ space: 'YOUR_SPACE_ID',
50
+ environment: 'YOUR_ENVIRONMENT_ID',
51
+ host: 'preview.contentful.com', // Supported values: 'preview.contentful.com' or 'cdn.contentful.com',
52
+ accessToken: 'YOUR_PREVIEW_OR_DELIVERY_TOKEN', // needs to be preview token if host = 'preview.contentful.com' and delivery token if 'cdn.contentful.com'
53
+ });
54
+
55
+ // 1. Define components outside of React
56
+ defineComponents([
57
+ {
58
+ component: MyButton, // example component defined above in this file
59
+ definition: {
60
+ id: 'my-button',
61
+ name: 'MyButton',
62
+ variables: {
63
+ buttonTitle: { type: 'Text', defaultValue: 'Click me' },
64
+ buttonUrl: {
65
+ type: 'Text',
66
+ defaultValue: 'https://www.contentful.com/',
67
+ },
68
+ },
69
+ },
70
+ },
71
+ ]);
72
+
73
+ // 2. Define design tokens (optional)
74
+ defineDesignTokens({
75
+ spacing: { XS: '4px', S: '16px', M: '32px', L: '64px', XL: '128px' },
76
+ color: { Slate: '#94a3b8', Azure: 'azure', Orange: '#fdba74' },
77
+ });
78
+
79
+ const Home = () => {
80
+
81
+ // You could pull these values from your router, state, etc...
82
+ const localeCode = 'en-US'; // the initial locale to use
83
+ const slug = 'homePage'; // the slug of the experience to fetch
84
+ const experienceTypeId = 'layout'; // the content id of the experience to fetch
85
+
86
+ // 3. Fetch the experience
87
+ const { experience, error, isLoading } = useFetchBySlug({
88
+ client,
89
+ slug,
90
+ experienceTypeId,
91
+ localeCode,
92
+ });
93
+
94
+ // 4. Handle loading state
95
+ if(isLoading) {
96
+ return <div>Loading...</div>
97
+ }
98
+
99
+ // 5. Handle errors
100
+ if (error) {
101
+ return <div>{error.message}</div>;
102
+ }
103
+
104
+ // 6. Render the experience
105
+ return (
106
+ <ExperienceRoot
107
+ experience={experience}
108
+ // The locale that will appear on the website first
109
+ locale={locale}
110
+ />
111
+ );
112
+ };
113
+ ```
@@ -0,0 +1,38 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { useEffect } from 'react';
3
+ import { VISUAL_EDITOR_CONTAINER_ID } from '@contentful/experiences-core/constants';
4
+
5
+ /**
6
+ * The version can be any semver version or dist tag
7
+ * Examples: @latest, @dev, @pre, @0.0.1-pre-20231213T103332.0
8
+ *
9
+ * If the version is empty, unpkg will redirect to the latest version of the package
10
+ */
11
+ const version = '@0.0.1-pre-20231213T210727.0';
12
+ /**
13
+ * CDN URL for the visual editor script
14
+ *
15
+ * This can be changed to a local file path for development
16
+ *
17
+ * TODO: Reconsider unpkg
18
+ */
19
+ const scriptUrl = `https://unpkg.com/@contentful/experience-builder-visual-editor${version}/dist/renderApp.js`;
20
+ /**
21
+ * This component injects the visual editor script into the page
22
+ */
23
+ function VisualEditorInjectScript() {
24
+ useEffect(() => {
25
+ const script = document.createElement('script');
26
+ script.type = 'module';
27
+ script.src = scriptUrl;
28
+ script.async = true;
29
+ document.body.appendChild(script);
30
+ return () => {
31
+ document.body.removeChild(script);
32
+ };
33
+ }, []);
34
+ return jsx("div", { id: VISUAL_EDITOR_CONTAINER_ID });
35
+ }
36
+
37
+ export { VisualEditorInjectScript as default };
38
+ //# sourceMappingURL=VisualEditorInjectScript-6a92528d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VisualEditorInjectScript-6a92528d.js","sources":["../../src/blocks/editor/VisualEditorInjectScript.tsx"],"sourcesContent":["import React, { useEffect } from 'react';\nimport { VISUAL_EDITOR_CONTAINER_ID } from '@contentful/experiences-core/constants';\n\n/**\n * The version can be any semver version or dist tag\n * Examples: @latest, @dev, @pre, @0.0.1-pre-20231213T103332.0\n *\n * If the version is empty, unpkg will redirect to the latest version of the package\n */\nconst version = '@0.0.1-pre-20231213T210727.0';\n\n/**\n * CDN URL for the visual editor script\n *\n * This can be changed to a local file path for development\n *\n * TODO: Reconsider unpkg\n */\nconst scriptUrl = `https://unpkg.com/@contentful/experience-builder-visual-editor${version}/dist/renderApp.js`;\n\n/**\n * This component injects the visual editor script into the page\n */\nexport default function VisualEditorInjectScript() {\n useEffect(() => {\n const script = document.createElement('script');\n script.type = 'module';\n script.src = scriptUrl;\n script.async = true;\n\n document.body.appendChild(script);\n return () => {\n document.body.removeChild(script);\n };\n }, []);\n\n return <div id={VISUAL_EDITOR_CONTAINER_ID} />;\n}\n"],"names":["_jsx"],"mappings":";;;;AAGA;;;;;AAKG;AACH,MAAM,OAAO,GAAG,8BAA8B,CAAC;AAE/C;;;;;;AAMG;AACH,MAAM,SAAS,GAAG,CAAiE,8DAAA,EAAA,OAAO,oBAAoB,CAAC;AAE/G;;AAEG;AACW,SAAU,wBAAwB,GAAA;IAC9C,SAAS,CAAC,MAAK;QACb,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAChD,QAAA,MAAM,CAAC,IAAI,GAAG,QAAQ,CAAC;AACvB,QAAA,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC;AACvB,QAAA,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;AAEpB,QAAA,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AAClC,QAAA,OAAO,MAAK;AACV,YAAA,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AACpC,SAAC,CAAC;KACH,EAAE,EAAE,CAAC,CAAC;AAEP,IAAA,OAAOA,GAAK,CAAA,KAAA,EAAA,EAAA,EAAE,EAAE,0BAA0B,GAAI,CAAC;AACjD;;;;"}
@@ -0,0 +1,28 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { useState, useEffect } from 'react';
3
+ import { VisualEditorMode } from '@contentful/experiences-core';
4
+
5
+ const VisualEditorLoader = ({ visualEditorMode }) => {
6
+ const [VisualEditor, setVisualEditor] = useState(null);
7
+ useEffect(() => {
8
+ // Dynamically import the visual editor based on the configured mode
9
+ switch (visualEditorMode) {
10
+ case VisualEditorMode.InjectScript:
11
+ import('./VisualEditorInjectScript-6a92528d.js').then((module) => {
12
+ setVisualEditor(() => module.default);
13
+ });
14
+ break;
15
+ // VisualEditorMode.LazyLoad:
16
+ default:
17
+ import('@contentful/experiences-visual-editor-react').then((module) => {
18
+ setVisualEditor(() => module.default);
19
+ });
20
+ }
21
+ }, [visualEditorMode]);
22
+ if (!VisualEditor)
23
+ return null;
24
+ return jsx(VisualEditor, {});
25
+ };
26
+
27
+ export { VisualEditorLoader, VisualEditorLoader as default };
28
+ //# sourceMappingURL=VisualEditorLoader-255ce544.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VisualEditorLoader-255ce544.js","sources":["../../src/blocks/editor/VisualEditorLoader.tsx"],"sourcesContent":["import React, { useEffect, useState } from 'react';\nimport { VisualEditorMode } from '@contentful/experiences-core';\n\ntype VisualEditorLoaderProps = {\n visualEditorMode: VisualEditorMode;\n};\n\nexport const VisualEditorLoader: React.FC<VisualEditorLoaderProps> = ({ visualEditorMode }) => {\n const [VisualEditor, setVisualEditor] = useState<React.ComponentType | null>(null);\n\n useEffect(() => {\n // Dynamically import the visual editor based on the configured mode\n switch (visualEditorMode) {\n case VisualEditorMode.InjectScript:\n import('./VisualEditorInjectScript').then((module) => {\n setVisualEditor(() => module.default);\n });\n break;\n\n // VisualEditorMode.LazyLoad:\n default:\n import('@contentful/experiences-visual-editor-react').then((module) => {\n setVisualEditor(() => module.default);\n });\n }\n }, [visualEditorMode]);\n\n if (!VisualEditor) return null;\n\n return <VisualEditor />;\n};\n\nexport default VisualEditorLoader;\n"],"names":["_jsx"],"mappings":";;;;MAOa,kBAAkB,GAAsC,CAAC,EAAE,gBAAgB,EAAE,KAAI;IAC5F,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAA6B,IAAI,CAAC,CAAC;IAEnF,SAAS,CAAC,MAAK;;QAEb,QAAQ,gBAAgB;YACtB,KAAK,gBAAgB,CAAC,YAAY;gBAChC,OAAO,wCAA4B,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAI;oBACnD,eAAe,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;AACxC,iBAAC,CAAC,CAAC;gBACH,MAAM;;AAGR,YAAA;gBACE,OAAO,6CAA6C,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAI;oBACpE,eAAe,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;AACxC,iBAAC,CAAC,CAAC;SACN;AACH,KAAC,EAAE,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAEvB,IAAA,IAAI,CAAC,YAAY;AAAE,QAAA,OAAO,IAAI,CAAC;IAE/B,OAAOA,GAAA,CAAC,YAAY,EAAA,EAAA,CAAG,CAAC;AAC1B;;;;"}
@@ -0,0 +1,51 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as _contentful_experiences_core from '@contentful/experiences-core';
3
+ import { EntityStore, VisualEditorMode } from '@contentful/experiences-core';
4
+ export { VisualEditorMode, createExperience, defineDesignTokens, fetchById, fetchBySlug } from '@contentful/experiences-core';
5
+ import * as _contentful_experiences_core_types from '@contentful/experiences-core/types';
6
+ import { Experience, ComponentRegistration, ComponentRegistrationOptions } from '@contentful/experiences-core/types';
7
+ export { ComponentDefinition, ExternalSDKMode } from '@contentful/experiences-core/types';
8
+ import { ContentfulClientApi } from 'contentful';
9
+ export { CF_STYLE_ATTRIBUTES, CONTENTFUL_COMPONENTS, LATEST_SCHEMA_VERSION } from '@contentful/experiences-core/constants';
10
+
11
+ type ExperienceRootProps = {
12
+ experience?: Experience<EntityStore>;
13
+ locale: string;
14
+ visualEditorMode?: VisualEditorMode;
15
+ };
16
+ declare const ExperienceRoot: ({ locale, experience, visualEditorMode, }: ExperienceRootProps) => react_jsx_runtime.JSX.Element | null;
17
+
18
+ type UseFetchByIdArgs = {
19
+ client: ContentfulClientApi<undefined>;
20
+ id: string;
21
+ experienceTypeId: string;
22
+ localeCode: string;
23
+ };
24
+ declare const useFetchById: ({ id, localeCode, client, experienceTypeId }: UseFetchByIdArgs) => {
25
+ error: Error | undefined;
26
+ experience: _contentful_experiences_core_types.Experience<_contentful_experiences_core.EntityStore> | undefined;
27
+ isLoading: boolean;
28
+ isEditorMode: boolean;
29
+ };
30
+
31
+ type UseFetchBySlugArgs = {
32
+ client: ContentfulClientApi<undefined>;
33
+ slug: string;
34
+ experienceTypeId: string;
35
+ localeCode: string;
36
+ };
37
+ declare const useFetchBySlug: ({ slug, localeCode, client, experienceTypeId, }: UseFetchBySlugArgs) => {
38
+ error: Error | undefined;
39
+ experience: _contentful_experiences_core_types.Experience<_contentful_experiences_core.EntityStore> | undefined;
40
+ isLoading: boolean;
41
+ isEditorMode: boolean;
42
+ };
43
+
44
+ /**
45
+ * Registers multiple components and their component definitions at once
46
+ * @param componentRegistrations - ComponentRegistration[]
47
+ * @returns void
48
+ */
49
+ declare const defineComponents: (componentRegistrations: ComponentRegistration[], options?: ComponentRegistrationOptions) => void;
50
+
51
+ export { ExperienceRoot, defineComponents, useFetchById, useFetchBySlug };