@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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.js +135 -90
  3. 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 as S, jsx as u } from "react/jsx-runtime";
3
- import { useState as b, useRef as G, useEffect as te } from "react";
4
- import { useTranslations as ne } from "next-intl";
5
- import { twMerge as F } from "tailwind-merge";
6
- const le = ({
7
- text: s,
8
- maxLines: f = 2,
9
- expandText: W,
10
- collapseText: z,
11
- className: J,
12
- showToggle: g = !0,
13
- ellipsis: w = "...",
14
- loading: K,
15
- onExpand: y,
16
- onCollapse: $,
17
- expandSymbol: H,
18
- collapseSymbol: P,
19
- onEllipsis: x,
20
- suffix: i = "",
21
- expandable: k = !0
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 [t, Q] = b(!1), [l, U] = b(!1), [c, V] = b(!1), [I, C] = b(null), j = G(null), B = G(null), h = ne("common.expandableText"), M = W || h("expand");
24
- te(() => {
25
- const a = j.current, e = B.current;
26
- if (!a || !e) return;
27
- const m = !g || !k ? "" : typeof H == "string" ? String(H) : W || h("expand"), L = g && k && !t, q = (n) => {
28
- const r = getComputedStyle(n), o = parseFloat(r.lineHeight);
29
- return isNaN(o) ? parseFloat(r.fontSize) * 1.2 : o;
30
- }, Y = () => {
31
- const n = getComputedStyle(a);
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
- ].forEach((o) => {
46
- e.style[o] = n[o];
47
- }), e.style.width = `${a.clientWidth}px`;
48
- }, Z = (n) => {
49
- const r = L && m ? `${n} ${m}` : n;
50
- e.textContent = r;
51
- const T = q(e) * f;
52
- return e.scrollHeight <= T + 0.5;
53
- }, A = () => {
54
- Y();
55
- const n = `${s ?? ""}${i || ""}`, r = L && m ? `${n} ${m}` : n;
56
- e.textContent = r;
57
- const T = q(e) * f, v = e.scrollHeight > T + 0.5;
58
- if (U((d) => (d !== v && (x == null || x(v)), v)), v && !t && s) {
59
- const d = `${s}${i || ""}`;
60
- let O = 0, R = d.length, D = 0;
61
- for (; O <= R; ) {
62
- const p = Math.floor((O + R) / 2), ee = `${d.slice(0, p)}${w || ""}`;
63
- Z(ee) ? (D = p, O = p + 1) : R = p - 1;
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 E = `${d.slice(0, D)}${w || ""}`;
66
- C(E);
67
- } else
68
- C(null);
69
- V(!0);
70
- }, _ = setTimeout(A, 0), N = new ResizeObserver(A);
71
- return N.observe(a), N.observe(e), () => {
72
- clearTimeout(_), N.disconnect();
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
- }, [s, f, i, w, t]);
75
- const X = () => {
76
- Q((a) => {
77
- const e = !a;
78
- return e ? y == null || y() : $ == null || $(), e;
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
- return s ? /* @__PURE__ */ S("div", { className: F("relative", J), children: [
82
- (K || !c) && /* @__PURE__ */ S("div", { className: F("animate-pulse"), children: [
83
- /* @__PURE__ */ u("div", { className: "h-4 bg-card rounded w-full mb-2" }),
84
- /* @__PURE__ */ u("div", { className: "h-4 bg-card rounded w-3/4" })
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__ */ u(
131
+ /* @__PURE__ */ jsx(
87
132
  "div",
88
133
  {
89
- ref: B,
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__ */ S(
147
+ /* @__PURE__ */ jsxs(
103
148
  "div",
104
149
  {
105
- ref: j,
106
- className: F(
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: c ? "visible" : "hidden",
111
- position: c ? "static" : "absolute",
112
- left: c ? void 0 : 0,
113
- top: c ? void 0 : 0,
114
- width: c ? void 0 : "100%",
115
- display: !t && l ? "-webkit-box" : "block",
116
- WebkitLineClamp: !t && l ? f : "unset",
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: !t && l ? "hidden" : "visible",
119
- textOverflow: !t && l ? "ellipsis" : "clip"
163
+ overflow: !isExpanded && isOverflowing ? "hidden" : "visible",
164
+ textOverflow: !isExpanded && isOverflowing ? "ellipsis" : "clip"
120
165
  },
121
- "aria-label": s,
166
+ "aria-label": text,
122
167
  children: [
123
- /* @__PURE__ */ u("span", { children: !t && l && I != null ? I : `${s ?? ""}${t && i ? i : ""}` }),
124
- l && g && k && /* @__PURE__ */ u(
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: X,
173
+ onClick: handleExpand,
129
174
  className: "text-primary hover:underline focus:outline-none text-sm",
130
- "aria-expanded": t,
131
- "aria-label": t ? z || h("collapse") : M,
132
- children: t ? P ?? z ?? h("collapse") : H ?? M
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
- ] }) : null;
183
+ ] });
139
184
  };
140
185
  export {
141
- le as ExpandableText,
142
- le as default
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.2",
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
- "dependencies": {
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
- "next-intl": "4.6.1"
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"