@semcore/ellipsis 1.0.0-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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ CHANGELOG.md standards are inspired by [keepachangelog.com](https://keepachangelog.com/en/1.0.0/).
4
+
5
+ ## [1.0.0] - 2022-10-14
6
+
7
+ ### Added
8
+
9
+ - Initial release
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @semcore/ellipsis
2
+
3
+ [![version](https://img.shields.io/npm/v/@semcore/ellipsis.svg)](https://www.npmjs.com/@semcore/ellipsis)
4
+ [![downloads](https://img.shields.io/npm/dt/@semcore/ellipsis.svg)](https://www.npmjs.com/package/@semcore/ellipsis)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/semrush/intergalactic/blob/master/LICENSE)
6
+
7
+ > This component is part of the Intergalactic design system
8
+
9
+ [//]: # '### 📖 [Component documentation](https://developer.semrush.com/intergalactic/components/ellipsis/)'
10
+
11
+ ### 🏠 [Design system](https://developer.semrush.com/intergalactic/)
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ npm install @semcore/ui
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```jsx
22
+ import Ellipsis from '@semcore/ui/ellipsis';
23
+ ```
24
+
25
+ <details>
26
+ <summary>Alternative installation and use</summary>
27
+
28
+ **We do not recommend this usage path due to possible dependency and update issues.**
29
+
30
+ ### Install
31
+
32
+ You can only install one package from the design system
33
+
34
+ ```sh
35
+ npm install @semcore/ellipsis @semcore/core
36
+ ```
37
+
38
+ `@semcore/core` - _is the basic package by which we create our components, and it contains all of the common logic
39
+ of the components that is discussed below. There should only be one version of the package in the project._
40
+
41
+ </details>
42
+
43
+ ### Usage
44
+
45
+ You can use the package the same way but without `/ui/` in the import path.
46
+
47
+ ```jsx
48
+ import Ellipsis from '@semcore/ellipsis';
49
+ ```
50
+
51
+ ## 👤 Author
52
+
53
+ [UI-kit team](https://github.com/semrush/intergalactic/blob/master/MAINTAINERS) and [other ❤️](https://github.com/semrush/intergalactic/graphs/contributors)
54
+
55
+ ## 🤝 Contributing
56
+
57
+ Contributions, issues and feature requests are welcome!
58
+
59
+ Feel free to check [issues page](https://github.com/semrush/intergalactic/issues). You can also take a look at the [contributing guide](https://github.com/semrush/intergalactic/blob/master/CONTRIBUTING.md).
60
+
61
+ ## Show your support
62
+
63
+ Give a ⭐️ if this project helped you!
64
+
65
+ ## 📝 License
66
+
67
+ This project is [MIT](https://github.com/semrush/intergalactic/blob/master/LICENSE) licensed.
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports["default"] = void 0;
11
+
12
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
+
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
+
16
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
+
18
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
19
+
20
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
21
+
22
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
23
+
24
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
25
+
26
+ var _core = _interopRequireWildcard(require("@semcore/core"));
27
+
28
+ var _react = _interopRequireWildcard(require("react"));
29
+
30
+ var _tooltip = _interopRequireDefault(require("@semcore/tooltip"));
31
+
32
+ var _flexBox = require("@semcore/flex-box");
33
+
34
+ var _useResizeObserver = require("./useResizeObserver");
35
+
36
+ var _reactToText = _interopRequireDefault(require("@semcore/utils/lib/reactToText"));
37
+
38
+ var _getOriginChildren = _interopRequireDefault(require("@semcore/utils/lib/getOriginChildren"));
39
+
40
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
41
+
42
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
43
+
44
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
45
+
46
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
47
+
48
+ /*__reshadow-styles__:"./style/ellipsis.shadow.css"*/
49
+ var style = (
50
+ /*__reshadow_css_start__*/
51
+ _core.sstyled.insert(
52
+ /*__inner_css_start__*/
53
+ ":root {\n\n /* DEPRECATED START\n Deprecated variables should never be used in\n components styles but preserved for backward\n compatibility if they used in projects\n like `<Badge bg=\"red\">alpha</Badge>`\n */\n /* DEPRECATED END */\n}\n\n.___SContainer_1b3bb_gg_,\n.___SContainerMiddle_1b3bb_gg_ {\n overflow: hidden;\n}\n\n.___SBeginning_1b3bb_gg_,\n.___SEllipsis_1b3bb_gg_ {\n overflow: hidden;\n display: inline-block;\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n\n.___SEllipsis_1b3bb_gg_.__maxLine_1b3bb_gg_ {\n display: -webkit-box;\n -webkit-line-clamp: var(--maxLine_1b3bb);\n -webkit-box-orient: vertical;\n overflow: hidden;\n white-space: normal;\n}\n\n.___STail_1b3bb_gg_ {\n white-space: nowrap;\n}\n"
54
+ /*__inner_css_end__*/
55
+ , "1b3bb_gg_")
56
+ /*__reshadow_css_end__*/
57
+ , {
58
+ "__SContainer": "___SContainer_1b3bb_gg_",
59
+ "__SContainerMiddle": "___SContainerMiddle_1b3bb_gg_",
60
+ "__SBeginning": "___SBeginning_1b3bb_gg_",
61
+ "__SEllipsis": "___SEllipsis_1b3bb_gg_",
62
+ "_maxLine": "__maxLine_1b3bb_gg_",
63
+ "--maxLine": "--maxLine_1b3bb",
64
+ "__STail": "___STail_1b3bb_gg_"
65
+ });
66
+
67
+ var RootEllipsis = /*#__PURE__*/function (_Component) {
68
+ (0, _inherits2["default"])(RootEllipsis, _Component);
69
+
70
+ var _super = _createSuper(RootEllipsis);
71
+
72
+ function RootEllipsis() {
73
+ (0, _classCallCheck2["default"])(this, RootEllipsis);
74
+ return _super.apply(this, arguments);
75
+ }
76
+
77
+ (0, _createClass2["default"])(RootEllipsis, [{
78
+ key: "render",
79
+ value: function render() {
80
+ var _ref3;
81
+
82
+ var SEllipsis = this.Root;
83
+ var SContainer = _flexBox.Flex;
84
+ var _this$asProps = this.asProps,
85
+ styles = _this$asProps.styles,
86
+ Children = _this$asProps.Children,
87
+ maxLine = _this$asProps.maxLine,
88
+ tooltip = _this$asProps.tooltip,
89
+ trim = _this$asProps.trim,
90
+ containerRect = _this$asProps.containerRect,
91
+ containerRef = _this$asProps.containerRef;
92
+ var text = (0, _reactToText["default"])((0, _getOriginChildren["default"])(Children));
93
+
94
+ if (trim === 'middle') {
95
+ var _ref;
96
+
97
+ return _ref = (0, _core.sstyled)(styles), /*#__PURE__*/_react["default"].createElement(EllipsisMiddle, _ref.cn("EllipsisMiddle", {
98
+ "text": text,
99
+ "styles": styles,
100
+ "tooltip": tooltip,
101
+ "containerRect": containerRect,
102
+ "containerRef": containerRef
103
+ }));
104
+ }
105
+
106
+ if (tooltip) {
107
+ var _ref2;
108
+
109
+ return _ref2 = (0, _core.sstyled)(styles), /*#__PURE__*/_react["default"].createElement(SContainer, _ref2.cn("SContainer", {
110
+ "interaction": "hover",
111
+ "title": text,
112
+ "tag": _tooltip["default"]
113
+ }), /*#__PURE__*/_react["default"].createElement(SEllipsis, _ref2.cn("SEllipsis", {
114
+ "use:maxLine": maxLine,
115
+ "render": "div",
116
+ "tag": "div"
117
+ }), /*#__PURE__*/_react["default"].createElement(Children, _ref2.cn("Children", {}))));
118
+ }
119
+
120
+ return _ref3 = (0, _core.sstyled)(styles), /*#__PURE__*/_react["default"].createElement(SEllipsis, _ref3.cn("SEllipsis", {
121
+ "use:maxLine": maxLine,
122
+ "render": "div",
123
+ "tag": "div"
124
+ }), /*#__PURE__*/_react["default"].createElement(Children, _ref3.cn("Children", {})));
125
+ }
126
+ }]);
127
+ return RootEllipsis;
128
+ }(_core.Component);
129
+
130
+ (0, _defineProperty2["default"])(RootEllipsis, "displayName", 'Ellipsis');
131
+ (0, _defineProperty2["default"])(RootEllipsis, "style", style);
132
+ (0, _defineProperty2["default"])(RootEllipsis, "defaultProps", {
133
+ trim: 'end',
134
+ tooltip: true
135
+ });
136
+
137
+ var EllipsisMiddle = function EllipsisMiddle(props) {
138
+ var styles = props.styles,
139
+ text = props.text,
140
+ tooltip = props.tooltip,
141
+ containerRect = props.containerRect,
142
+ containerRef = props.containerRef;
143
+ var resizeElement = (0, _react.useRef)(null);
144
+
145
+ var _useState = (0, _react.useState)({
146
+ fontSize: '14',
147
+ symbolWidth: 0
148
+ }),
149
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
150
+ dimension = _useState2[0],
151
+ setDimension = _useState2[1];
152
+
153
+ var blockWidth = (0, _useResizeObserver.useResizeObserver)(resizeElement, containerRect).width;
154
+ (0, _react.useLayoutEffect)(function () {
155
+ var _containerRef$current;
156
+
157
+ var dateSpan = document.createElement('temporary-block');
158
+ dateSpan.setAttribute('style', "fontSize: ".concat(dimension.fontSize, "px"));
159
+ dateSpan.innerHTML = 'a';
160
+ document.body.appendChild(dateSpan);
161
+ var rect = dateSpan.getBoundingClientRect();
162
+ dateSpan.remove();
163
+ setDimension({
164
+ fontSize: window.getComputedStyle((_containerRef$current = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current) !== null && _containerRef$current !== void 0 ? _containerRef$current : resizeElement.current, null).getPropertyValue('font-size'),
165
+ symbolWidth: rect.width
166
+ });
167
+ }, []);
168
+ var STail = 'span';
169
+ var SBeginning = 'span';
170
+ var SContainerMiddle = _flexBox.Flex;
171
+ var displayedSymbols = (0, _react.useMemo)(function () {
172
+ return Math.round(blockWidth / dimension.symbolWidth);
173
+ }, [blockWidth, dimension.symbolWidth]);
174
+
175
+ if (tooltip) {
176
+ var _ref4;
177
+
178
+ return _ref4 = (0, _core.sstyled)(styles), /*#__PURE__*/_react["default"].createElement(SContainerMiddle, _ref4.cn("SContainerMiddle", {
179
+ "interaction": "hover",
180
+ "title": text,
181
+ "ref": containerRef !== null && containerRef !== void 0 ? containerRef : resizeElement,
182
+ "tag": _tooltip["default"]
183
+ }), /*#__PURE__*/_react["default"].createElement(SBeginning, _ref4.cn("SBeginning", {}), text.substring(0, text.length - displayedSymbols / 2 - 1)), /*#__PURE__*/_react["default"].createElement(STail, _ref4.cn("STail", {}), text.substring(text.length - displayedSymbols / 2 - 1)));
184
+ } else {
185
+ var _ref5;
186
+
187
+ return _ref5 = (0, _core.sstyled)(styles), /*#__PURE__*/_react["default"].createElement(SContainerMiddle, _ref5.cn("SContainerMiddle", {
188
+ "ref": containerRef !== null && containerRef !== void 0 ? containerRef : resizeElement
189
+ }), /*#__PURE__*/_react["default"].createElement(SBeginning, _ref5.cn("SBeginning", {}), text.substring(0, text.length - displayedSymbols / 2 - 1)), /*#__PURE__*/_react["default"].createElement(STail, _ref5.cn("STail", {}), text.substring(text.length - displayedSymbols / 2 - 1)));
190
+ }
191
+ };
192
+
193
+ var Ellipsis = (0, _core["default"])(RootEllipsis);
194
+ var _default = Ellipsis;
195
+ exports["default"] = _default;
196
+ //# sourceMappingURL=Ellipsis.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Ellipsis.js","names":["RootEllipsis","SEllipsis","Root","SContainer","Flex","asProps","styles","Children","maxLine","tooltip","trim","containerRect","containerRef","text","reactToText","getOriginChildren","sstyled","Tooltip","Component","style","EllipsisMiddle","props","resizeElement","useRef","useState","fontSize","symbolWidth","dimension","setDimension","blockWidth","useResizeObserver","width","useLayoutEffect","dateSpan","document","createElement","setAttribute","innerHTML","body","appendChild","rect","getBoundingClientRect","remove","window","getComputedStyle","current","getPropertyValue","STail","SBeginning","SContainerMiddle","displayedSymbols","useMemo","Math","round","substring","length","Ellipsis","createComponent"],"sources":["../../src/Ellipsis.tsx"],"sourcesContent":["import React, { RefObject, useRef, useMemo, useState, useLayoutEffect } from 'react';\nimport createComponent, { Component, sstyled } from '@semcore/core';\nimport Tooltip from '@semcore/tooltip';\nimport { Flex } from '@semcore/flex-box';\nimport { useResizeObserver } from './useResizeObserver';\n\nimport style from './style/ellipsis.shadow.css';\nimport reactToText from '@semcore/utils/lib/reactToText';\nimport getOriginChildren from '@semcore/utils/lib/getOriginChildren';\n\ntype AsProps = {\n maxLine?: number;\n trim?: 'end' | 'middle';\n tooltip?: boolean;\n styles?: React.CSSProperties;\n containerRect?: { width: number };\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n};\n\ntype AsPropsMiddle = {\n text: string;\n tooltip?: boolean;\n styles?: React.CSSProperties;\n containerRect?: { width: number };\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n};\n\nclass RootEllipsis extends Component<AsProps> {\n static displayName = 'Ellipsis';\n static style = style;\n static defaultProps: AsProps = {\n trim: 'end',\n tooltip: true,\n };\n\n render() {\n const SEllipsis = this.Root;\n const SContainer = Flex;\n const { styles, Children, maxLine, tooltip, trim, containerRect, containerRef } = this.asProps;\n const text = reactToText(getOriginChildren(Children));\n\n if (trim === 'middle') {\n return sstyled(styles)(\n <EllipsisMiddle\n text={text}\n styles={styles}\n tooltip={tooltip}\n containerRect={containerRect}\n containerRef={containerRef}\n />,\n );\n }\n if (tooltip) {\n return sstyled(styles)(\n <SContainer interaction=\"hover\" title={text} tag={Tooltip}>\n <SEllipsis use:maxLine={maxLine} render=\"div\" tag=\"div\">\n <Children />\n </SEllipsis>\n </SContainer>,\n );\n }\n return sstyled(styles)(\n <SEllipsis use:maxLine={maxLine} render=\"div\" tag=\"div\">\n <Children />\n </SEllipsis>,\n );\n }\n}\n\nconst EllipsisMiddle: React.FC<AsPropsMiddle> = (props) => {\n const { styles, text, tooltip, containerRect, containerRef } = props;\n const resizeElement = useRef<RefObject<HTMLElement | null>>(null);\n const [dimension, setDimension] = useState<{ fontSize: string; symbolWidth: number }>({\n fontSize: '14',\n symbolWidth: 0,\n });\n const blockWidth = useResizeObserver(resizeElement, containerRect).width;\n\n useLayoutEffect(() => {\n const dateSpan = document.createElement('temporary-block');\n dateSpan.setAttribute('style', `fontSize: ${dimension.fontSize}px`);\n dateSpan.innerHTML = 'a';\n document.body.appendChild(dateSpan);\n const rect = dateSpan.getBoundingClientRect();\n dateSpan.remove();\n\n setDimension({\n fontSize: window\n .getComputedStyle(containerRef?.current ?? resizeElement.current, null)\n .getPropertyValue('font-size'),\n symbolWidth: rect.width,\n });\n }, []);\n\n const STail = 'span';\n const SBeginning = 'span';\n const SContainerMiddle = Flex;\n const displayedSymbols = useMemo(\n () => Math.round(blockWidth / dimension.symbolWidth),\n [blockWidth, dimension.symbolWidth],\n );\n\n if (tooltip) {\n return sstyled(styles)(\n <SContainerMiddle\n interaction=\"hover\"\n title={text}\n ref={containerRef ?? resizeElement}\n tag={Tooltip}\n >\n <SBeginning>{text.substring(0, text.length - displayedSymbols / 2 - 1)}</SBeginning>\n <STail>{text.substring(text.length - displayedSymbols / 2 - 1)}</STail>\n </SContainerMiddle>,\n ) as any;\n } else {\n return sstyled(styles)(\n <SContainerMiddle ref={containerRef ?? resizeElement}>\n <SBeginning>{text.substring(0, text.length - displayedSymbols / 2 - 1)}</SBeginning>\n <STail>{text.substring(text.length - displayedSymbols / 2 - 1)}</STail>\n </SContainerMiddle>,\n ) as any;\n }\n};\n\nconst Ellipsis = createComponent(RootEllipsis);\n\nexport default Ellipsis;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA;;AADA;;AAEA;;AACA;;AACA;;AAGA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAqBMA,Y;;;;;;;;;;;;WAQJ,kBAAS;MAAA;;MACP,IAAMC,SAAS,GAAG,KAAKC,IAAvB;MACA,IAAMC,UAAU,GAAGC,aAAnB;MACA,oBAAkF,KAAKC,OAAvF;MAAA,IAAQC,MAAR,iBAAQA,MAAR;MAAA,IAAgBC,QAAhB,iBAAgBA,QAAhB;MAAA,IAA0BC,OAA1B,iBAA0BA,OAA1B;MAAA,IAAmCC,OAAnC,iBAAmCA,OAAnC;MAAA,IAA4CC,IAA5C,iBAA4CA,IAA5C;MAAA,IAAkDC,aAAlD,iBAAkDA,aAAlD;MAAA,IAAiEC,YAAjE,iBAAiEA,YAAjE;MACA,IAAMC,IAAI,GAAG,IAA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V,MAAR,CAAP,eACE,gCAAC,gBAAD;MAAA,eACc,OADd;MAAA,SAESO,IAFT;MAAA,OAGOD,YAHP,aAGOA,YAHP,cAGOA,YAHP,GAGuBU,aAHvB;MAAA,OAIOL;IAJP,iBAME,gCAAC,UAAD,8BAAaJ,IAAI,CAACyC,SAAL,CAAe,CAAf,EAAkBzC,IAAI,CAAC0C,MAAL,GAAcL,gBAAgB,GAAG,CAAjC,GAAqC,CAAvD,CAAb,CANF,eAOE,gCAAC,KAAD,yBAAQrC,IAAI,CAACyC,SAAL,CAAezC,IAAI,CAAC0C,MAAL,GAAcL,gBAAgB,GAAG,CAAjC,GAAqC,CAApD,CAAR,CAPF,CADF;EAWD,CAZD,MAYO;IAAA;;IACL,eAAO,IAAAlC,aAAA,EAAQV,MAAR,CAAP,eACE,gCAAC,gBAAD;MAAA,OAAuBM,YAAvB,aAAuBA,YAAvB,cAAuBA,YAAvB,GAAuCU;IAAvC,iBACE,gCAAC,UAAD,8BAAaT,IAAI,CAACyC,SAAL,CAAe,CAAf,EAAkBzC,IAAI,CAAC0C,MAAL,GAAcL,gBAAgB,GAAG,CAAjC,GAAqC,CAAvD,CAAb,CADF,eAEE,gCAAC,KAAD,yBAAQrC,IAAI,CAACyC,SAAL,CAAezC,IAAI,CAAC0C,MAAL,GAAcL,gBAAgB,GAAG,CAAjC,GAAqC,CAApD,CAAR,CAFF,CADF;EAMD;AACF,CArDD;;AAuDA,IAAMM,QAAQ,GAAG,IAAAC,gBAAA,EAAgBzD,YAAhB,CAAjB;eAEewD,Q"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=index.d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.js","names":[],"sources":["../../src/index.d.ts"],"sourcesContent":["import { CProps } from '@semcore/core';\nimport { RefObject } from 'react';\n\nexport interface IEllipsisProps {\n /**\n * Rows count in multiline Ellipsis\n * @default 1\n */\n maxLine?: number;\n /**\n * Trimming type\n * @default end\n */\n trim?: 'end' | 'middle';\n /**\n * Show tooltip\n * @default true\n */\n tooltip?: boolean;\n /**\n * Ref to the item that will be observed by ResizeObserver\n */\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n /**\n * Explicit sizes of container text should be trimmed in\n **/\n containerRect?: { width: number };\n}\n\ndeclare const useResizeObserver: (\n ref: RefObject<HTMLElement>,\n hookOverride?: { width: number },\n) => { width: number };\n\ndeclare const Ellipsis: <T>(props: CProps<IEllipsisProps & T>) => ReturnEl;\n\nexport { useResizeObserver };\nexport default Ellipsis;\n"],"mappings":""}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "default", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _Ellipsis["default"];
12
+ }
13
+ });
14
+ Object.defineProperty(exports, "useResizeObserver", {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return _useResizeObserver.useResizeObserver;
18
+ }
19
+ });
20
+
21
+ var _Ellipsis = _interopRequireDefault(require("./Ellipsis"));
22
+
23
+ var _useResizeObserver = require("./useResizeObserver");
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './Ellipsis';\nexport { useResizeObserver } from './useResizeObserver';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;;AACA"}
@@ -0,0 +1,26 @@
1
+ @import '@semcore/utils/style/var.css';
2
+
3
+ SContainer,
4
+ SContainerMiddle {
5
+ overflow: hidden;
6
+ }
7
+
8
+ SBeginning,
9
+ SEllipsis {
10
+ overflow: hidden;
11
+ display: inline-block;
12
+ text-overflow: ellipsis;
13
+ white-space: nowrap;
14
+ }
15
+
16
+ SEllipsis[maxLine] {
17
+ display: -webkit-box;
18
+ -webkit-line-clamp: var(--maxLine);
19
+ -webkit-box-orient: vertical;
20
+ overflow: hidden;
21
+ white-space: normal;
22
+ }
23
+
24
+ STail {
25
+ white-space: nowrap;
26
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.useResizeObserver = void 0;
9
+
10
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
+
12
+ var _react = require("react");
13
+
14
+ var _useEnhancedEffect = _interopRequireDefault(require("@semcore/utils/lib/use/useEnhancedEffect"));
15
+
16
+ var useResizeObserver = function useResizeObserver(ref, hookOverride) {
17
+ var _useState = (0, _react.useState)(0),
18
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
19
+ width = _useState2[0],
20
+ setWidth = _useState2[1];
21
+
22
+ var handleResize = (0, _react.useCallback)(function (entries) {
23
+ setWidth(entries[0].contentRect.width);
24
+ }, []);
25
+ (0, _useEnhancedEffect["default"])(function () {
26
+ if (!ref.current) {
27
+ return;
28
+ }
29
+
30
+ if (hookOverride) {
31
+ return;
32
+ }
33
+
34
+ var ro = new ResizeObserver(function (entries) {
35
+ handleResize(entries);
36
+ });
37
+ ro.observe(ref.current);
38
+ return function () {
39
+ ro.disconnect();
40
+ };
41
+ }, [hookOverride]);
42
+
43
+ if (hookOverride) {
44
+ return hookOverride;
45
+ }
46
+
47
+ return {
48
+ width: width
49
+ };
50
+ };
51
+
52
+ exports.useResizeObserver = useResizeObserver;
53
+ //# sourceMappingURL=useResizeObserver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useResizeObserver.js","names":["useResizeObserver","ref","hookOverride","useState","width","setWidth","handleResize","useCallback","entries","contentRect","useEnhancedEffect","current","ro","ResizeObserver","observe","disconnect"],"sources":["../../src/useResizeObserver.tsx"],"sourcesContent":["import { RefObject, useCallback, useState } from 'react';\nimport useEnhancedEffect from '@semcore/utils/lib/use/useEnhancedEffect';\n\nexport const useResizeObserver = (\n ref: RefObject<HTMLElement>,\n hookOverride?: { width: number },\n) => {\n const [width, setWidth] = useState<number>(0);\n\n const handleResize = useCallback((entries: ResizeObserverEntry[]) => {\n setWidth(entries[0].contentRect.width);\n }, []);\n\n useEnhancedEffect(() => {\n if (!ref.current) {\n return;\n }\n\n if (hookOverride) {\n return;\n }\n\n const ro = new ResizeObserver((entries: ResizeObserverEntry[]) => {\n handleResize(entries);\n });\n\n ro.observe(ref.current);\n\n return () => {\n ro.disconnect();\n };\n }, [hookOverride]);\n\n if (hookOverride) {\n return hookOverride;\n }\n return { width };\n};\n"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAEO,IAAMA,iBAAiB,GAAG,SAApBA,iBAAoB,CAC/BC,GAD+B,EAE/BC,YAF+B,EAG5B;EACH,gBAA0B,IAAAC,eAAA,EAAiB,CAAjB,CAA1B;EAAA;EAAA,IAAOC,KAAP;EAAA,IAAcC,QAAd;;EAEA,IAAMC,YAAY,GAAG,IAAAC,kBAAA,EAAY,UAACC,OAAD,EAAoC;IACnEH,QAAQ,CAACG,OAAO,CAAC,CAAD,CAAP,CAAWC,WAAX,CAAuBL,KAAxB,CAAR;EACD,CAFoB,EAElB,EAFkB,CAArB;EAIA,IAAAM,6BAAA,EAAkB,YAAM;IACtB,IAAI,CAACT,GAAG,CAACU,OAAT,EAAkB;MAChB;IACD;;IAED,IAAIT,YAAJ,EAAkB;MAChB;IACD;;IAED,IAAMU,EAAE,GAAG,IAAIC,cAAJ,CAAmB,UAACL,OAAD,EAAoC;MAChEF,YAAY,CAACE,OAAD,CAAZ;IACD,CAFU,CAAX;IAIAI,EAAE,CAACE,OAAH,CAAWb,GAAG,CAACU,OAAf;IAEA,OAAO,YAAM;MACXC,EAAE,CAACG,UAAH;IACD,CAFD;EAGD,CAlBD,EAkBG,CAACb,YAAD,CAlBH;;EAoBA,IAAIA,YAAJ,EAAkB;IAChB,OAAOA,YAAP;EACD;;EACD,OAAO;IAAEE,KAAK,EAALA;EAAF,CAAP;AACD,CAlCM"}
@@ -0,0 +1,173 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _inherits from "@babel/runtime/helpers/inherits";
5
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
+ import { sstyled as _sstyled } from "@semcore/core";
9
+
10
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
11
+
12
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
13
+
14
+ import React, { useRef, useMemo, useState, useLayoutEffect } from 'react';
15
+ import createComponent, { Component, sstyled } from '@semcore/core';
16
+ import Tooltip from '@semcore/tooltip';
17
+ import { Flex } from '@semcore/flex-box';
18
+ import { useResizeObserver } from './useResizeObserver';
19
+
20
+ /*__reshadow-styles__:"./style/ellipsis.shadow.css"*/
21
+ var style = (
22
+ /*__reshadow_css_start__*/
23
+ _sstyled.insert(
24
+ /*__inner_css_start__*/
25
+ ":root {\n\n /* DEPRECATED START\n Deprecated variables should never be used in\n components styles but preserved for backward\n compatibility if they used in projects\n like `<Badge bg=\"red\">alpha</Badge>`\n */\n /* DEPRECATED END */\n}\n\n.___SContainer_1b3bb_gg_,\n.___SContainerMiddle_1b3bb_gg_ {\n overflow: hidden;\n}\n\n.___SBeginning_1b3bb_gg_,\n.___SEllipsis_1b3bb_gg_ {\n overflow: hidden;\n display: inline-block;\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n\n.___SEllipsis_1b3bb_gg_.__maxLine_1b3bb_gg_ {\n display: -webkit-box;\n -webkit-line-clamp: var(--maxLine_1b3bb);\n -webkit-box-orient: vertical;\n overflow: hidden;\n white-space: normal;\n}\n\n.___STail_1b3bb_gg_ {\n white-space: nowrap;\n}\n"
26
+ /*__inner_css_end__*/
27
+ , "1b3bb_gg_")
28
+ /*__reshadow_css_end__*/
29
+ , {
30
+ "__SContainer": "___SContainer_1b3bb_gg_",
31
+ "__SContainerMiddle": "___SContainerMiddle_1b3bb_gg_",
32
+ "__SBeginning": "___SBeginning_1b3bb_gg_",
33
+ "__SEllipsis": "___SEllipsis_1b3bb_gg_",
34
+ "_maxLine": "__maxLine_1b3bb_gg_",
35
+ "--maxLine": "--maxLine_1b3bb",
36
+ "__STail": "___STail_1b3bb_gg_"
37
+ });
38
+ import reactToText from '@semcore/utils/lib/reactToText';
39
+ import getOriginChildren from '@semcore/utils/lib/getOriginChildren';
40
+
41
+ var RootEllipsis = /*#__PURE__*/function (_Component) {
42
+ _inherits(RootEllipsis, _Component);
43
+
44
+ var _super = _createSuper(RootEllipsis);
45
+
46
+ function RootEllipsis() {
47
+ _classCallCheck(this, RootEllipsis);
48
+
49
+ return _super.apply(this, arguments);
50
+ }
51
+
52
+ _createClass(RootEllipsis, [{
53
+ key: "render",
54
+ value: function render() {
55
+ var _ref3;
56
+
57
+ var SEllipsis = this.Root;
58
+ var SContainer = Flex;
59
+ var _this$asProps = this.asProps,
60
+ styles = _this$asProps.styles,
61
+ Children = _this$asProps.Children,
62
+ maxLine = _this$asProps.maxLine,
63
+ tooltip = _this$asProps.tooltip,
64
+ trim = _this$asProps.trim,
65
+ containerRect = _this$asProps.containerRect,
66
+ containerRef = _this$asProps.containerRef;
67
+ var text = reactToText(getOriginChildren(Children));
68
+
69
+ if (trim === 'middle') {
70
+ var _ref;
71
+
72
+ return _ref = sstyled(styles), /*#__PURE__*/React.createElement(EllipsisMiddle, _ref.cn("EllipsisMiddle", {
73
+ "text": text,
74
+ "styles": styles,
75
+ "tooltip": tooltip,
76
+ "containerRect": containerRect,
77
+ "containerRef": containerRef
78
+ }));
79
+ }
80
+
81
+ if (tooltip) {
82
+ var _ref2;
83
+
84
+ return _ref2 = sstyled(styles), /*#__PURE__*/React.createElement(SContainer, _ref2.cn("SContainer", {
85
+ "interaction": "hover",
86
+ "title": text,
87
+ "tag": Tooltip
88
+ }), /*#__PURE__*/React.createElement(SEllipsis, _ref2.cn("SEllipsis", {
89
+ "use:maxLine": maxLine,
90
+ "render": "div",
91
+ "tag": "div"
92
+ }), /*#__PURE__*/React.createElement(Children, _ref2.cn("Children", {}))));
93
+ }
94
+
95
+ return _ref3 = sstyled(styles), /*#__PURE__*/React.createElement(SEllipsis, _ref3.cn("SEllipsis", {
96
+ "use:maxLine": maxLine,
97
+ "render": "div",
98
+ "tag": "div"
99
+ }), /*#__PURE__*/React.createElement(Children, _ref3.cn("Children", {})));
100
+ }
101
+ }]);
102
+
103
+ return RootEllipsis;
104
+ }(Component);
105
+
106
+ _defineProperty(RootEllipsis, "displayName", 'Ellipsis');
107
+
108
+ _defineProperty(RootEllipsis, "style", style);
109
+
110
+ _defineProperty(RootEllipsis, "defaultProps", {
111
+ trim: 'end',
112
+ tooltip: true
113
+ });
114
+
115
+ var EllipsisMiddle = function EllipsisMiddle(props) {
116
+ var styles = props.styles,
117
+ text = props.text,
118
+ tooltip = props.tooltip,
119
+ containerRect = props.containerRect,
120
+ containerRef = props.containerRef;
121
+ var resizeElement = useRef(null);
122
+
123
+ var _useState = useState({
124
+ fontSize: '14',
125
+ symbolWidth: 0
126
+ }),
127
+ _useState2 = _slicedToArray(_useState, 2),
128
+ dimension = _useState2[0],
129
+ setDimension = _useState2[1];
130
+
131
+ var blockWidth = useResizeObserver(resizeElement, containerRect).width;
132
+ useLayoutEffect(function () {
133
+ var _containerRef$current;
134
+
135
+ var dateSpan = document.createElement('temporary-block');
136
+ dateSpan.setAttribute('style', "fontSize: ".concat(dimension.fontSize, "px"));
137
+ dateSpan.innerHTML = 'a';
138
+ document.body.appendChild(dateSpan);
139
+ var rect = dateSpan.getBoundingClientRect();
140
+ dateSpan.remove();
141
+ setDimension({
142
+ fontSize: window.getComputedStyle((_containerRef$current = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current) !== null && _containerRef$current !== void 0 ? _containerRef$current : resizeElement.current, null).getPropertyValue('font-size'),
143
+ symbolWidth: rect.width
144
+ });
145
+ }, []);
146
+ var STail = 'span';
147
+ var SBeginning = 'span';
148
+ var SContainerMiddle = Flex;
149
+ var displayedSymbols = useMemo(function () {
150
+ return Math.round(blockWidth / dimension.symbolWidth);
151
+ }, [blockWidth, dimension.symbolWidth]);
152
+
153
+ if (tooltip) {
154
+ var _ref4;
155
+
156
+ return _ref4 = sstyled(styles), /*#__PURE__*/React.createElement(SContainerMiddle, _ref4.cn("SContainerMiddle", {
157
+ "interaction": "hover",
158
+ "title": text,
159
+ "ref": containerRef !== null && containerRef !== void 0 ? containerRef : resizeElement,
160
+ "tag": Tooltip
161
+ }), /*#__PURE__*/React.createElement(SBeginning, _ref4.cn("SBeginning", {}), text.substring(0, text.length - displayedSymbols / 2 - 1)), /*#__PURE__*/React.createElement(STail, _ref4.cn("STail", {}), text.substring(text.length - displayedSymbols / 2 - 1)));
162
+ } else {
163
+ var _ref5;
164
+
165
+ return _ref5 = sstyled(styles), /*#__PURE__*/React.createElement(SContainerMiddle, _ref5.cn("SContainerMiddle", {
166
+ "ref": containerRef !== null && containerRef !== void 0 ? containerRef : resizeElement
167
+ }), /*#__PURE__*/React.createElement(SBeginning, _ref5.cn("SBeginning", {}), text.substring(0, text.length - displayedSymbols / 2 - 1)), /*#__PURE__*/React.createElement(STail, _ref5.cn("STail", {}), text.substring(text.length - displayedSymbols / 2 - 1)));
168
+ }
169
+ };
170
+
171
+ var Ellipsis = createComponent(RootEllipsis);
172
+ export default Ellipsis;
173
+ //# sourceMappingURL=Ellipsis.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Ellipsis.js","names":["React","useRef","useMemo","useState","useLayoutEffect","createComponent","Component","sstyled","Tooltip","Flex","useResizeObserver","reactToText","getOriginChildren","RootEllipsis","SEllipsis","Root","SContainer","asProps","styles","Children","maxLine","tooltip","trim","containerRect","containerRef","text","style","EllipsisMiddle","props","resizeElement","fontSize","symbolWidth","dimension","setDimension","blockWidth","width","dateSpan","document","createElement","setAttribute","innerHTML","body","appendChild","rect","getBoundingClientRect","remove","window","getComputedStyle","current","getPropertyValue","STail","SBeginning","SContainerMiddle","displayedSymbols","Math","round","substring","length","Ellipsis"],"sources":["../../src/Ellipsis.tsx"],"sourcesContent":["import React, { RefObject, useRef, useMemo, useState, useLayoutEffect } from 'react';\nimport createComponent, { Component, sstyled } from '@semcore/core';\nimport Tooltip from '@semcore/tooltip';\nimport { Flex } from '@semcore/flex-box';\nimport { useResizeObserver } from './useResizeObserver';\n\nimport style from './style/ellipsis.shadow.css';\nimport reactToText from '@semcore/utils/lib/reactToText';\nimport getOriginChildren from '@semcore/utils/lib/getOriginChildren';\n\ntype AsProps = {\n maxLine?: number;\n trim?: 'end' | 'middle';\n tooltip?: boolean;\n styles?: React.CSSProperties;\n containerRect?: { width: number };\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n};\n\ntype AsPropsMiddle = {\n text: string;\n tooltip?: boolean;\n styles?: React.CSSProperties;\n containerRect?: { width: number };\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n};\n\nclass RootEllipsis extends Component<AsProps> {\n static displayName = 'Ellipsis';\n static style = style;\n static defaultProps: AsProps = {\n trim: 'end',\n tooltip: true,\n };\n\n render() {\n const SEllipsis = this.Root;\n const SContainer = Flex;\n const { styles, Children, maxLine, tooltip, trim, containerRect, containerRef } = this.asProps;\n const text = reactToText(getOriginChildren(Children));\n\n if (trim === 'middle') {\n return sstyled(styles)(\n <EllipsisMiddle\n text={text}\n styles={styles}\n tooltip={tooltip}\n containerRect={containerRect}\n containerRef={containerRef}\n />,\n );\n }\n if (tooltip) {\n return sstyled(styles)(\n <SContainer interaction=\"hover\" title={text} tag={Tooltip}>\n <SEllipsis use:maxLine={maxLine} render=\"div\" tag=\"div\">\n <Children />\n </SEllipsis>\n </SContainer>,\n );\n }\n return sstyled(styles)(\n <SEllipsis use:maxLine={maxLine} render=\"div\" tag=\"div\">\n <Children />\n </SEllipsis>,\n );\n }\n}\n\nconst EllipsisMiddle: React.FC<AsPropsMiddle> = (props) => {\n const { styles, text, tooltip, containerRect, containerRef } = props;\n const resizeElement = useRef<RefObject<HTMLElement | null>>(null);\n const [dimension, setDimension] = useState<{ fontSize: string; symbolWidth: number }>({\n fontSize: '14',\n symbolWidth: 0,\n });\n const blockWidth = useResizeObserver(resizeElement, containerRect).width;\n\n useLayoutEffect(() => {\n const dateSpan = document.createElement('temporary-block');\n dateSpan.setAttribute('style', `fontSize: ${dimension.fontSize}px`);\n dateSpan.innerHTML = 'a';\n document.body.appendChild(dateSpan);\n const rect = dateSpan.getBoundingClientRect();\n dateSpan.remove();\n\n setDimension({\n fontSize: window\n .getComputedStyle(containerRef?.current ?? resizeElement.current, null)\n .getPropertyValue('font-size'),\n symbolWidth: rect.width,\n });\n }, []);\n\n const STail = 'span';\n const SBeginning = 'span';\n const SContainerMiddle = Flex;\n const displayedSymbols = useMemo(\n () => Math.round(blockWidth / dimension.symbolWidth),\n [blockWidth, dimension.symbolWidth],\n );\n\n if (tooltip) {\n return sstyled(styles)(\n <SContainerMiddle\n interaction=\"hover\"\n title={text}\n ref={containerRef ?? resizeElement}\n tag={Tooltip}\n >\n <SBeginning>{text.substring(0, text.length - displayedSymbols / 2 - 1)}</SBeginning>\n <STail>{text.substring(text.length - displayedSymbols / 2 - 1)}</STail>\n </SContainerMiddle>,\n ) as any;\n } else {\n return sstyled(styles)(\n <SContainerMiddle ref={containerRef ?? resizeElement}>\n <SBeginning>{text.substring(0, text.length - displayedSymbols / 2 - 1)}</SBeginning>\n <STail>{text.substring(text.length - displayedSymbols / 2 - 1)}</STail>\n </SContainerMiddle>,\n ) as any;\n }\n};\n\nconst Ellipsis = createComponent(RootEllipsis);\n\nexport default Ellipsis;\n"],"mappings":";;;;;;;;;;;;;AAAA,OAAOA,KAAP,IAA2BC,MAA3B,EAAmCC,OAAnC,EAA4CC,QAA5C,EAAsDC,eAAtD,QAA6E,OAA7E;AACA,OAAOC,eAAP,IAA0BC,SAA1B,EAAqCC,OAArC,QAAoD,eAApD;AACA,OAAOC,OAAP,MAAoB,kBAApB;AACA,SAASC,IAAT,QAAqB,mBAArB;AACA,SAASC,iBAAT,QAAkC,qBAAlC;;;;;;;;;;;;;;;;;;;;AAGA,OAAOC,WAAP,MAAwB,gCAAxB;AACA,OAAOC,iBAAP,MAA8B,sCAA9B;;IAqBMC,Y;;;;;;;;;;;;;WAQJ,kBAAS;MAAA;;MACP,IAAMC,SAAS,GAAG,KAAKC,IAAvB;MACA,IAAMC,UAAU,GAAGP,IAAnB;MACA,oBAAkF,KAAKQ,OAAvF;MAAA,IAAQC,MAAR,iBAAQA,MAAR;MAAA,IAAgBC,QAAhB,iBAAgBA,QAAhB;MAAA,IAA0BC,OAA1B,iBAA0BA,OAA1B;MAAA,IAAmCC,OAAnC,iBAAmCA,OAAnC;MAAA,IAA4CC,IAA5C,iBAA4CA,IAA5C;MAAA,IAAkDC,aAAlD,iBAAkDA,aAAlD;MAAA,IAAiEC,YAAjE,iBAAiEA,YAAjE;MACA,IAAMC,IAAI,GAAGd,WAAW,CAACC,iBAAiB,CAACO,QAAD,CAAlB,CAAxB;;MAEA,IAAIG,IAAI,KAAK,QAAb,EAAuB;QAAA;;QACrB,cAAOf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AAC,UAAD,8BAAaiB,IAAI,CAAC+B,SAAL,CAAe,CAAf,EAAkB/B,IAAI,CAACgC,MAAL,GAAcJ,gBAAgB,GAAG,CAAjC,GAAqC,CAAvD,CAAb,CANF,eAOE,oBAAC,KAAD,yBAAQ5B,IAAI,CAAC+B,SAAL,CAAe/B,IAAI,CAACgC,MAAL,GAAcJ,gBAAgB,GAAG,CAAjC,GAAqC,CAApD,CAAR,CAPF,CADF;EAWD,CAZD,MAYO;IAAA;;IACL,eAAO9C,OAAO,CAACW,MAAD,CAAd,eACE,oBAAC,gBAAD;MAAA,OAAuBM,YAAvB,aAAuBA,YAAvB,cAAuBA,YAAvB,GAAuCK;IAAvC,iBACE,oBAAC,UAAD,8BAAaJ,IAAI,CAAC+B,SAAL,CAAe,CAAf,EAAkB/B,IAAI,CAACgC,MAAL,GAAcJ,gBAAgB,GAAG,CAAjC,GAAqC,CAAvD,CAAb,CADF,eAEE,oBAAC,KAAD,yBAAQ5B,IAAI,CAAC+B,SAAL,CAAe/B,IAAI,CAACgC,MAAL,GAAcJ,gBAAgB,GAAG,CAAjC,GAAqC,CAApD,CAAR,CAFF,CADF;EAMD;AACF,CArDD;;AAuDA,IAAMK,QAAQ,GAAGrD,eAAe,CAACQ,YAAD,CAAhC;AAEA,eAAe6C,QAAf"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.js","names":[],"sources":["../../src/index.d.ts"],"sourcesContent":["import { CProps } from '@semcore/core';\nimport { RefObject } from 'react';\n\nexport interface IEllipsisProps {\n /**\n * Rows count in multiline Ellipsis\n * @default 1\n */\n maxLine?: number;\n /**\n * Trimming type\n * @default end\n */\n trim?: 'end' | 'middle';\n /**\n * Show tooltip\n * @default true\n */\n tooltip?: boolean;\n /**\n * Ref to the item that will be observed by ResizeObserver\n */\n // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope\n containerRef?: RefObject<HTMLElement | null>;\n /**\n * Explicit sizes of container text should be trimmed in\n **/\n containerRect?: { width: number };\n}\n\ndeclare const useResizeObserver: (\n ref: RefObject<HTMLElement>,\n hookOverride?: { width: number },\n) => { width: number };\n\ndeclare const Ellipsis: <T>(props: CProps<IEllipsisProps & T>) => ReturnEl;\n\nexport { useResizeObserver };\nexport default Ellipsis;\n"],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export { default } from './Ellipsis';
2
+ export { useResizeObserver } from './useResizeObserver';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["default","useResizeObserver"],"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './Ellipsis';\nexport { useResizeObserver } from './useResizeObserver';\n"],"mappings":"AAAA,SAASA,OAAT,QAAwB,YAAxB;AACA,SAASC,iBAAT,QAAkC,qBAAlC"}
@@ -0,0 +1,26 @@
1
+ @import '@semcore/utils/style/var.css';
2
+
3
+ SContainer,
4
+ SContainerMiddle {
5
+ overflow: hidden;
6
+ }
7
+
8
+ SBeginning,
9
+ SEllipsis {
10
+ overflow: hidden;
11
+ display: inline-block;
12
+ text-overflow: ellipsis;
13
+ white-space: nowrap;
14
+ }
15
+
16
+ SEllipsis[maxLine] {
17
+ display: -webkit-box;
18
+ -webkit-line-clamp: var(--maxLine);
19
+ -webkit-box-orient: vertical;
20
+ overflow: hidden;
21
+ white-space: normal;
22
+ }
23
+
24
+ STail {
25
+ white-space: nowrap;
26
+ }
@@ -0,0 +1,39 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useCallback, useState } from 'react';
3
+ import useEnhancedEffect from '@semcore/utils/lib/use/useEnhancedEffect';
4
+ export var useResizeObserver = function useResizeObserver(ref, hookOverride) {
5
+ var _useState = useState(0),
6
+ _useState2 = _slicedToArray(_useState, 2),
7
+ width = _useState2[0],
8
+ setWidth = _useState2[1];
9
+
10
+ var handleResize = useCallback(function (entries) {
11
+ setWidth(entries[0].contentRect.width);
12
+ }, []);
13
+ useEnhancedEffect(function () {
14
+ if (!ref.current) {
15
+ return;
16
+ }
17
+
18
+ if (hookOverride) {
19
+ return;
20
+ }
21
+
22
+ var ro = new ResizeObserver(function (entries) {
23
+ handleResize(entries);
24
+ });
25
+ ro.observe(ref.current);
26
+ return function () {
27
+ ro.disconnect();
28
+ };
29
+ }, [hookOverride]);
30
+
31
+ if (hookOverride) {
32
+ return hookOverride;
33
+ }
34
+
35
+ return {
36
+ width: width
37
+ };
38
+ };
39
+ //# sourceMappingURL=useResizeObserver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useResizeObserver.js","names":["useCallback","useState","useEnhancedEffect","useResizeObserver","ref","hookOverride","width","setWidth","handleResize","entries","contentRect","current","ro","ResizeObserver","observe","disconnect"],"sources":["../../src/useResizeObserver.tsx"],"sourcesContent":["import { RefObject, useCallback, useState } from 'react';\nimport useEnhancedEffect from '@semcore/utils/lib/use/useEnhancedEffect';\n\nexport const useResizeObserver = (\n ref: RefObject<HTMLElement>,\n hookOverride?: { width: number },\n) => {\n const [width, setWidth] = useState<number>(0);\n\n const handleResize = useCallback((entries: ResizeObserverEntry[]) => {\n setWidth(entries[0].contentRect.width);\n }, []);\n\n useEnhancedEffect(() => {\n if (!ref.current) {\n return;\n }\n\n if (hookOverride) {\n return;\n }\n\n const ro = new ResizeObserver((entries: ResizeObserverEntry[]) => {\n handleResize(entries);\n });\n\n ro.observe(ref.current);\n\n return () => {\n ro.disconnect();\n };\n }, [hookOverride]);\n\n if (hookOverride) {\n return hookOverride;\n }\n return { width };\n};\n"],"mappings":";AAAA,SAAoBA,WAApB,EAAiCC,QAAjC,QAAiD,OAAjD;AACA,OAAOC,iBAAP,MAA8B,0CAA9B;AAEA,OAAO,IAAMC,iBAAiB,GAAG,SAApBA,iBAAoB,CAC/BC,GAD+B,EAE/BC,YAF+B,EAG5B;EACH,gBAA0BJ,QAAQ,CAAS,CAAT,CAAlC;EAAA;EAAA,IAAOK,KAAP;EAAA,IAAcC,QAAd;;EAEA,IAAMC,YAAY,GAAGR,WAAW,CAAC,UAACS,OAAD,EAAoC;IACnEF,QAAQ,CAACE,OAAO,CAAC,CAAD,CAAP,CAAWC,WAAX,CAAuBJ,KAAxB,CAAR;EACD,CAF+B,EAE7B,EAF6B,CAAhC;EAIAJ,iBAAiB,CAAC,YAAM;IACtB,IAAI,CAACE,GAAG,CAACO,OAAT,EAAkB;MAChB;IACD;;IAED,IAAIN,YAAJ,EAAkB;MAChB;IACD;;IAED,IAAMO,EAAE,GAAG,IAAIC,cAAJ,CAAmB,UAACJ,OAAD,EAAoC;MAChED,YAAY,CAACC,OAAD,CAAZ;IACD,CAFU,CAAX;IAIAG,EAAE,CAACE,OAAH,CAAWV,GAAG,CAACO,OAAf;IAEA,OAAO,YAAM;MACXC,EAAE,CAACG,UAAH;IACD,CAFD;EAGD,CAlBgB,EAkBd,CAACV,YAAD,CAlBc,CAAjB;;EAoBA,IAAIA,YAAJ,EAAkB;IAChB,OAAOA,YAAP;EACD;;EACD,OAAO;IAAEC,KAAK,EAALA;EAAF,CAAP;AACD,CAlCM"}
@@ -0,0 +1,39 @@
1
+ import { CProps } from '@semcore/core';
2
+ import { RefObject } from 'react';
3
+
4
+ export interface IEllipsisProps {
5
+ /**
6
+ * Rows count in multiline Ellipsis
7
+ * @default 1
8
+ */
9
+ maxLine?: number;
10
+ /**
11
+ * Trimming type
12
+ * @default end
13
+ */
14
+ trim?: 'end' | 'middle';
15
+ /**
16
+ * Show tooltip
17
+ * @default true
18
+ */
19
+ tooltip?: boolean;
20
+ /**
21
+ * Ref to the item that will be observed by ResizeObserver
22
+ */
23
+ // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope
24
+ containerRef?: RefObject<HTMLElement | null>;
25
+ /**
26
+ * Explicit sizes of container text should be trimmed in
27
+ **/
28
+ containerRect?: { width: number };
29
+ }
30
+
31
+ declare const useResizeObserver: (
32
+ ref: RefObject<HTMLElement>,
33
+ hookOverride?: { width: number },
34
+ ) => { width: number };
35
+
36
+ declare const Ellipsis: <T>(props: CProps<IEllipsisProps & T>) => ReturnEl;
37
+
38
+ export { useResizeObserver };
39
+ export default Ellipsis;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@semcore/ellipsis",
3
+ "description": "Semrush Ellipsis Component",
4
+ "version": "1.0.0-0",
5
+ "main": "lib/cjs/index.js",
6
+ "module": "lib/es6/index.js",
7
+ "typings": "lib/types/index.d.ts",
8
+ "sideEffects": false,
9
+ "author": "UI-KIT team <ui-kit-team@semrush.com>",
10
+ "license": "MIT",
11
+ "scripts": {
12
+ "build": "pnpm semcore-builder --source=js",
13
+ "test": "jest"
14
+ },
15
+ "dependencies": {
16
+ "@semcore/utils": "^3.15",
17
+ "@semcore/flex-box": "^4",
18
+ "@semcore/tooltip": "^5",
19
+ "resize-observer-polyfill": "1.5.1"
20
+ },
21
+ "peerDependencies": {
22
+ "@semcore/core": "^1.8.3",
23
+ "react": "16.8 - 17"
24
+ },
25
+ "devDependencies": {
26
+ "@semcore/jest-preset-ui": "1.0.0"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/semrush/intergalactic.git",
31
+ "directory": "semcore/ellipsis"
32
+ },
33
+ "jest": {
34
+ "preset": "@semcore/jest-preset-ui"
35
+ }
36
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["src", "lib"],
4
+ "compilerOptions": {
5
+ "strict": true
6
+ }
7
+ }