@lumx/react 4.22.1-next.3 → 4.23.0-next.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.
Files changed (3) hide show
  1. package/index.js +32 -5
  2. package/index.js.map +1 -1
  3. package/package.json +3 -3
package/index.js CHANGED
@@ -2760,20 +2760,47 @@ const useOverflowTooltipLabel = content => {
2760
2760
  */
2761
2761
  const isComponentType = type => node => /*#__PURE__*/React__default.isValidElement(node) && node.type === type;
2762
2762
 
2763
- /** Force wrap spaces around icons to make sure they are never stuck against text. */
2764
- function wrapChildrenIconWithSpaces(children) {
2763
+ /** Whether the node is a text that already ends with a space (a normal space or a non breaking space). */
2764
+ const endsWithSpace = node => typeof node === 'string' && /\s$/.test(node);
2765
+
2766
+ /** Whether the node is a text that already starts with a space (a normal space or a non breaking space). */
2767
+ const startsWithSpace = node => typeof node === 'string' && /^\s/.test(node);
2768
+
2769
+ /**
2770
+ * Recursively wrap icons with spaces.
2771
+ *
2772
+ * `isFirst`/`isLast` tell whether the given children are at the very start/end of the whole
2773
+ * children tree, so that an icon on an edge is detected even when nested in a fragment or element.
2774
+ */
2775
+ function wrap(children, isFirst, isLast) {
2765
2776
  if (children === null || children === undefined) return undefined;
2766
- return Children.toArray(children).flatMap(child => {
2777
+ const childArray = Children.toArray(children);
2778
+ return childArray.flatMap((child, index) => {
2779
+ const childIsFirst = isFirst && index === 0;
2780
+ const childIsLast = isLast && index === childArray.length - 1;
2767
2781
  if (isComponentType(Icon)(child)) {
2768
- return [' ', child, ' '];
2782
+ // No space on the edges of the text: the icon is not stuck against anything there.
2783
+ // No space either when the adjacent text already provides one.
2784
+ const spaceBefore = !childIsFirst && !endsWithSpace(childArray[index - 1]);
2785
+ const spaceAfter = !childIsLast && !startsWithSpace(childArray[index + 1]);
2786
+ return [...(spaceBefore ? [' '] : []), child, ...(spaceAfter ? [' '] : [])];
2769
2787
  }
2770
2788
  if (/*#__PURE__*/React__default.isValidElement(child) && child.props && typeof child.props === 'object' && 'children' in child.props) {
2771
- return /*#__PURE__*/React__default.cloneElement(child, undefined, wrapChildrenIconWithSpaces(child.props.children));
2789
+ return /*#__PURE__*/React__default.cloneElement(child, undefined, wrap(child.props.children, childIsFirst, childIsLast));
2772
2790
  }
2773
2791
  return child;
2774
2792
  });
2775
2793
  }
2776
2794
 
2795
+ /**
2796
+ * Force wrap spaces around icons to make sure they are never stuck against text.
2797
+ * Icons at the very start or end of the text are not padded on that side.
2798
+ * No space is added on a side where the adjacent text already provides one.
2799
+ */
2800
+ function wrapChildrenIconWithSpaces(children) {
2801
+ return wrap(children, true, true);
2802
+ }
2803
+
2777
2804
  /**
2778
2805
  * Defines the props of the component.
2779
2806
  */