|
1 | | -# Angular-Draggable |
2 | | -A angular directive provide html block to move on html plain. |
3 | | - |
4 | | -## Usages |
5 | | - |
6 | | -```js |
7 | | -import { NgDraggableModule } from 'angular-draggable'; |
8 | | -@NgModule({ |
9 | | - imports: [ |
10 | | - ...., |
11 | | - NgDraggableModule |
12 | | - ], |
13 | | - declarations: [YourAppComponent ], |
14 | | - exports: [YourAppComponent], |
15 | | - bootstrap: [YourAppComponent], |
| 1 | +# Angular Draggable |
| 2 | + |
| 3 | +An Angular directive library that makes any HTML element draggable and provides a drop-target directive with developer hooks. |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/angular-draggable) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Requirements |
| 11 | + |
| 12 | +- Angular **19+** |
| 13 | +- RxJS **7+** |
| 14 | +- zone.js **0.15+** |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install angular-draggable |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Quick start |
| 27 | + |
| 28 | +### Standalone components (recommended) |
| 29 | + |
| 30 | +```ts |
| 31 | +import { DraggableDirective, DroppableDirective } from 'angular-draggable'; |
| 32 | + |
| 33 | +@Component({ |
| 34 | + standalone: true, |
| 35 | + imports: [DraggableDirective, DroppableDirective], |
| 36 | + template: `...` |
16 | 37 | }) |
17 | | -..... |
| 38 | +export class AppComponent {} |
| 39 | +``` |
| 40 | + |
| 41 | +### NgModule |
18 | 42 |
|
| 43 | +```ts |
| 44 | +import { NgDraggableModule } from 'angular-draggable'; |
19 | 45 |
|
| 46 | +@NgModule({ |
| 47 | + imports: [NgDraggableModule], |
| 48 | +}) |
| 49 | +export class AppModule {} |
20 | 50 | ``` |
21 | 51 |
|
| 52 | +--- |
| 53 | + |
| 54 | +## Draggable directive |
| 55 | + |
| 56 | +Apply `[draggable]` to any element that has `position: absolute`, `fixed`, or `relative`. |
| 57 | + |
22 | 58 | ```html |
23 | | -<div draggable> |
24 | | - content |
25 | | - </div> |
| 59 | +<!-- Always draggable --> |
| 60 | +<div draggable style="position: fixed;">Drag me</div> |
| 61 | + |
| 62 | +<!-- Conditionally draggable --> |
| 63 | +<div [draggable]="isDraggable" style="position: fixed;">Drag me</div> |
26 | 64 |
|
| 65 | +<!-- With drag data passed to the drop target --> |
| 66 | +<div [draggable]="true" [dragData]="{ id: 1, label: 'Task A' }" style="position: fixed;"> |
| 67 | + Task A |
| 68 | +</div> |
27 | 69 | ``` |
28 | 70 |
|
| 71 | +| Input | Type | Default | Description | |
| 72 | +|---|---|---|---| |
| 73 | +| `[draggable]` | `boolean \| 'true' \| 'false'` | `true` | Enable or disable dragging | |
| 74 | +| `[dragData]` | `unknown` | `undefined` | Arbitrary data forwarded to the drop target | |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Droppable directive |
| 79 | + |
| 80 | +Apply `[droppable]` to any element you want to act as a drop zone. |
29 | 81 |
|
30 | 82 | ```html |
31 | | -<div draggable="true"> |
32 | | - content |
33 | | -</div> |
| 83 | +<div |
| 84 | + droppable |
| 85 | + (itemDropped)="onDrop($event)" |
| 86 | + (dragEnter)="isOver = true" |
| 87 | + (dragLeave)="isOver = false" |
| 88 | + [class.highlight]="isOver" |
| 89 | +> |
| 90 | + Drop zone |
| 91 | +</div> |
| 92 | +``` |
34 | 93 |
|
| 94 | +| Input | Type | Default | Description | |
| 95 | +|---|---|---|---| |
| 96 | +| `[droppable]` | `boolean \| 'true' \| 'false'` | `true` | Enable or disable this drop zone | |
| 97 | + |
| 98 | +| Output | Payload | When | |
| 99 | +|---|---|---| |
| 100 | +| `(itemDropped)` | `DropEvent` | A draggable element is released over this zone | |
| 101 | +| `(dragEnter)` | `void` | The cursor enters the zone during a drag | |
| 102 | +| `(dragLeave)` | `void` | The cursor leaves the zone, or the drag ends | |
| 103 | + |
| 104 | +### DropEvent |
| 105 | + |
| 106 | +```ts |
| 107 | +interface DropEvent<T = unknown> { |
| 108 | + data: T; // value from [dragData] |
| 109 | + sourceElement: HTMLElement; // the element being dragged |
| 110 | + targetElement: HTMLElement; // the drop zone element |
| 111 | + x: number; // drop x coordinate (clientX) |
| 112 | + y: number; // drop y coordinate (clientY) |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Example handler |
| 117 | + |
| 118 | +```ts |
| 119 | +import { DropEvent } from 'angular-draggable'; |
| 120 | + |
| 121 | +onDrop(event: DropEvent<{ id: number; label: string }>): void { |
| 122 | + console.log(`Dropped "${event.data.label}" at (${event.x}, ${event.y})`); |
| 123 | +} |
35 | 124 | ``` |
36 | 125 |
|
| 126 | +--- |
| 127 | + |
| 128 | +## Full example |
| 129 | + |
37 | 130 | ```html |
38 | | -<div draggable="false"> |
39 | | - content |
40 | | -</div> |
| 131 | +<!-- Draggable items --> |
| 132 | +<div |
| 133 | + *ngFor="let item of items" |
| 134 | + [draggable]="true" |
| 135 | + [dragData]="item" |
| 136 | + [style.top]="item.top" |
| 137 | + [style.left]="item.left" |
| 138 | + style="position: fixed;" |
| 139 | +> |
| 140 | + {{ item.label }} |
| 141 | +</div> |
| 142 | + |
| 143 | +<!-- Drop zones --> |
| 144 | +<div |
| 145 | + droppable |
| 146 | + (itemDropped)="onDrop('Zone A', $event)" |
| 147 | + (dragEnter)="zoneAOver = true" |
| 148 | + (dragLeave)="zoneAOver = false" |
| 149 | + [class.active]="zoneAOver" |
| 150 | +> |
| 151 | + Zone A |
| 152 | +</div> |
| 153 | + |
| 154 | +<div |
| 155 | + droppable |
| 156 | + (itemDropped)="onDrop('Zone B', $event)" |
| 157 | + (dragEnter)="zoneBOver = true" |
| 158 | + (dragLeave)="zoneBOver = false" |
| 159 | + [class.active]="zoneBOver" |
| 160 | +> |
| 161 | + Zone B |
| 162 | +</div> |
| 163 | +``` |
41 | 164 |
|
| 165 | +```ts |
| 166 | +onDrop(zone: string, event: DropEvent): void { |
| 167 | + console.log(`${event.data} dropped onto ${zone}`); |
| 168 | +} |
42 | 169 | ``` |
43 | | -# Example |
44 | | - [#demo](https://coderajay.github.io/angular-draggable) |
45 | | - |
46 | | - |
47 | | - |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## Live demo |
| 174 | + |
| 175 | +[https://coderajay.github.io/angular-draggable](https://coderajay.github.io/angular-draggable) |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## Development |
| 180 | + |
| 181 | +```bash |
| 182 | +# Build the library |
| 183 | +npm run build |
| 184 | + |
| 185 | +# Run tests |
| 186 | +npm test |
| 187 | + |
| 188 | +# Run the example app |
| 189 | +cd example && npm install && npm start |
| 190 | +``` |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## License |
| 195 | + |
| 196 | +MIT © [Ajay Singh](https://github.com/ajaysinghj8) |
0 commit comments