@chayns-components/core 5.0.0-beta.362 → 5.0.0-beta.363

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.
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _react = _interopRequireWildcard(require("react"));
8
+ var _truncation = require("../../utils/truncation");
8
9
  var _Truncation = require("./Truncation.styles");
9
10
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
10
11
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -19,7 +20,11 @@ const Truncation = _ref => {
19
20
  const [isOpen, setIsOpen] = (0, _react.useState)(false);
20
21
  const [showClamp, setShowClamp] = (0, _react.useState)(true);
21
22
  const [newCollapsedHeight, setNewCollapsedHeight] = (0, _react.useState)(collapsedHeight);
23
+ const [originalHeight, setOriginalHeight] = (0, _react.useState)(0);
24
+ const [shouldShowCollapsedElement, setShouldShowCollapsedElement] = (0, _react.useState)(true);
22
25
  const pseudoChildrenRef = (0, _react.useRef)(null);
26
+ const childrenRef = (0, _react.useRef)(null);
27
+ const originalChildrenRef = (0, _react.useRef)(null);
23
28
 
24
29
  // Changes the state of the truncation
25
30
  const handleClampClick = (0, _react.useCallback)(event => {
@@ -30,63 +35,64 @@ const Truncation = _ref => {
30
35
  return !current;
31
36
  });
32
37
  }, [onChange]);
38
+ const handleAnimationEnd = (0, _react.useCallback)(() => {
39
+ if (!isOpen) {
40
+ setShouldShowCollapsedElement(true);
41
+ } else {
42
+ setShouldShowCollapsedElement(false);
43
+ }
44
+ }, [isOpen]);
33
45
  (0, _react.useEffect)(() => {
34
46
  if (!pseudoChildrenRef.current) {
35
47
  return;
36
48
  }
37
- let summedHeight = 0;
38
- const elementChildren = pseudoChildrenRef.current.children;
39
- if (!elementChildren[0]) {
40
- return;
41
- }
42
- const element = Array.from(elementChildren[0].children);
43
- const heights = element.map((child, index) => {
44
- const computedStyle = window.getComputedStyle(child);
45
- const marginTop = parseFloat(computedStyle.marginTop);
46
- const marginBottom = parseFloat(computedStyle.marginBottom);
47
-
48
- // Höhe des Elements inklusive Margin berechnen
49
- const totalHeight = child.clientHeight + marginTop + marginBottom;
50
- return {
51
- index,
52
- height: totalHeight
53
- };
54
- });
55
- let isSummedHeightCalculated = false;
56
- heights.forEach(_ref2 => {
57
- let {
58
- height
59
- } = _ref2;
60
- if (summedHeight + height <= collapsedHeight && !isSummedHeightCalculated) {
61
- summedHeight += height;
62
- return;
63
- }
64
- isSummedHeightCalculated = true;
65
- });
66
- setNewCollapsedHeight(summedHeight > 0 ? summedHeight : collapsedHeight);
49
+ setOriginalHeight(pseudoChildrenRef.current.offsetHeight);
50
+ (0, _truncation.truncateElement)(pseudoChildrenRef.current, collapsedHeight);
51
+ setNewCollapsedHeight(pseudoChildrenRef.current.offsetHeight);
67
52
  }, [collapsedHeight, pseudoChildrenRef]);
68
53
 
69
54
  // Checks if the clamp should be shown
