@arshad-shah/detent-react 0.2.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.
- package/LICENSE +21 -0
- package/README.md +65 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -0
- package/dist/latest.d.ts +11 -0
- package/dist/use-draggable.d.ts +13 -0
- package/dist/use-resizable.d.ts +8 -0
- package/dist/use-sortable.d.ts +8 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arshad Shah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# detent-react
|
|
2
|
+
|
|
3
|
+
React bindings for [detent](https://www.npmjs.com/package/detent) — drag,
|
|
4
|
+
reorder and resize with no dependencies.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install detent detent-react
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```jsx
|
|
11
|
+
import { useSortable } from '@arshad-shah/detent-react';
|
|
12
|
+
|
|
13
|
+
function List({ items, onReorder }) {
|
|
14
|
+
const ref = useSortable({
|
|
15
|
+
animation: 180,
|
|
16
|
+
onSort: ({ from, to }) => onReorder(from.index, to.index),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<ul ref={ref}>
|
|
21
|
+
{items.map((item) => (
|
|
22
|
+
<li key={item.id}>{item.label}</li>
|
|
23
|
+
))}
|
|
24
|
+
</ul>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Each hook returns a callback ref. Attach it to the element you want bound.
|
|
30
|
+
|
|
31
|
+
| Hook | Binds |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `useDraggable(options?)` | `draggable` |
|
|
34
|
+
| `useSortable(options?)` | `sortable` |
|
|
35
|
+
| `useResizable(options?)` | `resizable` |
|
|
36
|
+
|
|
37
|
+
Options are the same as the core library's — see
|
|
38
|
+
[the documentation](https://detent.arshadshah.com).
|
|
39
|
+
|
|
40
|
+
## Re-rendering never re-binds
|
|
41
|
+
|
|
42
|
+
Pass inline objects and arrow functions freely:
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
const ref = useDraggable({
|
|
46
|
+
bounds: 'parent',
|
|
47
|
+
onEnd: (event) => save(event.offset), // recreated every render, and fine
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Options are read through a ref at call time, so a re-render mid-drag does not
|
|
52
|
+
interrupt it and callbacks are always the newest ones. You do not need
|
|
53
|
+
`useCallback` or `useMemo` here.
|
|
54
|
+
|
|
55
|
+
## Styles
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import '@arshad-shah/detent/styles.css';
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Required for resize handles, optional otherwise.
|
|
62
|
+
|
|
63
|
+
Peer `react >= 18`; works with 19. No `react-dom` dependency.
|
|
64
|
+
|
|
65
|
+
MIT.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var s=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var R=(t,e)=>{for(var n in e)s(t,n,{get:e[n],enumerable:!0})},z=(t,e,n,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of x(e))!S.call(t,r)&&r!==n&&s(t,r,{get:()=>e[r],enumerable:!(u=y(e,r))||u.enumerable});return t};var H=t=>z(s({},"__esModule",{value:!0}),t);var E={};R(E,{useDraggable:()=>m,useLatest:()=>a,useResizable:()=>h,useSortable:()=>b});module.exports=H(E);var c=require("react"),g=require("@arshad-shah/detent");var d=require("react");function a(t){let e=(0,d.useRef)(t);return e.current=t,e}function m(t={}){let e=a(t),n=(0,c.useRef)(null);return(0,c.useCallback)(u=>{n.current?.destroy(),n.current=null,u&&(n.current=(0,g.draggable)(u,{get axis(){return e.current.axis},get bounds(){return e.current.bounds},get grid(){return e.current.grid},get gridOrigin(){return e.current.gridOrigin},get disabled(){return e.current.disabled},get handle(){return e.current.handle},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},get touchAction(){return e.current.touchAction},onStart:r=>e.current.onStart?.(r),onMove:r=>e.current.onMove?.(r),onEnd:(r,o)=>e.current.onEnd?.(r,o)}))},[e])}var l=require("react"),p=require("@arshad-shah/detent");function b(t={}){let e=a(t),n=(0,l.useRef)(null);return(0,l.useCallback)(u=>{n.current?.destroy(),n.current=null,u&&(n.current=(0,p.sortable)(u,{get group(){return e.current.group},get items(){return e.current.items},get direction(){return e.current.direction},get animation(){return e.current.animation},get autoScroll(){return e.current.autoScroll},get keyboard(){return e.current.keyboard},get zIndex(){return e.current.zIndex},get disabled(){return e.current.disabled},get handle(){return e.current.handle},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},get touchAction(){return e.current.touchAction},onStart:(r,o)=>e.current.onStart?.(r,o),onMove:(r,o)=>e.current.onMove?.(r,o),onSort:r=>e.current.onSort?.(r),onEnd:(r,o)=>e.current.onEnd?.(r,o)}))},[e])}var i=require("react"),f=require("@arshad-shah/detent");function h(t={}){let e=a(t),n=(0,i.useRef)(null);return(0,i.useCallback)(u=>{n.current?.destroy(),n.current=null,u&&(n.current=(0,f.resizable)(u,{handles:e.current.handles,get minWidth(){return e.current.minWidth},get minHeight(){return e.current.minHeight},get maxWidth(){return e.current.maxWidth},get maxHeight(){return e.current.maxHeight},get aspectRatio(){return e.current.aspectRatio},get grid(){return e.current.grid},get bounds(){return e.current.bounds},get disabled(){return e.current.disabled},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},onStart:r=>e.current.onStart?.(r),onResize:r=>e.current.onResize?.(r),onEnd:(r,o)=>e.current.onEnd?.(r,o)}))},[e])}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { useDraggable } from './use-draggable';
|
|
2
|
+
export { useSortable } from './use-sortable';
|
|
3
|
+
export { useResizable } from './use-resizable';
|
|
4
|
+
export { useLatest } from './latest';
|
|
5
|
+
export type { DraggableOptions, SortableOptions, ResizableOptions, DragEvent, SortEvent, SortLocation, ResizeEvent, } from '@arshad-shah/detent';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useCallback as l,useRef as i}from"react";import{draggable as s}from"@arshad-shah/detent";import{useRef as c}from"react";function a(u){let e=c(u);return e.current=u,e}function d(u={}){let e=a(u),t=i(null);return l(o=>{t.current?.destroy(),t.current=null,o&&(t.current=s(o,{get axis(){return e.current.axis},get bounds(){return e.current.bounds},get grid(){return e.current.grid},get gridOrigin(){return e.current.gridOrigin},get disabled(){return e.current.disabled},get handle(){return e.current.handle},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},get touchAction(){return e.current.touchAction},onStart:r=>e.current.onStart?.(r),onMove:r=>e.current.onMove?.(r),onEnd:(r,n)=>e.current.onEnd?.(r,n)}))},[e])}import{useCallback as g,useRef as m}from"react";import{sortable as p}from"@arshad-shah/detent";function b(u={}){let e=a(u),t=m(null);return g(o=>{t.current?.destroy(),t.current=null,o&&(t.current=p(o,{get group(){return e.current.group},get items(){return e.current.items},get direction(){return e.current.direction},get animation(){return e.current.animation},get autoScroll(){return e.current.autoScroll},get keyboard(){return e.current.keyboard},get zIndex(){return e.current.zIndex},get disabled(){return e.current.disabled},get handle(){return e.current.handle},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},get touchAction(){return e.current.touchAction},onStart:(r,n)=>e.current.onStart?.(r,n),onMove:(r,n)=>e.current.onMove?.(r,n),onSort:r=>e.current.onSort?.(r),onEnd:(r,n)=>e.current.onEnd?.(r,n)}))},[e])}import{useCallback as f,useRef as h}from"react";import{resizable as y}from"@arshad-shah/detent";function x(u={}){let e=a(u),t=h(null);return f(o=>{t.current?.destroy(),t.current=null,o&&(t.current=y(o,{handles:e.current.handles,get minWidth(){return e.current.minWidth},get minHeight(){return e.current.minHeight},get maxWidth(){return e.current.maxWidth},get maxHeight(){return e.current.maxHeight},get aspectRatio(){return e.current.aspectRatio},get grid(){return e.current.grid},get bounds(){return e.current.bounds},get disabled(){return e.current.disabled},get cancel(){return e.current.cancel},get distance(){return e.current.distance},get delay(){return e.current.delay},get tolerance(){return e.current.tolerance},onStart:r=>e.current.onStart?.(r),onResize:r=>e.current.onResize?.(r),onEnd:(r,n)=>e.current.onEnd?.(r,n)}))},[e])}export{d as useDraggable,a as useLatest,x as useResizable,b as useSortable};
|
package/dist/latest.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hold the most recent value in a ref, updated during render.
|
|
3
|
+
*
|
|
4
|
+
* This is what lets a binding read fresh options and callbacks without being
|
|
5
|
+
* torn down and rebuilt. Assigning during render rather than in an effect
|
|
6
|
+
* matters: an event can fire between render and effect flush, and the ref has
|
|
7
|
+
* to be current by then.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useLatest<T>(value: T): {
|
|
10
|
+
readonly current: T;
|
|
11
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type DraggableOptions } from '@arshad-shah/detent';
|
|
2
|
+
/**
|
|
3
|
+
* Bind `draggable` to whatever element the returned ref is attached to.
|
|
4
|
+
*
|
|
5
|
+
* Options are read through a ref at call time, so passing an inline object or
|
|
6
|
+
* arrow callback — which is the normal way to write React — never re-binds and
|
|
7
|
+
* never interrupts a drag in progress. No useCallback or useMemo required.
|
|
8
|
+
*
|
|
9
|
+
* The getters matter: the core reads several options once per drag rather than
|
|
10
|
+
* once at bind time, so a changed `bounds` or `disabled` takes effect on the
|
|
11
|
+
* next drag without rebinding.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useDraggable(options?: DraggableOptions): (node: HTMLElement | null) => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ResizableOptions } from '@arshad-shah/detent';
|
|
2
|
+
/**
|
|
3
|
+
* Bind `resizable` to whatever element the returned ref is attached to.
|
|
4
|
+
*
|
|
5
|
+
* As with useDraggable, options are read at call time so re-rendering never
|
|
6
|
+
* re-binds and callbacks are always the newest ones.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useResizable(options?: ResizableOptions): (node: HTMLElement | null) => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type SortableOptions } from '@arshad-shah/detent';
|
|
2
|
+
/**
|
|
3
|
+
* Bind `sortable` to whatever element the returned ref is attached to.
|
|
4
|
+
*
|
|
5
|
+
* As with useDraggable, options are read at call time so re-rendering never
|
|
6
|
+
* re-binds and callbacks are always the newest ones.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useSortable(options?: SortableOptions): (node: HTMLElement | null) => void;
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arshad-shah/detent-react",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "React bindings for detent \u2014 drag, reorder and resize.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "Arshad Shah",
|
|
9
|
+
"homepage": "https://detent.arshadshah.com",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/arshad-shah/detent.git",
|
|
13
|
+
"directory": "packages/detent-react"
|
|
14
|
+
},
|
|
15
|
+
"bugs": "https://github.com/arshad-shah/detent/issues",
|
|
16
|
+
"main": "./dist/index.cjs",
|
|
17
|
+
"module": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node build.mjs",
|
|
34
|
+
"size": "node scripts/size-check.mjs",
|
|
35
|
+
"test": "pnpm test:browser",
|
|
36
|
+
"test:browser": "vitest run --project browser",
|
|
37
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.test.json"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@arshad-shah/detent": "workspace:^"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^24.13.3",
|
|
44
|
+
"@types/react": "^19.2.0",
|
|
45
|
+
"@types/react-dom": "^19.2.0",
|
|
46
|
+
"@vitejs/plugin-react": "^5.2.0",
|
|
47
|
+
"@vitest/browser": "^5.0.0",
|
|
48
|
+
"@vitest/browser-playwright": "^5.0.0",
|
|
49
|
+
"esbuild": "^0.28.2",
|
|
50
|
+
"playwright": "^1.63.0",
|
|
51
|
+
"react": "^19.2.8",
|
|
52
|
+
"react-dom": "^19.2.8",
|
|
53
|
+
"typescript": "^7.0.2",
|
|
54
|
+
"vite": "^7.3.6",
|
|
55
|
+
"vitest": "^5.0.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"react": ">=18"
|
|
59
|
+
},
|
|
60
|
+
"keywords": [
|
|
61
|
+
"react",
|
|
62
|
+
"drag",
|
|
63
|
+
"sortable",
|
|
64
|
+
"resize",
|
|
65
|
+
"hooks",
|
|
66
|
+
"@arshad-shah/detent"
|
|
67
|
+
],
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
}
|
|
71
|
+
}
|