ui.startDrag

API reference for the ui.startDrag method.

Handles a drag event. At the end of the drag event, the specified type of element — for example, an image — will be added at the drop coordinates.

import React from "react";
import { Text, TypographyCard } from "@canva/app-ui-kit";
import { addNativeElement, ui } from "@canva/design";
export function App() {
return (
<TypographyCard
ariaLabel="Add text to design"
onClick={() => {
addNativeElement({
type: "TEXT",
children: ["Add text into a design"],
});
}}
onDragStart={(event: React.DragEvent<HTMLElement>) => {
ui.startDrag(event, {
type: "TEXT",
children: ["Add text into a design"],
});
}}
>
<Text>Add text into a design</Text>
</TypographyCard>
);
}
tsx

The expected parameters depend on what will be added to the user's design at the end of a drag event:

#eventobject
Required

The event that's fired when the user starts dragging an element.

#dragDataobject
Required

The options for configuring what's added to the user's design at the end of a drag event.

#dragData.typestring
Required

The type of element to add to the design. For embeds, this must be "EMBED".

#dragData.embedUrlstring
Required

The URL of the embeddable media.

#dragData.previewUrlstring
Required

The URL or data URL of the image to use for the drag preview. This image will appear under the user's cursor during the drag event.

#dragData.previewSizeobject
Required

The dimensions of the drag preview, in pixels.

#dragData.previewSize.widthnumber
Required

The width of the drag preview, in pixels.

#dragData.previewSize.heightnumber
Required

The height of the drag preview, in pixels.

Promise<void>