70
55
  (0, _react.useEffect)(() => {
71
56
  if (pseudoChildrenRef.current) {
72
- setShowClamp(pseudoChildrenRef.current.offsetHeight > newCollapsedHeight);
57
+ setShowClamp(originalHeight > newCollapsedHeight);
58
+ }
59
+ }, [collapsedHeight, newCollapsedHeight, originalHeight]);
60
+ (0, _react.useEffect)(() => {
61
+ if (childrenRef.current && pseudoChildrenRef.current && originalChildrenRef.current) {
62
+ if (shouldShowCollapsedElement && !isOpen) {
63
+ while (childrenRef.current.firstChild) {
64
+ childrenRef.current.removeChild(childrenRef.current.firstChild);
65
+ }
66
+ childrenRef.current.appendChild(pseudoChildrenRef.current);
67
+ childrenRef.current.children[0].style.visibility = 'visible';
68
+ } else {
69
+ while (childrenRef.current.firstChild) {
70
+ childrenRef.current.removeChild(childrenRef.current.firstChild);
71
+ }
72
+ childrenRef.current.appendChild(originalChildrenRef.current);
73
+ childrenRef.current.children[0].style.visibility = 'visible';
74
+ }
73
75
  }
74
- }, [collapsedHeight, newCollapsedHeight]);
76
+ }, [children, isOpen, shouldShowCollapsedElement]);
75
77
  return (0, _react.useMemo)(() => /*#__PURE__*/_react.default.createElement(_Truncation.StyledTruncation, {
76
78
  className: "beta-chayns-truncation"
77
79
  }, /*#__PURE__*/_react.default.createElement(_Truncation.StyledTruncationPseudoContent, {
78
80
  ref: pseudoChildrenRef
81
+ }, children), /*#__PURE__*/_react.default.createElement(_Truncation.StyledTruncationPseudoContent, {
82
+ ref: originalChildrenRef
79
83
  }, children), /*#__PURE__*/_react.default.createElement(_Truncation.StyledMotionTruncationContent, {
80
84
  animate: {
81
- height: isOpen ? 'auto' : newCollapsedHeight
85
+ height: isOpen ? originalHeight : newCollapsedHeight
82
86
  },
83
87
  initial: false,
84
88
  transition: {
85
89
  type: 'tween'
86
- }
87
- }, children), showClamp && /*#__PURE__*/_react.default.createElement(_Truncation.StyledTruncationClamp, {
90
+ },
91
+ onAnimationComplete: handleAnimationEnd,
92
+ ref: childrenRef
93
+ }), showClamp && /*#__PURE__*/_react.default.createElement(_Truncation.StyledTruncationClamp, {
88
94
  onClick: handleClampClick
89
- }, isOpen ? lessLabel : moreLabel)), [children, handleClampClick, isOpen, lessLabel, moreLabel, newCollapsedHeight, showClamp]);
95
+ }, isOpen ? lessLabel : moreLabel)), [children, handleAnimationEnd, handleClampClick, isOpen, lessLabel, moreLabel, newCollapsedHeight, originalHeight, showClamp]);
90
96
  };
91
97
  var _default = exports.default = Truncation;
