@skalfa/skalfa-component 1.0.7 → 1.0.8
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 +2 -2
- package/src/accordion/Accordion.component.tsx +87 -0
- package/src/breadcrumb/Breadcrumb.component.tsx +79 -0
- package/src/button/Button.component.tsx +89 -0
- package/src/card/AlertCard.component.tsx +69 -0
- package/src/card/Card.component.tsx +25 -0
- package/src/card/DashboardCard.component.tsx +44 -0
- package/src/card/GalleryCard.component.tsx +50 -0
- package/src/card/ProductCard.component.tsx +65 -0
- package/src/card/ProfileCard.component.tsx +71 -0
- package/src/carousel/Carousel.component.tsx +111 -0
- package/src/chip/Chip.component.tsx +39 -0
- package/src/index.ts +70 -0
- package/src/input/Checkbox.component.tsx +102 -0
- package/src/input/Input.component.tsx +334 -0
- package/src/input/InputCheckbox.component.tsx +174 -0
- package/src/input/InputCurrency.component.tsx +165 -0
- package/src/input/InputDate.component.tsx +356 -0
- package/src/input/InputDatetime.component.tsx +267 -0
- package/src/input/InputDocument.component.tsx +360 -0
- package/src/input/InputImage.component.tsx +535 -0
- package/src/input/InputNumber.component.tsx +194 -0
- package/src/input/InputOtp.component.tsx +169 -0
- package/src/input/InputPassword.component.tsx +245 -0
- package/src/input/InputRadio.component.tsx +174 -0
- package/src/input/InputTime.component.tsx +280 -0
- package/src/input/InputValues.component.tsx +71 -0
- package/src/input/Radio.component.tsx +98 -0
- package/src/input/Select.component.tsx +557 -0
- package/src/modal/BottomSheet.component.tsx +246 -0
- package/src/modal/FloatingPage.component.tsx +103 -0
- package/src/modal/Modal.component.tsx +95 -0
- package/src/modal/ModalConfirm.component.tsx +219 -0
- package/src/modal/Toast.component.tsx +125 -0
- package/src/nav/Bottombar.component.tsx +72 -0
- package/src/nav/Footer.component.tsx +177 -0
- package/src/nav/Headbar.component.tsx +33 -0
- package/src/nav/Navbar.component.tsx +138 -0
- package/src/nav/Sidebar.component.tsx +298 -0
- package/src/nav/Tabbar.component.tsx +61 -0
- package/src/nav/Wizard.component.tsx +80 -0
- package/src/supervision/FormSupervision.component.tsx +425 -0
- package/src/supervision/TableSupervision.component.tsx +688 -0
- package/src/table/ControlBar.component.tsx +501 -0
- package/src/table/FilterComponent.tsx +519 -0
- package/src/table/Pagination.component.tsx +152 -0
- package/src/table/Table.component.tsx +436 -0
- package/src/types.d.ts +7 -0
- package/src/typography/TypographyArticle.component.tsx +26 -0
- package/src/typography/TypographyColumn.component.tsx +20 -0
- package/src/typography/TypographyContent.component.tsx +20 -0
- package/src/typography/TypographyTips.component.tsx +20 -0
- package/src/wrap/Draggable.component.tsx +303 -0
- package/src/wrap/Image.component.tsx +10 -0
- package/src/wrap/OutsideClick.component.tsx +48 -0
- package/src/wrap/ScrollContainer.component.tsx +107 -0
- package/src/wrap/ShortcutProvider.tsx +57 -0
- package/src/wrap/Swipe.component.tsx +121 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useRef, useEffect, useState, ReactNode, forwardRef, useImperativeHandle, CSSProperties } from 'react';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
type SpringConfig = {
|
|
8
|
+
stiffness ?: number;
|
|
9
|
+
damping ?: number;
|
|
10
|
+
mass ?: number;
|
|
11
|
+
stopThreshold ?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type Bounds = {
|
|
15
|
+
left ?: number;
|
|
16
|
+
top ?: number;
|
|
17
|
+
right ?: number;
|
|
18
|
+
bottom ?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type DragState = {
|
|
22
|
+
x : number;
|
|
23
|
+
y : number;
|
|
24
|
+
vx : number;
|
|
25
|
+
vy : number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type DraggableProps = {
|
|
29
|
+
children ?: ReactNode;
|
|
30
|
+
className ?: string;
|
|
31
|
+
style ?: CSSProperties;
|
|
32
|
+
initial ?: { x?: number; y?: number };
|
|
33
|
+
bounds ?: Bounds;
|
|
34
|
+
spring ?: SpringConfig;
|
|
35
|
+
onDrag ?: (state: { x: number; y: number; vx: number; vy: number; dragging: boolean }) => void;
|
|
36
|
+
useTranslate3d ?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// -----------------------------------------------------
|
|
40
|
+
// DraggableComponent
|
|
41
|
+
// -----------------------------------------------------
|
|
42
|
+
export const DraggableComponent = forwardRef(function DraggableComponent(
|
|
43
|
+
{
|
|
44
|
+
children,
|
|
45
|
+
className,
|
|
46
|
+
style,
|
|
47
|
+
initial = { x: 0, y: 0 },
|
|
48
|
+
bounds,
|
|
49
|
+
spring,
|
|
50
|
+
onDrag,
|
|
51
|
+
useTranslate3d = true,
|
|
52
|
+
}: DraggableProps,
|
|
53
|
+
ref
|
|
54
|
+
) {
|
|
55
|
+
const elRef = useRef<HTMLDivElement | null>(null);
|
|
56
|
+
const frameRef = useRef<number | null>(null);
|
|
57
|
+
const draggingRef = useRef(false);
|
|
58
|
+
const pointerIdRef = useRef<number | null>(null);
|
|
59
|
+
const stateRef = useRef<DragState>({ x: initial.x || 0, y: initial.y || 0, vx: 0, vy: 0 });
|
|
60
|
+
const targetRef = useRef<{ x: number; y: number }>({ x: initial.x || 0, y: initial.y || 0 });
|
|
61
|
+
|
|
62
|
+
const historyRef = useRef<Array<{ t: number; x: number; y: number }>>([]);
|
|
63
|
+
|
|
64
|
+
const cfg: Required<SpringConfig> = {
|
|
65
|
+
stiffness : spring?.stiffness ?? 180,
|
|
66
|
+
damping : spring?.damping ?? 24,
|
|
67
|
+
mass : spring?.mass ?? 1,
|
|
68
|
+
stopThreshold : spring?.stopThreshold ?? 0.02,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const [, setTick] = useState(0);
|
|
72
|
+
|
|
73
|
+
const now = () => performance.now();
|
|
74
|
+
|
|
75
|
+
function applyStyle(x: number, y: number) {
|
|
76
|
+
const el = elRef.current;
|
|
77
|
+
if (!el) return;
|
|
78
|
+
const transform = useTranslate3d ? `translate3d(${x}px, ${y}px, 0)` : `translate(${x}px, ${y}px)`;
|
|
79
|
+
el.style.transform = transform;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function clampToBounds(x: number, y: number) {
|
|
83
|
+
if (!bounds) return { x, y };
|
|
84
|
+
return {
|
|
85
|
+
x: Math.max(bounds.left ?? -Infinity, Math.min(x, bounds.right ?? Infinity)),
|
|
86
|
+
y: Math.max(bounds.top ?? -Infinity, Math.min(y, bounds.bottom ?? Infinity)),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function estimateVelocity() {
|
|
91
|
+
const h = historyRef.current;
|
|
92
|
+
if (h.length < 2) return { vx: 0, vy: 0 };
|
|
93
|
+
const last = h[h.length - 1];
|
|
94
|
+
const threshold = 50;
|
|
95
|
+
let idx = h.length - 2;
|
|
96
|
+
while (idx > 0 && last.t - h[idx].t < threshold) idx--;
|
|
97
|
+
const prev = h[idx];
|
|
98
|
+
const dt = Math.max(1, last.t - prev.t);
|
|
99
|
+
return {
|
|
100
|
+
vx: (last.x - prev.x) / (dt / 1000),
|
|
101
|
+
vy: (last.y - prev.y) / (dt / 1000),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function cancelFrame() {
|
|
106
|
+
if (frameRef.current != null) {
|
|
107
|
+
cancelAnimationFrame(frameRef.current);
|
|
108
|
+
frameRef.current = null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function startSpringAnimation(initVX = 0, initVY = 0) {
|
|
113
|
+
cancelFrame();
|
|
114
|
+
|
|
115
|
+
const mass = cfg.mass;
|
|
116
|
+
const k = cfg.stiffness;
|
|
117
|
+
const c = cfg.damping;
|
|
118
|
+
|
|
119
|
+
stateRef.current.vx = initVX;
|
|
120
|
+
stateRef.current.vy = initVY;
|
|
121
|
+
|
|
122
|
+
let lastTime = now();
|
|
123
|
+
|
|
124
|
+
function step() {
|
|
125
|
+
const t = now();
|
|
126
|
+
const dt = Math.min(32, t - lastTime) / 1000;
|
|
127
|
+
lastTime = t;
|
|
128
|
+
|
|
129
|
+
const sx = stateRef.current.x;
|
|
130
|
+
const sy = stateRef.current.y;
|
|
131
|
+
const vx = stateRef.current.vx;
|
|
132
|
+
const vy = stateRef.current.vy;
|
|
133
|
+
const tx = targetRef.current.x;
|
|
134
|
+
const ty = targetRef.current.y;
|
|
135
|
+
|
|
136
|
+
const ax = (-c * vx - k * (sx - tx)) / mass;
|
|
137
|
+
const ay = (-c * vy - k * (sy - ty)) / mass;
|
|
138
|
+
|
|
139
|
+
const nvx = vx + ax * dt;
|
|
140
|
+
const nvy = vy + ay * dt;
|
|
141
|
+
const nx = sx + nvx * dt;
|
|
142
|
+
const ny = sy + nvy * dt;
|
|
143
|
+
|
|
144
|
+
stateRef.current.x = nx;
|
|
145
|
+
stateRef.current.y = ny;
|
|
146
|
+
stateRef.current.vx = nvx;
|
|
147
|
+
stateRef.current.vy = nvy;
|
|
148
|
+
|
|
149
|
+
applyStyle(nx, ny);
|
|
150
|
+
onDrag?.({ x: nx, y: ny, vx: nvx, vy: nvy, dragging: false });
|
|
151
|
+
|
|
152
|
+
const disp = Math.hypot(nx - tx, ny - ty);
|
|
153
|
+
const vel = Math.hypot(nvx, nvy);
|
|
154
|
+
|
|
155
|
+
if (disp < cfg.stopThreshold && vel < cfg.stopThreshold) {
|
|
156
|
+
stateRef.current.x = tx;
|
|
157
|
+
stateRef.current.y = ty;
|
|
158
|
+
stateRef.current.vx = 0;
|
|
159
|
+
stateRef.current.vy = 0;
|
|
160
|
+
applyStyle(tx, ty);
|
|
161
|
+
onDrag?.({ x: tx, y: ty, vx: 0, vy: 0, dragging: false });
|
|
162
|
+
frameRef.current = null;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
frameRef.current = requestAnimationFrame(step);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
frameRef.current = requestAnimationFrame(step);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// -------------------------------------------------------------------
|
|
173
|
+
// Expose API ke parent: setTarget & setPositionImmediate
|
|
174
|
+
// -------------------------------------------------------------------
|
|
175
|
+
useImperativeHandle(ref, () => ({
|
|
176
|
+
setTarget(pos: { x: number; y: number }) {
|
|
177
|
+
targetRef.current.x = pos.x;
|
|
178
|
+
targetRef.current.y = pos.y;
|
|
179
|
+
startSpringAnimation(0, 0);
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
setPosition(pos: { x: number; y: number }) {
|
|
183
|
+
stateRef.current.x = pos.x;
|
|
184
|
+
stateRef.current.y = pos.y;
|
|
185
|
+
targetRef.current.x = pos.x;
|
|
186
|
+
targetRef.current.y = pos.y;
|
|
187
|
+
applyStyle(pos.x, pos.y);
|
|
188
|
+
},
|
|
189
|
+
}));
|
|
190
|
+
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
const el = elRef.current;
|
|
193
|
+
if (!el) return;
|
|
194
|
+
|
|
195
|
+
function onDown(e: PointerEvent) {
|
|
196
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
197
|
+
|
|
198
|
+
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
199
|
+
pointerIdRef.current = e.pointerId;
|
|
200
|
+
draggingRef.current = true;
|
|
201
|
+
cancelFrame();
|
|
202
|
+
|
|
203
|
+
historyRef.current = [{ t: now(), x: stateRef.current.x, y: stateRef.current.y }];
|
|
204
|
+
|
|
205
|
+
(el as any).__dragBase = {
|
|
206
|
+
px: e.clientX,
|
|
207
|
+
py: e.clientY,
|
|
208
|
+
startX: stateRef.current.x,
|
|
209
|
+
startY: stateRef.current.y,
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
onDrag?.({
|
|
213
|
+
x: stateRef.current.x,
|
|
214
|
+
y: stateRef.current.y,
|
|
215
|
+
vx: 0,
|
|
216
|
+
vy: 0,
|
|
217
|
+
dragging: true,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function onMove(e: PointerEvent) {
|
|
222
|
+
if (!draggingRef.current) return;
|
|
223
|
+
if (pointerIdRef.current !== null && e.pointerId !== pointerIdRef.current) return;
|
|
224
|
+
|
|
225
|
+
const base = (el as any).__dragBase;
|
|
226
|
+
if (!base) return;
|
|
227
|
+
|
|
228
|
+
let nx = base.startX + (e.clientX - base.px);
|
|
229
|
+
let ny = base.startY + (e.clientY - base.py);
|
|
230
|
+
|
|
231
|
+
({ x: nx, y: ny } = clampToBounds(nx, ny));
|
|
232
|
+
|
|
233
|
+
historyRef.current.push({ t: now(), x: nx, y: ny });
|
|
234
|
+
if (historyRef.current.length > 8) historyRef.current.shift();
|
|
235
|
+
|
|
236
|
+
stateRef.current.x = nx;
|
|
237
|
+
stateRef.current.y = ny;
|
|
238
|
+
stateRef.current.vx = 0;
|
|
239
|
+
stateRef.current.vy = 0;
|
|
240
|
+
|
|
241
|
+
applyStyle(nx, ny);
|
|
242
|
+
onDrag?.({ x: nx, y: ny, vx: 0, vy: 0, dragging: true });
|
|
243
|
+
|
|
244
|
+
setTick(t => t + 1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function onUp(e: PointerEvent) {
|
|
248
|
+
if (!draggingRef.current) return;
|
|
249
|
+
if (pointerIdRef.current !== null && e.pointerId !== pointerIdRef.current) return;
|
|
250
|
+
|
|
251
|
+
(e.target as Element).releasePointerCapture?.(e.pointerId);
|
|
252
|
+
draggingRef.current = false;
|
|
253
|
+
pointerIdRef.current = null;
|
|
254
|
+
|
|
255
|
+
const { vx, vy } = estimateVelocity();
|
|
256
|
+
|
|
257
|
+
let tx = stateRef.current.x;
|
|
258
|
+
let ty = stateRef.current.y;
|
|
259
|
+
({ x: tx, y: ty } = clampToBounds(tx, ty));
|
|
260
|
+
|
|
261
|
+
targetRef.current.x = tx;
|
|
262
|
+
targetRef.current.y = ty;
|
|
263
|
+
|
|
264
|
+
startSpringAnimation(vx, vy);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
el.addEventListener('pointerdown', onDown as EventListener);
|
|
268
|
+
window.addEventListener('pointermove', onMove as EventListener);
|
|
269
|
+
window.addEventListener('pointerup', onUp as EventListener);
|
|
270
|
+
window.addEventListener('pointercancel', onUp as EventListener);
|
|
271
|
+
|
|
272
|
+
return () => {
|
|
273
|
+
el.removeEventListener('pointerdown', onDown as EventListener);
|
|
274
|
+
window.removeEventListener('pointermove', onMove as EventListener);
|
|
275
|
+
window.removeEventListener('pointerup', onUp as EventListener);
|
|
276
|
+
window.removeEventListener('pointercancel', onUp as EventListener);
|
|
277
|
+
cancelFrame();
|
|
278
|
+
};
|
|
279
|
+
}, [bounds]);
|
|
280
|
+
|
|
281
|
+
// initial pos
|
|
282
|
+
useEffect(() => {
|
|
283
|
+
stateRef.current.x = initial.x || 0;
|
|
284
|
+
stateRef.current.y = initial.y || 0;
|
|
285
|
+
applyStyle(initial.x || 0, initial.y || 0);
|
|
286
|
+
}, []);
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
<div
|
|
290
|
+
ref={elRef}
|
|
291
|
+
className={className}
|
|
292
|
+
style={{
|
|
293
|
+
touchAction: 'none',
|
|
294
|
+
cursor: 'grab',
|
|
295
|
+
display: 'inline-block',
|
|
296
|
+
...style,
|
|
297
|
+
}}
|
|
298
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
299
|
+
>
|
|
300
|
+
{children}
|
|
301
|
+
</div>
|
|
302
|
+
);
|
|
303
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* eslint-disable jsx-a11y/alt-text */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import Image, { ImageProps } from "next/image";
|
|
5
|
+
|
|
6
|
+
export function ImageComponent(props: ImageProps) {
|
|
7
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
8
|
+
|
|
9
|
+
return <Image {...props} unoptimized={!isProd} />;
|
|
10
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { FC, ReactNode, useEffect, useRef } from "react";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export interface OutsideClickHandlerProps {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
onOutsideClick?: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export const OutsideClickComponent: FC<OutsideClickHandlerProps> = ({
|
|
15
|
+
children,
|
|
16
|
+
onOutsideClick,
|
|
17
|
+
}) => {
|
|
18
|
+
const ref = useRef<HTMLDivElement | null>(null);
|
|
19
|
+
const handlerRef = useRef(onOutsideClick);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
handlerRef.current = onOutsideClick;
|
|
23
|
+
}, [onOutsideClick]);
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
27
|
+
const el = ref.current;
|
|
28
|
+
if (!el || !(event.target instanceof Node)) return;
|
|
29
|
+
|
|
30
|
+
const isOutside = !el.contains(event.target);
|
|
31
|
+
|
|
32
|
+
if (isOutside) {
|
|
33
|
+
queueMicrotask(() => {
|
|
34
|
+
if (ref.current && !ref.current.contains(event.target as Node)) {
|
|
35
|
+
handlerRef.current?.();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
document.addEventListener("pointerdown", handlePointerDown, true);
|
|
42
|
+
return () => {
|
|
43
|
+
document.removeEventListener("pointerdown", handlePointerDown, true);
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
|
|
47
|
+
return <div ref={ref}>{children}</div>;
|
|
48
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { cn, pcn } from "@utils";
|
|
4
|
+
import { useRef, useState, useEffect, ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
type CT = "base" | "container" | "floating-scroll";
|
|
9
|
+
|
|
10
|
+
export interface ScrollContainerProps {
|
|
11
|
+
children : ReactNode;
|
|
12
|
+
footer ?: ReactNode;
|
|
13
|
+
scrollFloating ?: boolean;
|
|
14
|
+
onScroll ?: (e: any) => void;
|
|
15
|
+
|
|
16
|
+
/** Use custom class with: "container::", "floating-scroll::"". */
|
|
17
|
+
className?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export function ScrollContainerComponent({
|
|
23
|
+
children,
|
|
24
|
+
footer,
|
|
25
|
+
className = "",
|
|
26
|
+
scrollFloating = false,
|
|
27
|
+
onScroll,
|
|
28
|
+
}: ScrollContainerProps) {
|
|
29
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
30
|
+
const floatingScrollbarRef = useRef<HTMLDivElement | null>(null);
|
|
31
|
+
const floatingScrollbarContainerRef = useRef<HTMLDivElement | null>(null);
|
|
32
|
+
const [containerWidth, setContainerWidth] = useState(0);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const handleScroll = () => {
|
|
36
|
+
if (!containerRef.current) return;
|
|
37
|
+
onScroll?.(containerRef.current);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const container = containerRef.current;
|
|
41
|
+
if (container) {
|
|
42
|
+
handleScroll();
|
|
43
|
+
container.addEventListener("scroll", handleScroll);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return () => container?.removeEventListener("scroll", handleScroll);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const scrollContainer = containerRef.current;
|
|
51
|
+
const floatingScrollbar = floatingScrollbarRef.current;
|
|
52
|
+
|
|
53
|
+
if (!scrollContainer || !floatingScrollbar) return;
|
|
54
|
+
|
|
55
|
+
const syncScroll = () => floatingScrollbar.scrollLeft = scrollContainer.scrollLeft;
|
|
56
|
+
const syncScrollReverse = () => scrollContainer.scrollLeft = floatingScrollbar.scrollLeft;
|
|
57
|
+
|
|
58
|
+
scrollContainer.addEventListener("scroll", syncScroll);
|
|
59
|
+
floatingScrollbar.addEventListener("scroll", syncScrollReverse);
|
|
60
|
+
|
|
61
|
+
return () => {
|
|
62
|
+
scrollContainer.removeEventListener("scroll", syncScroll);
|
|
63
|
+
floatingScrollbar.removeEventListener("scroll", syncScrollReverse);
|
|
64
|
+
};
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
const handleResize = () => {
|
|
68
|
+
if (containerRef.current) setContainerWidth(containerRef.current.offsetWidth);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
handleResize();
|
|
73
|
+
window.addEventListener("resize", handleResize);
|
|
74
|
+
|
|
75
|
+
return () => window.removeEventListener("resize", handleResize);
|
|
76
|
+
}, []);
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (floatingScrollbarContainerRef.current) floatingScrollbarContainerRef.current.style.width = `${containerWidth}px`;
|
|
80
|
+
}, [containerWidth]);
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div className={cn("relative", pcn<CT>(className, "base"))}>
|
|
84
|
+
<div
|
|
85
|
+
ref={containerRef}
|
|
86
|
+
className={cn(
|
|
87
|
+
"scroll-container-main scroll",
|
|
88
|
+
scrollFloating && "scroll-none",
|
|
89
|
+
pcn<CT>(className, "container"),
|
|
90
|
+
)}
|
|
91
|
+
>
|
|
92
|
+
{children}
|
|
93
|
+
</div>
|
|
94
|
+
|
|
95
|
+
<div className="scroll-container-floating-bar" ref={floatingScrollbarContainerRef}>
|
|
96
|
+
<div
|
|
97
|
+
ref={floatingScrollbarRef}
|
|
98
|
+
className={cn(scrollFloating && "scroll-container-floating-wrapper scroll")}
|
|
99
|
+
>
|
|
100
|
+
<div className="scroll-container-phantom-element">{children}</div>
|
|
101
|
+
</div>
|
|
102
|
+
|
|
103
|
+
{footer}
|
|
104
|
+
</div>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, Fragment } from "react";
|
|
4
|
+
import { shortcut } from "@utils"
|
|
5
|
+
import { useToggleContext } from "@contexts"
|
|
6
|
+
import { ModalComponent } from "../modal/Modal.component";
|
|
7
|
+
|
|
8
|
+
export function ShortcutProvider() {
|
|
9
|
+
const { setToggle, toggle } = useToggleContext();
|
|
10
|
+
const shortcuts = shortcut.list()
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
shortcut.init()
|
|
14
|
+
}, [])
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
shortcut.register("ctrl+/", () => {
|
|
18
|
+
setToggle("MODAL_SHORTCUT_HELP")
|
|
19
|
+
}, "List Shortcut")
|
|
20
|
+
}, [])
|
|
21
|
+
|
|
22
|
+
function formatShortcutKey(key: string) {
|
|
23
|
+
return key
|
|
24
|
+
.split("+")
|
|
25
|
+
.map(k => {
|
|
26
|
+
if (k === "ctrl") return "Ctrl"
|
|
27
|
+
if (k === "shift") return "Shift"
|
|
28
|
+
if (k === "alt") return "Alt"
|
|
29
|
+
if (k === "arrowup") return "↑"
|
|
30
|
+
if (k === "arrowdown") return "↓"
|
|
31
|
+
if (k === " ") return "SPACE"
|
|
32
|
+
return k.toUpperCase()
|
|
33
|
+
})
|
|
34
|
+
.join(" + ")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<>
|
|
39
|
+
<ModalComponent
|
|
40
|
+
show={!!toggle["MODAL_SHORTCUT_HELP"]}
|
|
41
|
+
onClose={() => setToggle("MODAL_SHORTCUT_HELP")}
|
|
42
|
+
title="Shortcut"
|
|
43
|
+
>
|
|
44
|
+
<div className="shortcut-help-grid">
|
|
45
|
+
{shortcuts.map(({ key, description }) => (
|
|
46
|
+
<Fragment key={key}>
|
|
47
|
+
<kbd className="shortcut-key-badge">
|
|
48
|
+
{formatShortcutKey(key)}
|
|
49
|
+
</kbd>
|
|
50
|
+
<span>: {description}</span>
|
|
51
|
+
</Fragment>
|
|
52
|
+
))}
|
|
53
|
+
</div>
|
|
54
|
+
</ModalComponent>
|
|
55
|
+
</>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { ReactNode, useRef, useState } from 'react'
|
|
4
|
+
import { cn } from '@utils';
|
|
5
|
+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export type SwipeActionType = {
|
|
9
|
+
label ?: string,
|
|
10
|
+
icon ?: any,
|
|
11
|
+
onAction ?: () => void;
|
|
12
|
+
className ?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type SwipeProps = {
|
|
16
|
+
leftActionControl ?: SwipeActionType;
|
|
17
|
+
rightActionControl ?: SwipeActionType;
|
|
18
|
+
children ?: ReactNode;
|
|
19
|
+
className ?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function SwipeComponent({
|
|
23
|
+
leftActionControl,
|
|
24
|
+
rightActionControl,
|
|
25
|
+
children,
|
|
26
|
+
className,
|
|
27
|
+
} : SwipeProps) {
|
|
28
|
+
|
|
29
|
+
const startX = useRef(0);
|
|
30
|
+
const [offset, setOffset] = useState(0);
|
|
31
|
+
const [dragging, setDragging] = useState(false);
|
|
32
|
+
|
|
33
|
+
function onTouchStart(e: React.TouchEvent) {
|
|
34
|
+
setDragging(true);
|
|
35
|
+
startX.current = e.touches[0].clientX - offset;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function onTouchMove(e: React.TouchEvent) {
|
|
39
|
+
if (!dragging) return;
|
|
40
|
+
|
|
41
|
+
const currentX = e.touches[0].clientX;
|
|
42
|
+
const delta = currentX - startX.current;
|
|
43
|
+
|
|
44
|
+
let allowed = delta;
|
|
45
|
+
|
|
46
|
+
// Jika tidak ada aksi kiri → larang geser ke kanan
|
|
47
|
+
if (!leftActionControl && delta > 0) allowed = 0;
|
|
48
|
+
|
|
49
|
+
// Jika tidak ada aksi kanan → larang geser ke kiri
|
|
50
|
+
if (!rightActionControl && delta < 0) allowed = 0;
|
|
51
|
+
|
|
52
|
+
// Batasi maksimal ±100px
|
|
53
|
+
allowed = Math.max(Math.min(allowed, 100), -100);
|
|
54
|
+
|
|
55
|
+
setOffset(allowed);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function onTouchEnd() {
|
|
59
|
+
if (!dragging) return;
|
|
60
|
+
setDragging(false);
|
|
61
|
+
|
|
62
|
+
if (offset > 40 && leftActionControl) {
|
|
63
|
+
setOffset(80);
|
|
64
|
+
} else if (offset < -40 && rightActionControl) {
|
|
65
|
+
setOffset(-80);
|
|
66
|
+
} else {
|
|
67
|
+
setOffset(0);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function handleAction(side: 'left' | 'right') {
|
|
72
|
+
const control = side === 'left' ? leftActionControl : rightActionControl;
|
|
73
|
+
control?.onAction?.();
|
|
74
|
+
setOffset(0);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div className={cn("swipe-container", className)}>
|
|
79
|
+
<div
|
|
80
|
+
onClick={() => handleAction('left')}
|
|
81
|
+
className={cn(
|
|
82
|
+
"swipe-left-action",
|
|
83
|
+
leftActionControl?.className,
|
|
84
|
+
offset > 0 ? "opacity-100" : "opacity-0"
|
|
85
|
+
)}
|
|
86
|
+
>
|
|
87
|
+
{leftActionControl?.icon && <FontAwesomeIcon icon={leftActionControl?.icon} />} {leftActionControl?.label}
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
<div
|
|
91
|
+
onClick={() => handleAction('right')}
|
|
92
|
+
className={cn(
|
|
93
|
+
"swipe-right-action",
|
|
94
|
+
rightActionControl?.className,
|
|
95
|
+
offset < 0 ? "opacity-100" : "opacity-0"
|
|
96
|
+
)}
|
|
97
|
+
>
|
|
98
|
+
{rightActionControl?.icon && <FontAwesomeIcon icon={rightActionControl?.icon} />} {rightActionControl?.label}
|
|
99
|
+
</div>
|
|
100
|
+
|
|
101
|
+
<div
|
|
102
|
+
className={cn("swipe-content", className)}
|
|
103
|
+
style={{
|
|
104
|
+
transform: `translateX(${offset}px)`,
|
|
105
|
+
transition: dragging ? "none" : "transform 0.2s ease",
|
|
106
|
+
}}
|
|
107
|
+
onTouchStart={onTouchStart}
|
|
108
|
+
onTouchMove={onTouchMove}
|
|
109
|
+
onTouchEnd={onTouchEnd}
|
|
110
|
+
onClickCapture={(e) => {
|
|
111
|
+
if (offset !== 0) {
|
|
112
|
+
setOffset(0);
|
|
113
|
+
e.stopPropagation();
|
|
114
|
+
}
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
{children}
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
)
|
|
121
|
+
}
|