Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: port SnapCentertoCursor to experimental branch #1599

Open
wants to merge 4 commits into
base: experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {Meta, StoryObj} from '@storybook/react';
import type { Meta, StoryObj } from '@storybook/react';
import {
RestrictToHorizontalAxis,
RestrictToVerticalAxis,
} from '@dnd-kit/abstract/modifiers';
import {RestrictToElement, RestrictToWindow} from '@dnd-kit/dom/modifiers';
import { RestrictToElement, RestrictToWindow, SnapCenterToCursor } from '@dnd-kit/dom/modifiers';

import docs from './docs/ModifierDocs.mdx';
import {DraggableExample} from '../DraggableExample';
import {SnapToGridExample} from './SnapToGridExample';
import { DraggableExample } from '../DraggableExample';
import { SnapToGridExample } from './SnapToGridExample';
import styles from './styles.module.css';

const meta: Meta<typeof DraggableExample> = {
Expand Down Expand Up @@ -48,7 +48,7 @@ export const WindowModifier: Story = {
export const ContainerModifier: Story = {
name: 'Restrict to container',
args: {
container({children}) {
container({ children }) {
return (
<div className={styles.Container} data-container>
{children}
Expand All @@ -69,3 +69,10 @@ export const SnapModifierExample: Story = {
name: 'Snap to grid',
render: SnapToGridExample,
};

export const SnapToCursor: Story = {
name: 'Snap to cursor',
args: {
modifiers: [SnapCenterToCursor],
},
};
4 changes: 2 additions & 2 deletions packages/abstract/src/modifiers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export {
RestrictToVerticalAxis,
} from './axis.ts';

export {restrictShapeToBoundingRectangle} from './boundingRectangle.ts';
export { restrictShapeToBoundingRectangle } from './boundingRectangle.ts';

export {SnapModifier} from './snap.ts';
export { SnapModifier, SnapCenterToCursor } from './snap.ts';
29 changes: 26 additions & 3 deletions packages/abstract/src/modifiers/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
} from '@dnd-kit/abstract';

interface Options {
size: number | {x: number; y: number};
size: number | { x: number; y: number };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why your editor isn't respecting the prettier settings, it should be:

{
  "bracketSpacing": false,
  "singleQuote": true,
  "semi": true,
  "trailingComma": "es5"
}

Copy link
Author

@khuezy khuezy Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's really strange. I see the prettier loading .prettierrc but it's not applying those settings.
When I run bun format, it picks up 28 file changes... Should I include that or just leave everything as it is for you to format.

}

export class SnapModifier extends Modifier<
DragDropManager<Draggable, Droppable>,
Options
> {
apply({transform}: DragOperation) {
const {size = 20} = this.options ?? {};
apply({ transform }: DragOperation) {
const { size = 20 } = this.options ?? {};
const x = typeof size === 'number' ? size : size.x;
const y = typeof size === 'number' ? size : size.y;

Expand All @@ -29,3 +29,26 @@ export class SnapModifier extends Modifier<

static configure = configurator(SnapModifier);
}

export class SnapCenterToCursor extends Modifier<
DragDropManager<Draggable, Droppable>
> {
apply({ shape, position, transform }: DragOperation) {
if (!position.initial || !shape) {
return transform;
}

const { initial, current } = shape;
const { left, top } = initial.boundingRectangle;
const { height, width } = current.boundingRectangle;

const offsetX = position.initial.x - left;
const offsetY = position.initial.y - top;

return {
...transform,
x: transform.x + offsetX - width / 2,
y: transform.y + offsetY - height / 2,
};
}
}
4 changes: 2 additions & 2 deletions packages/dom/src/modifiers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {RestrictToWindow} from './RestrictToWindow.ts';
export {RestrictToElement} from './RestrictToElement.ts';
export { RestrictToWindow } from './RestrictToWindow.ts';
export { RestrictToElement } from './RestrictToElement.ts';