@brightlocal/ui-components 1.2.3 → 1.3.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/README.md +1 -1
- package/component-meta.json +492 -113
- package/dist/blocks/input-list.cjs +2 -2
- package/dist/blocks/input-list.d.ts +2 -9
- package/dist/blocks/input-list.d.ts.map +1 -1
- package/dist/blocks/input-list.js +134 -173
- package/dist/hooks/use-prefers-reduced-motion.cjs +1 -0
- package/dist/hooks/use-prefers-reduced-motion.d.ts +7 -0
- package/dist/hooks/use-prefers-reduced-motion.d.ts.map +1 -0
- package/dist/hooks/use-prefers-reduced-motion.js +18 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +133 -129
- package/dist/text-reveal-motion.cjs +1 -0
- package/dist/text-reveal-motion.d.ts +73 -0
- package/dist/text-reveal-motion.d.ts.map +1 -0
- package/dist/text-reveal-motion.js +315 -0
- package/package.json +5 -3
- package/src/blocks/input-list.tsx +97 -152
- package/src/components/text-reveal-motion.tsx +568 -0
- package/src/hooks/use-prefers-reduced-motion.ts +28 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { jsx as m, jsxs as I } from "react/jsx-runtime";
|
|
2
|
+
import * as V from "react";
|
|
3
|
+
import { useId as gt, useRef as R, useState as Ct, useCallback as yt, useEffect as Z } from "react";
|
|
4
|
+
import { animate as X } from "framer-motion";
|
|
5
|
+
import { interpolate as bt } from "flubber";
|
|
6
|
+
import { cn as wt } from "@brightlocal/ui-components/lib/utils";
|
|
7
|
+
import { usePrefersReducedMotion as vt } from "./hooks/use-prefers-reduced-motion.js";
|
|
8
|
+
const Y = "M250 500C388.071 500 500 388.071 500 250C500 111.929 388.071 0 250 0C111.929 0 0 111.929 0 250C0 388.071 111.929 500 250 500Z", tt = "M492.425 259.355C332.265 293.145 293.145 332.265 259.355 492.425C257.225 502.525 242.775 502.525 240.645 492.425C206.855 332.265 167.735 293.145 7.575 259.355C-2.525 257.225 -2.525 242.775 7.575 240.645C167.735 206.855 206.855 167.735 240.645 7.575C242.775 -2.525 257.225 -2.525 259.355 7.575C293.145 167.735 332.265 206.855 492.425 240.645C502.525 242.775 502.525 257.225 492.425 259.355Z", et = "M258.366 5.58795L296.379 97.3854C298.509 102.526 304.729 104.546 309.47 101.636L394.177 49.7215C401.057 45.5112 409.598 51.7117 407.708 59.5523L384.506 156.16C383.206 161.571 387.046 166.861 392.596 167.301L491.645 175.092C499.685 175.722 502.945 185.763 496.815 191.003L421.259 255.528C417.029 259.138 417.029 265.679 421.259 269.299L496.815 333.824C502.945 339.065 499.685 349.106 491.645 349.736L392.596 357.526C387.046 357.966 383.206 363.257 384.506 368.667L407.708 465.275C409.588 473.116 401.047 479.326 394.177 475.106L309.47 423.192C304.729 420.282 298.509 422.302 296.379 427.442L258.366 519.24C255.275 526.69 244.725 526.69 241.634 519.24L203.621 427.442C201.491 422.302 195.271 420.282 190.53 423.192L105.823 475.106C98.9428 479.316 90.4021 473.116 92.2922 465.275L115.494 368.667C116.794 363.257 112.954 357.966 107.403 357.526L8.35543 349.736C0.314784 349.106 -2.94548 339.065 3.18502 333.824L78.7411 269.299C82.9715 265.689 82.9715 259.148 78.7411 255.528L3.18502 191.003C-2.94548 185.763 0.314784 175.722 8.35543 175.092L107.403 167.301C112.954 166.861 116.794 161.571 115.494 156.16L92.2922 59.5523C90.4121 51.7117 98.9528 45.5012 105.823 49.7215L190.53 101.636C195.271 104.546 201.491 102.526 203.621 97.3854L241.634 5.58795C244.725 -1.86265 255.275 -1.86265 258.366 5.58795Z", L = (A) => A < 0 ? 0 : A > 1 ? 1 : A, xt = [
|
|
9
|
+
Y,
|
|
10
|
+
tt,
|
|
11
|
+
et,
|
|
12
|
+
Y,
|
|
13
|
+
et,
|
|
14
|
+
tt
|
|
15
|
+
], Mt = V.forwardRef(
|
|
16
|
+
({
|
|
17
|
+
className: A,
|
|
18
|
+
dataHook: nt,
|
|
19
|
+
holdTime: S = 1300,
|
|
20
|
+
shimmer: j = !0,
|
|
21
|
+
gradientFrom: rt = "var(--loading-gradient-from)",
|
|
22
|
+
gradientTo: st = "var(--loading-gradient-to)",
|
|
23
|
+
trackingEl: at,
|
|
24
|
+
trackingLabel: ot,
|
|
25
|
+
children: it,
|
|
26
|
+
...lt
|
|
27
|
+
}, ct) => {
|
|
28
|
+
const o = xt, T = 0.8, F = 180, z = 24, H = 1, k = vt(), D = `text-reveal-gradient-${gt().replace(/:/g, "")}`, N = R(null), U = R(null), B = R(null), _ = R(null), G = R(null), C = R(/* @__PURE__ */ new Map()), P = V.Children.toArray(it), [ft, pt] = Ct({ widths: [], ballSize: 0 }), { widths: b, ballSize: i } = ft, dt = yt(
|
|
29
|
+
(e) => (t) => {
|
|
30
|
+
t ? C.current.set(e, t) : C.current.delete(e);
|
|
31
|
+
},
|
|
32
|
+
[]
|
|
33
|
+
);
|
|
34
|
+
Z(() => {
|
|
35
|
+
if (C.current.size === 0) return;
|
|
36
|
+
const e = requestAnimationFrame(() => {
|
|
37
|
+
const t = [];
|
|
38
|
+
let c = 32;
|
|
39
|
+
C.current.forEach((p, n) => {
|
|
40
|
+
if (t[n] = p.scrollWidth, n === 0) {
|
|
41
|
+
const w = p.firstElementChild ?? p, h = parseFloat(getComputedStyle(w).fontSize) || 16;
|
|
42
|
+
c = Math.round(h * 1.5);
|
|
43
|
+
}
|
|
44
|
+
}), pt({ widths: t, ballSize: c });
|
|
45
|
+
});
|
|
46
|
+
return () => cancelAnimationFrame(e);
|
|
47
|
+
}, [P.length]), Z(() => {
|
|
48
|
+
if (k) return;
|
|
49
|
+
const e = G.current;
|
|
50
|
+
if (!e) return;
|
|
51
|
+
const t = e;
|
|
52
|
+
let c, p = null;
|
|
53
|
+
const n = 0.12 + Math.random() * 0.06, w = 0.08 + Math.random() * 0.04, h = Math.random() * Math.PI * 2, q = Math.random() * Math.PI * 2;
|
|
54
|
+
let s = -1 / 0;
|
|
55
|
+
function a(l) {
|
|
56
|
+
p || (p = l);
|
|
57
|
+
const v = (l - p) / 1e3, f = Math.sin(v * n + h) * Math.PI + Math.sin(v * w + q) * 0.5;
|
|
58
|
+
if (Math.abs(f - s) > 0.01) {
|
|
59
|
+
s = f;
|
|
60
|
+
const u = Math.cos(f), x = Math.sin(f);
|
|
61
|
+
t.setAttribute("x1", `${50 + 45 * u}%`), t.setAttribute("y1", `${50 + 45 * x}%`), t.setAttribute("x2", `${50 - 45 * u}%`), t.setAttribute("y2", `${50 - 45 * x}%`);
|
|
62
|
+
}
|
|
63
|
+
c = requestAnimationFrame(a);
|
|
64
|
+
}
|
|
65
|
+
return c = requestAnimationFrame(a), () => cancelAnimationFrame(c);
|
|
66
|
+
}, [k]);
|
|
67
|
+
function mt(e) {
|
|
68
|
+
e.style.opacity = "1", X(0, 1, {
|
|
69
|
+
duration: 1.2,
|
|
70
|
+
ease: [0.25, 1, 0.5, 1],
|
|
71
|
+
onUpdate: (t) => {
|
|
72
|
+
e.style.transform = `translateX(${-100 + t * 200}%)`;
|
|
73
|
+
},
|
|
74
|
+
onComplete: () => {
|
|
75
|
+
e.style.opacity = "0";
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
Z(() => {
|
|
80
|
+
if (!B.current || !_.current || !N.current || !U.current || b.length === 0 || i === 0)
|
|
81
|
+
return;
|
|
82
|
+
const e = B.current, t = _.current, c = N.current, p = U.current;
|
|
83
|
+
if (k) {
|
|
84
|
+
e.setAttribute("d", o[0]), t.style.opacity = "1";
|
|
85
|
+
const s = C.current.get(0);
|
|
86
|
+
s && (s.style.visibility = "visible", s.style.clipPath = "none");
|
|
87
|
+
const a = i * 0.5, l = (b[0] ?? 0) + a + i / 2;
|
|
88
|
+
c.style.transform = `translateX(${l}px)`;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let n = !1;
|
|
92
|
+
const w = i * 0.5, h = [];
|
|
93
|
+
for (let s = 0; s < o.length; s++)
|
|
94
|
+
h.push(
|
|
95
|
+
bt(o[s], o[(s + 1) % o.length])
|
|
96
|
+
);
|
|
97
|
+
async function q() {
|
|
98
|
+
t.style.opacity = "1";
|
|
99
|
+
let s = 0, a = 0, l = 0;
|
|
100
|
+
for (; !n; ) {
|
|
101
|
+
const v = s % P.length, f = b[v] ?? 0, u = -(w + i / 2), x = f + w + i / 2, J = x - u, W = C.current.get(v);
|
|
102
|
+
if (!W) break;
|
|
103
|
+
C.current.forEach((r, y) => {
|
|
104
|
+
r.style.visibility = y === v ? "visible" : "hidden", r.style.clipPath = "inset(0 100% 0 0)";
|
|
105
|
+
});
|
|
106
|
+
const ht = h[a % o.length], $ = 360, K = l;
|
|
107
|
+
if (await new Promise((r) => {
|
|
108
|
+
if (n) {
|
|
109
|
+
r();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const y = X(u, x, {
|
|
113
|
+
type: "spring",
|
|
114
|
+
stiffness: F * T,
|
|
115
|
+
damping: z,
|
|
116
|
+
mass: H,
|
|
117
|
+
onUpdate: (d) => {
|
|
118
|
+
if (n) {
|
|
119
|
+
y.stop(), r();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
c.style.transform = `translateX(${d}px)`;
|
|
123
|
+
const M = f > 0 ? 100 - L(d / f) * 100 : 100;
|
|
124
|
+
W.style.clipPath = `inset(0 ${M}% 0 0)`;
|
|
125
|
+
const g = (d - u) / J;
|
|
126
|
+
t.style.transform = `rotate(${K + g * $}deg)`, e.setAttribute("d", ht(L(g)));
|
|
127
|
+
},
|
|
128
|
+
onComplete: () => {
|
|
129
|
+
l = K + $, a = (a + 1) % o.length, r();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}), n) break;
|
|
133
|
+
if (j && mt(p), S > 0) {
|
|
134
|
+
const r = performance.now();
|
|
135
|
+
for (; !n && performance.now() - r < S && (await new Promise((E) => setTimeout(E, 300)), !(n || performance.now() - r >= S)); ) {
|
|
136
|
+
const y = (a + 1) % o.length, d = h[a % o.length], M = l, g = l + 360;
|
|
137
|
+
await X(0, 1, {
|
|
138
|
+
type: "spring",
|
|
139
|
+
stiffness: 30,
|
|
140
|
+
damping: 9,
|
|
141
|
+
mass: 0.5,
|
|
142
|
+
onUpdate: (E) => {
|
|
143
|
+
e.setAttribute("d", d(L(E))), t.style.transform = `rotate(${M + E * (g - M)}deg)`;
|
|
144
|
+
}
|
|
145
|
+
}), l = g, a = y;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (n) break;
|
|
149
|
+
const ut = h[a % o.length], Q = l;
|
|
150
|
+
if (await new Promise((r) => {
|
|
151
|
+
if (n) {
|
|
152
|
+
r();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const y = X(x, u, {
|
|
156
|
+
type: "spring",
|
|
157
|
+
stiffness: F * T,
|
|
158
|
+
damping: z,
|
|
159
|
+
mass: H,
|
|
160
|
+
onUpdate: (d) => {
|
|
161
|
+
if (n) {
|
|
162
|
+
y.stop(), r();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
c.style.transform = `translateX(${d}px)`;
|
|
166
|
+
const M = f > 0 ? 100 - L(d / f) * 100 : 100;
|
|
167
|
+
W.style.clipPath = `inset(0 ${M}% 0 0)`;
|
|
168
|
+
const g = 1 - (d - u) / J;
|
|
169
|
+
t.style.transform = `rotate(${Q - g * $}deg)`, e.setAttribute("d", ut(L(g)));
|
|
170
|
+
},
|
|
171
|
+
onComplete: () => {
|
|
172
|
+
l = Q - $, a = (a + 1) % o.length, r();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}), n) break;
|
|
176
|
+
await new Promise((r) => setTimeout(r, 300)), s++;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return q(), () => {
|
|
180
|
+
n = !0;
|
|
181
|
+
};
|
|
182
|
+
}, [
|
|
183
|
+
b,
|
|
184
|
+
i,
|
|
185
|
+
P.length,
|
|
186
|
+
k,
|
|
187
|
+
T,
|
|
188
|
+
S,
|
|
189
|
+
j,
|
|
190
|
+
F,
|
|
191
|
+
z,
|
|
192
|
+
H,
|
|
193
|
+
o
|
|
194
|
+
]);
|
|
195
|
+
const O = b.length > 0 ? Math.max(...b) : 0;
|
|
196
|
+
return /* @__PURE__ */ m(
|
|
197
|
+
"div",
|
|
198
|
+
{
|
|
199
|
+
ref: ct,
|
|
200
|
+
className: wt("relative inline-flex items-center", A),
|
|
201
|
+
"data-hook": nt,
|
|
202
|
+
"data-slot": "text-reveal",
|
|
203
|
+
"data-tracking-el": at,
|
|
204
|
+
"data-tracking-label": ot,
|
|
205
|
+
...lt,
|
|
206
|
+
children: /* @__PURE__ */ I(
|
|
207
|
+
"div",
|
|
208
|
+
{
|
|
209
|
+
className: "relative flex items-center",
|
|
210
|
+
style: { width: O || void 0 },
|
|
211
|
+
children: [
|
|
212
|
+
/* @__PURE__ */ I(
|
|
213
|
+
"span",
|
|
214
|
+
{
|
|
215
|
+
className: "relative inline-block overflow-hidden",
|
|
216
|
+
style: { width: O || void 0 },
|
|
217
|
+
children: [
|
|
218
|
+
P.map((e, t) => /* @__PURE__ */ m(
|
|
219
|
+
"span",
|
|
220
|
+
{
|
|
221
|
+
ref: dt(t),
|
|
222
|
+
style: {
|
|
223
|
+
display: "block",
|
|
224
|
+
whiteSpace: "nowrap",
|
|
225
|
+
visibility: "hidden",
|
|
226
|
+
clipPath: "inset(0 100% 0 0)",
|
|
227
|
+
willChange: "clip-path, visibility",
|
|
228
|
+
...t > 0 ? { position: "absolute", top: 0, left: 0 } : {}
|
|
229
|
+
},
|
|
230
|
+
children: e
|
|
231
|
+
},
|
|
232
|
+
t
|
|
233
|
+
)),
|
|
234
|
+
/* @__PURE__ */ m(
|
|
235
|
+
"div",
|
|
236
|
+
{
|
|
237
|
+
ref: U,
|
|
238
|
+
className: "pointer-events-none absolute inset-0",
|
|
239
|
+
style: {
|
|
240
|
+
opacity: 0,
|
|
241
|
+
transform: "translateX(-100%)",
|
|
242
|
+
willChange: "transform, opacity",
|
|
243
|
+
background: "linear-gradient(90deg, transparent 0%, color-mix(in srgb, var(--background) 40%, transparent) 40%, color-mix(in srgb, var(--background) 70%, transparent) 50%, color-mix(in srgb, var(--background) 40%, transparent) 60%, transparent 100%)"
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
]
|
|
248
|
+
}
|
|
249
|
+
),
|
|
250
|
+
/* @__PURE__ */ m(
|
|
251
|
+
"div",
|
|
252
|
+
{
|
|
253
|
+
ref: N,
|
|
254
|
+
className: "absolute",
|
|
255
|
+
style: {
|
|
256
|
+
left: -i / 2,
|
|
257
|
+
top: "50%",
|
|
258
|
+
marginTop: -i / 2,
|
|
259
|
+
width: i,
|
|
260
|
+
height: i,
|
|
261
|
+
transform: "translateX(0px)",
|
|
262
|
+
willChange: "transform"
|
|
263
|
+
},
|
|
264
|
+
children: /* @__PURE__ */ I(
|
|
265
|
+
"svg",
|
|
266
|
+
{
|
|
267
|
+
"aria-label": "Loading",
|
|
268
|
+
"data-slot": "text-reveal-ball",
|
|
269
|
+
fill: "none",
|
|
270
|
+
height: i,
|
|
271
|
+
role: "img",
|
|
272
|
+
style: { overflow: "visible" },
|
|
273
|
+
viewBox: "-30 -30 560 560",
|
|
274
|
+
width: i,
|
|
275
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
276
|
+
children: [
|
|
277
|
+
/* @__PURE__ */ m("defs", { children: /* @__PURE__ */ I("linearGradient", { ref: G, id: D, children: [
|
|
278
|
+
/* @__PURE__ */ m("stop", { offset: "-20%", stopColor: rt }),
|
|
279
|
+
/* @__PURE__ */ m("stop", { offset: "120%", stopColor: st })
|
|
280
|
+
] }) }),
|
|
281
|
+
/* @__PURE__ */ m(
|
|
282
|
+
"g",
|
|
283
|
+
{
|
|
284
|
+
ref: _,
|
|
285
|
+
style: {
|
|
286
|
+
opacity: 0,
|
|
287
|
+
transformOrigin: "250px 250px",
|
|
288
|
+
willChange: "transform"
|
|
289
|
+
},
|
|
290
|
+
children: /* @__PURE__ */ m(
|
|
291
|
+
"path",
|
|
292
|
+
{
|
|
293
|
+
ref: B,
|
|
294
|
+
d: o[0],
|
|
295
|
+
fill: `url(#${D})`
|
|
296
|
+
}
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
)
|
|
300
|
+
]
|
|
301
|
+
}
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
)
|
|
305
|
+
]
|
|
306
|
+
}
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
Mt.displayName = "TextRevealMotion";
|
|
313
|
+
export {
|
|
314
|
+
Mt as TextRevealMotion
|
|
315
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brightlocal/ui-components",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "BrightLocal Design System UI Components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -95,6 +95,7 @@
|
|
|
95
95
|
},
|
|
96
96
|
"devDependencies": {
|
|
97
97
|
"@size-limit/file": "^12.0.0",
|
|
98
|
+
"@types/flubber": "^0.4.0",
|
|
98
99
|
"@types/node": "^25.0.10",
|
|
99
100
|
"@types/react": "^19.2.10",
|
|
100
101
|
"@types/react-dom": "^19.2.3",
|
|
@@ -105,8 +106,8 @@
|
|
|
105
106
|
"vite-bundle-visualizer": "^1.2.1",
|
|
106
107
|
"vite-plugin-dts": "^4.5.4",
|
|
107
108
|
"vite-plugin-lib-inject-css": "^2.2.2",
|
|
108
|
-
"@workspace/
|
|
109
|
-
"@workspace/
|
|
109
|
+
"@workspace/eslint-config": "0.0.0",
|
|
110
|
+
"@workspace/typescript-config": "0.0.0"
|
|
110
111
|
},
|
|
111
112
|
"publishConfig": {
|
|
112
113
|
"access": "public",
|
|
@@ -150,6 +151,7 @@
|
|
|
150
151
|
"cmdk": "^1.1.1",
|
|
151
152
|
"date-fns": "^4.1.0",
|
|
152
153
|
"embla-carousel-react": "^8.6.0",
|
|
154
|
+
"flubber": "^0.4.2",
|
|
153
155
|
"framer-motion": "^12.29.2",
|
|
154
156
|
"input-otp": "^1.4.2",
|
|
155
157
|
"lucide-react": "^0.475.0",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
|
+
import { motion, AnimatePresence } from "framer-motion";
|
|
4
5
|
import { X } from "@brightlocal/icons";
|
|
5
6
|
|
|
6
7
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
@@ -26,11 +27,6 @@ export interface InputListProps {
|
|
|
26
27
|
* Called when the list of items changes.
|
|
27
28
|
*/
|
|
28
29
|
onValueChange: (value: string[]) => void;
|
|
29
|
-
/**
|
|
30
|
-
* Maximum number of items allowed.
|
|
31
|
-
* When reached, the input and add button are disabled and a count indicator is shown.
|
|
32
|
-
*/
|
|
33
|
-
maxItems?: number;
|
|
34
30
|
/**
|
|
35
31
|
* Placeholder text for the text input.
|
|
36
32
|
* @default "Add item"
|
|
@@ -87,8 +83,7 @@ export interface InputListProps {
|
|
|
87
83
|
/**
|
|
88
84
|
* InputList block — a managed list of text items with an add-input and per-row remove controls.
|
|
89
85
|
*
|
|
90
|
-
* Supports a
|
|
91
|
-
* multi-line paste (each line becomes a separate item), loading skeletons, disabled state,
|
|
86
|
+
* Supports multi-line paste (each line becomes a separate item), loading skeletons, disabled state,
|
|
92
87
|
* and input error/validation state.
|
|
93
88
|
*
|
|
94
89
|
* @example
|
|
@@ -96,7 +91,6 @@ export interface InputListProps {
|
|
|
96
91
|
* <InputList
|
|
97
92
|
* value={keywords}
|
|
98
93
|
* onValueChange={setKeywords}
|
|
99
|
-
* maxItems={10}
|
|
100
94
|
* dataHook="keywords"
|
|
101
95
|
* />
|
|
102
96
|
* ```
|
|
@@ -104,7 +98,6 @@ export interface InputListProps {
|
|
|
104
98
|
function InputList({
|
|
105
99
|
value,
|
|
106
100
|
onValueChange,
|
|
107
|
-
maxItems,
|
|
108
101
|
placeholder = "Add item",
|
|
109
102
|
addButtonLabel = "Add",
|
|
110
103
|
skeletonCount = 3,
|
|
@@ -119,104 +112,57 @@ function InputList({
|
|
|
119
112
|
className,
|
|
120
113
|
}: InputListProps) {
|
|
121
114
|
const [inputValue, setInputValue] = React.useState("");
|
|
122
|
-
const [removingIndices, setRemovingIndices] = React.useState<Set<number>>(
|
|
123
|
-
() => new Set()
|
|
124
|
-
);
|
|
125
|
-
const [addingIndices, setAddingIndices] = React.useState<Set<number>>(
|
|
126
|
-
() => new Set()
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
// Expand newly added items after initial collapsed paint (triggers CSS transition)
|
|
130
|
-
React.useEffect(() => {
|
|
131
|
-
if (addingIndices.size === 0) return;
|
|
132
|
-
const frame = requestAnimationFrame(() => {
|
|
133
|
-
setAddingIndices(new Set());
|
|
134
|
-
});
|
|
135
|
-
return () => cancelAnimationFrame(frame);
|
|
136
|
-
}, [addingIndices]);
|
|
137
|
-
const valueRef = React.useRef(value);
|
|
138
|
-
React.useEffect(() => {
|
|
139
|
-
valueRef.current = value;
|
|
140
|
-
}, [value]);
|
|
141
|
-
const timersRef = React.useRef(new Set<ReturnType<typeof setTimeout>>());
|
|
142
|
-
React.useEffect(() => {
|
|
143
|
-
const timers = timersRef.current;
|
|
144
|
-
return () => {
|
|
145
|
-
timers.forEach(clearTimeout);
|
|
146
|
-
timers.clear();
|
|
147
|
-
};
|
|
148
|
-
}, []);
|
|
149
115
|
|
|
150
|
-
// Stable keys so
|
|
116
|
+
// Stable keys so AnimatePresence can track items across additions/removals
|
|
151
117
|
const nextIdRef = React.useRef(value.length);
|
|
152
118
|
const [stableKeys, setStableKeys] = React.useState<number[]>(() =>
|
|
153
119
|
value.map((_, i) => i)
|
|
154
120
|
);
|
|
121
|
+
|
|
122
|
+
// Sync keys when value changes externally (not via addItems/removeItem).
|
|
123
|
+
// Simple appends preserve existing keys; other changes (removal, reorder)
|
|
124
|
+
// regenerate all keys to avoid incorrect exit animations.
|
|
155
125
|
React.useEffect(() => {
|
|
156
126
|
setStableKeys((prev) => {
|
|
157
|
-
if (prev.length
|
|
158
|
-
|
|
127
|
+
if (prev.length === value.length) return prev;
|
|
128
|
+
if (value.length > prev.length) {
|
|
129
|
+
// Append: preserve existing keys, generate new ones
|
|
130
|
+
return [
|
|
131
|
+
...prev,
|
|
132
|
+
...Array.from(
|
|
133
|
+
{ length: value.length - prev.length },
|
|
134
|
+
() => nextIdRef.current++
|
|
135
|
+
),
|
|
136
|
+
];
|
|
159
137
|
}
|
|
160
|
-
|
|
138
|
+
// Removal or reorder: regenerate all keys
|
|
139
|
+
return value.map(() => nextIdRef.current++);
|
|
161
140
|
});
|
|
162
141
|
}, [value]);
|
|
163
142
|
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
const buttonDisabled = disabled || atLimit || loading;
|
|
143
|
+
const inputDisabled = disabled || loading;
|
|
144
|
+
const buttonDisabled = disabled || loading;
|
|
167
145
|
|
|
168
146
|
const addItems = React.useCallback(
|
|
169
147
|
(items: string[]) => {
|
|
170
|
-
const
|
|
171
|
-
if (!
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
).matches;
|
|
179
|
-
if (!prefersReducedMotion) {
|
|
180
|
-
const startIndex = value.length;
|
|
181
|
-
setAddingIndices(new Set(toAdd.map((_, i) => startIndex + i)));
|
|
182
|
-
}
|
|
183
|
-
setStableKeys((prev) => [
|
|
184
|
-
...prev,
|
|
185
|
-
...toAdd.map(() => nextIdRef.current++),
|
|
186
|
-
]);
|
|
187
|
-
onValueChange([...value, ...toAdd]);
|
|
188
|
-
setInputValue("");
|
|
189
|
-
}
|
|
148
|
+
const toAdd = items.map((s) => s.trim()).filter(Boolean);
|
|
149
|
+
if (!toAdd.length) return;
|
|
150
|
+
setStableKeys((prev) => [
|
|
151
|
+
...prev,
|
|
152
|
+
...toAdd.map(() => nextIdRef.current++),
|
|
153
|
+
]);
|
|
154
|
+
onValueChange([...value, ...toAdd]);
|
|
155
|
+
setInputValue("");
|
|
190
156
|
},
|
|
191
|
-
[value,
|
|
157
|
+
[value, onValueChange]
|
|
192
158
|
);
|
|
193
159
|
|
|
194
160
|
const removeItem = React.useCallback(
|
|
195
161
|
(index: number) => {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
).matches;
|
|
199
|
-
|
|
200
|
-
if (prefersReducedMotion) {
|
|
201
|
-
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
202
|
-
onValueChange(valueRef.current.filter((_, i) => i !== index));
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
setRemovingIndices((prev) => new Set(prev).add(index));
|
|
207
|
-
const timer = setTimeout(() => {
|
|
208
|
-
timersRef.current.delete(timer);
|
|
209
|
-
setRemovingIndices((prev) => {
|
|
210
|
-
const next = new Set(prev);
|
|
211
|
-
next.delete(index);
|
|
212
|
-
return next;
|
|
213
|
-
});
|
|
214
|
-
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
215
|
-
onValueChange(valueRef.current.filter((_, i) => i !== index));
|
|
216
|
-
}, 200);
|
|
217
|
-
timersRef.current.add(timer);
|
|
162
|
+
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
163
|
+
onValueChange(value.filter((_, i) => i !== index));
|
|
218
164
|
},
|
|
219
|
-
[onValueChange]
|
|
165
|
+
[value, onValueChange]
|
|
220
166
|
);
|
|
221
167
|
|
|
222
168
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
@@ -250,59 +196,67 @@ function InputList({
|
|
|
250
196
|
<Item key={i} variant="loading" />
|
|
251
197
|
))}
|
|
252
198
|
</ul>
|
|
253
|
-
) :
|
|
199
|
+
) : (
|
|
254
200
|
<ul
|
|
255
|
-
className="flex flex-col"
|
|
201
|
+
className="flex flex-col gap-2"
|
|
256
202
|
data-hook={`${dataHook}-list`}
|
|
257
203
|
data-slot="list"
|
|
258
204
|
>
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const isCollapsed = isAdding || isRemoving;
|
|
263
|
-
return (
|
|
264
|
-
<li
|
|
205
|
+
<AnimatePresence initial={false}>
|
|
206
|
+
{value.map((item, index) => (
|
|
207
|
+
<motion.li
|
|
265
208
|
key={stableKeys[index]}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
isCollapsed
|
|
269
|
-
? "mb-0 grid-rows-[0fr] opacity-0"
|
|
270
|
-
: "mb-2 grid-rows-[1fr] opacity-100 last:mb-0",
|
|
271
|
-
isRemoving ? "duration-200" : "duration-300"
|
|
272
|
-
)}
|
|
209
|
+
layout
|
|
210
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
273
211
|
data-hook={`${dataHook}-item-${index}`}
|
|
274
212
|
data-slot="item"
|
|
213
|
+
exit={{
|
|
214
|
+
opacity: 0,
|
|
215
|
+
scale: 0.8,
|
|
216
|
+
transition: {
|
|
217
|
+
opacity: { duration: 0.15 },
|
|
218
|
+
scale: { duration: 0.12 },
|
|
219
|
+
},
|
|
220
|
+
}}
|
|
221
|
+
initial={{ opacity: 0, scale: 0.8 }}
|
|
222
|
+
transition={{
|
|
223
|
+
opacity: { duration: 0.15 },
|
|
224
|
+
scale: { duration: 0.15 },
|
|
225
|
+
layout: {
|
|
226
|
+
type: "spring",
|
|
227
|
+
stiffness: 500,
|
|
228
|
+
damping: 35,
|
|
229
|
+
},
|
|
230
|
+
}}
|
|
275
231
|
>
|
|
276
|
-
<div className="
|
|
277
|
-
<
|
|
278
|
-
<
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
<
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
</ItemActions>
|
|
299
|
-
</div>
|
|
232
|
+
<div className="bg-card flex items-center gap-4 rounded-lg py-2 pr-2 pl-4">
|
|
233
|
+
<ItemContent>
|
|
234
|
+
<ItemTitle>{item}</ItemTitle>
|
|
235
|
+
</ItemContent>
|
|
236
|
+
<ItemActions>
|
|
237
|
+
<button
|
|
238
|
+
aria-label={`Remove ${item}`}
|
|
239
|
+
className={cn(
|
|
240
|
+
"inline-flex size-9 cursor-pointer items-center justify-center rounded-full",
|
|
241
|
+
"text-muted-foreground",
|
|
242
|
+
"focus-visible:shadow-default outline-none",
|
|
243
|
+
"transition-colors duration-150 ease-in-out",
|
|
244
|
+
"disabled:pointer-events-none disabled:opacity-50"
|
|
245
|
+
)}
|
|
246
|
+
data-hook={`${dataHook}-remove-${index}`}
|
|
247
|
+
disabled={disabled}
|
|
248
|
+
type="button"
|
|
249
|
+
onClick={() => removeItem(index)}
|
|
250
|
+
>
|
|
251
|
+
<X />
|
|
252
|
+
</button>
|
|
253
|
+
</ItemActions>
|
|
300
254
|
</div>
|
|
301
|
-
</li>
|
|
302
|
-
)
|
|
303
|
-
|
|
255
|
+
</motion.li>
|
|
256
|
+
))}
|
|
257
|
+
</AnimatePresence>
|
|
304
258
|
</ul>
|
|
305
|
-
)
|
|
259
|
+
)}
|
|
306
260
|
|
|
307
261
|
{/* Input row */}
|
|
308
262
|
<div className="flex flex-col gap-2" data-slot="input-list-input-area">
|
|
@@ -327,34 +281,25 @@ function InputList({
|
|
|
327
281
|
</Button>
|
|
328
282
|
</div>
|
|
329
283
|
|
|
330
|
-
{/*
|
|
331
|
-
{
|
|
332
|
-
<FieldDescription
|
|
333
|
-
data-slot="input-list-description"
|
|
334
|
-
dataHook={`${dataHook}-description`}
|
|
335
|
-
>
|
|
336
|
-
{description}
|
|
337
|
-
</FieldDescription>
|
|
338
|
-
)}
|
|
339
|
-
|
|
340
|
-
{/* Count indicator — always shown when maxItems is set */}
|
|
341
|
-
{maxItems !== undefined && (
|
|
342
|
-
<FieldDescription
|
|
343
|
-
data-slot="input-list-count"
|
|
344
|
-
dataHook={`${dataHook}-count`}
|
|
345
|
-
>
|
|
346
|
-
{value.length}/{maxItems}
|
|
347
|
-
</FieldDescription>
|
|
348
|
-
)}
|
|
349
|
-
|
|
350
|
-
{/* Error message */}
|
|
351
|
-
{error && errorMessage && (
|
|
284
|
+
{/* Error message replaces description */}
|
|
285
|
+
{error && errorMessage ? (
|
|
352
286
|
<FieldError
|
|
287
|
+
className="text-left"
|
|
353
288
|
data-slot="input-list-error"
|
|
354
289
|
dataHook={`${dataHook}-error`}
|
|
355
290
|
>
|
|
356
291
|
{errorMessage}
|
|
357
292
|
</FieldError>
|
|
293
|
+
) : (
|
|
294
|
+
description && (
|
|
295
|
+
<FieldDescription
|
|
296
|
+
className="text-left"
|
|
297
|
+
data-slot="input-list-description"
|
|
298
|
+
dataHook={`${dataHook}-description`}
|
|
299
|
+
>
|
|
300
|
+
{description}
|
|
301
|
+
</FieldDescription>
|
|
302
|
+
)
|
|
358
303
|
)}
|
|
359
304
|
</div>
|
|
360
305
|
</div>
|