Skip to content

Commit d6b5736

Browse files
committed
Add disabled prop to <DragOverlay>
1 parent e502979 commit d6b5736

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dnd-kit/react': patch
3+
---
4+
5+
**<DragOverlay>**: Added `disabled` prop to temporarily disable `<DragOverlay>` without unmounting it. The `disabled` prop accepts either a `boolean` or function that receives the `source` as input and returns a `boolean`, which can be useful to disable the `<DragOverlay>` based on the `type` or `data` of the `source`.

packages/react/src/core/draggable/DragOverlay.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@ export interface Props<T extends Data, U extends Draggable<T>> {
1111
children: ReactNode | ((source: U) => ReactNode);
1212
style?: React.CSSProperties;
1313
tag?: string;
14+
disabled?: boolean | ((source: U) => boolean);
1415
}
1516

1617
export function DragOverlay<T extends Data, U extends Draggable<T>>({
1718
children,
1819
className,
1920
style,
2021
tag,
22+
disabled,
2123
}: Props<T, U>) {
2224
const ref = useRef<HTMLDivElement | null>(null);
2325
const manager = useDragDropManager();
2426
const source = useComputed(
2527
() => manager?.dragOperation.source,
2628
[manager]
2729
).value;
30+
const isDisabled =
31+
typeof disabled === 'function' ? disabled(source) : disabled;
2832

2933
useEffect(() => {
30-
if (!ref.current || !manager) return;
34+
if (!ref.current || !manager || isDisabled) return;
3135

3236
const feedback = manager.plugins.find(
3337
(plugin) => plugin instanceof Feedback
@@ -40,7 +44,7 @@ export function DragOverlay<T extends Data, U extends Draggable<T>>({
4044
return () => {
4145
feedback.overlay = undefined;
4246
};
43-
}, [manager]);
47+
}, [manager, isDisabled]);
4448

4549
// Prevent children of the overlay from registering themselves as draggables or droppables
4650
const patchedManager = useMemo(() => {
@@ -79,7 +83,7 @@ export function DragOverlay<T extends Data, U extends Draggable<T>>({
7983
);
8084

8185
function renderChildren() {
82-
if (!source) return null;
86+
if (!source || isDisabled) return null;
8387

8488
if (typeof children === 'function') {
8589
return <Children source={source}>{children}</Children>;

0 commit comments

Comments
 (0)