cynx-lib 1.3.90 → 1.3.92

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cynx-lib",
3
- "version": "1.3.90",
3
+ "version": "1.3.92",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -1,169 +1,357 @@
1
- import { useState, useRef, useEffect, useCallback, useLayoutEffect } from 'react';
2
- import { createPortal } from 'react-dom';
3
-
4
- export function ActionsMenu({ items, align = 'right' }) {
5
- const [open, setOpen] = useState(false);
6
- const [closing, setClosing] = useState(false);
7
- const wrapRef = useRef(null);
8
- const menuRef = useRef(null);
9
- const btnRef = useRef(null);
10
-
11
- const close = useCallback(() => {
12
- setClosing(true);
13
- setTimeout(() => { setClosing(false); setOpen(false); }, 140);
14
- }, []);
15
-
16
- const positionMenu = useCallback(() => {
17
- const btn = btnRef.current;
18
- const menu = menuRef.current;
19
- if (!btn || !menu) return;
20
- const br = btn.getBoundingClientRect();
21
- const mw = menu.offsetWidth || 180;
22
- const mh = menu.offsetHeight || 120;
23
- const spaceBelow = window.innerHeight - br.bottom;
24
- const spaceAbove = br.top;
25
- const openUp = spaceBelow < mh + 12 && spaceAbove > spaceBelow;
26
-
27
- let top, left;
28
- if (openUp) {
29
- top = br.top - mh - 6;
30
- } else {
31
- top = br.bottom + 6;
32
- }
33
- if (align === 'left') {
34
- left = br.left;
35
- } else {
36
- left = br.right - mw;
37
- }
38
- left = Math.max(8, Math.min(left, window.innerWidth - mw - 8));
39
-
40
- menu.style.top = top + 'px';
41
- menu.style.left = left + 'px';
42
- menu.style.transformOrigin = openUp
43
- ? (align === 'left' ? 'bottom left' : 'bottom right')
44
- : (align === 'left' ? 'top left' : 'top right');
45
- menu.classList.toggle('open-up', openUp);
46
- if (!closing) {
47
- menu.style.animationName = openUp ? 'actionsDropUp' : 'actionsDrop';
48
- }
49
- }, [align, closing]);
50
-
51
- useEffect(() => {
52
- if (!open) return;
53
- function onDocClick(e) {
54
- if (
55
- wrapRef.current && !wrapRef.current.contains(e.target) &&
56
- menuRef.current && !menuRef.current.contains(e.target)
57
- ) {
58
- close();
59
- }
60
- }
61
- function onScroll() { positionMenu(); }
62
- document.addEventListener('mousedown', onDocClick);
63
- document.addEventListener('scroll', onScroll, true);
64
- window.addEventListener('resize', onScroll);
65
- return () => {
66
- document.removeEventListener('mousedown', onDocClick);
67
- document.removeEventListener('scroll', onScroll, true);
68
- window.removeEventListener('resize', onScroll);
69
- };
70
- }, [open, close, positionMenu]);
71
-
72
- useLayoutEffect(() => {
73
- if (open) {
74
- positionMenu();
75
- }
76
- }, [open, positionMenu]);
77
-
78
- function toggle() {
79
- if (open) close();
80
- else { setOpen(true); setClosing(false); }
81
- }
82
-
83
- const isOpen = open && !closing;
84
-
85
- return (
86
- <div ref={wrapRef} style={{ position: 'relative', display: 'inline-block' }}>
87
- <button
88
- ref={btnRef}
89
- type="button"
90
- onClick={toggle}
91
- style={{
92
- width: 32, height: 32, borderRadius: 'var(--rads,8px)',
93
- border: isOpen ? '1px solid var(--accent,#f99e2c)' : '1px solid var(--border-light,#d7d8e0)',
94
- background: 'var(--surface,#fff)',
95
- cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
96
- color: isOpen ? 'var(--accent, #f99e2c)' : 'var(--muted,#797b8d)', transition: 'all .14s',
97
- boxShadow: isOpen ? 'var(--pd, rgba(249,158,44,.12))' : 'var(--shadow, 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06))',
98
- }}
99
- >
100
- <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
101
- <circle cx="5" cy="12" r="1.5" /><circle cx="12" cy="12" r="1.5" /><circle cx="19" cy="12" r="1.5" />
102
- </svg>
103
- </button>
104
-
105
- {open && typeof document !== 'undefined' && createPortal(
106
- <div
107
- ref={menuRef}
108
- style={{
109
- position: 'fixed', zIndex: 99999, minWidth: 180, padding: 5,
110
- background: 'var(--surface,#fff)', border: '1.5px solid var(--border-light,#e2e8f0)',
111
- borderRadius: 'var(--rads,8px)',
112
- boxShadow: 'var(--shadow-lg, 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.06))',
113
- transformOrigin: align === 'left' ? 'top left' : 'top right',
114
- animation: closing
115
- ? 'actionsClose .14s ease forwards'
116
- : 'actionsDrop .14s cubic-bezier(.16,1,.3,1)',
117
- }}
118
- >
119
- {items.map((item, i) => (
120
- item.dividerBefore ? (
121
- <div key={i} style={{ height: 1, background: 'var(--border-light,#e2e8f0)', margin: '4px 0' }} />
122
- ) : (
123
- <div
124
- key={i}
125
- onClick={() => { close(); item.onClick?.(); }}
126
- style={{
127
- display: 'flex', alignItems: 'center', gap: 8,
128
- padding: '6px 10px', borderRadius: 'var(--rads,8px)',
129
- fontSize: 12, cursor: 'pointer', transition: 'all .14s',
130
- color: (item.variant === 'danger' || item.danger) ? 'var(--danger, #ef4444)' : item.variant === 'success' ? 'var(--success, #16a34a)' : item.variant === 'warning' ? 'var(--warning, #f59e0b)' : 'var(--primary-text, #18181b)',
131
- fontWeight: 500,
132
- }}
133
- onMouseEnter={e => {
134
- e.currentTarget.style.background = (item.variant === 'danger' || item.danger) ? 'var(--danger-bg, rgba(239,68,68,.10))' : item.variant === 'success' ? 'var(--success-bg, rgba(22,163,74,.10))' : item.variant === 'warning' ? 'var(--warning-bg, rgba(245,158,11,.12))' : 'var(--grey-1, #f4f4f5)';
135
- }}
136
- onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; }}
137
- >
138
- {item.icon && (
139
- <span style={{
140
- width: 24, height: 24, borderRadius: 5,
141
- display: 'flex', alignItems: 'center', justifyContent: 'center',
142
- flexShrink: 0, color: 'inherit', fontSize: 12,
143
- }}>{item.icon}</span>
144
- )}
145
- <span>{item.label}</span>
146
- </div>
147
- )
148
- ))}
149
- </div>,
150
- document.body
151
- )}
152
-
153
- <style>{`
154
- @keyframes actionsDrop {
155
- from { opacity: 0; transform: translateY(-6px); }
156
- to { opacity: 1; transform: translateY(0); }
157
- }
158
- @keyframes actionsDropUp {
159
- from { opacity: 0; transform: translateY(6px); }
160
- to { opacity: 1; transform: translateY(0); }
161
- }
162
- @keyframes actionsClose {
163
- from { opacity: 1; transform: translateY(0); }
164
- to { opacity: 0; transform: translateY(-4px); }
165
- }
166
- `}</style>
167
- </div>
168
- );
169
- }
1
+ import { useState, useRef, useEffect, useCallback, useLayoutEffect } from 'react';
2
+ import { createPortal } from 'react-dom';
3
+
4
+ function ActionSubmenu({ items, menuRect, itemRect, closeAll, onMouseEnter, onMouseLeave }) {
5
+ const subRef = useRef(null);
6
+
7
+ useLayoutEffect(() => {
8
+ const sub = subRef.current;
9
+ if (!sub || !menuRect || !itemRect) return;
10
+
11
+ const subWidth = sub.offsetWidth || 190;
12
+ const subHeight = sub.offsetHeight || 150;
13
+
14
+ // Determine horizontal docking relative to PARENT MENU container
15
+ const spaceRight = window.innerWidth - menuRect.right;
16
+ let left;
17
+ let originX = 'left';
18
+
19
+ if (spaceRight >= subWidth + 8) {
20
+ // Dock cleanly to the right side of parent menu
21
+ left = menuRect.right + 4;
22
+ originX = 'left';
23
+ } else {
24
+ // Dock cleanly to the left side of parent menu (never overlaps!)
25
+ left = menuRect.left - subWidth - 4;
26
+ originX = 'right';
27
+ }
28
+
29
+ // Keep within viewport bounds
30
+ left = Math.max(8, Math.min(left, window.innerWidth - subWidth - 8));
31
+
32
+ // Determine vertical docking relative to the active item
33
+ let top = itemRect.top - 5;
34
+ let originY = 'top';
35
+
36
+ if (top + subHeight > window.innerHeight - 8) {
37
+ top = Math.max(8, window.innerHeight - subHeight - 8);
38
+ originY = 'bottom';
39
+ }
40
+
41
+ sub.style.top = top + 'px';
42
+ sub.style.left = left + 'px';
43
+ sub.style.transformOrigin = `${originY} ${originX}`;
44
+ }, [menuRect, itemRect]);
45
+
46
+ return createPortal(
47
+ <div
48
+ ref={subRef}
49
+ data-cynx-submenu="true"
50
+ onMouseEnter={onMouseEnter}
51
+ onMouseLeave={onMouseLeave}
52
+ style={{
53
+ position: 'fixed',
54
+ zIndex: 100000,
55
+ minWidth: 190,
56
+ padding: 5,
57
+ top: itemRect ? (itemRect.top - 5) + 'px' : '0px',
58
+ left: menuRect ? (menuRect.left - 200) + 'px' : '0px',
59
+ background: 'var(--surface,#fff)',
60
+ border: '1.5px solid var(--border-light,#e2e8f0)',
61
+ borderRadius: 'var(--rads,8px)',
62
+ boxShadow: 'var(--shadow-lg, 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.06))',
63
+ animation: 'actionsDrop .12s cubic-bezier(.16,1,.3,1)',
64
+ }}
65
+ >
66
+ {items.map((subItem, j) => (
67
+ subItem.dividerBefore ? (
68
+ <div key={j} style={{ height: 1, background: 'var(--border-light,#e2e8f0)', margin: '4px 0' }} />
69
+ ) : (
70
+ <div
71
+ key={j}
72
+ onClick={() => { closeAll(); subItem.onClick?.(); }}
73
+ style={{
74
+ display: 'flex', alignItems: 'center', gap: 8,
75
+ padding: '6px 10px', borderRadius: 'var(--rads,8px)',
76
+ fontSize: 12, cursor: 'pointer', transition: 'all .14s',
77
+ whiteSpace: 'nowrap',
78
+ color: (subItem.variant === 'danger' || subItem.danger) ? 'var(--danger, #ef4444)' : subItem.variant === 'success' ? 'var(--success, #16a34a)' : subItem.variant === 'warning' ? 'var(--warning, #f59e0b)' : 'var(--primary-text, #18181b)',
79
+ fontWeight: 500,
80
+ }}
81
+ onMouseEnter={e => {
82
+ e.currentTarget.style.background = (subItem.variant === 'danger' || subItem.danger) ? 'var(--danger-bg, rgba(239,68,68,.10))' : subItem.variant === 'success' ? 'var(--success-bg, rgba(22,163,74,.10))' : subItem.variant === 'warning' ? 'var(--warning-bg, rgba(245,158,11,.12))' : 'var(--grey-1, #f4f4f5)';
83
+ }}
84
+ onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; }}
85
+ >
86
+ {subItem.icon && (
87
+ <span style={{
88
+ width: 24, height: 24, borderRadius: 5,
89
+ display: 'flex', alignItems: 'center', justifyContent: 'center',
90
+ flexShrink: 0, color: 'inherit', fontSize: 12,
91
+ }}>{subItem.icon}</span>
92
+ )}
93
+ <span>{subItem.label}</span>
94
+ </div>
95
+ )
96
+ ))}
97
+ </div>,
98
+ document.body
99
+ );
100
+ }
101
+
102
+ export function ActionsMenu({ items, align = 'right' }) {
103
+ const [open, setOpen] = useState(false);
104
+ const [closing, setClosing] = useState(false);
105
+ const [activeSub, setActiveSub] = useState(null); // { index, items, itemRect, menuRect }
106
+ const wrapRef = useRef(null);
107
+ const menuRef = useRef(null);
108
+ const btnRef = useRef(null);
109
+ const subTimerRef = useRef(null);
110
+
111
+ const close = useCallback(() => {
112
+ setClosing(true);
113
+ setActiveSub(null);
114
+ clearTimeout(subTimerRef.current);
115
+ setTimeout(() => { setClosing(false); setOpen(false); }, 140);
116
+ }, []);
117
+
118
+ const openSub = (index, subItems, e) => {
119
+ clearTimeout(subTimerRef.current);
120
+ const itemRect = e.currentTarget.getBoundingClientRect();
121
+ const menuRect = menuRef.current ? menuRef.current.getBoundingClientRect() : itemRect;
122
+ setActiveSub({ index, items: subItems, itemRect, menuRect });
123
+ };
124
+
125
+ const scheduleCloseSub = () => {
126
+ clearTimeout(subTimerRef.current);
127
+ subTimerRef.current = setTimeout(() => {
128
+ setActiveSub(null);
129
+ }, 150);
130
+ };
131
+
132
+ const cancelCloseSub = () => {
133
+ clearTimeout(subTimerRef.current);
134
+ };
135
+
136
+ const positionMenu = useCallback(() => {
137
+ const btn = btnRef.current;
138
+ const menu = menuRef.current;
139
+ if (!btn || !menu) return;
140
+ const br = btn.getBoundingClientRect();
141
+ const mw = menu.offsetWidth || 180;
142
+ const mh = menu.offsetHeight || 120;
143
+ const spaceBelow = window.innerHeight - br.bottom;
144
+ const spaceAbove = br.top;
145
+ const openUp = spaceBelow < mh + 12 && spaceAbove > spaceBelow;
146
+
147
+ let top, left;
148
+ if (openUp) {
149
+ top = br.top - mh - 6;
150
+ } else {
151
+ top = br.bottom + 6;
152
+ }
153
+ if (align === 'left') {
154
+ left = br.left;
155
+ } else {
156
+ left = br.right - mw;
157
+ }
158
+ left = Math.max(8, Math.min(left, window.innerWidth - mw - 8));
159
+
160
+ menu.style.top = top + 'px';
161
+ menu.style.left = left + 'px';
162
+ menu.style.transformOrigin = openUp
163
+ ? (align === 'left' ? 'bottom left' : 'bottom right')
164
+ : (align === 'left' ? 'top left' : 'top right');
165
+ menu.classList.toggle('open-up', openUp);
166
+ if (!closing) {
167
+ menu.style.animationName = openUp ? 'actionsDropUp' : 'actionsDrop';
168
+ }
169
+ }, [align, closing]);
170
+
171
+ useEffect(() => {
172
+ if (!open) return;
173
+ function onDocClick(e) {
174
+ if (
175
+ wrapRef.current && !wrapRef.current.contains(e.target) &&
176
+ menuRef.current && !menuRef.current.contains(e.target) &&
177
+ !e.target.closest?.('[data-cynx-submenu]')
178
+ ) {
179
+ close();
180
+ }
181
+ }
182
+ function onScroll() { positionMenu(); }
183
+ document.addEventListener('mousedown', onDocClick);
184
+ document.addEventListener('scroll', onScroll, true);
185
+ window.addEventListener('resize', onScroll);
186
+ return () => {
187
+ document.removeEventListener('mousedown', onDocClick);
188
+ document.removeEventListener('scroll', onScroll, true);
189
+ window.removeEventListener('resize', onScroll);
190
+ };
191
+ }, [open, close, positionMenu]);
192
+
193
+ useLayoutEffect(() => {
194
+ if (open) {
195
+ positionMenu();
196
+ }
197
+ }, [open, positionMenu]);
198
+
199
+ function toggle() {
200
+ if (open) close();
201
+ else { setOpen(true); setClosing(false); setActiveSub(null); }
202
+ }
203
+
204
+ const isOpen = open && !closing;
205
+
206
+ return (
207
+ <div ref={wrapRef} style={{ position: 'relative', display: 'inline-block' }}>
208
+ <button
209
+ ref={btnRef}
210
+ type="button"
211
+ onClick={toggle}
212
+ style={{
213
+ width: 32, height: 32, borderRadius: 'var(--rads,8px)',
214
+ border: isOpen ? '1px solid var(--accent,#f99e2c)' : '1px solid var(--border-light,#d7d8e0)',
215
+ background: 'var(--surface,#fff)',
216
+ cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
217
+ color: isOpen ? 'var(--accent, #f99e2c)' : 'var(--muted,#797b8d)', transition: 'all .14s',
218
+ boxShadow: isOpen ? 'var(--pd, rgba(249,158,44,.12))' : 'var(--shadow, 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06))',
219
+ }}
220
+ >
221
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
222
+ <circle cx="5" cy="12" r="1.5" /><circle cx="12" cy="12" r="1.5" /><circle cx="19" cy="12" r="1.5" />
223
+ </svg>
224
+ </button>
225
+
226
+ {open && typeof document !== 'undefined' && createPortal(
227
+ <div
228
+ ref={menuRef}
229
+ style={{
230
+ position: 'fixed', zIndex: 99999, minWidth: 180, padding: 5,
231
+ background: 'var(--surface,#fff)', border: '1.5px solid var(--border-light,#e2e8f0)',
232
+ borderRadius: 'var(--rads,8px)',
233
+ boxShadow: 'var(--shadow-lg, 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.06))',
234
+ transformOrigin: align === 'left' ? 'top left' : 'top right',
235
+ animation: closing
236
+ ? 'actionsClose .14s ease forwards'
237
+ : 'actionsDrop .14s cubic-bezier(.16,1,.3,1)',
238
+ }}
239
+ >
240
+ {items.map((item, i) => {
241
+ if (item.dividerBefore) {
242
+ return <div key={i} style={{ height: 1, background: 'var(--border-light,#e2e8f0)', margin: '4px 0' }} />;
243
+ }
244
+
245
+ const hasSub = Array.isArray(item.items) && item.items.length > 0;
246
+ const isSubActive = activeSub && activeSub.index === i;
247
+
248
+ return (
249
+ <div
250
+ key={i}
251
+ onClick={(e) => {
252
+ if (hasSub) {
253
+ e.stopPropagation();
254
+ openSub(i, item.items, e);
255
+ } else {
256
+ close();
257
+ item.onClick?.();
258
+ }
259
+ }}
260
+ onMouseEnter={(e) => {
261
+ if (hasSub) {
262
+ openSub(i, item.items, e);
263
+ } else {
264
+ clearTimeout(subTimerRef.current);
265
+ setActiveSub(null);
266
+ }
267
+ e.currentTarget.style.background = (item.variant === 'danger' || item.danger)
268
+ ? 'var(--danger-bg, rgba(239,68,68,.10))'
269
+ : item.variant === 'success'
270
+ ? 'var(--success-bg, rgba(22,163,74,.10))'
271
+ : item.variant === 'warning'
272
+ ? 'var(--warning-bg, rgba(245,158,11,.12))'
273
+ : 'var(--grey-1, #f4f4f5)';
274
+ }}
275
+ onMouseLeave={(e) => {
276
+ if (hasSub) {
277
+ scheduleCloseSub();
278
+ }
279
+ if (!isSubActive) {
280
+ e.currentTarget.style.background = 'transparent';
281
+ }
282
+ }}
283
+ style={{
284
+ display: 'flex', alignItems: 'center', gap: 8,
285
+ padding: '6px 10px', borderRadius: 'var(--rads,8px)',
286
+ fontSize: 12, cursor: 'pointer', transition: 'all .14s',
287
+ whiteSpace: 'nowrap',
288
+ background: isSubActive ? 'var(--grey-1, #f4f4f5)' : 'transparent',
289
+ color: (item.variant === 'danger' || item.danger)
290
+ ? 'var(--danger, #ef4444)'
291
+ : item.variant === 'success'
292
+ ? 'var(--success, #16a34a)'
293
+ : item.variant === 'warning'
294
+ ? 'var(--warning, #f59e0b)'
295
+ : 'var(--primary-text, #18181b)',
296
+ fontWeight: 500,
297
+ }}
298
+ >
299
+ {item.icon && (
300
+ <span style={{
301
+ width: 24, height: 24, borderRadius: 5,
302
+ display: 'flex', alignItems: 'center', justifyContent: 'center',
303
+ flexShrink: 0, color: 'inherit', fontSize: 12,
304
+ }}>{item.icon}</span>
305
+ )}
306
+ <span>{item.label}</span>
307
+
308
+ {hasSub && (
309
+ <svg
310
+ width="12"
311
+ height="12"
312
+ viewBox="0 0 24 24"
313
+ fill="none"
314
+ stroke="currentColor"
315
+ strokeWidth="2.5"
316
+ strokeLinecap="round"
317
+ strokeLinejoin="round"
318
+ style={{ marginLeft: 'auto', opacity: 0.55 }}
319
+ >
320
+ <polyline points="9 18 15 12 9 6" />
321
+ </svg>
322
+ )}
323
+ </div>
324
+ );
325
+ })}
326
+ </div>,
327
+ document.body
328
+ )}
329
+
330
+ {open && activeSub && (
331
+ <ActionSubmenu
332
+ items={activeSub.items}
333
+ menuRect={activeSub.menuRect}
334
+ itemRect={activeSub.itemRect}
335
+ closeAll={close}
336
+ onMouseEnter={cancelCloseSub}
337
+ onMouseLeave={scheduleCloseSub}
338
+ />
339
+ )}
340
+
341
+ <style>{`
342
+ @keyframes actionsDrop {
343
+ from { opacity: 0; transform: translateY(-6px); }
344
+ to { opacity: 1; transform: translateY(0); }
345
+ }
346
+ @keyframes actionsDropUp {
347
+ from { opacity: 0; transform: translateY(6px); }
348
+ to { opacity: 1; transform: translateY(0); }
349
+ }
350
+ @keyframes actionsClose {
351
+ from { opacity: 1; transform: translateY(0); }
352
+ to { opacity: 0; transform: translateY(-4px); }
353
+ }
354
+ `}</style>
355
+ </div>
356
+ );
357
+ }