Skip to content

Commit 11334a5

Browse files
committed
feat(modifiers): add restrictToSpecificParent modifier
1 parent e9215e8 commit 11334a5

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

.changeset/tricky-camels-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@dnd-kit/modifiers": minor
3+
---
4+
5+
Added new `restrictToSpecificParent` modifier that allows restricting dragging within a specific parent element's boundaries using React refs. This is useful when you need to constrain movement to a container that isn't the immediate parent element.

packages/modifiers/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ Restrict movement to only the vertical axis.
5454

5555
### Restrict motion to a container's bounding rectangle
5656

57+
#### `restrictToSpecificParent`
58+
59+
Restrict movement to a specified parent element's bounding rectangle. This modifier can be useful when you need to constrain movement to a specific container rather than the immediate parent.
60+
61+
```jsx
62+
import {DndContext} from '@dnd-kit/core';
63+
import {restrictToSpecificParent} from '@dnd-kit/modifiers';
64+
65+
function App() {
66+
const containerRef = useRef(null);
67+
68+
return (
69+
<div ref={containerRef}>
70+
<DndContext modifiers={[restrictToSpecificParent({
71+
parentElement: containerRef.current
72+
})]}>
73+
{/* ... */}
74+
</DndContext>
75+
</div>
76+
);
77+
}
78+
```
79+
5780
#### `restrictToWindowEdges`
5881

5982
Restrict movement to the edges of the window. This modifier can be useful to prevent the `DragOverlay` from being moved outside of the bounds of the window.

packages/modifiers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {createSnapModifier} from './createSnapModifier';
22
export {restrictToHorizontalAxis} from './restrictToHorizontalAxis';
33
export {restrictToParentElement} from './restrictToParentElement';
4+
export {restrictToSpecificParent} from './restrictToSpecificParent';
45
export {restrictToFirstScrollableAncestor} from './restrictToFirstScrollableAncestor';
56
export {restrictToVerticalAxis} from './restrictToVerticalAxis';
67
export {restrictToWindowEdges} from './restrictToWindowEdges';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type {Modifier} from '@dnd-kit/core';
2+
import type {RefObject} from 'react';
3+
import {restrictToBoundingRect} from './utilities';
4+
5+
export interface RestrictToSpecificParentOptions {
6+
parentRef: RefObject<HTMLElement>;
7+
}
8+
9+
export const restrictToSpecificParent = (
10+
options: RestrictToSpecificParentOptions
11+
): Modifier => {
12+
return ({transform, draggingNodeRect}) => {
13+
if (!draggingNodeRect || !options.parentRef.current) {
14+
return transform;
15+
}
16+
17+
const parentRect = options.parentRef.current.getBoundingClientRect();
18+
return restrictToBoundingRect(transform, draggingNodeRect, parentRect);
19+
};
20+
};

0 commit comments

Comments
 (0)