@elementor/editor-ui 0.1.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/.turbo/turbo-build.log +22 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +84 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +47 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -0
- package/src/components/ellipsis-with-tooltip.tsx +74 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
> @elementor/editor-ui@0.1.0 build
|
|
3
|
+
> tsup --config=../../tsup.build.ts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: ../../../tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.3.5
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/elementor-packages/elementor-packages/tsup.build.ts
|
|
9
|
+
[34mCLI[39m Target: esnext
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mESM[39m Build start
|
|
12
|
+
[34mCJS[39m Build start
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m1.38 KB[39m
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m2.97 KB[39m
|
|
15
|
+
[32mESM[39m ⚡️ Build success in 116ms
|
|
16
|
+
[32mCJS[39m [1mdist/index.js [22m[32m3.05 KB[39m
|
|
17
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m3.10 KB[39m
|
|
18
|
+
[32mCJS[39m ⚡️ Build success in 121ms
|
|
19
|
+
[34mDTS[39m Build start
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 32739ms
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m388.00 B[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m388.00 B[39m
|
package/CHANGELOG.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type EllipsisWithTooltipProps<T extends React.ElementType> = {
|
|
4
|
+
maxWidth?: React.CSSProperties['maxWidth'];
|
|
5
|
+
title: string;
|
|
6
|
+
as?: T;
|
|
7
|
+
} & React.ComponentProps<T>;
|
|
8
|
+
declare const EllipsisWithTooltip: <T extends React.ElementType>({ maxWidth, title, as, ...props }: EllipsisWithTooltipProps<T>) => React.JSX.Element;
|
|
9
|
+
|
|
10
|
+
export { EllipsisWithTooltip };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type EllipsisWithTooltipProps<T extends React.ElementType> = {
|
|
4
|
+
maxWidth?: React.CSSProperties['maxWidth'];
|
|
5
|
+
title: string;
|
|
6
|
+
as?: T;
|
|
7
|
+
} & React.ComponentProps<T>;
|
|
8
|
+
declare const EllipsisWithTooltip: <T extends React.ElementType>({ maxWidth, title, as, ...props }: EllipsisWithTooltipProps<T>) => React.JSX.Element;
|
|
9
|
+
|
|
10
|
+
export { EllipsisWithTooltip };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
EllipsisWithTooltip: () => EllipsisWithTooltip
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/components/ellipsis-with-tooltip.tsx
|
|
38
|
+
var React = __toESM(require("react"));
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
var import_ui = require("@elementor/ui");
|
|
41
|
+
var EllipsisWithTooltip = ({
|
|
42
|
+
maxWidth,
|
|
43
|
+
title,
|
|
44
|
+
as,
|
|
45
|
+
...props
|
|
46
|
+
}) => {
|
|
47
|
+
const [setRef, isOverflowing] = useIsOverflowing();
|
|
48
|
+
if (isOverflowing) {
|
|
49
|
+
return /* @__PURE__ */ React.createElement(import_ui.Tooltip, { title, placement: "top" }, /* @__PURE__ */ React.createElement(Content, { maxWidth, ref: setRef, as, ...props }, title));
|
|
50
|
+
}
|
|
51
|
+
return /* @__PURE__ */ React.createElement(Content, { maxWidth, ref: setRef, as, ...props }, title);
|
|
52
|
+
};
|
|
53
|
+
var Content = React.forwardRef(
|
|
54
|
+
({ maxWidth, as: Component = import_ui.Box, ...props }, ref) => /* @__PURE__ */ React.createElement(
|
|
55
|
+
Component,
|
|
56
|
+
{
|
|
57
|
+
ref,
|
|
58
|
+
position: "relative",
|
|
59
|
+
...props,
|
|
60
|
+
style: { overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", maxWidth }
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
var useIsOverflowing = () => {
|
|
65
|
+
const [el, setEl] = (0, import_react.useState)(null);
|
|
66
|
+
const [isOverflowing, setIsOverflown] = (0, import_react.useState)(false);
|
|
67
|
+
(0, import_react.useEffect)(() => {
|
|
68
|
+
const observer = new ResizeObserver(([{ target }]) => {
|
|
69
|
+
setIsOverflown(target.scrollWidth > target.clientWidth);
|
|
70
|
+
});
|
|
71
|
+
if (el) {
|
|
72
|
+
observer.observe(el);
|
|
73
|
+
}
|
|
74
|
+
return () => {
|
|
75
|
+
observer.disconnect();
|
|
76
|
+
};
|
|
77
|
+
}, [el]);
|
|
78
|
+
return [setEl, isOverflowing];
|
|
79
|
+
};
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
EllipsisWithTooltip
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/components/ellipsis-with-tooltip.tsx"],"sourcesContent":["export { EllipsisWithTooltip } from './components/ellipsis-with-tooltip';\n","import * as React from 'react';\nimport { useEffect, useState } from 'react';\nimport { Box, Tooltip } from '@elementor/ui';\n\ntype EllipsisWithTooltipProps< T extends React.ElementType > = {\n\tmaxWidth?: React.CSSProperties[ 'maxWidth' ];\n\ttitle: string;\n\tas?: T;\n} & React.ComponentProps< T >;\n\nexport const EllipsisWithTooltip = < T extends React.ElementType >( {\n\tmaxWidth,\n\ttitle,\n\tas,\n\t...props\n}: EllipsisWithTooltipProps< T > ) => {\n\tconst [ setRef, isOverflowing ] = useIsOverflowing();\n\n\tif ( isOverflowing ) {\n\t\treturn (\n\t\t\t<Tooltip title={ title } placement=\"top\">\n\t\t\t\t<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>\n\t\t\t\t\t{ title }\n\t\t\t\t</Content>\n\t\t\t</Tooltip>\n\t\t);\n\t}\n\n\treturn (\n\t\t<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>\n\t\t\t{ title }\n\t\t</Content>\n\t);\n};\n\ntype ContentProps< T extends React.ElementType > = React.PropsWithChildren<\n\tOmit< EllipsisWithTooltipProps< T >, 'title' >\n>;\n\nconst Content = React.forwardRef(\n\t< T extends React.ElementType >(\n\t\t{ maxWidth, as: Component = Box, ...props }: ContentProps< T >,\n\t\t// forwardRef loses the typing when using generic components.\n\t\tref: unknown\n\t) => (\n\t\t<Component\n\t\t\tref={ ref }\n\t\t\tposition=\"relative\"\n\t\t\t{ ...props }\n\t\t\tstyle={ { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth } }\n\t\t/>\n\t)\n);\n\nconst useIsOverflowing = () => {\n\tconst [ el, setEl ] = useState< HTMLElement | null >( null );\n\tconst [ isOverflowing, setIsOverflown ] = useState( false );\n\n\tuseEffect( () => {\n\t\tconst observer = new ResizeObserver( ( [ { target } ] ) => {\n\t\t\tsetIsOverflown( target.scrollWidth > target.clientWidth );\n\t\t} );\n\n\t\tif ( el ) {\n\t\t\tobserver.observe( el );\n\t\t}\n\n\t\treturn () => {\n\t\t\tobserver.disconnect();\n\t\t};\n\t}, [ el ] );\n\n\treturn [ setEl, isOverflowing ] as const;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,mBAAoC;AACpC,gBAA6B;AAQtB,IAAM,sBAAsB,CAAiC;AAAA,EACnE;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,MAAsC;AACrC,QAAM,CAAE,QAAQ,aAAc,IAAI,iBAAiB;AAEnD,MAAK,eAAgB;AACpB,WACC,oCAAC,qBAAQ,OAAgB,WAAU,SAClC,oCAAC,WAAQ,UAAsB,KAAM,QAAS,IAAY,GAAG,SAC1D,KACH,CACD;AAAA,EAEF;AAEA,SACC,oCAAC,WAAQ,UAAsB,KAAM,QAAS,IAAY,GAAG,SAC1D,KACH;AAEF;AAMA,IAAM,UAAgB;AAAA,EACrB,CACC,EAAE,UAAU,IAAI,YAAY,eAAK,GAAG,MAAM,GAE1C,QAEA;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA,UAAS;AAAA,MACP,GAAG;AAAA,MACL,OAAQ,EAAE,UAAU,UAAU,cAAc,YAAY,YAAY,UAAU,SAAS;AAAA;AAAA,EACxF;AAEF;AAEA,IAAM,mBAAmB,MAAM;AAC9B,QAAM,CAAE,IAAI,KAAM,QAAI,uBAAgC,IAAK;AAC3D,QAAM,CAAE,eAAe,cAAe,QAAI,uBAAU,KAAM;AAE1D,8BAAW,MAAM;AAChB,UAAM,WAAW,IAAI,eAAgB,CAAE,CAAE,EAAE,OAAO,CAAE,MAAO;AAC1D,qBAAgB,OAAO,cAAc,OAAO,WAAY;AAAA,IACzD,CAAE;AAEF,QAAK,IAAK;AACT,eAAS,QAAS,EAAG;AAAA,IACtB;AAEA,WAAO,MAAM;AACZ,eAAS,WAAW;AAAA,IACrB;AAAA,EACD,GAAG,CAAE,EAAG,CAAE;AAEV,SAAO,CAAE,OAAO,aAAc;AAC/B;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/components/ellipsis-with-tooltip.tsx
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { Box, Tooltip } from "@elementor/ui";
|
|
5
|
+
var EllipsisWithTooltip = ({
|
|
6
|
+
maxWidth,
|
|
7
|
+
title,
|
|
8
|
+
as,
|
|
9
|
+
...props
|
|
10
|
+
}) => {
|
|
11
|
+
const [setRef, isOverflowing] = useIsOverflowing();
|
|
12
|
+
if (isOverflowing) {
|
|
13
|
+
return /* @__PURE__ */ React.createElement(Tooltip, { title, placement: "top" }, /* @__PURE__ */ React.createElement(Content, { maxWidth, ref: setRef, as, ...props }, title));
|
|
14
|
+
}
|
|
15
|
+
return /* @__PURE__ */ React.createElement(Content, { maxWidth, ref: setRef, as, ...props }, title);
|
|
16
|
+
};
|
|
17
|
+
var Content = React.forwardRef(
|
|
18
|
+
({ maxWidth, as: Component = Box, ...props }, ref) => /* @__PURE__ */ React.createElement(
|
|
19
|
+
Component,
|
|
20
|
+
{
|
|
21
|
+
ref,
|
|
22
|
+
position: "relative",
|
|
23
|
+
...props,
|
|
24
|
+
style: { overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", maxWidth }
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
var useIsOverflowing = () => {
|
|
29
|
+
const [el, setEl] = useState(null);
|
|
30
|
+
const [isOverflowing, setIsOverflown] = useState(false);
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const observer = new ResizeObserver(([{ target }]) => {
|
|
33
|
+
setIsOverflown(target.scrollWidth > target.clientWidth);
|
|
34
|
+
});
|
|
35
|
+
if (el) {
|
|
36
|
+
observer.observe(el);
|
|
37
|
+
}
|
|
38
|
+
return () => {
|
|
39
|
+
observer.disconnect();
|
|
40
|
+
};
|
|
41
|
+
}, [el]);
|
|
42
|
+
return [setEl, isOverflowing];
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
EllipsisWithTooltip
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/ellipsis-with-tooltip.tsx"],"sourcesContent":["import * as React from 'react';\nimport { useEffect, useState } from 'react';\nimport { Box, Tooltip } from '@elementor/ui';\n\ntype EllipsisWithTooltipProps< T extends React.ElementType > = {\n\tmaxWidth?: React.CSSProperties[ 'maxWidth' ];\n\ttitle: string;\n\tas?: T;\n} & React.ComponentProps< T >;\n\nexport const EllipsisWithTooltip = < T extends React.ElementType >( {\n\tmaxWidth,\n\ttitle,\n\tas,\n\t...props\n}: EllipsisWithTooltipProps< T > ) => {\n\tconst [ setRef, isOverflowing ] = useIsOverflowing();\n\n\tif ( isOverflowing ) {\n\t\treturn (\n\t\t\t<Tooltip title={ title } placement=\"top\">\n\t\t\t\t<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>\n\t\t\t\t\t{ title }\n\t\t\t\t</Content>\n\t\t\t</Tooltip>\n\t\t);\n\t}\n\n\treturn (\n\t\t<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>\n\t\t\t{ title }\n\t\t</Content>\n\t);\n};\n\ntype ContentProps< T extends React.ElementType > = React.PropsWithChildren<\n\tOmit< EllipsisWithTooltipProps< T >, 'title' >\n>;\n\nconst Content = React.forwardRef(\n\t< T extends React.ElementType >(\n\t\t{ maxWidth, as: Component = Box, ...props }: ContentProps< T >,\n\t\t// forwardRef loses the typing when using generic components.\n\t\tref: unknown\n\t) => (\n\t\t<Component\n\t\t\tref={ ref }\n\t\t\tposition=\"relative\"\n\t\t\t{ ...props }\n\t\t\tstyle={ { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth } }\n\t\t/>\n\t)\n);\n\nconst useIsOverflowing = () => {\n\tconst [ el, setEl ] = useState< HTMLElement | null >( null );\n\tconst [ isOverflowing, setIsOverflown ] = useState( false );\n\n\tuseEffect( () => {\n\t\tconst observer = new ResizeObserver( ( [ { target } ] ) => {\n\t\t\tsetIsOverflown( target.scrollWidth > target.clientWidth );\n\t\t} );\n\n\t\tif ( el ) {\n\t\t\tobserver.observe( el );\n\t\t}\n\n\t\treturn () => {\n\t\t\tobserver.disconnect();\n\t\t};\n\t}, [ el ] );\n\n\treturn [ setEl, isOverflowing ] as const;\n};\n"],"mappings":";AAAA,YAAY,WAAW;AACvB,SAAS,WAAW,gBAAgB;AACpC,SAAS,KAAK,eAAe;AAQtB,IAAM,sBAAsB,CAAiC;AAAA,EACnE;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,MAAsC;AACrC,QAAM,CAAE,QAAQ,aAAc,IAAI,iBAAiB;AAEnD,MAAK,eAAgB;AACpB,WACC,oCAAC,WAAQ,OAAgB,WAAU,SAClC,oCAAC,WAAQ,UAAsB,KAAM,QAAS,IAAY,GAAG,SAC1D,KACH,CACD;AAAA,EAEF;AAEA,SACC,oCAAC,WAAQ,UAAsB,KAAM,QAAS,IAAY,GAAG,SAC1D,KACH;AAEF;AAMA,IAAM,UAAgB;AAAA,EACrB,CACC,EAAE,UAAU,IAAI,YAAY,KAAK,GAAG,MAAM,GAE1C,QAEA;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA,UAAS;AAAA,MACP,GAAG;AAAA,MACL,OAAQ,EAAE,UAAU,UAAU,cAAc,YAAY,YAAY,UAAU,SAAS;AAAA;AAAA,EACxF;AAEF;AAEA,IAAM,mBAAmB,MAAM;AAC9B,QAAM,CAAE,IAAI,KAAM,IAAI,SAAgC,IAAK;AAC3D,QAAM,CAAE,eAAe,cAAe,IAAI,SAAU,KAAM;AAE1D,YAAW,MAAM;AAChB,UAAM,WAAW,IAAI,eAAgB,CAAE,CAAE,EAAE,OAAO,CAAE,MAAO;AAC1D,qBAAgB,OAAO,cAAc,OAAO,WAAY;AAAA,IACzD,CAAE;AAEF,QAAK,IAAK;AACT,eAAS,QAAS,EAAG;AAAA,IACtB;AAEA,WAAO,MAAM;AACZ,eAAS,WAAW;AAAA,IACrB;AAAA,EACD,GAAG,CAAE,EAAG,CAAE;AAEV,SAAO,CAAE,OAAO,aAAc;AAC/B;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elementor/editor-ui",
|
|
3
|
+
"description": "Elementor Editor UI",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"private": false,
|
|
6
|
+
"author": "Elementor Team",
|
|
7
|
+
"homepage": "https://elementor.com/",
|
|
8
|
+
"license": "GPL-3.0-or-later",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/elementor/elementor-packages.git",
|
|
23
|
+
"directory": "packages/libs/editor-ui"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/elementor/elementor-packages/issues"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup --config=../../tsup.build.ts",
|
|
33
|
+
"dev": "tsup --config=../../tsup.dev.ts"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": "^18.3.1"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@elementor/ui": "1.24.1"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { Box, Tooltip } from '@elementor/ui';
|
|
4
|
+
|
|
5
|
+
type EllipsisWithTooltipProps< T extends React.ElementType > = {
|
|
6
|
+
maxWidth?: React.CSSProperties[ 'maxWidth' ];
|
|
7
|
+
title: string;
|
|
8
|
+
as?: T;
|
|
9
|
+
} & React.ComponentProps< T >;
|
|
10
|
+
|
|
11
|
+
export const EllipsisWithTooltip = < T extends React.ElementType >( {
|
|
12
|
+
maxWidth,
|
|
13
|
+
title,
|
|
14
|
+
as,
|
|
15
|
+
...props
|
|
16
|
+
}: EllipsisWithTooltipProps< T > ) => {
|
|
17
|
+
const [ setRef, isOverflowing ] = useIsOverflowing();
|
|
18
|
+
|
|
19
|
+
if ( isOverflowing ) {
|
|
20
|
+
return (
|
|
21
|
+
<Tooltip title={ title } placement="top">
|
|
22
|
+
<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>
|
|
23
|
+
{ title }
|
|
24
|
+
</Content>
|
|
25
|
+
</Tooltip>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<Content maxWidth={ maxWidth } ref={ setRef } as={ as } { ...props }>
|
|
31
|
+
{ title }
|
|
32
|
+
</Content>
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type ContentProps< T extends React.ElementType > = React.PropsWithChildren<
|
|
37
|
+
Omit< EllipsisWithTooltipProps< T >, 'title' >
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
const Content = React.forwardRef(
|
|
41
|
+
< T extends React.ElementType >(
|
|
42
|
+
{ maxWidth, as: Component = Box, ...props }: ContentProps< T >,
|
|
43
|
+
// forwardRef loses the typing when using generic components.
|
|
44
|
+
ref: unknown
|
|
45
|
+
) => (
|
|
46
|
+
<Component
|
|
47
|
+
ref={ ref }
|
|
48
|
+
position="relative"
|
|
49
|
+
{ ...props }
|
|
50
|
+
style={ { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth } }
|
|
51
|
+
/>
|
|
52
|
+
)
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const useIsOverflowing = () => {
|
|
56
|
+
const [ el, setEl ] = useState< HTMLElement | null >( null );
|
|
57
|
+
const [ isOverflowing, setIsOverflown ] = useState( false );
|
|
58
|
+
|
|
59
|
+
useEffect( () => {
|
|
60
|
+
const observer = new ResizeObserver( ( [ { target } ] ) => {
|
|
61
|
+
setIsOverflown( target.scrollWidth > target.clientWidth );
|
|
62
|
+
} );
|
|
63
|
+
|
|
64
|
+
if ( el ) {
|
|
65
|
+
observer.observe( el );
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return () => {
|
|
69
|
+
observer.disconnect();
|
|
70
|
+
};
|
|
71
|
+
}, [ el ] );
|
|
72
|
+
|
|
73
|
+
return [ setEl, isOverflowing ] as const;
|
|
74
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EllipsisWithTooltip } from './components/ellipsis-with-tooltip';
|