Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@dnd-kit/modifiers

clauderic5.7mMIT9.0.0TypeScript support: included

Translate modifier presets for use with @dnd-kit packages.

readme

@dnd-kit/modifiers

Stable release

Modifiers let you dynamically modify the movement coordinates that are detected by sensors. They can be used for a wide range of use cases, for example:

  • Restricting motion to a single axis
  • Restricting motion to the draggable node container's bounding rectangle
  • Restricting motion to the draggable node's scroll container bounding rectangle
  • Applying resistance or clamping the motion

Installation

To start using modifiers, install the modifiers package via yarn or npm:

npm install @dnd-kit/modifiers

Usage

The modifiers repository contains a number of useful modifiers that can be applied on DndContext as well as DragOverlay.

import {DndContext, DragOverlay} from '@dnd-kit';
import {
  restrictToVerticalAxis,
  restrictToWindowEdges,
} from '@dnd-kit/modifiers';

function App() {
  return (
    <DndContext modifiers={[restrictToVerticalAxis]}>
      {/* ... */}
      <DragOverlay modifiers={[restrictToWindowEdges]}>{/* ... */}</DragOverlay>
    </DndContext>
  );
}

As you can see from the example above, DndContext and DragOverlay can both have different modifiers.

Built-in modifiers

Restricting motion to an axis

restrictToHorizontalAxis

Restrict movement to only the horizontal axis.

restrictToVerticalAxis

Restrict movement to only the vertical axis.

Restrict motion to a container's bounding rectangle

restrictToWindowEdges

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.

restrictToParentElement

Restrict movement to the parent element of the draggable item that is picked up.

restrictToFirstScrollableAncestor

Restrict movement to the first scrollable ancestor of the draggable item that is picked up.

Snap to grid

createSnapModifier

Function to create modifiers to snap to a given grid size.

import {createSnapModifier} from '@dnd-kit/modifiers';

const gridSize = 20; // pixels
const snapToGridModifier = createSnapModifier(gridSize);

Snap to cursor

snapCenterToCursor

Snaps the center of the draggable item to the cursor when it is picked up. Has no effect when using the Keyboard sensor.

Building custom modifiers

To build your own custom modifiers, refer to the implementation of the built-in modifiers of this package.

For example, here is an implementation to create a modifier to snap to grid:

const gridSize = 20;

function snapToGrid(args) {
  const {transform} = args;

  return {
    ...transform,
    x: Math.ceil(transform.x / gridSize) * gridSize,
    y: Math.ceil(transform.y / gridSize) * gridSize,
  };
}

changelog

@dnd-kit/modifiers

9.0.0

Patch Changes

8.0.0

Patch Changes

7.0.0

Patch Changes

6.0.1

Patch Changes

6.0.0

Patch Changes

5.0.0

Minor Changes

  • #567 cd3adf3 Thanks @clauderic! - Update modifiers to use draggingNodeRect instead of activeNodeRect. Modifiers should be based on the rect of the node being dragged, whether it is the draggable node or drag overlay node.

  • #518 6310227 Thanks @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-kit now 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 <DndContext>. Refer to the <Switch> example.
    • Reduced the number of concepts related to measuring from ViewRect, LayoutRect to just a single concept of ClientRect.
      • The ClientRect interface no longer holds the offsetTop and offsetLeft properties. For most use-cases, you can replace offsetTop with top and offsetLeft with left.
      • Replaced the following exports from the @dnd-kit/core package with getClientRect:
        • getBoundingClientRect
        • getViewRect
        • getLayoutRect
        • getViewportLayoutRect
    • Removed translatedRect from the SensorContext interface. Replace usage with collisionRect.
    • Removed activeNodeClientRect on the DndContext interface. Replace with activeNodeRect.

Patch Changes

4.0.0

Minor Changes

Patch Changes

3.0.0

Patch Changes

2.1.0

Minor Changes

  • 68960c4 #295 Thanks @akhmadullin! - @dnd-kit/core is now a peerDependency rather than a dependency for other @dnd-kit packages that depend on it, such as @dnd-kit/sortable and @dnd-kit/modifiers. This is done to avoid issues with multiple versions of @dnd-kit/core being installed by some package managers such as Yarn 2.

Patch Changes

2.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 dead

    If you need to support older browsers, include the appropriate polyfills in your project's build process.

Patch Changes

1.0.5

Patch Changes

1.0.4

Patch Changes

1.0.3

Patch Changes

1.0.2

Patch Changes

1.0.1

Patch Changes

1.0.0

Major Changes

Patch Changes

0.1.0

Minor Changes

Patch Changes