@reactuses/core 1.0.1 → 1.0.2
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/index.cjs +76 -0
- package/index.d.ts +64 -1
- package/index.mjs +76 -1
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -2148,6 +2148,81 @@ function useActiveElement() {
|
|
|
2148
2148
|
return active;
|
|
2149
2149
|
}
|
|
2150
2150
|
|
|
2151
|
+
function useDraggable(target, options = {}) {
|
|
2152
|
+
var _a, _b;
|
|
2153
|
+
const draggingElement = getTargetElement(options.draggingElement, window);
|
|
2154
|
+
const draggingHandle = getTargetElement((_a = options.handle) != null ? _a : target);
|
|
2155
|
+
const [position, setPositon] = react.useState(
|
|
2156
|
+
(_b = options.initialValue) != null ? _b : { x: 0, y: 0 }
|
|
2157
|
+
);
|
|
2158
|
+
const [pressedDelta, setPressedDelta] = react.useState();
|
|
2159
|
+
const filterEvent = (e) => {
|
|
2160
|
+
if (options.pointerTypes) {
|
|
2161
|
+
return options.pointerTypes.includes(e.pointerType);
|
|
2162
|
+
}
|
|
2163
|
+
return true;
|
|
2164
|
+
};
|
|
2165
|
+
const handleEvent = (e) => {
|
|
2166
|
+
if (options.preventDefault) {
|
|
2167
|
+
e.preventDefault();
|
|
2168
|
+
}
|
|
2169
|
+
if (options.stopPropagation) {
|
|
2170
|
+
e.stopPropagation();
|
|
2171
|
+
}
|
|
2172
|
+
};
|
|
2173
|
+
const start = (e) => {
|
|
2174
|
+
var _a2;
|
|
2175
|
+
const element = getTargetElement(target);
|
|
2176
|
+
if (!filterEvent(e) || !element) {
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
if (options.exact && e.target !== element) {
|
|
2180
|
+
return;
|
|
2181
|
+
}
|
|
2182
|
+
const rect = element.getBoundingClientRect();
|
|
2183
|
+
const pos = {
|
|
2184
|
+
x: e.pageX - rect.left,
|
|
2185
|
+
y: e.pageY - rect.top
|
|
2186
|
+
};
|
|
2187
|
+
if (((_a2 = options.onStart) == null ? void 0 : _a2.call(options, pos, e)) === false) {
|
|
2188
|
+
return;
|
|
2189
|
+
}
|
|
2190
|
+
setPressedDelta(pos);
|
|
2191
|
+
handleEvent(e);
|
|
2192
|
+
};
|
|
2193
|
+
const move = (e) => {
|
|
2194
|
+
var _a2;
|
|
2195
|
+
if (!filterEvent(e)) {
|
|
2196
|
+
return;
|
|
2197
|
+
}
|
|
2198
|
+
if (!pressedDelta) {
|
|
2199
|
+
return;
|
|
2200
|
+
}
|
|
2201
|
+
setPositon({
|
|
2202
|
+
x: e.pageX - pressedDelta.x,
|
|
2203
|
+
y: e.pageY - pressedDelta.y
|
|
2204
|
+
});
|
|
2205
|
+
(_a2 = options.onMove) == null ? void 0 : _a2.call(options, position, e);
|
|
2206
|
+
handleEvent(e);
|
|
2207
|
+
};
|
|
2208
|
+
const end = (e) => {
|
|
2209
|
+
var _a2;
|
|
2210
|
+
if (!filterEvent(e)) {
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
if (!pressedDelta) {
|
|
2214
|
+
return;
|
|
2215
|
+
}
|
|
2216
|
+
setPressedDelta(void 0);
|
|
2217
|
+
(_a2 = options.onEnd) == null ? void 0 : _a2.call(options, position, e);
|
|
2218
|
+
handleEvent(e);
|
|
2219
|
+
};
|
|
2220
|
+
useEventListener("pointerdown", start, draggingHandle, true);
|
|
2221
|
+
useEventListener("pointermove", move, draggingElement, true);
|
|
2222
|
+
useEventListener("pointerup", end, draggingElement, true);
|
|
2223
|
+
return [position.x, position.y, !!pressedDelta];
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2151
2226
|
exports.useActiveElement = useActiveElement;
|
|
2152
2227
|
exports.useCounter = useCounter;
|
|
2153
2228
|
exports.useCustomCompareEffect = useCustomCompareEffect;
|
|
@@ -2156,6 +2231,7 @@ exports.useDebounce = useDebounce;
|
|
|
2156
2231
|
exports.useDebounceFn = useDebounceFn;
|
|
2157
2232
|
exports.useDeepCompareEffect = useDeepCompareEffect;
|
|
2158
2233
|
exports.useDocumentVisibility = useDocumentVisibility;
|
|
2234
|
+
exports.useDraggable = useDraggable;
|
|
2159
2235
|
exports.useDropZone = useDropZone;
|
|
2160
2236
|
exports.useElementSize = useElementSize;
|
|
2161
2237
|
exports.useEvent = useEvent;
|
package/index.d.ts
CHANGED
|
@@ -45,6 +45,11 @@ declare function useSessionStorage<T = unknown>(key: string, defaults: null, opt
|
|
|
45
45
|
|
|
46
46
|
declare type Fn = (this: any, ...args: any[]) => any;
|
|
47
47
|
declare type Stoppable = [boolean, Fn, Fn];
|
|
48
|
+
declare type PointerType = "mouse" | "touch" | "pen";
|
|
49
|
+
interface Position {
|
|
50
|
+
x: number;
|
|
51
|
+
y: number;
|
|
52
|
+
}
|
|
48
53
|
|
|
49
54
|
declare function useEvent<T extends Fn>(fn: T): T;
|
|
50
55
|
|
|
@@ -585,4 +590,62 @@ declare function usePreferredContrast(defaultState?: Contrast): Contrast;
|
|
|
585
590
|
|
|
586
591
|
declare function useActiveElement<T extends Element>(): T | null;
|
|
587
592
|
|
|
588
|
-
|
|
593
|
+
interface UseDraggableOptions {
|
|
594
|
+
/**
|
|
595
|
+
* Only start the dragging when click on the element directly
|
|
596
|
+
*
|
|
597
|
+
* @default false
|
|
598
|
+
*/
|
|
599
|
+
exact?: boolean;
|
|
600
|
+
/**
|
|
601
|
+
* Prevent events defaults
|
|
602
|
+
*
|
|
603
|
+
* @default false
|
|
604
|
+
*/
|
|
605
|
+
preventDefault?: boolean;
|
|
606
|
+
/**
|
|
607
|
+
* Prevent events propagation
|
|
608
|
+
*
|
|
609
|
+
* @default false
|
|
610
|
+
*/
|
|
611
|
+
stopPropagation?: boolean;
|
|
612
|
+
/**
|
|
613
|
+
* Element to attach `pointermove` and `pointerup` events to.
|
|
614
|
+
*
|
|
615
|
+
* @default window
|
|
616
|
+
*/
|
|
617
|
+
draggingElement?: BasicTarget<HTMLElement | SVGElement | Window | Document>;
|
|
618
|
+
/**
|
|
619
|
+
* Handle that triggers the drag event
|
|
620
|
+
*
|
|
621
|
+
* @default target
|
|
622
|
+
*/
|
|
623
|
+
handle?: BasicTarget<HTMLElement | SVGElement>;
|
|
624
|
+
/**
|
|
625
|
+
* Pointer types that listen to.
|
|
626
|
+
*
|
|
627
|
+
* @default ['mouse', 'touch', 'pen']
|
|
628
|
+
*/
|
|
629
|
+
pointerTypes?: PointerType[];
|
|
630
|
+
/**
|
|
631
|
+
* Initial position of the element.
|
|
632
|
+
*
|
|
633
|
+
* @default { x: 0, y: 0 }
|
|
634
|
+
*/
|
|
635
|
+
initialValue?: Position;
|
|
636
|
+
/**
|
|
637
|
+
* Callback when the dragging starts. Return `false` to prevent dragging.
|
|
638
|
+
*/
|
|
639
|
+
onStart?: (position: Position, event: PointerEvent) => void | false;
|
|
640
|
+
/**
|
|
641
|
+
* Callback during dragging.
|
|
642
|
+
*/
|
|
643
|
+
onMove?: (position: Position, event: PointerEvent) => void;
|
|
644
|
+
/**
|
|
645
|
+
* Callback when dragging end.
|
|
646
|
+
*/
|
|
647
|
+
onEnd?: (position: Position, event: PointerEvent) => void;
|
|
648
|
+
}
|
|
649
|
+
declare function useDraggable(target: BasicTarget<HTMLElement | SVGElement>, options?: UseDraggableOptions): readonly [number, number, boolean];
|
|
650
|
+
|
|
651
|
+
export { useActiveElement, useCounter, useCustomCompareEffect, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementSize, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, _default as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, useMarkdown, _default$1 as useMediaDevices, useMediaQuery, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnline, useOrientation, usePageLeave, usePermission, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useResizeObserver, useScriptTag, useScroll, useScrollLock, useSessionStorage, useTextDirection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, _default$3 as useUpdateEffect, _default$2 as useUpdateLayoutEffect, useVirtualList };
|
package/index.mjs
CHANGED
|
@@ -2144,4 +2144,79 @@ function useActiveElement() {
|
|
|
2144
2144
|
return active;
|
|
2145
2145
|
}
|
|
2146
2146
|
|
|
2147
|
-
|
|
2147
|
+
function useDraggable(target, options = {}) {
|
|
2148
|
+
var _a, _b;
|
|
2149
|
+
const draggingElement = getTargetElement(options.draggingElement, window);
|
|
2150
|
+
const draggingHandle = getTargetElement((_a = options.handle) != null ? _a : target);
|
|
2151
|
+
const [position, setPositon] = useState(
|
|
2152
|
+
(_b = options.initialValue) != null ? _b : { x: 0, y: 0 }
|
|
2153
|
+
);
|
|
2154
|
+
const [pressedDelta, setPressedDelta] = useState();
|
|
2155
|
+
const filterEvent = (e) => {
|
|
2156
|
+
if (options.pointerTypes) {
|
|
2157
|
+
return options.pointerTypes.includes(e.pointerType);
|
|
2158
|
+
}
|
|
2159
|
+
return true;
|
|
2160
|
+
};
|
|
2161
|
+
const handleEvent = (e) => {
|
|
2162
|
+
if (options.preventDefault) {
|
|
2163
|
+
e.preventDefault();
|
|
2164
|
+
}
|
|
2165
|
+
if (options.stopPropagation) {
|
|
2166
|
+
e.stopPropagation();
|
|
2167
|
+
}
|
|
2168
|
+
};
|
|
2169
|
+
const start = (e) => {
|
|
2170
|
+
var _a2;
|
|
2171
|
+
const element = getTargetElement(target);
|
|
2172
|
+
if (!filterEvent(e) || !element) {
|
|
2173
|
+
return;
|
|
2174
|
+
}
|
|
2175
|
+
if (options.exact && e.target !== element) {
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
const rect = element.getBoundingClientRect();
|
|
2179
|
+
const pos = {
|
|
2180
|
+
x: e.pageX - rect.left,
|
|
2181
|
+
y: e.pageY - rect.top
|
|
2182
|
+
};
|
|
2183
|
+
if (((_a2 = options.onStart) == null ? void 0 : _a2.call(options, pos, e)) === false) {
|
|
2184
|
+
return;
|
|
2185
|
+
}
|
|
2186
|
+
setPressedDelta(pos);
|
|
2187
|
+
handleEvent(e);
|
|
2188
|
+
};
|
|
2189
|
+
const move = (e) => {
|
|
2190
|
+
var _a2;
|
|
2191
|
+
if (!filterEvent(e)) {
|
|
2192
|
+
return;
|
|
2193
|
+
}
|
|
2194
|
+
if (!pressedDelta) {
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
setPositon({
|
|
2198
|
+
x: e.pageX - pressedDelta.x,
|
|
2199
|
+
y: e.pageY - pressedDelta.y
|
|
2200
|
+
});
|
|
2201
|
+
(_a2 = options.onMove) == null ? void 0 : _a2.call(options, position, e);
|
|
2202
|
+
handleEvent(e);
|
|
2203
|
+
};
|
|
2204
|
+
const end = (e) => {
|
|
2205
|
+
var _a2;
|
|
2206
|
+
if (!filterEvent(e)) {
|
|
2207
|
+
return;
|
|
2208
|
+
}
|
|
2209
|
+
if (!pressedDelta) {
|
|
2210
|
+
return;
|
|
2211
|
+
}
|
|
2212
|
+
setPressedDelta(void 0);
|
|
2213
|
+
(_a2 = options.onEnd) == null ? void 0 : _a2.call(options, position, e);
|
|
2214
|
+
handleEvent(e);
|
|
2215
|
+
};
|
|
2216
|
+
useEventListener("pointerdown", start, draggingHandle, true);
|
|
2217
|
+
useEventListener("pointermove", move, draggingElement, true);
|
|
2218
|
+
useEventListener("pointerup", end, draggingElement, true);
|
|
2219
|
+
return [position.x, position.y, !!pressedDelta];
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
export { useActiveElement, useCounter, useCustomCompareEffect, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementSize, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, useFps$1 as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, useMarkdown, useMediaDevices$1 as useMediaDevices, useMediaQuery, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnline, useOrientation, usePageLeave, usePermission, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useResizeObserver, useScriptTag, useScroll, useScrollLock, useSessionStorage, useTextDirection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useVirtualList };
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactuses/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"main": "./index.cjs",
|
|
5
5
|
"module": "./index.mjs",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"license": "Unlicense",
|
|
9
|
-
"homepage": "https://
|
|
9
|
+
"homepage": "https://www.reactuse.com/",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "https://github.com/childrentime/reactuse",
|