Skip to content

fix(dom): keep dragged elements from being offset by their margins - #2177

Open
jhsgumban wants to merge 1 commit into
clauderic:mainfrom
jhsgumban:fix/feedback-ignore-item-margins
Open

jhsgumban wants to merge 1 commit into
clauderic:mainfrom
jhsgumban:fix/feedback-ignore-item-margins

Conversation

@jhsgumban

Copy link
Copy Markdown

Fixes #2092

Problem

While dragging, the Feedback plugin positions the element with position: fixed and explicit top / left taken from the element's border box. Margins live outside the border box, so any margin on the element was applied a second time and shifted it away from the pointer. With style={{marginLeft: 20}} from the issue's sandbox, the item jumps 20px to the right on drag start.

The only margin reset was in the dnd-kit layer:

@layer dnd-kit {
  :where([data-dnd-dragging][popover]) { margin: unset; ... }
}

That rule is intentionally low-priority, so that framework styles such as Tailwind v4 win (#1896, 7260746). That's right for visual styles like background, border and padding, but it means author margins, whether inline or from any stylesheet, always override it.

Change

  • Add margin: 0 !important to the main [data-dnd-dragging] rule, alongside the existing top / left / right / bottom !important positioning overrides. A margin doesn't style the element. It changes where it sits, and the position is already fully controlled by the plugin.
  • Remove margin: unset from the layered popover reset, since it's now redundant. The remaining layered resets (background, border, padding, color, overflow) are unchanged, so layered framework styles still win as [experimental] option for disabling data-dnd-dragging styles injection #1896 intended.

The placeholder keeps its margins, so the surrounding layout doesn't shift. When the drag ends, the element gets its own margins back.

Tests

  • New hidden story React/Sortable/Margins with sortable items offset by margins: odd items inline (marginLeft: 40, marginTop: 10), even items from a stylesheet (margin-left: 60px).
  • New Playwright spec apps/stories/tests/sortable-margins.spec.ts. It grabs an item 25% from its top-left, drags it, and asserts the pointer is still 25% into the dragged element, within 3px, for both the inline and the stylesheet margin.
    • On main: both fail. The element is offset by exactly the margin (Received: 40 and Received: 60).
    • With this PR: both pass.
  • Full Playwright suite (chromium): 75 passed.
  • packages/dom unit tests: 88 pass. bun run build passes.
  • Changeset: @dnd-kit/dom patch.

The Feedback plugin positions the dragged element with `position: fixed`
and explicit `top`/`left` taken from its border box. Any margin on the
element was then applied on top, shifting it away from the pointer by
the margin width.

The only margin reset lived in the low-priority `dnd-kit` layer and
targeted `[popover]` only, so author margins (inline or from a
stylesheet) always won. Force `margin: 0 !important` alongside the
existing `top`/`left`/`right`/`bottom` overrides instead, and drop the
now redundant layered reset. Visual styles stay overridable as before.

Fixes clauderic#2092
@changeset-bot

changeset-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e934aea

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@dnd-kit/dom Patch
@dnd-kit/abstract Patch
@dnd-kit/collision Patch
@dnd-kit/geometry Patch
@dnd-kit/helpers Patch
@dnd-kit/react Patch
@dnd-kit/solid Patch
@dnd-kit/state Patch
@dnd-kit/svelte Patch
@dnd-kit/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Margin left on sortable items behaves strangely

1 participant