@contentful/experiences-sdk-react 1.3.0 → 1.3.1-dev-20240508T1336-149a569.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 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, buildCfStyles, isEmptyStructureWithRelativeHeight, mediaQueryMatcher, getFallbackBreakpointIndex, getActiveBreakpointIndex, getValueForBreakpoint, doesMismatchMessageSchema, tryParseMessage, fetchById, fetchBySlug, validateExperienceBuilderConfig, VisualEditorMode, toCSSAttribute } from '@contentful/experiences-core';
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, { useMemo, useLayoutEffect, useState, useEffect, useCallback, useRef, Suspense } from '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, HYPERLINK_DEFAULT_PATTERN, EMPTY_CONTAINER_HEIGHT, CF_STYLE_ATTRIBUTES, INCOMING_EVENTS, VISUAL_EDITOR_EVENTS } from '@contentful/experiences-core/constants';
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-beta.0';
13
+ const SDK_VERSION = '1.3.0';
14
14
 
15
15
  var util;
16
16
  (function (util) {
@@ -4596,41 +4596,38 @@ const maintainBasicComponentIdsWithoutPrefix = () => {
4596
4596
  };
4597
4597
 
4598
4598
  /**
4599
+ * This hook can generate className and inject styles on a client side as a <style> tag
4600
+ * or it derives the className set on the server side
4599
4601
  *
4600
- * @param styles: the list of styles to apply
4601
- * @param nodeId: [Optional] the id of node that these styles will be applied to
4602
- * @returns className: the className that was used
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
4602
+ * @param node: the componenet node for which the styles will be injected
4603
+ * @param props: the props of the component, represented by the node
4604
+ * @returns the className that was eitner generated on the client side or derived from the value, set on server side
4606
4605
  */
4607
- const useStyleTag = ({ styles, nodeId }) => {
4608
- const [className, styleRule] = useMemo(() => {
4609
- if (Object.keys(styles).length === 0) {
4610
- return [''];
4611
- }
4612
- return buildStyleTag({ styles, nodeId });
4613
- }, [styles, nodeId]);
4606
+ const useClassName = ({ node, props, }) => {
4607
+ const [className, setClassName] = useState(props.cfSsrClassName ?? '');
4614
4608
  // Once our React support allows it (>=18), this should be implemented with useInsertionEffect.
4615
4609
  useLayoutEffect(() => {
4616
- if (!className || !styleRule) {
4610
+ if (props.cfSsrClassName) {
4611
+ // class name has been already set on the server side. No need to calculate it on client side anymore
4617
4612
  return;
4618
4613
  }
4619
- const existingTag = document.querySelector(`[data-cf-styles="${className}"]`);
4620
- if (existingTag) {
4621
- // editor mode - update existing
4622
- if (nodeId) {
4623
- existingTag.innerHTML = styleRule;
4624
- }
4625
- // preview/delivery mode - here we don't need to update the existing tag because
4626
- // the className is based on the md5 hash of the content so it hasn't changed
4614
+ const cfStyles = buildCfStyles(props, node.definitionId);
4615
+ if (Object.keys(cfStyles).length === 0) {
4616
+ return;
4617
+ }
4618
+ if (isEmptyStructureWithRelativeHeight(node.children.length, node.definitionId, cfStyles.height)) {
4619
+ cfStyles.minHeight = EMPTY_CONTAINER_HEIGHT;
4620
+ }
4621
+ const [className, styleRule] = buildStyleTag({ styles: cfStyles });
4622
+ if (!className || !styleRule) {
4627
4623
  return;
4628
4624
  }
4629
4625
  const styleTag = document.createElement('style');
4630
4626
  styleTag.dataset['cfStyles'] = className;
4627
+ setClassName(className);
4631
4628
  document.head.appendChild(styleTag).innerHTML = styleRule;
4632
- }, [className, nodeId, styleRule]);
4633
- return { className };
4629
+ }, [props, node]);
4630
+ return className;
4634
4631
  };
4635
4632
 
4636
4633
  const deserializeAssemblyNode = ({ node, componentInstanceVariables, }) => {
@@ -4735,7 +4732,11 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
4735
4732
  if (!componentRegistration || isAssembly) {
4736
4733
  return {};
4737
4734
  }
4738
- const propMap = {};
4735
+ const propMap = {
4736
+ cfSsrClassName: node.variables.cfSsrClassName
4737
+ ? resolveDesignValue(node.variables.cfSsrClassName.valuesByBreakpoint, 'cfSsrClassName')
4738
+ : undefined,
4739
+ };
4739
4740
  return Object.entries(componentRegistration.definition.variables).reduce((acc, [variableName, variableDefinition]) => {
4740
4741
  const variable = node.variables[variableName];
4741
4742
  if (!variable)
@@ -4784,14 +4785,7 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
4784
4785
  hyperlinkPattern,
4785
4786
  locale,
4786
4787
  ]);
4787
- const cfStyles = buildCfStyles(nodeProps, node.definitionId);
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;
4788
+ const className = useClassName({ props: nodeProps, node });
4795
4789
  if (!componentRegistration) {
4796
4790
  return null;
4797
4791
  }
@@ -4814,7 +4808,7 @@ const CompositionBlock = ({ node: rawNode, locale, entityStore, hyperlinkPattern
4814
4808
  const stylesToKeep = ['cfImageAsset'];
4815
4809
  const stylesToRemove = CF_STYLE_ATTRIBUTES.filter((style) => !stylesToKeep.includes(style));
4816
4810
  return React.createElement(component, {
4817
- ...omit(nodeProps, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab']),
4811
+ ...omit(nodeProps, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab', 'cfSsrClassName']),
4818
4812
  className,
4819
4813
  }, children ?? (typeof nodeProps.children === 'string' ? nodeProps.children : null));
4820
4814
  };