import { Extensible, ID } from "../../types/index.mjs"; import { RegistryContext, RegistryTicket, RegistryTicketInput } from "../createRegistry/index.mjs"; import { DragDropAdapter, DragDropAdapterContext, DragDropAdapterEmit } from "./adapters/adapter.mjs"; import { PointerAdapter, PointerAdapterOptions } from "./adapters/pointer.mjs"; import { KeyboardAdapter, KeyboardAdapterOptions } from "./adapters/keyboard.mjs"; import "./adapters/index.mjs"; import { MaybeRefOrGetter, Ref, ShallowRef } from "vue"; //#region src/composables/useDragDrop/index.d.ts /** * Layout axis a drop zone resolves indices along. */ type Orientation = 'vertical' | 'horizontal'; /** * `MaybeRefOrGetter` plus Vue 3.5.42 `useTemplateRef` / `shallowReadonly` * results. Those wrap in a deep `Readonly>` that is not assignable * to `Ref`. */ type MaybeReadonlyRefOrGetter = MaybeRefOrGetter | { readonly value: unknown; }; /** * Discriminated payload describing what is being dragged. Consumers extend * this with concrete `type` literals plus a matching `value`. * * @example * ```ts * import type { DragType } from '@vuetify/v0' * * type Cards = * | { type: 'card', value: Card } * | { type: 'column', value: Column } * // Cards extends DragType — distributive over the union. * ``` */ interface DragType { type: string; value: unknown; } /** * Resolved drop slot inside an oriented zone. Surfaced via {@link DropPosition} * so consumers can render a between-children indicator against the resolved * child rect. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * const zone = dnd.zones.register({ el, accept: ['card'], orientation: 'vertical' }) * * const indicator = zone.indicator.value * if (indicator) { * const y = indicator.edge === 'before' * ? indicator.rect.top * : indicator.rect.bottom * const x = indicator.rect.left * const width = indicator.rect.width * // Render a 2px bar at (x, y) with `width` to mark drop slot `indicator.index`. * } * ``` */ interface DropIndicator { index: number; edge: 'before' | 'after'; rect: DOMRect; } /** * Position passed to drop hooks. `index` is populated for oriented zones; * unoriented zones receive only the pointer location. `indicator` is absent * when no slot was proposed: an empty zone resolves `index` to `0`, a drop on * a slot flanking the drag's own position resolves to that position (a stay). * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * dnd.zones.register({ * el, * accept: ['card'], * orientation: 'vertical', * onDrop: (drag, position) => { * const target = position.index ?? items.length * items.splice(target, 0, drag.value) * }, * }) * ``` */ interface DropPosition { pointer: { x: number; y: number; }; index?: number; indicator?: DropIndicator; } /** * Source modality for an active drag. Adapters declare their own value via * the `Extensible<...>` escape hatch (e.g., 'touch', 'gamepad'). */ type DragVia = Extensible<'pointer' | 'keyboard'>; /** * Active drag state. Distributive over `Z` so that narrowing `drag.type` also * narrows `drag.value` when `Z` is a discriminated union. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * type Cards = { type: 'card', value: Card } | { type: 'column', value: Column } * * const dnd = useDragDrop() * * dnd.zones.register({ * el, * onEnter: drag => { * if (drag.type === 'card') drag.value.title // narrowed to Card * }, * }) * ``` */ type ActiveDrag = Z extends DragType ? { id: ID; type: Z['type']; value: Z['value']; origin: { x: number; y: number; }; current: { x: number; y: number; }; delta: { x: number; y: number; }; over: ID | null; willAccept: boolean; via: DragVia; } : never; /** * Input shape for a draggable. Distributive over `Z` so unions narrow. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * dnd.draggables.register({ * el: cardRef, * type: 'card', * value: card, * onBeforeStart: drag => !drag.value.locked, * }) * ``` */ type DraggableTicketInput = Z extends DragType ? RegistryTicketInput & { type: Z['type']; value: Z['value']; el: MaybeReadonlyRefOrGetter; disabled?: MaybeReadonlyRefOrGetter; onBeforeStart?: (drag: ActiveDrag) => boolean | void; onMove?: (drag: ActiveDrag) => void; onCancel?: (drag: ActiveDrag, reason: 'cancel' | 'reject') => void; } : never; /** * Output shape for a draggable. Distributive over `Z` so `ticket.type` * narrowing carries `ticket.value` to the matching union member. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * const ticket = dnd.draggables.register({ el, type: 'card', value: card }) * * ticket.isDragging.value // boolean * ticket.value // Card * ``` */ type DraggableTicket = Z extends DragType ? RegistryTicket & { type: Z['type']; value: Z['value']; el: Readonly>; isDragging: Readonly>; } : never; /** * Input shape for a drop zone. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * dnd.zones.register({ * el: listRef, * accept: ['card'], * orientation: 'vertical', * onDrop: (drag, position) => items.splice(position.index ?? 0, 0, drag.value), * }) * ``` */ interface DropZoneTicketInput extends RegistryTicketInput { el: MaybeReadonlyRefOrGetter; /** Allowed drag types or a synchronous predicate. Async predicates (Promise / thenable returns) are rejected with a warning — `accept` must return synchronously. */ accept?: Z['type'][] | ((drag: ActiveDrag) => boolean); orientation?: Orientation; disabled?: MaybeReadonlyRefOrGetter; onEnter?: (drag: ActiveDrag) => void; onLeave?: (drag: ActiveDrag) => void; onBeforeDrop?: (drag: ActiveDrag, position: DropPosition) => boolean | void; onDrop?: (drag: ActiveDrag, position: DropPosition) => void; } /** * Output shape for a drop zone. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * const zone = dnd.zones.register({ el, accept: ['card'] }) * * zone.isOver.value // boolean * zone.willAccept.value // boolean * zone.indicator.value // DropIndicator | null * ``` */ interface DropZoneTicket extends RegistryTicket { el: Readonly>; isOver: Readonly>; willAccept: Readonly>; indicator: Readonly>; } /** * Plugin signature. Receives the public context, optionally returns a * disposer that runs on scope teardown. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * import type { DragDropPlugin } from '@vuetify/v0' * * const logDrops: DragDropPlugin = context => { * const off = context.zones.on('register:ticket', ticket => console.log('zone', ticket.id)) * return () => off() * } * * const dnd = useDragDrop({ plugins: [logDrops] }) * ``` */ type DragDropPlugin = (context: DragDropContext) => (() => void) | void; /** * Options accepted by {@link useDragDrop}. * * @example * ```ts * import { useDragDrop, PointerAdapter } from '@vuetify/v0' * * const dnd = useDragDrop({ * adapters: [new PointerAdapter()], * onDrop: (drag, position) => console.log('drop', drag, position), * }) * ``` */ interface DragDropOptions { adapters?: DragDropAdapter[]; plugins?: DragDropPlugin[]; onBeforeStart?: (drag: ActiveDrag) => boolean | void; onMove?: (drag: ActiveDrag) => void; onBeforeDrop?: (drag: ActiveDrag, position: DropPosition) => boolean | void; onDrop?: (drag: ActiveDrag, position: DropPosition) => void; onCancel?: (drag: ActiveDrag, reason: 'cancel' | 'reject') => void; } /** * Registry context for draggable tickets, returned as `dnd.draggables` * from {@link useDragDrop}. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * const ticket = dnd.draggables.register({ el, type: 'card', value: card }) * ``` */ interface DraggablesContext extends Omit, DraggableTicketInput & DraggableTicket>, 'register'> { register: (registration: DraggableTicketInput) => DraggableTicket; } /** * Registry context for drop zones, returned as `dnd.zones` * from {@link useDragDrop}. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * const zone = dnd.zones.register({ el, accept: ['card'] }) * ``` */ interface ZonesContext extends Omit, DropZoneTicketInput & DropZoneTicket>, 'register'> { register: (registration: DropZoneTicketInput) => DropZoneTicket; } /** * Public context returned by {@link useDragDrop}. * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * * const dnd = useDragDrop<{ type: 'card', value: Card }>() * * dnd.active.value // ActiveDrag<{ type: 'card', value: Card }> | null * dnd.isDragging.value // boolean * dnd.cancel() // programmatic cancel * ``` */ interface DragDropContext { draggables: DraggablesContext; zones: ZonesContext; active: Readonly | null>>; isDragging: Readonly>; cancel: () => void; } /** * Create a headless drag-and-drop context. * * @param options - Adapters, plugins, and lifecycle hooks. All optional. * @returns A `DragDropContext` exposing `draggables` / `zones` registries, * the `active` drag, the `isDragging` boolean, and a programmatic `cancel()`. * * @see https://0.vuetifyjs.com/composables/system/use-drag-drop * * @example * ```ts * import { useDragDrop } from '@vuetify/v0' * import { useTemplateRef } from 'vue' * * const dnd = useDragDrop<{ type: 'card', value: string }>() * * const draggable = useTemplateRef('draggable') * const dropzone = useTemplateRef('dropzone') * * dnd.draggables.register({ el: draggable, type: 'card', value: 'card-1' }) * dnd.zones.register({ * el: dropzone, * accept: ['card'], * onDrop: (drag, position) => console.log(drag.value, position.index), * }) * ``` */ declare function useDragDrop(options?: DragDropOptions): DragDropContext; //#endregion export { ActiveDrag, DragDropAdapter, type DragDropAdapterContext, type DragDropAdapterEmit, DragDropContext, DragDropOptions, DragDropPlugin, DragType, DragVia, DraggableTicket, DraggableTicketInput, DraggablesContext, DropIndicator, DropPosition, DropZoneTicket, DropZoneTicketInput, KeyboardAdapter, type KeyboardAdapterOptions, Orientation, PointerAdapter, type PointerAdapterOptions, ZonesContext, useDragDrop };