cynx-ui 1.0.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.
@@ -0,0 +1,149 @@
1
+ import { useState, useRef, useEffect, useCallback } from 'react';
2
+
3
+ export function ActionsMenu({ items, align = 'right' }) {
4
+ const [open, setOpen] = useState(false);
5
+ const [closing, setClosing] = useState(false);
6
+ const wrapRef = useRef(null);
7
+ const menuRef = useRef(null);
8
+ const btnRef = useRef(null);
9
+
10
+ const close = useCallback(() => {
11
+ setClosing(true);
12
+ setTimeout(() => { setClosing(false); setOpen(false); }, 140);
13
+ }, []);
14
+
15
+ useEffect(() => {
16
+ if (!open) return;
17
+ function onDocClick(e) {
18
+ if (wrapRef.current && !wrapRef.current.contains(e.target)) close();
19
+ }
20
+ function onScroll() { if (menuRef.current) positionMenu(); }
21
+ document.addEventListener('mousedown', onDocClick);
22
+ document.addEventListener('scroll', onScroll, true);
23
+ window.addEventListener('resize', onScroll);
24
+ return () => {
25
+ document.removeEventListener('mousedown', onDocClick);
26
+ document.removeEventListener('scroll', onScroll, true);
27
+ window.removeEventListener('resize', onScroll);
28
+ };
29
+ }, [open, close]);
30
+
31
+ function positionMenu() {
32
+ const btn = btnRef.current;
33
+ const menu = menuRef.current;
34
+ if (!btn || !menu) return;
35
+ const br = btn.getBoundingClientRect();
36
+ const mw = menu.offsetWidth || 180;
37
+ const mh = menu.offsetHeight || 120;
38
+ const spaceBelow = window.innerHeight - br.bottom;
39
+ const spaceAbove = br.top;
40
+ const openUp = spaceBelow < mh + 12 && spaceAbove > spaceBelow;
41
+
42
+ let top, left;
43
+ if (openUp) {
44
+ top = br.top + window.scrollY - mh - 6;
45
+ } else {
46
+ top = br.bottom + window.scrollY + 6;
47
+ }
48
+ if (align === 'left') {
49
+ left = br.left + window.scrollX;
50
+ } else {
51
+ left = br.right + window.scrollX - mw;
52
+ }
53
+ left = Math.max(8, Math.min(left, window.innerWidth - mw - 8));
54
+
55
+ menu.style.top = top + 'px';
56
+ menu.style.left = left + 'px';
57
+ menu.classList.toggle('open-up', openUp);
58
+ }
59
+
60
+ useEffect(() => {
61
+ if (open) requestAnimationFrame(positionMenu);
62
+ }, [open]);
63
+
64
+ function toggle() {
65
+ if (open) close();
66
+ else { setOpen(true); setClosing(false); }
67
+ }
68
+
69
+ const isOpen = open && !closing;
70
+
71
+ return (
72
+ <div ref={wrapRef} style={{ position: 'relative', display: 'inline-block' }}>
73
+ <button
74
+ ref={btnRef}
75
+ type="button"
76
+ onClick={toggle}
77
+ style={{
78
+ width: 30, height: 30, borderRadius: 'var(--rads,8px)',
79
+ border: isOpen ? '1px solid var(--accent,#f99e2c)' : '1px solid var(--border-light,#d7d8e0)',
80
+ background: '#fff',
81
+ cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
82
+ color: isOpen ? 'var(--accent)' : 'var(--muted,#797b8d)', transition: 'all .14s',
83
+ boxShadow: isOpen ? '0 0 0 3px rgba(249,158,44,.12)' : '0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06)',
84
+ }}
85
+ >
86
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
87
+ <circle cx="5" cy="12" r="1.5" /><circle cx="12" cy="12" r="1.5" /><circle cx="19" cy="12" r="1.5" />
88
+ </svg>
89
+ </button>
90
+
91
+ {open && (
92
+ <div
93
+ ref={menuRef}
94
+ style={{
95
+ position: 'fixed', zIndex: 8100, minWidth: 180, padding: 5,
96
+ background: '#fff', border: '1.5px solid var(--border-light,#e2e8f0)',
97
+ borderRadius: 'var(--rads,8px)',
98
+ boxShadow: '0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.06)',
99
+ animation: closing
100
+ ? 'actionsClose .14s ease forwards'
101
+ : 'actionsDrop .14s cubic-bezier(.16,1,.3,1)',
102
+ }}
103
+ >
104
+ {items.map((item, i) => (
105
+ item.dividerBefore ? (
106
+ <div key={i} style={{ height: 1, background: 'var(--border-light,#e2e8f0)', margin: '4px 0' }} />
107
+ ) : (
108
+ <div
109
+ key={i}
110
+ onClick={() => { close(); item.onClick?.(); }}
111
+ style={{
112
+ display: 'flex', alignItems: 'center', gap: 8,
113
+ padding: '6px 10px', borderRadius: 'var(--rads,8px)',
114
+ fontSize: 12, cursor: 'pointer', transition: 'all .14s',
115
+ color: item.variant === 'danger' ? 'var(--danger)' : item.variant === 'success' ? 'var(--success)' : item.variant === 'warning' ? 'var(--accent)' : 'var(--text,#1a1a2e)',
116
+ fontWeight: 500,
117
+ }}
118
+ onMouseEnter={e => {
119
+ e.currentTarget.style.background = item.variant === 'danger' ? 'rgba(246,71,71,.08)' : item.variant === 'warning' ? 'rgba(249,158,44,.08)' : 'var(--grey-1,#f5f5f8)';
120
+ }}
121
+ onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; }}
122
+ >
123
+ {item.icon && (
124
+ <span style={{
125
+ width: 24, height: 24, borderRadius: 5,
126
+ display: 'flex', alignItems: 'center', justifyContent: 'center',
127
+ flexShrink: 0, color: 'inherit', fontSize: 12,
128
+ }}>{item.icon}</span>
129
+ )}
130
+ <span>{item.label}</span>
131
+ </div>
132
+ )
133
+ ))}
134
+ </div>
135
+ )}
136
+
137
+ <style>{`
138
+ @keyframes actionsDrop {
139
+ from { opacity: 0; transform: translateY(-6px) scaleY(.96); }
140
+ to { opacity: 1; transform: translateY(0) scaleY(1); }
141
+ }
142
+ @keyframes actionsClose {
143
+ from { opacity: 1; transform: translateY(0) scaleY(1); }
144
+ to { opacity: 0; transform: translateY(-4px) scaleY(.96); }
145
+ }
146
+ `}</style>
147
+ </div>
148
+ );
149
+ }
@@ -0,0 +1,184 @@
1
+ import { useMemo, useState, useRef } from 'react';
2
+
3
+ const MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
4
+
5
+ function LegendDot({ color, label }) {
6
+ return (
7
+ <div style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
8
+ <span style={{ width: 7, height: 7, borderRadius: 2, background: color, flexShrink: 0 }} />
9
+ <span style={{ fontSize: '.55rem', color: 'var(--muted)', fontWeight: 500 }}>{label}</span>
10
+ </div>
11
+ );
12
+ }
13
+
14
+ /**
15
+ * ActivityCard — day-block bar with hover tooltip
16
+ */
17
+ export default function ActivityCard({
18
+ title = 'Activity',
19
+ subtitle,
20
+ primary = [],
21
+ additional = [],
22
+ totalSlots = 90,
23
+ greenColor = 'var(--success)',
24
+ redColor = 'var(--danger)',
25
+ emptyColor = 'var(--grey-1)',
26
+ barHeight = 28,
27
+ barRadius = 3,
28
+ leftLabel,
29
+ rightLabel,
30
+ showDot = true,
31
+ dotColor,
32
+ style,
33
+ }) {
34
+ const [hovered, setHovered] = useState(null);
35
+ const [tooltipPos, setTooltipPos] = useState({ x: 0, y: 0 });
36
+ const containerRef = useRef(null);
37
+
38
+ const bars = useMemo(() => {
39
+ const len = Math.max(primary.length, additional.length, totalSlots);
40
+ const result = [];
41
+ const stepS = primary.length / len;
42
+ const stepF = additional.length / len;
43
+ for (let i = 0; i < len; i++) {
44
+ const si = Math.min(Math.floor(i * stepS), primary.length - 1);
45
+ const fi = Math.min(Math.floor(i * stepF), additional.length - 1);
46
+ result.push({ success: primary[si] || 0, failed: additional[fi] || 0 });
47
+ }
48
+ return result;
49
+ }, [primary, additional, totalSlots]);
50
+
51
+ const dates = useMemo(() => {
52
+ const result = [];
53
+ const now = new Date();
54
+ for (let i = 0; i < bars.length; i++) {
55
+ const d = new Date(now);
56
+ d.setDate(d.getDate() - (bars.length - 1 - i));
57
+ result.push(d);
58
+ }
59
+ return result;
60
+ }, [bars.length]);
61
+
62
+ const totalSuccess = bars.reduce((a, b) => a + b.success, 0);
63
+ const totalFailed = bars.reduce((a, b) => a + b.failed, 0);
64
+ const totalAll = totalSuccess + totalFailed;
65
+ const uptimePercent = totalAll > 0 ? Math.round((totalSuccess / totalAll) * 100) : 100;
66
+
67
+ function handleMouseEnter(e, idx) {
68
+ const rect = e.currentTarget.getBoundingClientRect();
69
+ const containerRect = containerRef.current.getBoundingClientRect();
70
+ setTooltipPos({
71
+ x: rect.left - containerRect.left + rect.width / 2,
72
+ y: rect.top - containerRect.top - 8,
73
+ });
74
+ setHovered(idx);
75
+ }
76
+
77
+ const h = hovered !== null ? bars[hovered] : null;
78
+ const hTotal = h ? h.success + h.failed : 0;
79
+
80
+ return (
81
+ <div ref={containerRef} style={{
82
+ background: '#fff', border: '1px solid var(--border)', borderRadius: 'var(--rad)',
83
+ boxShadow: '0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04)',
84
+ padding: '14px 18px', position: 'relative', ...style,
85
+ }}>
86
+ {/* Header */}
87
+ <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 12 }}>
88
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
89
+ {showDot && (
90
+ <span style={{ width: 8, height: 8, borderRadius: '50%', background: dotColor || greenColor, flexShrink: 0 }} />
91
+ )}
92
+ <span style={{ fontSize: '.78rem', fontWeight: 700, color: 'var(--primary-text)', letterSpacing: '-.01em' }}>{title}</span>
93
+ {subtitle && <span style={{ fontSize: '.65rem', color: 'var(--muted)', fontWeight: 500 }}>{subtitle}</span>}
94
+ </div>
95
+ <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
96
+ <LegendDot color={greenColor} label="Success" />
97
+ <LegendDot color={redColor} label="Failed" />
98
+ <LegendDot color={emptyColor} label="None" />
99
+ </div>
100
+ </div>
101
+
102
+ {/* Bars */}
103
+ <div style={{ display: 'flex', alignItems: 'flex-end', gap: 2, height: barHeight, marginBottom: 6 }}>
104
+ {bars.map((b, i) => {
105
+ const total = b.success + b.failed;
106
+ const hasData = total > 0;
107
+ const successPct = hasData ? (b.success / total) * 100 : 0;
108
+ const failedPct = hasData ? (b.failed / total) * 100 : 0;
109
+
110
+ return (
111
+ <div
112
+ key={i}
113
+ onMouseEnter={(e) => handleMouseEnter(e, i)}
114
+ onMouseLeave={() => setHovered(null)}
115
+ style={{
116
+ flex: 1, height: '100%', borderRadius: barRadius, overflow: 'hidden',
117
+ display: 'flex', flexDirection: 'column',
118
+ background: hasData ? 'transparent' : emptyColor,
119
+ cursor: hasData ? 'pointer' : 'default',
120
+ }}
121
+ >
122
+ {hasData && (
123
+ <>
124
+ {b.failed > 0 && <div style={{ height: `${failedPct}%`, background: redColor, marginTop: 'auto' }} />}
125
+ {b.success > 0 && <div style={{ height: `${successPct}%`, background: greenColor }} />}
126
+ </>
127
+ )}
128
+ </div>
129
+ );
130
+ })}
131
+ </div>
132
+
133
+ {/* Tooltip */}
134
+ {hovered !== null && (
135
+ <div style={{
136
+ position: 'absolute', left: tooltipPos.x, top: tooltipPos.y,
137
+ transform: 'translate(-50%, -100%)', background: '#fff',
138
+ border: '1px solid var(--border)', borderRadius: 'var(--rads, 8px)',
139
+ boxShadow: '0 4px 12px rgba(0,0,0,.12)', padding: '10px 14px',
140
+ pointerEvents: 'none', zIndex: 100, whiteSpace: 'nowrap',
141
+ animation: 'hoverCardIn .15s ease',
142
+ }}>
143
+ <div style={{ fontSize: '.65rem', color: 'var(--muted)', fontWeight: 500, marginBottom: 4 }}>
144
+ {dates[hovered]?.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}
145
+ </div>
146
+ {h.success > 0 && (
147
+ <div style={{ fontSize: '.7rem', fontWeight: 700, color: greenColor }}>
148
+ {h.success} success{h.success !== 1 ? 'es' : ''}
149
+ {h.failed > 0 && <span style={{ fontWeight: 500, color: 'var(--muted)' }}> ({Math.round((h.success / hTotal) * 100)}%)</span>}
150
+ </div>
151
+ )}
152
+ {h.failed > 0 && (
153
+ <div style={{ fontSize: '.7rem', fontWeight: 700, color: redColor }}>
154
+ {h.failed} failed{h.failed !== 1 ? 's' : ''}
155
+ {h.success > 0 && <span style={{ fontWeight: 500, color: 'var(--muted)' }}> ({Math.round((h.failed / hTotal) * 100)}%)</span>}
156
+ </div>
157
+ )}
158
+ {!h.success && !h.failed && (
159
+ <div style={{ fontSize: '.7rem', fontWeight: 500, color: 'var(--muted)' }}>No activity</div>
160
+ )}
161
+ <div style={{
162
+ position: 'absolute', left: '50%', bottom: -5,
163
+ transform: 'translateX(-50%) rotate(45deg)',
164
+ width: 8, height: 8, background: '#fff',
165
+ borderRight: '1px solid var(--border)', borderBottom: '1px solid var(--border)',
166
+ }} />
167
+ </div>
168
+ )}
169
+
170
+ {/* Footer */}
171
+ <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
172
+ <span style={{ fontSize: '.58rem', color: 'var(--muted)', fontWeight: 500 }}>{leftLabel || ''}</span>
173
+ <span style={{ fontSize: '.78rem', fontWeight: 700, color: greenColor, fontFamily: 'var(--mono)' }}>{uptimePercent}%</span>
174
+ <span style={{ fontSize: '.58rem', color: 'var(--muted)', fontWeight: 500 }}>{rightLabel || ''}</span>
175
+ </div>
176
+ <style>{`
177
+ @keyframes hoverCardIn {
178
+ from { opacity: 0; transform: translate(-50%, -100%) translateY(4px); }
179
+ to { opacity: 1; transform: translate(-50%, -100%) translateY(0); }
180
+ }
181
+ `}</style>
182
+ </div>
183
+ );
184
+ }