-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
useDndMonitor()
to dispatch events synchronously
- Loading branch information
Showing
13 changed files
with
115 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@dnd-kit/core': minor | ||
--- | ||
|
||
The `useDndMonitor()` hook has been refactored to be synchronously invoked at the same time as the events dispatched by `<DndContext>` (such as `onDragStart`, `onDragOver`, `onDragEnd`). | ||
|
||
The new refactor uses the subscribe/notify pattern and no longer causes re-renders in consuming components of `useDndMonitor()` when events are dispatched. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {createContext} from 'react'; | ||
|
||
import type {RegisterListener} from './types'; | ||
|
||
export const DndMonitorContext = createContext<RegisterListener | null>(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export {DndMonitorContext} from './context'; | ||
export type {DndMonitorListener, DndMonitorEvent} from './types'; | ||
export {useDndMonitor} from './useDndMonitor'; | ||
export {useDndMonitorProvider} from './useDndMonitorProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { | ||
DragStartEvent, | ||
DragCancelEvent, | ||
DragEndEvent, | ||
DragMoveEvent, | ||
DragOverEvent, | ||
} from '../../types'; | ||
|
||
export interface DndMonitorListener { | ||
onDragStart?(event: DragStartEvent): void; | ||
onDragMove?(event: DragMoveEvent): void; | ||
onDragOver?(event: DragOverEvent): void; | ||
onDragEnd?(event: DragEndEvent): void; | ||
onDragCancel?(event: DragCancelEvent): void; | ||
} | ||
|
||
export interface DndMonitorEvent { | ||
type: keyof DndMonitorListener; | ||
event: | ||
| DragStartEvent | ||
| DragMoveEvent | ||
| DragOverEvent | ||
| DragEndEvent | ||
| DragCancelEvent; | ||
} | ||
|
||
export type UnregisterListener = () => void; | ||
|
||
export type RegisterListener = ( | ||
listener: DndMonitorListener | ||
) => UnregisterListener; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {useContext, useEffect} from 'react'; | ||
|
||
import {DndMonitorContext} from './context'; | ||
import type {DndMonitorListener} from './types'; | ||
|
||
export function useDndMonitor(listener: DndMonitorListener) { | ||
const registerListener = useContext(DndMonitorContext); | ||
|
||
useEffect(() => { | ||
if (!registerListener) { | ||
throw new Error( | ||
'useDndMonitor must be used within a children of <DndContext>' | ||
); | ||
} | ||
|
||
const unsubscribe = registerListener(listener); | ||
|
||
return unsubscribe; | ||
}, [listener, registerListener]); | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/core/src/components/DndMonitor/useDndMonitorProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {useCallback, useState} from 'react'; | ||
|
||
import type {DndMonitorListener, DndMonitorEvent} from './types'; | ||
|
||
export function useDndMonitorProvider() { | ||
const [listeners] = useState(() => new Set<DndMonitorListener>()); | ||
|
||
const registerListener = useCallback( | ||
(listener) => { | ||
listeners.add(listener); | ||
return () => listeners.delete(listener); | ||
}, | ||
[listeners] | ||
); | ||
|
||
const dispatch = useCallback( | ||
({type, event}: DndMonitorEvent) => { | ||
listeners.forEach((listener) => listener[type]?.(event as any)); | ||
}, | ||
[listeners] | ||
); | ||
|
||
return [dispatch, registerListener] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters