@0xchain/expandable-text 1.1.0-beta.2 → 1.1.0-beta.20
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/LICENSE +21 -0
- package/dist/index.js +135 -90
- package/package.json +16 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present 0xchain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,51 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsxs
|
|
3
|
-
import { useState
|
|
4
|
-
import { useTranslations
|
|
5
|
-
import { twMerge
|
|
6
|
-
const
|
|
7
|
-
text
|
|
8
|
-
maxLines
|
|
9
|
-
expandText
|
|
10
|
-
collapseText
|
|
11
|
-
className
|
|
12
|
-
showToggle
|
|
13
|
-
ellipsis
|
|
14
|
-
loading
|
|
15
|
-
onExpand
|
|
16
|
-
onCollapse
|
|
17
|
-
expandSymbol
|
|
18
|
-
collapseSymbol
|
|
19
|
-
onEllipsis
|
|
20
|
-
suffix
|
|
21
|
-
expandable
|
|
2
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useState, useRef, useEffect } from "react";
|
|
4
|
+
import { useTranslations } from "next-intl";
|
|
5
|
+
import { twMerge } from "tailwind-merge";
|
|
6
|
+
const ExpandableText = ({
|
|
7
|
+
text,
|
|
8
|
+
maxLines = 2,
|
|
9
|
+
expandText,
|
|
10
|
+
collapseText,
|
|
11
|
+
className,
|
|
12
|
+
showToggle = true,
|
|
13
|
+
ellipsis = "...",
|
|
14
|
+
loading,
|
|
15
|
+
onExpand,
|
|
16
|
+
onCollapse,
|
|
17
|
+
expandSymbol,
|
|
18
|
+
collapseSymbol,
|
|
19
|
+
onEllipsis,
|
|
20
|
+
suffix = "",
|
|
21
|
+
expandable = true
|
|
22
22
|
}) => {
|
|
23
|
-
const [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
24
|
+
const [isOverflowing, setIsOverflowing] = useState(false);
|
|
25
|
+
const [isReady, setIsReady] = useState(false);
|
|
26
|
+
const [truncatedText, setTruncatedText] = useState(null);
|
|
27
|
+
const textRef = useRef(null);
|
|
28
|
+
const measureRef = useRef(null);
|
|
29
|
+
const t = useTranslations("common.expandableText");
|
|
30
|
+
const defaultExpandText = expandText || t("expand");
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const element = textRef.current;
|
|
33
|
+
const measurer = measureRef.current;
|
|
34
|
+
if (!element || !measurer) return;
|
|
35
|
+
const expandLabelCandidate = (() => {
|
|
36
|
+
if (!showToggle || !expandable) return "";
|
|
37
|
+
if (typeof expandSymbol === "string") return String(expandSymbol);
|
|
38
|
+
return expandText || t("expand");
|
|
39
|
+
})();
|
|
40
|
+
const shouldReserveToggle = showToggle && expandable && !isExpanded;
|
|
41
|
+
const computeLineHeight = (el) => {
|
|
42
|
+
const cs = getComputedStyle(el);
|
|
43
|
+
const lh = parseFloat(cs.lineHeight);
|
|
44
|
+
return isNaN(lh) ? parseFloat(cs.fontSize) * 1.2 : lh;
|
|
45
|
+
};
|
|
46
|
+
const syncMeasureStyles = () => {
|
|
47
|
+
const cs = getComputedStyle(element);
|
|
48
|
+
const props = [
|
|
33
49
|
"font",
|
|
34
50
|
"fontFamily",
|
|
35
51
|
"fontSize",
|
|
@@ -42,51 +58,80 @@ const le = ({
|
|
|
42
58
|
"textTransform",
|
|
43
59
|
"textIndent",
|
|
44
60
|
"textRendering"
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
];
|
|
62
|
+
props.forEach((p) => {
|
|
63
|
+
measurer.style[p] = cs[p];
|
|
64
|
+
});
|
|
65
|
+
measurer.style.width = `${element.clientWidth}px`;
|
|
66
|
+
};
|
|
67
|
+
const doesFit = (candidate) => {
|
|
68
|
+
const withToggle = shouldReserveToggle && expandLabelCandidate ? `${candidate} ${expandLabelCandidate}` : candidate;
|
|
69
|
+
measurer.textContent = withToggle;
|
|
70
|
+
const lineHeight = computeLineHeight(measurer);
|
|
71
|
+
const maxHeight = lineHeight * maxLines;
|
|
72
|
+
return measurer.scrollHeight <= maxHeight + 0.5;
|
|
73
|
+
};
|
|
74
|
+
const measure = () => {
|
|
75
|
+
syncMeasureStyles();
|
|
76
|
+
const fullForOverflowBase = `${text ?? ""}${suffix ? suffix : ""}`;
|
|
77
|
+
const fullForOverflow = shouldReserveToggle && expandLabelCandidate ? `${fullForOverflowBase} ${expandLabelCandidate}` : fullForOverflowBase;
|
|
78
|
+
measurer.textContent = fullForOverflow;
|
|
79
|
+
const lineHeight = computeLineHeight(measurer);
|
|
80
|
+
const maxHeight = lineHeight * maxLines;
|
|
81
|
+
const overflowing = measurer.scrollHeight > maxHeight + 0.5;
|
|
82
|
+
setIsOverflowing((prev) => {
|
|
83
|
+
if (prev !== overflowing) onEllipsis?.(overflowing);
|
|
84
|
+
return overflowing;
|
|
85
|
+
});
|
|
86
|
+
if (overflowing && !isExpanded && text) {
|
|
87
|
+
const full = `${text}${suffix ? suffix : ""}`;
|
|
88
|
+
let low = 0;
|
|
89
|
+
let high = full.length;
|
|
90
|
+
let best = 0;
|
|
91
|
+
while (low <= high) {
|
|
92
|
+
const mid = Math.floor((low + high) / 2);
|
|
93
|
+
const candidate = `${full.slice(0, mid)}${ellipsis || ""}`;
|
|
94
|
+
if (doesFit(candidate)) {
|
|
95
|
+
best = mid;
|
|
96
|
+
low = mid + 1;
|
|
97
|
+
} else {
|
|
98
|
+
high = mid - 1;
|
|
99
|
+
}
|
|
64
100
|
}
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
} else
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
101
|
+
const finalText = `${full.slice(0, best)}${ellipsis || ""}`;
|
|
102
|
+
setTruncatedText(finalText);
|
|
103
|
+
} else {
|
|
104
|
+
setTruncatedText(null);
|
|
105
|
+
}
|
|
106
|
+
setIsReady(true);
|
|
107
|
+
};
|
|
108
|
+
const id = setTimeout(measure, 0);
|
|
109
|
+
const resizeObserver = new ResizeObserver(measure);
|
|
110
|
+
resizeObserver.observe(element);
|
|
111
|
+
resizeObserver.observe(measurer);
|
|
112
|
+
return () => {
|
|
113
|
+
clearTimeout(id);
|
|
114
|
+
resizeObserver.disconnect();
|
|
73
115
|
};
|
|
74
|
-
}, [
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
116
|
+
}, [text, maxLines, suffix, ellipsis, isExpanded]);
|
|
117
|
+
const handleExpand = () => {
|
|
118
|
+
setIsExpanded((prev) => {
|
|
119
|
+
const next = !prev;
|
|
120
|
+
if (next) onExpand?.();
|
|
121
|
+
else onCollapse?.();
|
|
122
|
+
return next;
|
|
79
123
|
});
|
|
80
124
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/* @__PURE__ */
|
|
125
|
+
if (!text) return null;
|
|
126
|
+
return /* @__PURE__ */ jsxs("div", { className: twMerge("relative", className), children: [
|
|
127
|
+
(loading || !isReady) && /* @__PURE__ */ jsxs("div", { className: twMerge("animate-pulse"), children: [
|
|
128
|
+
/* @__PURE__ */ jsx("div", { className: "h-4 bg-card rounded w-full mb-2" }),
|
|
129
|
+
/* @__PURE__ */ jsx("div", { className: "h-4 bg-card rounded w-3/4" })
|
|
85
130
|
] }),
|
|
86
|
-
/* @__PURE__ */
|
|
131
|
+
/* @__PURE__ */ jsx(
|
|
87
132
|
"div",
|
|
88
133
|
{
|
|
89
|
-
ref:
|
|
134
|
+
ref: measureRef,
|
|
90
135
|
className: "absolute left-0 top-0 pointer-events-none whitespace-pre-wrap break-words",
|
|
91
136
|
style: {
|
|
92
137
|
visibility: "hidden",
|
|
@@ -99,45 +144,45 @@ const le = ({
|
|
|
99
144
|
}
|
|
100
145
|
}
|
|
101
146
|
),
|
|
102
|
-
/* @__PURE__ */
|
|
147
|
+
/* @__PURE__ */ jsxs(
|
|
103
148
|
"div",
|
|
104
149
|
{
|
|
105
|
-
ref:
|
|
106
|
-
className:
|
|
150
|
+
ref: textRef,
|
|
151
|
+
className: twMerge(
|
|
107
152
|
"transition-all duration-300 ease-in-out align-justify space-x-1"
|
|
108
153
|
),
|
|
109
154
|
style: {
|
|
110
|
-
visibility:
|
|
111
|
-
position:
|
|
112
|
-
left:
|
|
113
|
-
top:
|
|
114
|
-
width:
|
|
115
|
-
display: !
|
|
116
|
-
WebkitLineClamp: !
|
|
155
|
+
visibility: isReady ? "visible" : "hidden",
|
|
156
|
+
position: isReady ? "static" : "absolute",
|
|
157
|
+
left: isReady ? void 0 : 0,
|
|
158
|
+
top: isReady ? void 0 : 0,
|
|
159
|
+
width: isReady ? void 0 : "100%",
|
|
160
|
+
display: !isExpanded && isOverflowing ? "-webkit-box" : "block",
|
|
161
|
+
WebkitLineClamp: !isExpanded && isOverflowing ? maxLines : "unset",
|
|
117
162
|
WebkitBoxOrient: "vertical",
|
|
118
|
-
overflow: !
|
|
119
|
-
textOverflow: !
|
|
163
|
+
overflow: !isExpanded && isOverflowing ? "hidden" : "visible",
|
|
164
|
+
textOverflow: !isExpanded && isOverflowing ? "ellipsis" : "clip"
|
|
120
165
|
},
|
|
121
|
-
"aria-label":
|
|
166
|
+
"aria-label": text,
|
|
122
167
|
children: [
|
|
123
|
-
/* @__PURE__ */
|
|
124
|
-
|
|
168
|
+
/* @__PURE__ */ jsx("span", { children: !isExpanded && isOverflowing && truncatedText != null ? truncatedText : `${text ?? ""}${isExpanded && suffix ? suffix : ""}` }),
|
|
169
|
+
isOverflowing && (showToggle && expandable) && /* @__PURE__ */ jsx(
|
|
125
170
|
"button",
|
|
126
171
|
{
|
|
127
172
|
type: "button",
|
|
128
|
-
onClick:
|
|
173
|
+
onClick: handleExpand,
|
|
129
174
|
className: "text-primary hover:underline focus:outline-none text-sm",
|
|
130
|
-
"aria-expanded":
|
|
131
|
-
"aria-label":
|
|
132
|
-
children:
|
|
175
|
+
"aria-expanded": isExpanded,
|
|
176
|
+
"aria-label": isExpanded ? collapseText || t("collapse") : defaultExpandText,
|
|
177
|
+
children: isExpanded ? collapseSymbol ?? collapseText ?? t("collapse") : expandSymbol ?? defaultExpandText
|
|
133
178
|
}
|
|
134
179
|
)
|
|
135
180
|
]
|
|
136
181
|
}
|
|
137
182
|
)
|
|
138
|
-
] })
|
|
183
|
+
] });
|
|
139
184
|
};
|
|
140
185
|
export {
|
|
141
|
-
|
|
142
|
-
|
|
186
|
+
ExpandableText,
|
|
187
|
+
ExpandableText as default
|
|
143
188
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xchain/expandable-text",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -13,18 +13,27 @@
|
|
|
13
13
|
"default": "./dist/index.js"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"rollup-plugin-preserve-use-client": "3.0.1"
|
|
18
|
-
},
|
|
19
16
|
"files": [
|
|
20
17
|
"dist",
|
|
21
18
|
"!**/*.tsbuildinfo"
|
|
22
19
|
],
|
|
23
|
-
"
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"next-intl": "4.8.2",
|
|
24
22
|
"react": "19.1.1",
|
|
25
23
|
"react-dom": "19.1.1",
|
|
26
|
-
"tailwind-merge": "3.3.1"
|
|
27
|
-
|
|
24
|
+
"tailwind-merge": "3.3.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"next-intl": "4.8.2",
|
|
28
|
+
"react": "19.1.1",
|
|
29
|
+
"react-dom": "19.1.1",
|
|
30
|
+
"rollup-plugin-preserve-use-client": "3.0.1",
|
|
31
|
+
"tailwind-merge": "3.3.1"
|
|
32
|
+
},
|
|
33
|
+
"nx": {
|
|
34
|
+
"tags": [
|
|
35
|
+
"type:ui"
|
|
36
|
+
]
|
|
28
37
|
},
|
|
29
38
|
"publishConfig": {
|
|
30
39
|
"access": "public"
|