92
98
  //# sourceMappingURL=Truncation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Truncation.js","names":["_react","_interopRequireWildcard","require","_Truncation","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","Truncation","_ref","collapsedHeight","moreLabel","lessLabel","onChange","children","isOpen","setIsOpen","useState","showClamp","setShowClamp","newCollapsedHeight","setNewCollapsedHeight","pseudoChildrenRef","useRef","handleClampClick","useCallback","event","current","useEffect","summedHeight","elementChildren","element","Array","from","heights","map","child","index","computedStyle","window","getComputedStyle","marginTop","parseFloat","marginBottom","totalHeight","clientHeight","height","isSummedHeightCalculated","forEach","_ref2","offsetHeight","useMemo","createElement","StyledTruncation","className","StyledTruncationPseudoContent","ref","StyledMotionTruncationContent","animate","initial","transition","type","StyledTruncationClamp","onClick","_default","exports"],"sources":["../../../src/components/truncation/Truncation.tsx"],"sourcesContent":["import React, {\n FC,\n MouseEvent,\n MouseEventHandler,\n ReactElement,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport {\n StyledMotionTruncationContent,\n StyledTruncation,\n StyledTruncationClamp,\n StyledTruncationPseudoContent,\n} from './Truncation.styles';\n\nexport type TruncationProps = {\n /**\n * The elements that should be expanding or collapsing.\n */\n children: ReactElement;\n /**\n * The height of the children Element in it`s collapsed state.\n */\n collapsedHeight?: number;\n /**\n * A text that should be displayed if the content is expanded.\n */\n lessLabel?: string;\n /**\n * A text that should be displayed if the content is collapsed.\n */\n moreLabel?: string;\n /**\n * Function to be executed when the component is expanding or collapsing.\n */\n onChange?: (event: MouseEvent<HTMLAnchorElement>, isOpen: boolean) => void;\n};\n\nconst Truncation: FC<TruncationProps> = ({\n collapsedHeight = 150,\n moreLabel = 'Mehr',\n lessLabel = 'Weniger',\n onChange,\n children,\n}) => {\n const [isOpen, setIsOpen] = useState(false);\n const [showClamp, setShowClamp] = useState(true);\n const [newCollapsedHeight, setNewCollapsedHeight] = useState(collapsedHeight);\n\n const pseudoChildrenRef = useRef<HTMLDivElement>(null);\n\n // Changes the state of the truncation\n const handleClampClick = useCallback<MouseEventHandler<HTMLAnchorElement>>(\n (event) => {\n setIsOpen((current) => {\n if (typeof onChange === 'function') {\n onChange(event, !current);\n }\n\n return !current;\n });\n },\n [onChange],\n );\n\n useEffect(() => {\n if (!pseudoChildrenRef.current) {\n return;\n }\n\n let summedHeight = 0;\n\n const elementChildren = pseudoChildrenRef.current.children;\n\n if (!elementChildren[0]) {\n return;\n }\n\n const element = Array.from(elementChildren[0].children);\n\n const heights = element.map((child, index) => {\n const computedStyle = window.getComputedStyle(child);\n const marginTop = parseFloat(computedStyle.marginTop);\n const marginBottom = parseFloat(computedStyle.marginBottom);\n\n // Höhe des Elements inklusive Margin berechnen\n const totalHeight = child.clientHeight + marginTop + marginBottom;\n\n return {\n index,\n height: totalHeight,\n };\n });\n\n let isSummedHeightCalculated = false;\n\n heights.forEach(({ height }) => {\n if (summedHeight + height <= collapsedHeight && !isSummedHeightCalculated) {\n summedHeight += height;\n\n return;\n }\n\n isSummedHeightCalculated = true;\n });\n\n setNewCollapsedHeight(summedHeight > 0 ? summedHeight : collapsedHeight);\n }, [collapsedHeight, pseudoChildrenRef]);\n\n // Checks if the clamp should be shown\n useEffect(() => {\n if (pseudoChildrenRef.current) {\n setShowClamp(pseudoChildrenRef.current.offsetHeight > newCollapsedHeight);\n }\n }, [collapsedHeight, newCollapsedHeight]);\n\n return useMemo(\n () => (\n <StyledTruncation className=\"beta-chayns-truncation\">\n <StyledTruncationPseudoContent ref={pseudoChildrenRef}>\n {children}\n </StyledTruncationPseudoContent>\n <StyledMotionTruncationContent\n animate={{ height: isOpen ? 'auto' : newCollapsedHeight }}\n initial={false}\n transition={{ type: 'tween' }}\n >\n {children}\n </StyledMotionTruncationContent>\n {showClamp && (\n <StyledTruncationClamp onClick={handleClampClick}>\n {isOpen ? lessLabel : moreLabel}\n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6BAA6B;IAACC,GAAG,EAAElC;EAAkB,GACjDR,QAC0B,CAAC,eAChC/B,MAAA,CAAAU,OAAA,CAAA2D,aAAA,CAAClE,WAAA,CAAAuE,6BAA6B;IAC1BC,OAAO,EAAE;MAAEZ,MAAM,EAAE/B,MAAM,GAAG,MAAM,GAAGK;IAAmB,CAAE;IAC1DuC,OAAO,EAAE,KAAM;IACfC,UAAU,EAAE;MAAEC,IAAI,EAAE;IAAQ;EAAE,GAE7B/C,QAC0B,CAAC,EAC/BI,SAAS,iBACNnC,MAAA,CAAAU,OAAA,CAAA2D,aAAA,CAAClE,WAAA,CAAA4E,qBAAqB;IAACC,OAAO,EAAEvC;EAAiB,GAC5CT,MAAM,GAAGH,SAAS,GAAGD,SACH,CAEb,CACrB,EACD,CAACG,QAAQ,EAAEU,gBAAgB,EAAET,MAAM,EAAEH,SAAS,EAAED,SAAS,EAAES,kBAAkB,EAAEF,SAAS,CAC5F,CAAC;AACL,CAAC;AAAC,IAAA8C,QAAA,GAAAC,OAAA,CAAAxE,OAAA,GAEae,UAAU"}
