- 将所有图片路径从绝对路径改为使用 process.env.PUBLIC_URL - 修复 HomePage.tsx 中所有图片引用 - 修复 CoursePage.tsx 中所有图片引用 - 确保图片在 GitHub Pages 上正确加载 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
72 KiB
@dnd-kit/core
6.3.1
Patch Changes
- #1555
62f632aThanks @clauderic! - AddedTabto the list of default key codes that end a drag and drop operation. Can be customized by passing in a custom list ofkeyCodesto the KeyboardSensor options.
6.3.0
Minor Changes
-
#1539
0c6a28dThanks @irobot! - Make it possible to add visual cues when using activation constraints.Context
Activation constraints are used when we want to prevent accidental dragging or when pointer press can mean more than "start dragging".
A typical use case is a button that needs to respond to both "click" and "drag" gestures. Clicks can be distinguished from drags based on how long the pointer was held pressed.
The problem
A control that responds differently to a pointer press based on duration or distance can be confusing to use -- the user has to guess how long to keep holding or how far to keep dragging until their intent is acknowledged.
Implementing such cues is currently possible by attaching extra event listeners so that we know when a drag is pending. Furthermore, the listener needs to have access to the same constraints that were applied to the sensor initiating the drag. This can be made to work in simple cases, but it becomes error-prone and difficult to maintain in complex scenarios.
Solution
This changeset proposes the addition of two new events:
onDragPendingandonDragAbort.onDragPendingA drag is considered to be pending when the pointer has been pressed and there are activation constraints that need to be satisfied before a drag can start.
This event is initially fired on pointer press. At this time
offset(see below) will beundefined.It will subsequently be fired every time the pointer is moved. This is to enable visual cues for distance-based activation.
The event's payload contains all the information necessary for providing visual feedback:
export interface DragPendingEvent { id: UniqueIdentifier; constraint: PointerActivationConstraint; initialCoordinates: Coordinates; offset?: Coordinates | undefined; }onDragAbortA drag is considered aborted when an activation constraint for a pending drag was violated. Useful as a prompt to cancel any visual cue animations currently in progress. Note that this event will not be fired when dragging ends or is canceled.
6.2.0
Minor Changes
Patch Changes
-
#1494
00ec286Thanks @dinkinflickaa! - Improves performance by eliminating wasteful re-renders on every child item on click -
#1400
995dc23Thanks @12joan! - ExportdefaultKeyboardCoordinateGetter -
#1542
f629ec6Thanks @clauderic! - Fix bug with draggable and sortable elements with anidequal to0. -
#1541
99643f6Thanks @clauderic! - Handletouchcancelandpointercancelevents. -
#1435
6bbe39bThanks @knaveenkumar3576! - Faster Paint with delayed flush of Effects -
#1543
bcaf7c4Thanks @clauderic! - Fix a bug with auto-scroller continuing to observe stale elements, causing them to be considered as scrollable. -
Updated dependencies [
93602df]:- @dnd-kit/accessibility@3.1.1
6.1.0
Minor Changes
-
#1271
bc588c7Thanks @clauderic! - IntroducebypassActivationConstraint()option forPointerSensor,MouseSensorandTouchSensor. This optional argument can be used to conditionally bypass activation constraints. An example use-case would be to bypass activation constraints when the activator event target is theactivatorNodeof a draggable source.useSensor(PointerSensor, { activationConstraint: { delay: 250, tolerance: 5, }, bypassActivationConstraint({event, activeNode}) { return activeNode.activatorNode.current?.contains(event.target); }, }); -
#1269
b417f0fThanks @clauderic! - Allowdelayanddistanceactivation constraints to be used concurrently forMouseSensor,TouchSensorandPointerSensor.
Patch Changes
6.0.8
Patch Changes
- #1038
da888eeThanks @WillDonohoe! - Fix errors with calls togetComputedStylein Firefox when destructuring from the window object
6.0.7
Patch Changes
- #788
da94c02Thanks @clauderic! - Bug fixes for React 18 Strict Mode
6.0.6
Patch Changes
-
Updated dependencies [
da7c60d]:- @dnd-kit/utilities@3.2.1
6.0.5
Patch Changes
4a5132dThanks @clauderic! - Removed a strayconsole.login theKeyboardSensor
6.0.4
Patch Changes
- #797
eaa6e12Thanks @clauderic! - Fixed a regression in theKeyboardSensorscrolling logic.
6.0.3
Patch Changes
- #772
e97cb1fThanks @clauderic! - The ARIA live region element used for screen reader announcements is now positioned usingposition: fixedinstead ofposition: absolute. As of@dnd-kit/core^6.0.0, the live region element is no longer portaled by default into thedocument.body. This change was introduced in order to fix issues with portaled live regions. However, this change can introduce visual regressions when using absolutely positioned elements, since the live region element is constrained to the stacking and position context of its closest positioned ancestor. Using fixed position ensures the element does not introduce visual regressions.
6.0.2
Patch Changes
-
#769
8e3599fThanks @clauderic! - Fixed an issue with thecontainerNodeRectthat is exposed to modifiers having stale properties (top,left, etc.) when its scrollable ancestors were scrolled. -
#769
53cb962Thanks @clauderic! - Fixed a regression with scrollable ancestors detection.The scrollable ancestors should be determined by the active node or the over node exclusively. The
draggingNodevariable shouldn't be used to detect scrollable ancestors since it can be the drag overlay node, and the drag overlay node doesn't have any scrollable ancestors because it is a fixed position element.
6.0.1
Patch Changes
- #759
e5b9d38Thanks @clauderic! - Fixed a regression with the default drop animation of<DragOverlay>for consumers using React 18.
6.0.0
Major Changes
-
#746
4173087Thanks @clauderic! - Accessibility related changes.Regrouping accessibility-related props
Accessibility-related props have been regrouped under the
accessibilityprop of<DndContext>:<DndContext - announcements={customAnnouncements} - screenReaderInstructions={customScreenReaderInstructions} + accessibility={{ + announcements: customAnnouncements, + screenReaderInstructions: customScreenReaderInstructions, + }}This is a breaking change that will allow easier addition of new accessibility-related features without overloading the props namespace of
<DndContext>.Arguments object for announcements
The arguments passed to announcement callbacks have changed. They now receive an object that contains the
activeandoverproperties that match the signature of those passed to the DragEvent handlers (onDragStart,onDragMove, etc.). This change allows consumers to read thedataproperty of theactiveandovernode to customize the announcements based on the data.Example migration steps:
export const announcements: Announcements = { - onDragStart(id) { + onDragStart({active}) { - return `Picked up draggable item ${id}.`; + return `Picked up draggable item ${active.id}.`; }, - onDragOver(id, overId) { + onDragOver({active, over}) { - if (overId) { + if (over) { - return `Draggable item ${id} was moved over droppable area ${overId}.`; + return `Draggable item ${active.id} was moved over droppable area ${over.id}.`; } - return `Draggable item ${id} is no longer over a droppable area.`; + return `Draggable item ${active.id} is no longer over a droppable area.`; }, };Accessibility-related DOM nodes are no longer portaled by default
The DOM nodes for the screen reader instructions and announcements are no longer portaled into the
document.bodyelement by default.This change is motivated by the fact that screen readers do not always announce ARIA live regions that are rendered on the
document.body. Common examples of this include when rendering a<DndContext>within a<dialog>element or an element that hasrole="dialog", only ARIA live regions rendered within the dialog will be announced.Consumers can now opt to render announcements in the portal container of their choice using the
containerproperty of theaccessibilityprop:<DndContext + accessibility={{ + container: document.body, + }} -
#733
035021aThanks @clauderic! - The<DragOverlay>component's drop animation has been refactored, which fixes a number of bugs with the existing implementation and introduces new functionality.What's new?
Scrolling the draggable node into view if needed
The drop animation now ensures that the the draggable node that we are animating to is in the viewport before performing the drop animation and scrolls it into view if needed.
Changes to the
dropAnimationpropThe
dropAnimationprop of<DragOverlay>now accepts either a configuration object or a custom drop animation function.The configuration object adheres to the following shape:
interface DropAnimationOptions { duration?: number; easing?: string; keyframes?: DropAnimationKeyframeResolver; sideEffects?: DropAnimationSideEffects; }The default drop animation options are:
const defaultDropAnimationConfiguration: DropAnimationOptions = { duration: 250, easing: 'ease', keyframes: defaultDropAnimationKeyframes, sideEffects: defaultDropAnimationSideEffects({ styles: { active: { opacity: '0', }, }, }), };The
keyframesoption allows consumers to override the keyframes of the drop animation. For example, here is how you would add a fade out transition to the drop animation using keyframes:import {CSS} from '@dnd-kit/utilities'; const customDropAnimation = { keyframes({transform}) { return [ {opacity: 1, transform: CSS.Transform.toString(transform.initial)}, {opacity: 0, transform: CSS.Transform.toString(transform.final)}, ]; }, };The
dragSourceOpacityoption has been deprecated in favour of letting consumers define arbitrary side effects that should run before the animation starts. Side effects may return a cleanup function that should run when the drop animation has completed.type CleanupFunction = () => void; export type DropAnimationSideEffects = ( parameters: DropAnimationSideEffectsParameters ) => CleanupFunction | void;Drop animation side effects are a powerful abstraction that provide a lot of flexibility. The
defaultDropAnimationSideEffectsfunction is exported by@dnd-kit/coreand aims to facilitate the types of side-effects we anticipate most consumers will want to use out of the box:interface DefaultDropAnimationSideEffectsOptions { // Apply a className on the active draggable or drag overlay node during the drop animation className?: { active?: string; dragOverlay?: string; }; // Apply temporary styles to the active draggable node or drag overlay during the drop animation styles?: { active?: Styles; dragOverlay?: Styles; }; }For advanced side-effects, consumers may define a custom
sideEffectsfunction that may optionally return a cleanup function that will be executed when the drop animation completes:const customDropAnimation = { sideEffects({active}) { active.node.classList.add('dropAnimationInProgress'); active.node.animate([{opacity: 0}, {opacity: 1}], { easing: 'ease-in', duration: 250, }); return () => { // Clean up when the drop animation is complete active.node.classList.remove('dropAnimationInProgress'); }; }, };For even more advanced use-cases, consumers may also provide a function to the
dropAnimationprop, which adheres to the following shape:interface DropAnimationFunctionArguments { active: { id: UniqueIdentifier; data: DataRef; node: HTMLElement; rect: ClientRect; }; draggableNodes: DraggableNodes; dragOverlay: { node: HTMLElement; rect: ClientRect; }; droppableContainers: DroppableContainers; measuringConfiguration: DeepRequired<MeasuringConfiguration>; transform: Transform; } type DropAnimationFunction = ( args: DropAnimationFunctionArguments ) => Promise<void> | void;Bug fixes
- The
<DragOverlay>now respects themeasuringConfigurationspecified for thedragOverlayanddraggableproperties when measuring the rects to animate to and from. - The
<DragOverlay>component now supports rendering children while performing the drop animation. Previously, the drag overlay would be in a broken state when trying to pick up an item while a drop animation was in progress.
Migration steps
For consumers that were relying on the
dragSourceOpacityproperty in theirdropAnimationconfiguration:+ import {defaultDropAnimationSideEffects} from '@dnd-kit/core'; const dropAnimation = { - dragSourceOpacity: 0.5, + sideEffects: defaultDropAnimationSideEffects({ + styles : { + active: { + opacity: '0.5', + }, + }, + ), }; - The
-
#745
5f3c700Thanks @clauderic! - The keyboard sensor now keeps track of the initial coordinates of the collision rect to provide a translate delta when move events are dispatched.This is a breaking change that may affect consumers that had created custom keyboard coordinate getters.
Previously the keyboard sensor would measure the initial rect of the active node and store its top and left properties as its initial coordinates it would then compare all subsequent move coordinates to calculate the delta.
This approach suffered from the following issues:
- It didn't respect the measuring configuration defined on the
<DndContext>for the draggable node - Some consumers re-render the active node after dragging begins, which would lead to stale measurements
- An error had to be thrown if there was no active node during the activation phase of the keyboard sensor. This shouldn't be a concern of the keyboard sensor.
- The
currentCoordinatespassed to the coordinate getter were often stale and not an accurate representation of the current position of the collision rect, which can be affected by a number of different variables, such as modifiers.
- It didn't respect the measuring configuration defined on the
-
#755
33e6dd2Thanks @clauderic! - TheUniqueIdentifiertype has been updated to now accept eitherstringornumberidentifiers. As a result, theidproperty ofuseDraggable,useDroppableanduseSortableand theitemsprop of<SortableContext>now all accept eitherstringornumberidentifiers.Migration steps
For consumers that are using TypeScript, import the
UniqueIdentifiertype to have strongly typed local state:+ import type {UniqueIdentifier} from '@dnd-kit/core'; function MyComponent() { - const [items, setItems] = useState(['A', 'B', 'C']); + const [items, setItems] = useState<UniqueIdentifier>(['A', 'B', 'C']); }Alternatively, consumers can cast or convert the
idproperty to astringwhen reading theidproperty of interfaces such asActive,Over,DroppableContainerandDraggableNode.The
draggableNodesobject has also been converted to a map. Consumers that were reading from thedraggableNodesproperty that is available on the public context of<DndContext>should follow these migration steps:- draggableNodes[someId]; + draggableNodes.get(someId);
Minor Changes
-
#748
59ca82bThanks @clauderic! - Automatic focus management and activator node refs.Introducing activator node refs
Introducing the concept of activator node refs for
useDraggableanduseSortable. This allows @dnd-kit to handle common use-cases such as restoring focus on the activator node after dragging via the keyboard or only allowing the activator node to instantiate the keyboard sensor.Consumers of
useDraggableanduseSortablemay now optionally set the activator node ref on the element that receives listeners:import {useDraggable} from '@dnd-kit/core'; function Draggable(props) { const { listeners, setNodeRef, + setActivatorNodeRef, } = useDraggable({id: props.id}); return ( <div ref={setNodeRef}> Draggable element <button {...listeners} + ref={setActivatorNodeRef} > :: Drag Handle </button> </div> ) }It's common for the activator element (the element that receives the sensor listeners) to differ from the draggable node. When this happens, @dnd-kit has no reliable way to get a reference to the activator node after dragging ends, as the original
event.targetthat instantiated the sensor may no longer be mounted in the DOM or associated with the draggable node that was previously active.Automatically restoring focus
Focus management is now automatically handled by @dnd-kit. When the activator event is a Keyboard event, @dnd-kit will now attempt to automatically restore focus back to the first focusable node of the activator node or draggable node.
If no activator node is specified via the
setActivatorNodeRefsetter function ofuseDraggbleanduseSortable, @dnd-kit will automatically restore focus on the first focusable node of the draggable node set via thesetNodeRefsetter function ofuseDraggableanduseSortable.If you were previously managing focus manually and would like to opt-out of automatic focus management, use the newly introduced
restoreFocusproperty of theaccessibilityprop of<DndContext>:<DndContext accessibility={{ + restoreFocus: false }} -
#751
a52fba1Thanks @clauderic! - Added thearia-disabledattribute to theattribtuesobject returned byuseDraggableanduseSortable. The value of thearia-disabledattribute is populated based on whether or not thedisabledargument is passed touseDraggbleoruseSortable. -
#741
40707ceThanks @clauderic! - The auto scroller now keeps track of the drag direction to infer scroll intent. By default, auto-scrolling will now be disabled for a given direction if dragging in that direction hasn't occurred yet. This prevents accidental auto-scrolling when picking up a draggable item that is near the scroll boundary threshold. -
#660
a41e5b8Thanks @clauderic! - Fixed a bug with thedeltaproperty returned inonDragMove,onDragOver,onDragEndandonDragCancel. Thedeltaproperty represents thetransformdelta since dragging was initiated, along with the scroll delta. However, due to an oversight, thedeltaproperty was actually returning thetransformdelta and the current scroll offsets rather than the scroll delta.This same change has been made to the
scrollAdjustedTranslateproperty that is exposed to sensors. -
#750
bf30718Thanks @clauderic! - TheuseDndMonitor()hook has been refactored to be synchronously invoked at the same time as the events dispatched by<DndContext>(such asonDragStart,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. -
#660
a41e5b8Thanks @clauderic! - TheactiveNodeRectandcontainerNodeRectare now observed by aResizeObserverin case they resize while dragging. -
#660
a41e5b8Thanks @clauderic! - ImproveduseDraggableusage without<DragOverlay>:- The active draggable now scrolls with the page even if there is no
<DragOverlay>used. - Fixed issues when re-ordering the active draggable node in the DOM while dragging.
- The active draggable now scrolls with the page even if there is no
-
#660
77e3d44Thanks @clauderic! - Fixed an issue withuseDroppablehook needlessly dispatchingSetDroppableDisabledactions even if thedisabledproperty had not changed since registering the droppable. -
#749
188a450Thanks @clauderic! - TheonDragStart,onDragMove,onDragOver,onDragEndandonDragCancelevents of<DndContext>anduseDndMonitornow expose theactivatorEventevent that instantiated the activated sensor. -
#733
035021aThanks @clauderic! - TheKeyboardSensornow scrolls the focused activator draggable node into view if it is not within the viewport. -
#733
035021aThanks @clauderic! - By default, @dnd-kit now attempts to compensate for layout shifts that happen right after theonDragStartevent is dispatched by scrolling the first scrollable ancestor of the active draggable node.The
autoScrollprop of<DndContext>now optionally accepts alayoutShiftCompensationproperty to control this new behavior:interface AutoScrollOptions { acceleration?: number; activator?: AutoScrollActivator; canScroll?: CanScroll; enabled?: boolean; interval?: number; + layoutShiftCompensation?: boolean | {x: boolean, y: boolean}; order?: TraversalOrder; threshold?: { x: number; y: number; }; }To enable/disable layout shift scroll compensation for a single scroll axis, pass in the following autoscroll configuration to
<DndContext>:<DndContext autoScroll={{layoutShiftCompensation: {x: false, y: true}}} >To completely disable layout shift scroll compensation, pass in the following autoscroll configuration to
<DndContext>:<DndContext autoScroll={{layoutShiftCompensation: false}} > -
#672
10f6836Thanks @clauderic! - ThemeasureDroppableContainersmethod now properly respects the MeasuringStrategy defined on<DndContext />and will not measure containers while measuring is disabled. -
#656
c1b3b5aThanks @clauderic! - Fixed an issue with collision detection using stale rects. ThedroppableRectsproperty has been added to theCollisionDetectioninterface.All built-in collision detection algorithms have been updated to get the rect for a given droppable container from
droppableRectsrather than from therect.currentref:- const rect = droppableContainers.get(id).rect.current; + const rect = droppableRects.get(id);The
rect.currentref stored on DroppableContainers can be stale if measuring is scheduled but has not completed yet. Collision detection algorithms should use thedroppableRectsmap instead to get the latest, most up-to-date measurement of a droppable container in order to avoid computing collisions against stale rects.This is not a breaking change. However, if you've forked any of the built-in collision detection algorithms or you've authored custom ones, we highly recommend you update your use-cases to avoid possibly computing collisions against stale rects.
Patch Changes
-
#742
7161f70Thanks @clauderic! - Fallback to initial rect measured for the active draggable node if it unmounts during initialization (afteronDragStartis dispatched). -
#749
5811986Thanks @clauderic! - TheDataandDataReftypes are now exported by@dnd-kit/core. -
#699
e302bd4Thanks @JuAn-Kang! - ExportDragOverlayPropsfor consumers. -
750d726Thanks @clauderic! - Fixed a bug in theKeyboardSensorwhere it would not move the draggable on the horizontal axis if it could fully scroll to the new vertical coordinates, and would not move the draggable on the vertical axis if it could fully scroll to the new horizontal coordinates. -
#660
e6e242cThanks @clauderic! - TheKeyboardSensorwas updated to usescrollToinstead ofscrollBywhen it is able to fully scroll to the new coordinates returned by the coordinate getter function. This resolves issues that can happen withscrollBywhen called in rapid succession. -
Updated dependencies [
59ca82b,035021a]:- @dnd-kit/utilities@3.2.0
5.0.3
Patch Changes
- #650
2439aaeThanks @clauderic! - - Fixed React warning in development when unmounting a component that uses theuseDraggablehook by ensuring that theResizeObserveris disconnected in a cleanup effect.
5.0.2
Patch Changes
- #646
b3b185dThanks @lukesmurray! - ExportDraggableAttributesinterface for consumers to use when interfacing withuseDraggablehook.
5.0.1
Patch Changes
- #573
cee1d88Thanks @clauderic! - Only useResizeObserverinuseDroppableand<DragOverlay>if it is available in the execution environment.
5.0.0
Major Changes
-
#558
f3ad20dThanks @clauderic! - Refactor of theCollisionDetectioninterface to return an array ofCollisions:+export interface Collision { + id: UniqueIdentifier; + data?: Record<string, any>; +} export type CollisionDetection = (args: { active: Active; collisionRect: ClientRect; droppableContainers: DroppableContainer[]; pointerCoordinates: Coordinates | null; -}) => UniqueIdentifier; +}) => Collision[];This is a breaking change that requires all collision detection strategies to be updated to return an array of
Collisionrather than a singleUniqueIdentifierThe
overproperty remains a singleUniqueIdentifier, and is set to the first item in returned in the collisions array.Consumers can also access the
collisionsproperty which can be used to implement use-cases such as combining droppables in user-land.The
onDragMove,onDragOverandonDragEndcallbacks are also updated to receive the collisions array property.Built-in collision detections such as rectIntersection, closestCenter, closestCorners and pointerWithin adhere to the CollisionDescriptor interface, which extends the Collision interface:
export interface CollisionDescriptor extends Collision { data: { droppableContainer: DroppableContainer; value: number; [key: string]: any; }; }Consumers can also access the array of collisions in components wrapped by
<DndContext>via theuseDndContext()hook:import {useDndContext} from '@dnd-kit/core'; function MyComponent() { const {collisions} = useDndContext(); } -
#561
02edd26Thanks @clauderic! - Droppable containers now observe the node they are attached to viasetNodeRefusingResizeObserverwhile dragging.This behaviour can be configured using the newly introduced
resizeObserverConfigproperty.interface ResizeObserverConfig { /** Whether the ResizeObserver should be disabled entirely */ disabled?: boolean; /** Resize events may affect the layout and position of other droppable containers. * Specify an array of `UniqueIdentifier` of droppable containers that should also be re-measured * when this droppable container resizes. Specifying an empty array re-measures all droppable containers. */ updateMeasurementsFor?: UniqueIdentifier[]; /** Represents the debounce timeout between when resize events are observed and when elements are re-measured */ timeout?: number; }By default, only the current droppable is scheduled to be re-measured when a resize event is observed. However, this may not be suitable for all use-cases. When an element resizes, it can affect the layout and position of other elements, such that it may be necessary to re-measure other droppable nodes in response to that single resize event. The
recomputeIdsproperty can be used to specify which droppableids should be re-measured in response to resize events being observed.For example, the
useSortablepreset re-computes the measurements of all sortable elements after the element that resizes, so long as they are within the sameSortableContextas the element that resizes, since it's highly likely that their layout will also shift.Specifying an empty array for
recomputeIdsforces all droppable containers to be re-measured.For consumers that were relyings on the internals of
DndContextusinguseDndContext(), thewillRecomputeLayoutsproperty has been renamed tomeasuringScheduled, and therecomputeLayoutsmethod has been renamed tomeasureDroppableContainers, and now optionally accepts an array of droppableUniqueIdentifierthat should be scheduled to be re-measured. -
#518
6310227Thanks @clauderic! - Major internal refactor of measuring and collision detection.Summary of changes
Previously, all collision detection algorithms were relative to the top and left points of the document. While this approach worked in most situations, it broke down in a number of different use-cases, such as fixed position droppable containers and trying to drag between containers that had different scroll positions.
This new approach changes the frame of comparison to be relative to the viewport. This is a major breaking change, and will need to be released under a new major version bump.
Breaking changes:
- By default,
@dnd-kitnow ignores only the transforms applied to the draggable / droppable node itself, but considers all the transforms applied to its ancestors. This should provide the right balance of flexibility for most consumers.- Transforms applied to the droppable and draggable nodes are ignored by default, because the recommended approach for moving items on the screen is to use the transform property, which can interfere with the calculation of collisions.
- Consumers can choose an alternate approach that does consider transforms for specific use-cases if needed by configuring the measuring prop of . Refer to the example.
- Reduced the number of concepts related to measuring from
ViewRect,LayoutRectto just a single concept ofClientRect.- The
ClientRectinterface no longer holds theoffsetTopandoffsetLeftproperties. For most use-cases, you can replaceoffsetTopwithtopandoffsetLeftwithleft. - Replaced the following exports from the
@dnd-kit/corepackage withgetClientRect:getBoundingClientRectgetViewRectgetLayoutRectgetViewportLayoutRect
- The
- Removed
translatedRectfrom theSensorContextinterface. Replace usage withcollisionRect. - Removed
activeNodeClientRecton theDndContextinterface. Replace withactiveNodeRect.
- By default,
-
#569
e7ac3d4Thanks @clauderic! - Separated context into public and internal context providers. Certain properties that used to be available on the publicDndContextDescriptorinterface have been moved to the internal context provider and are no longer exposed to consumers:interface DndContextDescriptor { - dispatch: React.Dispatch<Actions>; - activators: SyntheticListeners; - ariaDescribedById: { - draggable: UniqueIdentifier; - }; }Having two distinct context providers will allow to keep certain internals such as
dispatchhidden from consumers.It also serves as an optimization until context selectors are implemented in React, properties that change often, such as the droppable containers and droppable rects, the transform value and array of collisions should be stored on a different context provider to limit un-necessary re-renders in
useDraggable,useDroppableanduseSortable.The
<InternalContext.Provider>is also reset to its default values within<DragOverlay>. This paves the way towards being able to seamlessly use components that use hooks such asuseDraggableanduseDroppableas children of<DragOverlay>without causing interference or namespace collisions.Consumers can still make calls to
useDndContext()to get theactiveoroverproperties if they wish to re-render the component rendered withinDragOverlayin response to user interaction, since those use thePublicContext
Minor Changes
- #556
c6c67cbThanks @avgrad! - - Added pointer coordinates to collision detection- Added
pointerWithincollision algorithm
- Added
Patch Changes
4.0.3
Patch Changes
-
#509
1c6369eThanks @clauderic! - Helpers have been updated to support rendering in foreignwindowcontexts (viaReactDOM.renderorReactDOM.createPortal).For example, checking if an element is an instance of an
HTMLElementis normally done like so:if (element instanceof HTMLElement)However, when rendering in a different window, this can return false even if the element is indeed an HTMLElement, because this code is equivalent to:
if (element instanceof window.HTMLElement)And in this case, the
windowof theelementis different from the main execution contextwindow, because we are rendering via a portal into another window.This can be solved by finding the local window of the element:
const elementWindow = element.ownerDocument.defaultView; if (element instanceof elementWindow.HTMLElement) -
Updated dependencies [
1c6369e]:- @dnd-kit/utilities@3.0.1
4.0.2
Patch Changes
- #504
d973cc6Thanks @clauderic! - Sensors that extend theAbstractPointerSensornow prevent HTML Drag and Drop API events from being triggered while the sensor is activated.
4.0.1
Patch Changes
- #479
5ec3310Thanks @mdrobny! - fix: bindhandleCancelhandler in AbstractPointerSensor to current execution context (this).
4.0.0
Major Changes
-
#337
05d6a78Thanks @clauderic! - React updates in non-synthetic event handlers are now batched to reduce re-renders and prepare for React 18.Also fixed issues with collision detection:
- Defer measurement of droppable node rects until second render after dragging.
- Use DragOverlay's width and height in collision rect (if it is used)
-
#427
f96cb5dThanks @clauderic! - - Using transform-agnostic measurements for the DragOverlay node.- Renamed the
overlayNodeproperty todragOverlayon theDndContextDescriptorinterface.
- Renamed the
-
#372
dbc9601Thanks @clauderic! - RefactoredDroppableContainerstype fromRecord<UniqueIdentifier, DroppableContainerto a custom instance that extends theMapconstructor and adds a few other methods such astoArray(),getEnabled()andgetNodeFor(id).A unique
keyproperty was also added to theDraggableNodeandDroppableContainerinterfaces. This prevents potential race conditions in the mount and cleanup effects ofuseDraggableanduseDroppable. It's possible for the clean-up effect to run after another React component usinguseDraggableoruseDroppablemounts, which causes the newly mounted element to accidentally be un-registered. -
#379
8d70540Thanks @clauderic! - ThelayoutMeasuringprop ofDndContexthas been renamed tomeasuring.The options that could previously be passed to the
layoutMeasuringprop now need to be passed as:<DndContext - layoutMeasuring={options} + measuring={{ + droppable: options + }}The
LayoutMeasuringtype has been renamed toMeasuringConfiguration. TheLayoutMeasuringStrategyandLayoutMeasuringFrequencyenums have also been renamed toMeasuringStrategyandMeasuringFrequency.This refactor allows consumers to configure how to measure both droppable and draggable nodes. By default,
@dnd-kitignores transforms when measuring draggable nodes. This beahviour can now be configured:import { DndContext, getBoundingClientRect, MeasuringConfiguration, } from '@dnd-kit/core'; const measuringConfig: MeasuringConfiguration = { draggable: { measure: getBoundingClientRect, }, }; function App() { return <DndContext measuring={measuringConfig} />; } -
#350
a13dbb6Thanks @wmain! - Breaking change: TheCollisionDetectioninterface has been refactored. It now receives an object that contains theactivedraggable node, along with thecollisionRectand an array ofdroppableContainers.If you've built custom collision detection algorithms, you'll need to update them. Refer to this PR for examples of how to refactor collision detection functions to the new
CollisionDetectioninterface.The
sortableKeyboardCoordinatesmethod has also been updated since it relies on theclosestCornerscollision detection algorithm. If you were using collision detection strategies in a customsortableKeyboardCoordinatesmethod, you'll need to update those as well.
Minor Changes
-
#334
13be602Thanks @trentmwillis! - Now passingactivatorEventas an argument tomodifiers -
#376
aede2ccThanks @clauderic! - Mouse, Pointer, Touch sensors now cancel dragging on visibility change and window resize. The Keyboard sensor already cancelled dragging on window resize. It now also cancels dragging on visibility change. -
#399
a32a4c5Thanks @supersebh! - Added support fortolerancein DistanceConstrain. As soon as thetoleranceis exceeded, the drag operation will be aborted, unless it has already started (because distance criteria was met).Example usage:
// Require the pointer be moved by 10 pixels vertically to initiate drag operation // Abort if the pointer is moved by more than 5 pixels horizontally. { distance: {y: 10}, tolerance: {x: 5}, }Be careful not to pick conflicting settings for distance and tolerance if used together. For example, picking a tolerance that is lower than the distance in the same axis would result in the activation constraint never being met.
-
#408
dea715cThanks @wmain! - The collision rect is now completely based on the position of theDragOverlaywhen it is used. Previously, only thewidthandheightproperties of theDragOverlaywere used for the collision rect, while thetop,left,bottomandrightproperties were derived from the active node rect. This new approach is more aligned with developers would expect, but could cause issues for consumers that were relying on the previous (incorrect) behavior. -
#433
c447880Thanks @clauderic! - Fix unwanted animations when items in sortable context change -
#415
2ba6dfeThanks @cantrellnm! - PreventgetScrollableAncestorsfrom continuing to search if a fixed position node is found. -
#377
422d083Thanks @clauderic! - Pointer, Mouse and Touch sensors now stop propagation of click events once activation constraints are met. -
#375
c4b21b4Thanks @clauderic! - Prevent context menu from opening when pointer sensor is active -
5a41340Thanks @clauderic! - Pointer, Mouse and Touch sensors now prevent selection changes and clear any existing selection ranges once activation constraints are met. -
e2ee0dcThanks @clauderic! - Reset theoverinternal state of<DndContext />on drop. -
1fe9b5cThanks @clauderic! - Sensors may now specify a staticsetupmethod that will be invoked when<DndContext>mounts. The setup method may optionally also return a teardown function that will be invoked when the<DndContext>associated with that sensor unmounts.
Patch Changes
-
#430
46ec5e4Thanks @clauderic! - Fix duplicate scroll ancestor detection. In some scenarios, an element could be added twice to the list of detected scrollable ancestors, resulting in invalid offsets. -
#371
7006464Thanks @clauderic! - fix: do not wrap consumer-defined handlers in batchedUpdates -
1fe9b5cThanks @clauderic! - The TouchSensor attempts to prevent the default browser behavior of scrolling the page by callingevent.preventDefault()in thetouchmoveevent listener. This wasn't working in iOS Safari due to a bug with dynamically attachedtouchmoveevent listeners. Adding a non-passive, non-capturetouchmoveevent listener before dynamically attaching othertouchmoveevent listeners solves the issue. -
Updated dependencies [
0e628bc,13be602,1f5ca27]:- @dnd-kit/utilities@3.0.0
3.1.1
Patch Changes
-
dbe0087#335 Thanks @clauderic! - FixgetEventListenerTargetwhen target element is not an instance ofHTMLElement -
5d4d292#331 Thanks @phungleson! - Fix typo inSensorContexttype (scrollAdjustedTransalte-->scrollAdjustedTranslate)
3.1.0
Minor Changes
d39ab11#316 Thanks @lsit! - Added ability to optionally return screen reader announcementsonDragMove.
3.0.4
Patch Changes
-
ae398deThanks @clauderic! - Allow setting an optionalidprop onDndContextto fix a warning during server-side rendering (especially in Next.js). By default, thisidis autogenerated and can lead to a mismatch between the server- and client-side rendered HTML. We also avoid this mismatch by rendering theAccessibilitycomponent only after everything else was initially mounted on the client. -
8b938ceThanks @clauderic! - Hide the node in the overlay after the drop animation is finished. This prevents some flickering with React concurrent mode.
3.0.3
Patch Changes
0ff788e#246 Thanks @inokawa! -DragOverlaycomponent now passes downstyleprop to the wrapper element it renders.
3.0.2
Patch Changes
-
54c8778#225 Thanks @clauderic! - Updated theactiverects type toViewRect(was previously incorrectly typed asLayoutRect) -
2ee96a5#243 Thanks @py-wai! - Update regex used in isScrollable, to consider element with overflow: overlay as a scrollable element.
3.0.1
Patch Changes
f9ec28f#217 Thanks @clauderic! - Fixes a regression introduced with@dnd-kit/core@3.0.0that was causeing sensors to stop working after a drag operation where activation constraints were not met.
3.0.0
Major Changes
-
a9d92cf#174 Thanks @clauderic! - Distributed assets now only target modern browsers. Browserlist config:defaults last 2 version not IE 11 not deadIf you need to support older browsers, include the appropriate polyfills in your project's build process.
-
b406cb9#187 Thanks @clauderic! - Introduced theuseDndMonitorhook. TheuseDndMonitorhook can be used within components wrapped in aDndContextprovider to monitor the different drag and drop events that happen for thatDndContext.Example usage:
import {DndContext, useDndMonitor} from '@dnd-kit/core'; function App() { return ( <DndContext> <Component /> </DndContext> ); } function Component() { useDndMonitor({ onDragStart(event) {}, onDragMove(event) {}, onDragOver(event) {}, onDragEnd(event) {}, onDragCancel(event) {}, }); }
Minor Changes
-
b7355d1#207 Thanks @clauderic! - Thedataargument foruseDraggableanduseDroppableis now exposed in event handlers and on theactiveandoverobjects.Example usage:
import {DndContext, useDraggable, useDroppable} from '@dnd-kit/core'; function Draggable() { const {attributes, listeners, setNodeRef, transform} = useDraggable({ id: 'draggable', data: { type: 'type1', }, }); /* ... */ } function Droppable() { const {setNodeRef} = useDroppable({ id: 'droppable', data: { accepts: ['type1', 'type2'], }, }); /* ... */ } function App() { return ( <DndContext onDragEnd={({active, over}) => { if (over?.data.current.accepts.includes(active.data.current.type)) { // do stuff } }} /> ); }
Patch Changes
2.1.2
Patch Changes
-
2833337#186 Thanks @clauderic! - SimplifyuseAnnouncementhook to only return a singleannouncementrather thanentries. Similarly, theLiveRegioncomponent now only accepts a singleannouncementrather than `entries.- The current strategy used in the useAnnouncement hook is needlessly complex. It's not actually necessary to render multiple announcements at once within the LiveRegion component. It's sufficient to render a single announcement at a time. It's also un-necessary to clean up the announcements after they have been announced, especially now that the role="status" attribute has been added to LiveRegion, keeping the last announcement rendered means users can refer to the last status.
-
Updated dependencies [
c24bdb3,2833337]:- @dnd-kit/accessibility@2.0.0
2.1.1
Patch Changes
2.1.0
Minor Changes
-
bdb1aa2#171 Thanks @mitchheddles! - Added more flexibility for thedistanceandtoleranceactivation constraints. Consumers can still provide anumberto calculate the distance or tolerance constraints, but can now also pass in an object that adheres to theDistanceMeasurementinterface instead. This change allows consumers to specify which axis the activation distance or tolerance should be measured against, eitherx,yor both.type DistanceMeasurement = | number | {x: number} | {y: number} | {x: number, y: number} interface DistanceConstraint { distance: DistanceMeasurement; } interface DelayConstraint { delay: number; tolerance: DistanceMeasurement; }Example usage:
For example, when building a list that can only be sorted vertically when using the
restrictToVerticalAxismodifier, a consumer can now configure sensors to only measure distance against theyaxis when using theMouseSensor, and afford more tolerance on theyaxis than thexaxis for theTouchSensor:const sensors = useSensors( useSensor(MouseSensor, { activationConstraint: { distance: { y: 10 }, }, }), useSensor(TouchSensor, { activationConstraint: { delay: 250, tolerance: { y: 15, x: 5 }, }, }), );This also fixes a bug with the way the distance is calculated when passing a number to the
distanceortoleranceoptions. Previously, the sum of the distance on both thexandyaxis was being calculated rather than the hypothenuse.
2.0.2
Patch Changes
-
f038174#166 Thanks @shayc! - AutoScrollActivator enum was being exported as a type rather than a value. -
7422e25#165 Thanks @clauderic! - Fix regression with autoscroll disabling by sensors
2.0.1
Patch Changes
-
a847a80#160 Thanks @clauderic! - Allow consumers to define drag source opacity in drop animation by setting thedragSourceOpacityoption to a number on thedropAnimationprop ofDragOverlay. -
ea9d878#164 Thanks @clauderic! - Export AutoScrollActivator enum for consumers.
2.0.0
Major Changes
-
8583825#140 Thanks @clauderic! - Auto-scrolling defaults have been updated, which should generally lead to improved user experience for most consumers.The auto-scroller now bases its calculations based on the position of the pointer rather than the edges of the draggable element's rect by default. This change is aligned with how the native HTML 5 Drag & Drop auto-scrolling behaves.
This behaviour can be customized using the
activatoroption of theautoScrollprop:import {AutoScrollActivator, DndContext} from '@dnd-kit/core'; <DndContext autoScroll={{activator: AutoScrollActivator.DraggableRect}} />;The auto-scroller now also looks at scrollable ancestors in order of appearance in the DOM tree, meaning it will first attempt to scroll the window, and narrow its focus down rather than the old behaviour of looking at scrollable ancestors in order of closeness to the draggable element in the DOM tree (reversed tree order).
This generally leads to an improved user experience, but can be customized by passing a configuration object to the
autoScrollprop that sets theorderoption toTraversalOrder.ReversedTreeOrderinstead of the new default value ofTraversalOrder.TreeOrder:import {DndContext, TraversalOrder} from '@dnd-kit/core'; <DndContext autoScroll={{order: TraversalOrder.ReversedTreeOrder}} />;The autoscrolling
thresholds,accelerationandintervalcan now also be customized using theautoScrollprop:import {DndContext} from '@dnd-kit/core'; <DndContext autoScroll={{ thresholds: { // Left and right 10% of the scroll container activate scrolling x: 0.1, // Top and bottom 25% of the scroll container activate scrolling y: 0.25, }, // Accelerate slower than the default value (10) acceleration: 5, // Auto-scroll every 10ms instead of the default value of 5ms interval: 10, }} />;Finally, consumers can now conditionally opt out of scrolling certain scrollable ancestors using the
canScrolloption of theautoScrollprop:import {DndContext} from '@dnd-kit/core'; <DndContext autoScroll={{ canScroll(element) { if (element === document.scrollingElement) { return false; } return true; }, }} />;
1.2.0
Minor Changes
-
79f6088#144 Thanks @clauderic! - Allow consumers to determine whether to animate layout changes and when to measure nodes. Consumers can now use theanimateLayoutChangesprop ofuseSortableto determine whether layout animations should occur. Consumers can now also decide when to measure layouts, and at what frequency using thelayoutMeasuringprop ofDndContext. By default,DndContextwill measure layouts just-in-time after sorting has begun. Consumers can override this behaviour to either only measure before dragging begins (on mount and after dragging), or always (on mount, before dragging, after dragging). Pairing thelayoutMeasuringprop onDndContextand theanimateLayoutChangesprop ofuseSortableopens up a number of new possibilities for consumers, such as animating insertion and removal of items in a sortable list. -
a76cd5a#136 Thanks @clauderic! - AddedonActivationoption to sensors. Delegated the responsibility of callingevent.preventDefault()on activation to consumers, as consumers have the most context to decide whether it is appropriate or not to prevent the default browser behaviour on activation. Consumers of the sensors can prevent the default behaviour on activation using theonActivationoption. Here is an example using the Pointer sensor:useSensor(PointerSensor, {onActivation: (event) => event.preventDefault()})
Patch Changes
adb7bd5#151 Thanks @clauderic! -DragOverlaynow attempts to perform drop animation even iftransform.xandtransform.yare both zero.
1.1.0
Minor Changes
-
ac674e8#135 Thanks @ranbena! - AddeddragCancelprop to DndContext. ThedragCancelprop can be used to cancel a drag operation on drop. The prop accepts a function that returns a boolean, or a promise returning a boolean once resolved. Returnfalseto cancel the drop. -
208f68e#111 Thanks @ranbena! - Keyboard sensor now cancels dragging on window resize
1.0.2
Patch Changes
-
423610c#56 Thanks @clauderic! - Add MIT license to package.json and distributed files -
594a24e#106 Thanks @ranbena! - Replaceanimation.finishedwithanimation.onfinishfor DragOverlay drop animation as the latter has much better support across browsers. -
fd25eaf#68 Thanks @Pustelto! - Wrap attributes returned from useDraggable hook in useMemo to allow pure component optimization -
Updated dependencies [
423610c]:- @dnd-kit/accessibility@1.0.2
- @dnd-kit/utilities@1.0.2
1.0.1
Patch Changes
-
5194696#51 Thanks @clauderic! - Fix issue with reducer initial state variable causing collisions due to variable references all pointing to the original initial state variable. -
310bbd6#37 Thanks @nickpresta! - Fix typo in package.json repository URL -
Updated dependencies [
0b343c7,310bbd6]:- @dnd-kit/utilities@1.0.1
- @dnd-kit/accessibility@1.0.1
1.0.0
Major Changes
2912350Thanks @clauderic! - Initial public release.
Patch Changes
- Updated dependencies [
2912350]:- @dnd-kit/accessibility@1.0.0
- @dnd-kit/utilities@1.0.0
0.1.1
Patch Changes
-
2bb3065Thanks @clauderic! - No longer settingpointer-eventstononefor DragOverlay component as it interferes with custom cursors. -
bf04b2bThanks @clauderic! - Allow nullish values to be passed touseSensorsfor conditional sensors.
0.1.0
Minor Changes
7bd4568#30 - Initial beta release, authored by @clauderic.
Patch Changes
- Updated dependencies [
7bd4568]:- @dnd-kit/accessibility@0.1.0
- @dnd-kit/utilities@0.1.0