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

Add insertBy option to allow prepending or appending elements #514

Open
wants to merge 1 commit into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ declare module '@shopify/draggable' {
export interface DroppableOptions extends DraggableOptions {
dropzone: string | NodeList | HTMLElement[] | (() => NodeList | HTMLElement[]);
classes?: { [key in DroppableClassNames]: string };
insertBy: 'prepend' | 'append';
}

export class Droppable<T = DroppableEventNames> extends Draggable<T> {
Expand Down
1 change: 1 addition & 0 deletions src/Draggable/Draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const defaultOptions = {
delay: {},
distance: 0,
placedTimeout: 800,
insertBy: 'append',
plugins: [],
sensors: [],
exclude: {
Expand Down
4 changes: 4 additions & 0 deletions src/Draggable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ You can set the same delay for all sensors by setting a number, or set an object
The distance you want the pointer to have moved before drag starts. This can be useful
for clickable draggable elements, such as links. Default: `0`

**`insertBy {String}`**
The method by which you wish to insert the dragged element. Valid values are `prepend`
or `append`. Default: `append`

**`plugins {Plugin[]}`**
Plugins add behaviour to Draggable by hooking into its life cycle, e.g. one of the default
plugins controls the mirror movement. Default: `[]`
Expand Down
13 changes: 11 additions & 2 deletions src/Droppable/Droppable.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ export default class Droppable extends Draggable {
this.lastDropzone.classList.remove(...occupiedClasses);
}

dropzone.appendChild(event.source);
if (this.options.insertBy === 'prepend') {
dropzone.prepend(event.source);
} else {
dropzone.appendChild(event.source);
}
dropzone.classList.add(...occupiedClasses);

return true;
Expand All @@ -260,7 +264,12 @@ export default class Droppable extends Draggable {
return;
}

this.initialDropzone.appendChild(event.source);
if (this.options.insertBy === 'prepend') {
this.initialDropzone.prepend(event.source);
} else {
this.initialDropzone.appendChild(event.source);
}

this.lastDropzone.classList.remove(...this.getClassNamesFor('droppable:occupied'));
}

Expand Down