PuruVJ / neodrag

One Draggable to rule them all 💍
https://neodrag.dev
MIT License
1.61k stars 48 forks source link

Angular support #106

Open eneajaho opened 1 year ago

eneajaho commented 1 year ago

Angular support is very easy to integrate using a directive:

import { Directive, ElementRef, inject, Input } from "@angular/core";
import { Draggable, DragOptions } from '@neodrag/vanilla';

@Directive({
    standalone: true,
    selector: "[draggable]"
})
export class DraggableDirective {
    #elementRef = inject(ElementRef);
    #dragInstance: Draggable | null = null;

    @Input('draggable') set dragOptions(dragOptions: DragOptions | undefined | null | "") {
        let options = dragOptions || {};

        if (this.#dragInstance) {
            this.#dragInstance.updateOptions(options);
        } else { // create new instance
            this.#dragInstance = new Draggable(this.#elementRef.nativeElement, options);
        }
    }

    ngOnDestroy() {
        this.#dragInstance?.destroy();
        this.#dragInstance = null;
    }
}

Usage:


@Component({
  selector: 'app-root',
  template: `
    <div draggable style="width: 100px">
      Drag me!
    </div>

    <!-- With options -->
    <div [draggable]="dragOptions" style="width: 100px">
      Drag me!
    </div>
  `
})
export class AppComponent {
  dragOptions: DragOptions = {
    axis: 'y',
    onDrag: (event) => {
      console.log(event);
    }
  }
}
PuruVJ commented 1 year ago

Adding a library itself isn't hard, it's adding the docs, and answering any angular integration questions. I'm not knowledgeable in angular and am not ready to undertake such a big task

If i get volunteers to maintain the angular side, I'm ready to add a @neodrag/angular

nartc commented 1 year ago

I've added Angular integration via #129 and I'm happy to maintain the Angular side of things.