@contentful/experiences-visual-editor-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,5 +1,5 @@
1
1
  import styleInject from 'style-inject';
2
- import React, { useRef, useMemo, useEffect, useState, forwardRef, useCallback } from 'react';
2
+ import React, { useRef, useMemo, useEffect, useState, useLayoutEffect, forwardRef, useCallback } from 'react';
3
3
  import md5 from 'md5';
4
4
  import { BLOCKS } from '@contentful/rich-text-types';
5
5
  import { isEqual, get as get$1, omit } from 'lodash-es';
@@ -2379,17 +2379,15 @@ const useSelectedInstanceCoordinates = ({ node }) => {
2379
2379
  };
2380
2380
 
2381
2381
  /**
2382
- *
2383
2382
  * @param styles: the list of styles to apply
2384
- * @param nodeId: [Optional] the id of node that these styles will be applied to
2383
+ * @param nodeId: the id of node that these styles will be applied to
2385
2384
  * @returns className: the className that was used
2386
2385
  * Builds and adds a style tag in the document. Returns the className to be attached to the element.
2387
2386
  * In editor mode the nodeId is used as the identifier in order to avoid creating endless tags as the styles are tweeked
2388
- * In preview/delivery mode the styles don't change oftem so we're using the md5 hash of the content of the tag
2389
2387
  */
2390
- const useStyleTag = ({ styles, nodeId }) => {
2388
+ const useEditorModeClassName = ({ styles, nodeId, }) => {
2391
2389
  const [className, setClassName] = useState('');
2392
- useEffect(() => {
2390
+ useLayoutEffect(() => {
2393
2391
  if (Object.keys(styles).length === 0) {
2394
2392
  return;
2395
2393
  }
@@ -2397,19 +2395,14 @@ const useStyleTag = ({ styles, nodeId }) => {
2397
2395
  setClassName(className);
2398
2396
  const existingTag = document.querySelector(`[data-cf-styles="${className}"]`);
2399
2397
  if (existingTag) {
2400
- // editor mode - update existing
2401
- if (nodeId) {
2402
- existingTag.innerHTML = styleRule;
2403
- }
2404
- // preview/delivery mode - here we don't need to update the existing tag because
2405
- // the className is based on the md5 hash of the content so it hasn't changed
2398
+ existingTag.innerHTML = styleRule;
2406
2399
  return;
2407
2400
  }
2408
2401
  const styleTag = document.createElement('style');
2409
2402
  styleTag.dataset['cfStyles'] = className;
2410
2403
  document.head.appendChild(styleTag).innerHTML = styleRule;
2411
2404
  }, [styles, nodeId]);
2412
- return { className };
2405
+ return className;
2413
2406
  };
2414
2407
 
2415
2408
  const getUnboundValues = ({ key, fallback, unboundValues, }) => {
@@ -2441,11 +2434,11 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
2441
2434
  const dataSource = useEditorStore((state) => state.dataSource);
2442
2435
  const entityStore = useEntityStore((state) => state.entityStore);
2443
2436
  const props = useMemo(() => {
2444
- const propsBase = node.data.props?.cfSsrClassName
2445
- ? {
2446
- cfSsrClassName: resolveDesignValue(node.data.props.cfSsrClassName.valuesByBreakpoint, 'cfSsrClassName'),
2447
- }
2448
- : {};
2437
+ const propsBase = {
2438
+ cfSsrClassName: node.data.props.cfSsrClassName
2439
+ ? resolveDesignValue(node.data.props.cfSsrClassName.valuesByBreakpoint, 'cfSsrClassName')
2440
+ : undefined,
2441
+ };
2449
2442
  // Don't enrich the assembly wrapper node with props
2450
2443
  if (!definition || node.type === ASSEMBLY_NODE_TYPE) {
2451
2444
  return propsBase;
@@ -2544,7 +2537,7 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
2544
2537
  // Separate the component styles from the editor wrapper styles
2545
2538
  const { margin, height, width, maxWidth, ...componentStyles } = cfStyles;
2546
2539
  // Styles that will be applied to the editor wrapper (draggable) element
2547
- const { className: wrapperClass } = useStyleTag({
2540
+ const wrapperClass = useEditorModeClassName({
2548
2541
  styles:
2549
2542
  // To ensure that assembly nodes are rendered like they are rendered in
2550
2543
  // the assembly editor, we need to use a normal block instead of a flex box.
@@ -2562,7 +2555,7 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
2562
2555
  nodeId: `editor-${node.data.id}`,
2563
2556
  });
2564
2557
  // Styles that will be applied to the component element
2565
- const { className: componentClass } = useStyleTag({
2558
+ const componentClass = useEditorModeClassName({
2566
2559
  styles: {
2567
2560
  ...componentStyles,
2568
2561
  margin: 0,
@@ -2594,7 +2587,7 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
2594
2587
  editorMode: true,
2595
2588
  node,
2596
2589
  renderDropzone,
2597
- ...omit(props, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab']),
2590
+ ...omit(props, stylesToRemove, ['cfHyperlink', 'cfOpenInNewTab', 'cfSsrClassName']),
2598
2591
  ...(definition.children ? { children: renderDropzone(node) } : {}),
2599
2592
  };
2600
2593
  return { componentProps, wrapperProps };