Skip to content

preserveOffsetOnSource always returns { x: 0, y: 0 } #177

Open
@richburdon

Description

1). Using the pattern below getOffset is called before the preview portal is created, so the container is empty. I've worked around this by passing the element itself as the container.

2). Even so, on drop, the current position is randomly 1 pixel off the native rendered position, so the object seems to jump if it is absolutely positioned. (This doesn't happen if I disable the native drag preview via disableNativeDragPreview).

      onGenerateDragPreview: ({ nativeSetDragImage, location, source }) => {
        setCustomNativeDragPreview({
          render: ({ container }) => {
            setPreview(container);
            return () => setPreview(undefined);
          },
          getOffset: ({ container }) => {
            // container is empty, so pass in source element.
            const f = preserveOffsetOnSource({ element: source.element, input: location.current.input });
            offset.current = f({ container: source.element });
            return offset.current;
          },
          nativeSetDragImage,
        });

  ...

  return (
    <>
      {preview ? createPortal(<DragElement />, preview) : null}
      <DragElement ref={draggingRef} classNames={[dragging && 'opacity-50']} />
    </>
  );

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions