@contentful/experiences-sdk-react 1.3.0 → 1.3.1-dev-20240515T1508-a1b36d4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +33 -38
- package/dist/index.js.map +1 -1
- package/dist/src/hooks/useClassName.d.ts +13 -0
- package/dist/src/sdkVersion.d.ts +1 -1
- package/package.json +5 -5
- package/dist/src/hooks/useStyleTag.d.ts +0 -16
- /package/dist/src/hooks/{useStyleTag.spec.d.ts → useClassName.spec.d.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
2
|
-
import { containerDefinition, sectionDefinition, columnsDefinition, singleColumnDefinition, dividerDefinition, builtInStyles, optionalBuiltInStyles, sendMessage, designTokensRegistry, buildStyleTag, checkIsAssemblyNode, resolveHyperlinkPattern, transformBoundContentValue,
|
|
2
|
+
import { containerDefinition, sectionDefinition, columnsDefinition, singleColumnDefinition, dividerDefinition, builtInStyles, optionalBuiltInStyles, sendMessage, designTokensRegistry, buildCfStyles, isEmptyStructureWithRelativeHeight, buildStyleTag, checkIsAssemblyNode, resolveHyperlinkPattern, transformBoundContentValue, mediaQueryMatcher, getFallbackBreakpointIndex, getActiveBreakpointIndex, getValueForBreakpoint, doesMismatchMessageSchema, tryParseMessage, fetchById, fetchBySlug, validateExperienceBuilderConfig, VisualEditorMode, toCSSAttribute } from '@contentful/experiences-core';
|
|
3
3
|
export { VisualEditorMode, createExperience, defineDesignTokens, fetchById, fetchBySlug } from '@contentful/experiences-core';
|
|
4
|
-
import React, {
|
|
4
|
+
import React, { useState, useLayoutEffect, useMemo, useEffect, useCallback, useRef, Suspense } from 'react';
|
|
5
5
|
import { omit } from 'lodash-es';
|
|
6
|
-
import { INTERNAL_EVENTS, CONTENTFUL_COMPONENTS, OUTGOING_EVENTS, ASSEMBLY_DEFAULT_CATEGORY,
|
|
6
|
+
import { INTERNAL_EVENTS, CONTENTFUL_COMPONENTS, OUTGOING_EVENTS, ASSEMBLY_DEFAULT_CATEGORY, EMPTY_CONTAINER_HEIGHT, HYPERLINK_DEFAULT_PATTERN, CF_STYLE_ATTRIBUTES, INCOMING_EVENTS, VISUAL_EDITOR_EVENTS } from '@contentful/experiences-core/constants';
|
|
7
7
|
export { CF_STYLE_ATTRIBUTES, CONTENTFUL_COMPONENTS, LATEST_SCHEMA_VERSION } from '@contentful/experiences-core/constants';
|
|
8
8
|
import * as Components from '@contentful/experiences-components-react';
|
|
9
9
|
import { ContentfulContainer, Columns, SingleColumn } from '@contentful/experiences-components-react';
|
|
10
10
|
import styleInject from 'style-inject';
|
|
11
11
|
import md5 from 'md5';
|
|
12
12
|
|
|
13
|
-
const SDK_VERSION = '1.3.0
|
|
13
|
+
const SDK_VERSION = '1.3.0';
|
|
14
14
|
|
|
15
15
|
var util;
|
|
16
16
|
(function (util) {
|
|
@@ -4141,6 +4141,7 @@ const UnboundValuesSchema = z.record(uuidKeySchema, z.object({
|
|
|
4141
4141
|
const BaseComponentTreeNodeSchema = z.object({
|
|
4142
4142
|
definitionId: DefinitionPropertyKeySchema,
|
|
4143
4143
|
displayName: z.string().optional(),
|
|
4144
|
+
slotId: z.string().optional(),
|
|
4144
4145
|
variables: z.record(propertyKeySchema, ComponentPropertyValueSchema),
|
|
4145
4146
|
});
|
|
4146
4147
|
const ComponentTreeNodeSchema = BaseComponentTreeNodeSchema.extend({
|
|
@@ -4596,41 +4597,38 @@ const maintainBasicComponentIdsWithoutPrefix = () => {
|
|
|
4596
4597
|
};
|
|
4597
4598
|
|
|
4598
4599
|
/**
|
|
4600
|
+
* This hook can generate className and inject styles on a client side as a <style> tag
|
|
4601
|
+
* or it derives the className set on the server side
|
|
4599
4602
|
*
|
|
4600
|
-
* @param
|
|
4601
|
-
* @param
|
|
4602
|
-
* @returns
|
|
4603
|
-
* Builds and adds a style tag in the document. Returns the className to be attached to the element.
|
|
4604
|
-
* In editor mode the nodeId is used as the identifier in order to avoid creating endless tags as the styles are tweeked
|
|
4605
|
-
* In preview/delivery mode the styles don't change oftem so we're using the md5 hash of the content of the tag
|
|
4603
|
+
* @param node: the componenet node for which the styles will be injected
|
|
4604
|
+
* @param props: the props of the component, represented by the node
|
|
4605
|
+
* @returns the className that was eitner generated on the client side or derived from the value, set on server side
|
|
4606
4606
|
*/
|
|
4607
|
-
const
|
|
4608
|
-
const [className,
|
|
4609
|
-
if (Object.keys(styles).length === 0) {
|
|
4610
|
-
return [''];
|
|
4611
|
-
}
|
|
4612
|
-
return buildStyleTag({ styles, nodeId });
|
|
4613
|
-
}, [styles, nodeId]);
|
|
4607
|
+
const useClassName = ({ node, props, }) => {
|
|
4608
|
+
const [className, setClassName] = useState(props.cfSsrClassName ?? '');
|
|
4614
4609
|
// Once our React support allows it (>=18), this should be implemented with useInsertionEffect.
|
|
4615
4610
|
useLayoutEffect(() => {
|
|
4616
|
-
if (
|
|
4611
|
+
if (props.cfSsrClassName) {
|
|
4612
|
+
// class name has been already set on the server side. No need to calculate it on client side anymore
|
|
4617
4613
|
return;
|
|
4618
4614
|
}
|
|
4619
|
-
const
|
|
4620
|
-
if (
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
|
|
4626
|
-
|
|
4615
|
+
const cfStyles = buildCfStyles(props, node.definitionId);
|
|
4616
|
+
if (Object.keys(cfStyles).length === 0) {
|
|
4617
|
+
return;
|
|
4618
|
+
}
|
|
4619
|
+
if (isEmptyStructureWithRelativeHeight(node.children.length, node.definitionId, cfStyles.height)) {
|
|
4620
|
+
cfStyles.minHeight = EMPTY_CONTAINER_HEIGHT;
|
|
4621
|
+
}
|
|
4622
|
+
const [className, styleRule] = buildStyleTag({ styles: cfStyles });
|
|
4623
|
+
if (!className || !styleRule) {
|
|
4627
4624
|
return;
|
|
4628
4625
|
}
|
|
4629
4626
|
const styleTag = document.createElement('style');
|
|
4630
4627
|
styleTag.dataset['cfStyles'] = className;
|
|
4628
|
+
setClassName(className);
|
|
4631
4629
|
document.head.appendChild(styleTag).innerHTML = styleRule;
|
|
4632
|
-
}, [
|
|
4633
|
-
return
|
|
4630
|
+
}, [props, node]);
|
|
4631
|
+
return className;
|
|
4634
4632
|
};
|
|
4635
4633
|
|
|
4636
4634
|
const deserializeAssemblyNode = ({ node, componentInstanceVariables, }) => {
|
|
@@ -4735,7 +4733,11 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
|
|
|
4735
4733
|
if (!componentRegistration || isAssembly) {
|
|
4736
4734
|
return {};
|
|
4737
4735
|
}
|
|
4738
|
-
const propMap = {
|
|
4736
|
+
const propMap = {
|
|
4737
|
+
cfSsrClassName: node.variables.cfSsrClassName
|
|
4738
|
+
? resolveDesignValue(node.variables.cfSsrClassName.valuesByBreakpoint, 'cfSsrClassName')
|
|
4739
|
+
: undefined,
|
|
4740
|
+
};
|
|
4739
4741
|
return Object.entries(componentRegistration.definition.variables).reduce((acc, [variableName, variableDefinition]) => {
|
|
4740
4742
|
const variable = node.variables[variableName];
|
|
4741
4743
|
if (!variable)
|
|
@@ -4784,14 +4786,7 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
|
|
|
4784
4786
|
hyperlinkPattern,
|
|
4785
4787
|
locale,
|
|
4786
4788
|
]);
|
|
4787
|
-
const
|
|
4788
|
-
if (isEmptyStructureWithRelativeHeight(node.children.length, node.definitionId, cfStyles.height)) {
|
|
4789
|
-
cfStyles.minHeight = EMPTY_CONTAINER_HEIGHT;
|
|
4790
|
-
}
|
|
4791
|
-
const { className: runtimeClassname } = useStyleTag({ styles: cfStyles });
|
|
4792
|
-
const className = node.variables.cfSsrClassName
|
|
4793
|
-
? resolveDesignValue(node.variables.cfSsrClassName.valuesByBreakpoint, 'cfSsrClassName')
|
|
4794
|
-
: runtimeClassname;
|
|
4789
|
+
const className = useClassName({ props: nodeProps, node });
|
|
4795
4790
|
if (!componentRegistration) {
|
|
4796
4791
|
return null;
|
|
4797
4792
|
}
|
|
@@ -4814,7 +4809,7 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
|
|
|
4814
4809
|
const stylesToKeep = ['cfImageAsset'];
|
|
4815
4810
|
const stylesToRemove = CF_STYLE_ATTRIBUTES.filter((style) => !stylesToKeep.includes(style));
|
|
4816
4811
|
return React.createElement(component, {
|
|
4817
|
-
...omit(nodeProps, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab']),
|
|
4812
|
+
...omit(nodeProps, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab', 'cfSsrClassName']),
|
|
4818
4813
|
className,
|
|
4819
4814
|
}, children ?? (typeof nodeProps.children === 'string' ? nodeProps.children : null));
|
|
4820
4815
|
};
|