@xwadex/fesd-next 0.3.44-beta.21 → 0.3.44-beta.22
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/dist/components/dragBoxs/dragBoxs.d.ts +8 -0
- package/dist/components/dragBoxs/dragBoxs.js +70 -0
- package/dist/components/dragBoxs/dragBoxsContext.d.ts +14 -0
- package/dist/components/dragBoxs/dragBoxsContext.js +10 -0
- package/dist/components/dragBoxs/index.d.ts +2 -0
- package/dist/components/dragBoxs/index.js +2 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface PropsTypes {
|
|
2
|
+
defaultPosX?: number;
|
|
3
|
+
defaultPosY?: number;
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}
|
|
6
|
+
declare const DragBoxsBase: (props: PropsTypes) => import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
declare const DragBoxs: React.MemoExoticComponent<typeof DragBoxsBase>;
|
|
8
|
+
export default DragBoxs;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { memo, useCallback, useEffect, useId, useState } from "react";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
import { DndContext } from "@dnd-kit/core";
|
|
6
|
+
import { DragBoxsContext } from "./dragBoxsContext";
|
|
7
|
+
const DragBoxsBase = (props) => {
|
|
8
|
+
const { defaultPosX = 0, defaultPosY = 0, children } = props;
|
|
9
|
+
const id = useId();
|
|
10
|
+
const [isMounded, setMounded] = useState(false);
|
|
11
|
+
const [positions, setPositions] = useState({
|
|
12
|
+
x: defaultPosX,
|
|
13
|
+
y: defaultPosY
|
|
14
|
+
});
|
|
15
|
+
const dragEndEvent = useCallback((event) => {
|
|
16
|
+
const { delta, active } = event;
|
|
17
|
+
const id = active.id;
|
|
18
|
+
setPositions((prev) => ({
|
|
19
|
+
...prev,
|
|
20
|
+
x: prev.x + delta.x,
|
|
21
|
+
y: prev.y + delta.y,
|
|
22
|
+
}));
|
|
23
|
+
}, []);
|
|
24
|
+
const dragModifiers = useCallback(({ transform, activeNodeRect }) => {
|
|
25
|
+
if (!activeNodeRect)
|
|
26
|
+
return transform;
|
|
27
|
+
const boxWidth = activeNodeRect.width;
|
|
28
|
+
const boxHeight = activeNodeRect.height;
|
|
29
|
+
const originalLeft = activeNodeRect.left;
|
|
30
|
+
const originalTop = activeNodeRect.top;
|
|
31
|
+
const maxX = window.innerWidth - (originalLeft + boxWidth);
|
|
32
|
+
const maxY = window.innerHeight - (originalTop + boxHeight);
|
|
33
|
+
const minX = -originalLeft;
|
|
34
|
+
const minY = -originalTop;
|
|
35
|
+
return {
|
|
36
|
+
x: Math.min(Math.max(transform.x, minX), maxX),
|
|
37
|
+
y: Math.min(Math.max(transform.y, minY), maxY),
|
|
38
|
+
};
|
|
39
|
+
}, []);
|
|
40
|
+
const resizeNextPositions = useCallback((target, prev) => {
|
|
41
|
+
if (typeof window !== "undefined" && target) {
|
|
42
|
+
const { x: currentX = 0, y: currentY = 0 } = prev || {};
|
|
43
|
+
const { innerWidth, innerHeight } = window;
|
|
44
|
+
const { clientWidth, clientHeight } = target;
|
|
45
|
+
const maxX = innerWidth - clientWidth;
|
|
46
|
+
const maxY = innerHeight - clientHeight;
|
|
47
|
+
return {
|
|
48
|
+
x: Math.min(currentX, maxX < 0 ? 0 : maxX),
|
|
49
|
+
y: Math.min(currentY, maxY < 0 ? 0 : maxY),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return { ...prev };
|
|
53
|
+
}, []);
|
|
54
|
+
const resizeEvent = useCallback(() => {
|
|
55
|
+
const target = document.getElementById(id);
|
|
56
|
+
if (target)
|
|
57
|
+
setPositions((prev) => resizeNextPositions(target, prev));
|
|
58
|
+
}, []);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
setMounded(true);
|
|
61
|
+
window.addEventListener("resize", resizeEvent);
|
|
62
|
+
return () => window.removeEventListener("resize", resizeEvent);
|
|
63
|
+
}, []);
|
|
64
|
+
return (_jsx(_Fragment, { children: isMounded
|
|
65
|
+
? createPortal(_jsx(DndContext, { onDragEnd: dragEndEvent, modifiers: [dragModifiers], children: _jsx(DragBoxsContext, { value: { id, positions }, children: children }) }), document.body)
|
|
66
|
+
: null }));
|
|
67
|
+
};
|
|
68
|
+
const DragBoxs = memo(DragBoxsBase);
|
|
69
|
+
DragBoxs.displayName = "DragBoxs";
|
|
70
|
+
export default DragBoxs;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useDraggable } from "@dnd-kit/core";
|
|
2
|
+
export type PositionsType = {
|
|
3
|
+
x?: number;
|
|
4
|
+
y?: number;
|
|
5
|
+
};
|
|
6
|
+
export interface DragBoxsContextType {
|
|
7
|
+
id: string;
|
|
8
|
+
positions: PositionsType;
|
|
9
|
+
}
|
|
10
|
+
export declare const DragBoxsContext: import("react").Context<DragBoxsContextType | undefined> & {
|
|
11
|
+
displayName: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const useDragBoxsContext: () => DragBoxsContextType;
|
|
14
|
+
export { useDraggable };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createContext, use } from 'react';
|
|
2
|
+
import { useDraggable } from "@dnd-kit/core";
|
|
3
|
+
export const DragBoxsContext = Object.assign(createContext(undefined), { displayName: "DragBoxsContext" });
|
|
4
|
+
export const useDragBoxsContext = () => {
|
|
5
|
+
const context = use(DragBoxsContext);
|
|
6
|
+
if (context)
|
|
7
|
+
return context;
|
|
8
|
+
throw new Error('useDragPanel must be used inside provider');
|
|
9
|
+
};
|
|
10
|
+
export { useDraggable };
|
package/dist/components/index.js
CHANGED