@ohos-ports/react-truncate-list 0.0.0-stage → 1.0.5-beta.1
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 +41 -0
- package/dist/cjs/index.js +123 -0
- package/dist/esm/index.js +120 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/styles.css +13 -0
- package/package.json +38 -4
- package/src/index.tsx +168 -0
- package/src/styles.css +13 -0
- package/README.md +0 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.5 - 2025-10-24
|
|
4
|
+
|
|
5
|
+
### Fixes
|
|
6
|
+
|
|
7
|
+
- Fix `onResize` prop not updating if changed by the user.
|
|
8
|
+
|
|
9
|
+
## 1.0.4 - 2025-10-22
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
- Add onResize prop.
|
|
14
|
+
|
|
15
|
+
## 1.0.3 - 2023-09-03
|
|
16
|
+
|
|
17
|
+
### Fixes
|
|
18
|
+
|
|
19
|
+
- Add CommonJS support back in, as it was accidentally removed in 1.0.2.
|
|
20
|
+
|
|
21
|
+
## 1.0.2 - 2023-09-01
|
|
22
|
+
|
|
23
|
+
### Misc.
|
|
24
|
+
|
|
25
|
+
- Significant performance improvements for large lists.
|
|
26
|
+
|
|
27
|
+
## 1.0.1 - 2023-02-20
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
- Add CommonJS support.
|
|
32
|
+
|
|
33
|
+
## 1.0.0 - 2023-02-17
|
|
34
|
+
|
|
35
|
+
### Breaking changes
|
|
36
|
+
|
|
37
|
+
- The package's default export is now a named export: 'TruncatedList'.
|
|
38
|
+
|
|
39
|
+
### Fixes
|
|
40
|
+
|
|
41
|
+
- React peer dependency bumped from >=16.x.x to >=16.8.x, as the library uses hooks.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TruncatedList = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const rectContainsRect = (parent, child) => {
|
|
6
|
+
return (child.top >= parent.top && child.bottom <= parent.bottom && child.left >= parent.left && child.right <= parent.right);
|
|
7
|
+
};
|
|
8
|
+
const TruncatedList = ({ renderTruncator, alwaysShowTruncator, onResize, className, style, children, }) => {
|
|
9
|
+
const containerRef = (0, react_1.useRef)(null);
|
|
10
|
+
const truncate = (0, react_1.useCallback)(() => {
|
|
11
|
+
if (!containerRef.current) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const childNodes = Array.from(containerRef.current.children);
|
|
15
|
+
//
|
|
16
|
+
// Put the list in its initial state.
|
|
17
|
+
//
|
|
18
|
+
// Change from a scrollable container to overflow: hidden during hydration
|
|
19
|
+
containerRef.current.style.overflow = "hidden";
|
|
20
|
+
// Show all items, hide all truncators.
|
|
21
|
+
for (let i = 0; i < childNodes.length; ++i) {
|
|
22
|
+
childNodes[i].hidden = i % 2 === 0;
|
|
23
|
+
}
|
|
24
|
+
// If there are no items (the last truncator is always included).
|
|
25
|
+
if (childNodes.length === 1) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
//
|
|
29
|
+
// Test if truncation is necessary.
|
|
30
|
+
//
|
|
31
|
+
if (alwaysShowTruncator) {
|
|
32
|
+
// if the last truncator fits, exit
|
|
33
|
+
const truncatorEl = childNodes[childNodes.length - 1];
|
|
34
|
+
truncatorEl.hidden = false;
|
|
35
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
truncatorEl.hidden = true;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// if the last item fits, exit
|
|
42
|
+
const itemEl = childNodes[childNodes.length - 2];
|
|
43
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), itemEl.getBoundingClientRect())) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//
|
|
48
|
+
// Truncation is necessary - binary search to find the last truncator that can fit.
|
|
49
|
+
//
|
|
50
|
+
const numTruncators = Math.floor((childNodes.length - 1) / 2);
|
|
51
|
+
let left = 0;
|
|
52
|
+
let right = numTruncators - 1;
|
|
53
|
+
let truncatorIndex = null;
|
|
54
|
+
while (left <= right) {
|
|
55
|
+
const middle = Math.floor((left + right) / 2);
|
|
56
|
+
// show all items before the truncator
|
|
57
|
+
for (let i = 0; i < middle; i += 1) {
|
|
58
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
59
|
+
}
|
|
60
|
+
// hide all items after the truncator
|
|
61
|
+
for (let i = middle; i < numTruncators; i += 1) {
|
|
62
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
63
|
+
}
|
|
64
|
+
const truncatorEl = childNodes[middle * 2];
|
|
65
|
+
truncatorEl.hidden = false;
|
|
66
|
+
// check if this truncator fits
|
|
67
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
68
|
+
truncatorIndex = middle;
|
|
69
|
+
left = middle + 1;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
right = middle - 1;
|
|
73
|
+
}
|
|
74
|
+
truncatorEl.hidden = true;
|
|
75
|
+
}
|
|
76
|
+
// If we didn't find a truncator that fits, everything will be hidden at this point and we can exit early
|
|
77
|
+
if (truncatorIndex === null) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
//
|
|
81
|
+
// Now we have found the last truncator that fits, show it.
|
|
82
|
+
//
|
|
83
|
+
// show all items before the truncator
|
|
84
|
+
for (let i = 0; i < truncatorIndex; i += 1) {
|
|
85
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
86
|
+
}
|
|
87
|
+
// hide all items after truncator
|
|
88
|
+
for (let i = truncatorIndex; i < numTruncators; i += 1) {
|
|
89
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
90
|
+
}
|
|
91
|
+
const truncatorEl = childNodes[truncatorIndex * 2];
|
|
92
|
+
truncatorEl.hidden = false;
|
|
93
|
+
}, [alwaysShowTruncator]);
|
|
94
|
+
// Set up a resize observer
|
|
95
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
96
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
97
|
+
if (onResize) {
|
|
98
|
+
onResize({ truncate });
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
truncate();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
const containerEl = containerRef.current;
|
|
105
|
+
if (!containerEl) {
|
|
106
|
+
throw new Error("assert: container is mounted");
|
|
107
|
+
}
|
|
108
|
+
resizeObserver.observe(containerEl);
|
|
109
|
+
truncate();
|
|
110
|
+
return () => {
|
|
111
|
+
resizeObserver.unobserve(containerEl);
|
|
112
|
+
};
|
|
113
|
+
// trigger if children change also
|
|
114
|
+
}, [truncate, onResize, children]);
|
|
115
|
+
const childArray = react_1.Children.toArray(children);
|
|
116
|
+
const items = childArray.map((item, i) => (react_1.createElement(react_1.Fragment, { key: i },
|
|
117
|
+
react_1.createElement("li", { hidden: true }, renderTruncator({ hiddenItemsCount: childArray.length - i })),
|
|
118
|
+
react_1.createElement("li", null, item))));
|
|
119
|
+
return (react_1.createElement("ul", { ref: containerRef, className: `react-truncate-list ${className !== null && className !== void 0 ? className : ""}`, style: style },
|
|
120
|
+
items,
|
|
121
|
+
react_1.createElement("li", { hidden: true }, renderTruncator({ hiddenItemsCount: 0 }))));
|
|
122
|
+
};
|
|
123
|
+
exports.TruncatedList = TruncatedList;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import React, { useRef, useLayoutEffect, useCallback } from "react";
|
|
2
|
+
const rectContainsRect = (parent, child) => {
|
|
3
|
+
return (child.top >= parent.top && child.bottom <= parent.bottom && child.left >= parent.left && child.right <= parent.right);
|
|
4
|
+
};
|
|
5
|
+
export const TruncatedList = ({ renderTruncator, alwaysShowTruncator, onResize, className, style, children, }) => {
|
|
6
|
+
const containerRef = useRef(null);
|
|
7
|
+
const truncate = useCallback(() => {
|
|
8
|
+
if (!containerRef.current) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const childNodes = Array.from(containerRef.current.children);
|
|
12
|
+
//
|
|
13
|
+
// Put the list in its initial state.
|
|
14
|
+
//
|
|
15
|
+
// Change from a scrollable container to overflow: hidden during hydration
|
|
16
|
+
containerRef.current.style.overflow = "hidden";
|
|
17
|
+
// Show all items, hide all truncators.
|
|
18
|
+
for (let i = 0; i < childNodes.length; ++i) {
|
|
19
|
+
childNodes[i].hidden = i % 2 === 0;
|
|
20
|
+
}
|
|
21
|
+
// If there are no items (the last truncator is always included).
|
|
22
|
+
if (childNodes.length === 1) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
//
|
|
26
|
+
// Test if truncation is necessary.
|
|
27
|
+
//
|
|
28
|
+
if (alwaysShowTruncator) {
|
|
29
|
+
// if the last truncator fits, exit
|
|
30
|
+
const truncatorEl = childNodes[childNodes.length - 1];
|
|
31
|
+
truncatorEl.hidden = false;
|
|
32
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
truncatorEl.hidden = true;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// if the last item fits, exit
|
|
39
|
+
const itemEl = childNodes[childNodes.length - 2];
|
|
40
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), itemEl.getBoundingClientRect())) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//
|
|
45
|
+
// Truncation is necessary - binary search to find the last truncator that can fit.
|
|
46
|
+
//
|
|
47
|
+
const numTruncators = Math.floor((childNodes.length - 1) / 2);
|
|
48
|
+
let left = 0;
|
|
49
|
+
let right = numTruncators - 1;
|
|
50
|
+
let truncatorIndex = null;
|
|
51
|
+
while (left <= right) {
|
|
52
|
+
const middle = Math.floor((left + right) / 2);
|
|
53
|
+
// show all items before the truncator
|
|
54
|
+
for (let i = 0; i < middle; i += 1) {
|
|
55
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
56
|
+
}
|
|
57
|
+
// hide all items after the truncator
|
|
58
|
+
for (let i = middle; i < numTruncators; i += 1) {
|
|
59
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
60
|
+
}
|
|
61
|
+
const truncatorEl = childNodes[middle * 2];
|
|
62
|
+
truncatorEl.hidden = false;
|
|
63
|
+
// check if this truncator fits
|
|
64
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
65
|
+
truncatorIndex = middle;
|
|
66
|
+
left = middle + 1;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
right = middle - 1;
|
|
70
|
+
}
|
|
71
|
+
truncatorEl.hidden = true;
|
|
72
|
+
}
|
|
73
|
+
// If we didn't find a truncator that fits, everything will be hidden at this point and we can exit early
|
|
74
|
+
if (truncatorIndex === null) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
//
|
|
78
|
+
// Now we have found the last truncator that fits, show it.
|
|
79
|
+
//
|
|
80
|
+
// show all items before the truncator
|
|
81
|
+
for (let i = 0; i < truncatorIndex; i += 1) {
|
|
82
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
83
|
+
}
|
|
84
|
+
// hide all items after truncator
|
|
85
|
+
for (let i = truncatorIndex; i < numTruncators; i += 1) {
|
|
86
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
87
|
+
}
|
|
88
|
+
const truncatorEl = childNodes[truncatorIndex * 2];
|
|
89
|
+
truncatorEl.hidden = false;
|
|
90
|
+
}, [alwaysShowTruncator]);
|
|
91
|
+
// Set up a resize observer
|
|
92
|
+
useLayoutEffect(() => {
|
|
93
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
94
|
+
if (onResize) {
|
|
95
|
+
onResize({ truncate });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
truncate();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
const containerEl = containerRef.current;
|
|
102
|
+
if (!containerEl) {
|
|
103
|
+
throw new Error("assert: container is mounted");
|
|
104
|
+
}
|
|
105
|
+
resizeObserver.observe(containerEl);
|
|
106
|
+
truncate();
|
|
107
|
+
return () => {
|
|
108
|
+
resizeObserver.unobserve(containerEl);
|
|
109
|
+
};
|
|
110
|
+
// trigger if children change also
|
|
111
|
+
}, [truncate, onResize, children]);
|
|
112
|
+
const childArray = React.Children.toArray(children);
|
|
113
|
+
const items = childArray.map((item, i) => (React.createElement(React.Fragment, { key: i },
|
|
114
|
+
React.createElement("li", { hidden: true }, renderTruncator({ hiddenItemsCount: childArray.length - i })),
|
|
115
|
+
React.createElement("li", null, item))));
|
|
116
|
+
return (React.createElement("ul", { ref: containerRef, className: `react-truncate-list ${className ?? ""}`, style: style },
|
|
117
|
+
items,
|
|
118
|
+
React.createElement("li", { hidden: true }, renderTruncator({ hiddenItemsCount: 0 }))));
|
|
119
|
+
};
|
|
120
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,MAAM,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAepE,MAAM,gBAAgB,GAAG,CAAC,MAAe,EAAE,KAAc,EAAE,EAAE;IAC3D,OAAO,CACL,KAAK,CAAC,GAAG,IAAI,MAAM,CAAC,GAAG,IAAI,KAAK,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,KAAK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emsCount: number;
|
|
4
|
+
}) => React.ReactNode;
|
|
5
|
+
type OnResizeFn = (bag: {
|
|
6
|
+
truncate: () => void;
|
|
7
|
+
}) => void;
|
|
8
|
+
export type TruncatedListProps = {
|
|
9
|
+
renderTruncator: RenderTruncatorFn;
|
|
10
|
+
alwaysShowTruncator?: boolean;
|
|
11
|
+
onResize?: OnResizeFn;
|
|
12
|
+
className?: string;
|
|
13
|
+
style?: React.CSSProperties;
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
};
|
|
16
|
+
export declare const TruncatedList: ({ renderTruncator, alwaysShowTruncator, onResize, className, style, children, }: TruncatedListProps) => React.JSX.Element;
|
|
17
|
+
export {};
|
package/dist/styles.css
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/react-truncate-list",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.5-beta.1",
|
|
4
|
+
"description": "Truncate a list of elements with a symbol or component of your choice",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ohos-ports/ohos-ports.git",
|
|
8
|
+
"directory": "ports/react-truncate-list/1.0.5"
|
|
9
|
+
},
|
|
10
|
+
"author": "bradford.lachlan@gmail.com",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"truncate",
|
|
13
|
+
"truncate list",
|
|
14
|
+
"ellipsis",
|
|
15
|
+
"react"
|
|
16
|
+
],
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"main": "./dist/cjs/index.js",
|
|
19
|
+
"module": "./dist/esm/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/",
|
|
23
|
+
"src/",
|
|
24
|
+
"package.json",
|
|
25
|
+
"CHANGELOG.md"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/react": "^19.2.2",
|
|
29
|
+
"eslint": "^9.36.0",
|
|
30
|
+
"eslint-plugin-react-hooks": "^7.0.0",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"typescript-eslint": "^8.45.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=16.8.x"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import React, { useRef, useLayoutEffect, useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
type RenderTruncatorFn = (state: { hiddenItemsCount: number }) => React.ReactNode;
|
|
4
|
+
|
|
5
|
+
type OnResizeFn = (bag: { truncate: () => void }) => void;
|
|
6
|
+
|
|
7
|
+
export type TruncatedListProps = {
|
|
8
|
+
renderTruncator: RenderTruncatorFn;
|
|
9
|
+
alwaysShowTruncator?: boolean;
|
|
10
|
+
onResize?: OnResizeFn;
|
|
11
|
+
className?: string;
|
|
12
|
+
style?: React.CSSProperties;
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const rectContainsRect = (parent: DOMRect, child: DOMRect) => {
|
|
17
|
+
return (
|
|
18
|
+
child.top >= parent.top && child.bottom <= parent.bottom && child.left >= parent.left && child.right <= parent.right
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const TruncatedList = ({
|
|
23
|
+
renderTruncator,
|
|
24
|
+
alwaysShowTruncator,
|
|
25
|
+
onResize,
|
|
26
|
+
className,
|
|
27
|
+
style,
|
|
28
|
+
children,
|
|
29
|
+
}: TruncatedListProps) => {
|
|
30
|
+
const containerRef = useRef<HTMLUListElement>(null);
|
|
31
|
+
|
|
32
|
+
const truncate = useCallback(() => {
|
|
33
|
+
if (!containerRef.current) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const childNodes = Array.from(containerRef.current.children) as HTMLElement[];
|
|
37
|
+
|
|
38
|
+
//
|
|
39
|
+
// Put the list in its initial state.
|
|
40
|
+
//
|
|
41
|
+
// Change from a scrollable container to overflow: hidden during hydration
|
|
42
|
+
containerRef.current.style.overflow = "hidden";
|
|
43
|
+
// Show all items, hide all truncators.
|
|
44
|
+
for (let i = 0; i < childNodes.length; ++i) {
|
|
45
|
+
childNodes[i].hidden = i % 2 === 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// If there are no items (the last truncator is always included).
|
|
49
|
+
if (childNodes.length === 1) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//
|
|
54
|
+
// Test if truncation is necessary.
|
|
55
|
+
//
|
|
56
|
+
if (alwaysShowTruncator) {
|
|
57
|
+
// if the last truncator fits, exit
|
|
58
|
+
const truncatorEl = childNodes[childNodes.length - 1];
|
|
59
|
+
truncatorEl.hidden = false;
|
|
60
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
truncatorEl.hidden = true;
|
|
64
|
+
} else {
|
|
65
|
+
// if the last item fits, exit
|
|
66
|
+
const itemEl = childNodes[childNodes.length - 2];
|
|
67
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), itemEl.getBoundingClientRect())) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//
|
|
73
|
+
// Truncation is necessary - binary search to find the last truncator that can fit.
|
|
74
|
+
//
|
|
75
|
+
const numTruncators = Math.floor((childNodes.length - 1) / 2);
|
|
76
|
+
let left = 0;
|
|
77
|
+
let right = numTruncators - 1;
|
|
78
|
+
let truncatorIndex: number | null = null;
|
|
79
|
+
|
|
80
|
+
while (left <= right) {
|
|
81
|
+
const middle = Math.floor((left + right) / 2);
|
|
82
|
+
|
|
83
|
+
// show all items before the truncator
|
|
84
|
+
for (let i = 0; i < middle; i += 1) {
|
|
85
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
86
|
+
}
|
|
87
|
+
// hide all items after the truncator
|
|
88
|
+
for (let i = middle; i < numTruncators; i += 1) {
|
|
89
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const truncatorEl = childNodes[middle * 2];
|
|
93
|
+
truncatorEl.hidden = false;
|
|
94
|
+
|
|
95
|
+
// check if this truncator fits
|
|
96
|
+
if (rectContainsRect(containerRef.current.getBoundingClientRect(), truncatorEl.getBoundingClientRect())) {
|
|
97
|
+
truncatorIndex = middle;
|
|
98
|
+
left = middle + 1;
|
|
99
|
+
} else {
|
|
100
|
+
right = middle - 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
truncatorEl.hidden = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// If we didn't find a truncator that fits, everything will be hidden at this point and we can exit early
|
|
107
|
+
if (truncatorIndex === null) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//
|
|
112
|
+
// Now we have found the last truncator that fits, show it.
|
|
113
|
+
//
|
|
114
|
+
// show all items before the truncator
|
|
115
|
+
for (let i = 0; i < truncatorIndex; i += 1) {
|
|
116
|
+
childNodes[i * 2 + 1].hidden = false;
|
|
117
|
+
}
|
|
118
|
+
// hide all items after truncator
|
|
119
|
+
for (let i = truncatorIndex; i < numTruncators; i += 1) {
|
|
120
|
+
childNodes[i * 2 + 1].hidden = true;
|
|
121
|
+
}
|
|
122
|
+
const truncatorEl = childNodes[truncatorIndex * 2];
|
|
123
|
+
truncatorEl.hidden = false;
|
|
124
|
+
}, [alwaysShowTruncator]);
|
|
125
|
+
|
|
126
|
+
// Set up a resize observer
|
|
127
|
+
useLayoutEffect(() => {
|
|
128
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
129
|
+
if (onResize) {
|
|
130
|
+
onResize({ truncate });
|
|
131
|
+
} else {
|
|
132
|
+
truncate();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const containerEl = containerRef.current;
|
|
137
|
+
|
|
138
|
+
if (!containerEl) {
|
|
139
|
+
throw new Error("assert: container is mounted");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
resizeObserver.observe(containerEl);
|
|
143
|
+
|
|
144
|
+
truncate();
|
|
145
|
+
|
|
146
|
+
return () => {
|
|
147
|
+
resizeObserver.unobserve(containerEl);
|
|
148
|
+
};
|
|
149
|
+
// trigger if children change also
|
|
150
|
+
}, [truncate, onResize, children]);
|
|
151
|
+
|
|
152
|
+
const childArray = React.Children.toArray(children);
|
|
153
|
+
|
|
154
|
+
const items = childArray.map((item, i) => (
|
|
155
|
+
<React.Fragment key={i}>
|
|
156
|
+
<li hidden>{renderTruncator({ hiddenItemsCount: childArray.length - i })}</li>
|
|
157
|
+
<li>{item}</li>
|
|
158
|
+
</React.Fragment>
|
|
159
|
+
));
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<ul ref={containerRef} className={`react-truncate-list ${className ?? ""}`} style={style}>
|
|
163
|
+
{items}
|
|
164
|
+
|
|
165
|
+
<li hidden>{renderTruncator({ hiddenItemsCount: 0 })}</li>
|
|
166
|
+
</ul>
|
|
167
|
+
);
|
|
168
|
+
};
|
package/src/styles.css
ADDED
package/README.md
DELETED