1
+ {"version":3,"file":"Truncation.js","names":["_react","_interopRequireWildcard","require","_truncation","_Truncation","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","Truncation","_ref","collapsedHeight","moreLabel","lessLabel","onChange","children","isOpen","setIsOpen","useState","showClamp","setShowClamp","newCollapsedHeight","setNewCollapsedHeight","originalHeight","setOriginalHeight","shouldShowCollapsedElement","setShouldShowCollapsedElement","pseudoChildrenRef","useRef","childrenRef","originalChildrenRef","handleClampClick","useCallback","event","current","handleAnimationEnd","useEffect","offsetHeight","truncateElement","firstChild","removeChild","appendChild","style","visibility","useMemo","createElement","StyledTruncation","className","StyledTruncationPseudoContent","ref","StyledMotionTruncationContent","animate","height","initial","transition","type","onAnimationComplete","StyledTruncationClamp","onClick","_default","exports"],"sources":["../../../src/components/truncation/Truncation.tsx"],"sourcesContent":["import React, {\n FC,\n MouseEvent,\n MouseEventHandler,\n ReactElement,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { truncateElement } from '../../utils/truncation';\nimport {\n StyledMotionTruncationContent,\n StyledTruncation,\n StyledTruncationClamp,\n StyledTruncationPseudoContent,\n} from './Truncation.styles';\n\nexport type TruncationProps = {\n /**\n * The elements that should be expanding or collapsing.\n */\n children: ReactElement;\n /**\n * The height of the children Element in it`s collapsed state.\n */\n collapsedHeight?: number;\n /**\n * A text that should be displayed if the content is expanded.\n */\n lessLabel?: string;\n /**\n * A text that should be displayed if the content is collapsed.\n */\n moreLabel?: string;\n /**\n * Function to be executed when the component is expanding or collapsing.\n */\n onChange?: (event: MouseEvent<HTMLAnchorElement>, isOpen: boolean) => void;\n};\n\nconst Truncation: FC<TruncationProps> = ({\n collapsedHeight = 150,\n moreLabel = 'Mehr',\n lessLabel = 'Weniger',\n onChange,\n children,\n}) => {\n const [isOpen, setIsOpen] = useState(false);\n const [showClamp, setShowClamp] = useState(true);\n const [newCollapsedHeight, setNewCollapsedHeight] = useState(collapsedHeight);\n const [originalHeight, setOriginalHeight] = useState(0);\n const [shouldShowCollapsedElement, setShouldShowCollapsedElement] = useState(true);\n\n const pseudoChildrenRef = useRef<HTMLDivElement>(null);\n const childrenRef = useRef<HTMLDivElement>(null);\n const originalChildrenRef = useRef<HTMLDivElement>(null);\n\n // Changes the state of the truncation\n const handleClampClick = useCallback<MouseEventHandler<HTMLAnchorElement>>(\n (event) => {\n setIsOpen((current) => {\n if (typeof onChange === 'function') {\n onChange(event, !current);\n }\n\n return !current;\n });\n },\n [onChange],\n );\n\n const handleAnimationEnd = useCallback(() => {\n if (!isOpen) {\n setShouldShowCollapsedElement(true);\n } else {\n setShouldShowCollapsedElement(false);\n }\n }, [isOpen]);\n\n useEffect(() => {\n if (!pseudoChildrenRef.current) {\n return;\n }\n\n setOriginalHeight(pseudoChildrenRef.current.offsetHeight);\n\n truncateElement(pseudoChildrenRef.current, collapsedHeight);\n\n setNewCollapsedHeight(pseudoChildrenRef.current.offsetHeight);\n }, [collapsedHeight, pseudoChildrenRef]);\n\n // Checks if the clamp should be shown\n useEffect(() => {\n if (pseudoChildrenRef.current) {\n setShowClamp(originalHeight > newCollapsedHeight);\n }\n }, [collapsedHeight, newCollapsedHeight, originalHeight]);\n\n useEffect(() => {\n if (childrenRef.current && pseudoChildrenRef.current && originalChildrenRef.current) {\n if (shouldShowCollapsedElement && !isOpen) {\n while (childrenRef.current.firstChild) {\n childrenRef.current.removeChild(childrenRef.current.firstChild);\n }\n\n childrenRef.current.appendChild(pseudoChildrenRef.current);\n\n (childrenRef.current.children[0] as HTMLDivElement).style.visibility = 'visible';\n } else {\n while (childrenRef.current.firstChild) {\n childrenRef.current.removeChild(childrenRef.current.firstChild);\n }\n\n childrenRef.current.appendChild(originalChildrenRef.current);\n\n (childrenRef.current.children[0] as HTMLDivElement).style.visibility = 'visible';\n }\n }\n }, [children, isOpen, shouldShowCollapsedElement]);\n\n return useMemo(\n () => (\n <StyledTruncation className=\"beta-chayns-truncation\">\n <StyledTruncationPseudoContent ref={pseudoChildrenRef}>\n {children}\n </StyledTruncationPseudoContent>\n <StyledTruncationPseudoContent ref={originalChildrenRef}>\n {children}\n </StyledTruncationPseudoContent>\n <StyledMotionTruncationContent\n animate={{ height: isOpen ? originalHeight : newCollapsedHeight }}\n initial={false}\n transition={{ type: 'tween' }}\n onAnimationComplete={handleAnimationEnd}\n ref={childrenRef}\n />\n {showClamp && (\n <StyledTruncationClamp onClick={handleClampClick}>\n {isOpen ? lessLabel : moreLabel}\n </StyledTruncationClamp>\n )}\n </StyledTruncation>\n ),\n [\n children,\n handleAnimationEnd,\n handleClampClick,\n isOpen,\n lessLabel,\n moreLabel,\n newCollapsedHeight,\n originalHeight,\n showClamp,\n ],\n );\n};\n\nexport default Truncation;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAA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GAEae,UAAU"}
@@ -0,0 +1 @@
1
+ export declare const truncateElement: (element: HTMLElement, referenceHeight: number) => void;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.truncateElement = void 0;
7
+ const doesElementOverflow = (element, referenceHeight) => element.scrollHeight > referenceHeight;
8
+ const doesElementHasOnlyText = element => {
9
+ // Check if element has no child elements.
10
+ if (element.children.length === 0) {
11
+ // If element has text (not empty), it is only text.
12
+ return element.textContent !== '';
13
+ }
14
+ // Element has child elements or no text, so it's not only text.
15
+ return false;
16
+ };
17
+ const removeLastLeafElement = element => {
18
+ // remove last element of html element where the last element is a leaf element and its content is a string
19
+ const lastChild = element.lastElementChild;
20
+ if (lastChild && !doesElementHasOnlyText(lastChild) && lastChild.hasChildNodes()) {
21
+ removeLastLeafElement(lastChild);
22
+ } else if (lastChild && doesElementHasOnlyText(lastChild) && lastChild.textContent && lastChild.textContent.length > 25) {
23
+ lastChild.textContent = `${lastChild.textContent.substring(0, lastChild.textContent.length - 25)} ...`;
24
+ } else if (lastChild) {
25
+ element.removeChild(lastChild);
26
+ }
27
+ };
28
+ const truncateElement = (element, referenceHeight) => {
29
+ while (doesElementOverflow(element, referenceHeight)) {
30
+ removeLastLeafElement(element);
31
+ }
32
+ };
33
+ exports.truncateElement = truncateElement;
34
+ //# sourceMappingURL=truncation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncation.js","names":["doesElementOverflow","element","referenceHeight","scrollHeight","doesElementHasOnlyText","children","length","textContent","removeLastLeafElement","lastChild","lastElementChild","hasChildNodes","substring","removeChild","truncateElement","exports"],"sources":["../../src/utils/truncation.ts"],"sourcesContent":["const doesElementOverflow = (element: HTMLElement, referenceHeight: number): boolean =>\n element.scrollHeight > referenceHeight;\n\nconst doesElementHasOnlyText = (element: HTMLElement): boolean => {\n // Check if element has no child elements.\n if (element.children.length === 0) {\n // If element has text (not empty), it is only text.\n return element.textContent !== '';\n }\n // Element has child elements or no text, so it's not only text.\n return false;\n};\n\nconst removeLastLeafElement = (element: HTMLElement) => {\n // remove last element of html element where the last element is a leaf element and its content is a string\n const lastChild: Element | null = element.lastElementChild;\n if (\n lastChild &&\n !doesElementHasOnlyText(lastChild as HTMLElement) &&\n lastChild.hasChildNodes()\n ) {\n removeLastLeafElement(lastChild as HTMLElement);\n } else if (\n lastChild &&\n doesElementHasOnlyText(lastChild as HTMLElement) &&\n lastChild.textContent &&\n lastChild.textContent.length > 25\n ) {\n lastChild.textContent = `${lastChild.textContent.substring(\n 0,\n lastChild.textContent.length - 25,\n )} ...`;\n } else if (lastChild) {\n element.removeChild(lastChild);\n }\n};\nexport const truncateElement = (element: HTMLElement, referenceHeight: number) => {\n while (doesElementOverflow(element, referenceHeight)) {\n removeLastLeafElement(element);\n }\n};\n"],"mappings":";;;;;;AAAA,MAAMA,mBAAmB,GAAGA,CAACC,OAAoB,EAAEC,eAAuB,KACtED,OAAO,CAACE,YAAY,GAAGD,eAAe;AAE1C,MAAME,sBAAsB,GAAIH,OAAoB,IAAc;EAC9D;EACA,IAAIA,OAAO,CAACI,QAAQ,CAACC,MAAM,KAAK,CAAC,EAAE;IAC/B;IACA,OAAOL,OAAO,CAACM,WAAW,KAAK,EAAE;EACrC;EACA;EACA,OAAO,KAAK;AAChB,CAAC;AAED,MAAMC,qBAAqB,GAAIP,OAAoB,IAAK;EACpD;EACA,MAAMQ,SAAyB,GAAGR,OAAO,CAACS,gBAAgB;EAC1D,IACID,SAAS,IACT,CAACL,sBAAsB,CAACK,SAAwB,CAAC,IACjDA,SAAS,CAACE,aAAa,CAAC,CAAC,EAC3B;IACEH,qBAAqB,CAACC,SAAwB,CAAC;EACnD,CAAC,MAAM,IACHA,SAAS,IACTL,sBAAsB,CAACK,SAAwB,CAAC,IAChDA,SAAS,CAACF,WAAW,IACrBE,SAAS,CAACF,WAAW,CAACD,MAAM,GAAG,EAAE,EACnC;IACEG,SAAS,CAACF,WAAW,GAAI,GAAEE,SAAS,CAACF,WAAW,CAACK,SAAS,CACtD,CAAC,EACDH,SAAS,CAACF,WAAW,CAACD,MAAM,GAAG,EACnC,CAAE,MAAK;EACX,CAAC,MAAM,IAAIG,SAAS,EAAE;IAClBR,OAAO,CAACY,WAAW,CAACJ,SAAS,CAAC;EAClC;AACJ,CAAC;AACM,MAAMK,eAAe,GAAGA,CAACb,OAAoB,EAAEC,eAAuB,KAAK;EAC9E,OAAOF,mBAAmB,CAACC,OAAO,EAAEC,eAAe,CAAC,EAAE;IAClDM,qBAAqB,CAACP,OAAO,CAAC;EAClC;AACJ,CAAC;AAACc,OAAA,CAAAD,eAAA,GAAAA,eAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chayns-components/core",
3
- "version": "5.0.0-beta.362",
3
+ "version": "5.0.0-beta.363",
4
4
  "description": "A set of beautiful React components for developing your own applications with chayns.",
5
5
  "keywords": [
6
6
  "chayns",
@@ -70,5 +70,5 @@
70
70
  "publishConfig": {
71
71
  "access": "public"
72
72
  },
73
- "gitHead": "62bb4ee52839e065d0ce342a80648b56e371bfe0"
73
+ "gitHead": "c50d2de2bcafd9742450849f4dc09e5e26ab0925"
74
74
  }