|
| 1 | + |
| 2 | + |
| 3 | +Automatically scrolls a container when a dragged item approaches the top or bottom edge. Apply to any scrollable element used with drag-and-drop. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **CDK & Native Compatible** — Listens on `document` so it works with CDK drag-drop and native HTML5 drag |
| 8 | +- **Proportional Speed** — Scrolls faster as the cursor gets closer to the edge |
| 9 | +- **Horizontal Bounds** — Only triggers when the cursor is horizontally over the container (with configurable tolerance) |
| 10 | +- **Configurable** — Customize margin zone, max speed, and horizontal tolerance |
| 11 | +- **Zone Optimized** — Runs outside Angular zone for better performance |
| 12 | +- **SSR Safe** — Only activates in the browser |
| 13 | +- **Toggleable** — Enable/disable via `dragAutoScrollDisabled` input |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { DragAutoScrollDirective } from 'ngx-oneforall/directives/drag-auto-scroll'; |
| 21 | +``` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Basic Usage |
| 26 | + |
| 27 | +```html |
| 28 | +<ul dragAutoScroll style="overflow-y: auto; height: 400px"> |
| 29 | + @for (item of items; track $index) { |
| 30 | + <li draggable="true">{{ item }}</li> |
| 31 | + } |
| 32 | +</ul> |
| 33 | +``` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## API Reference |
| 38 | + |
| 39 | +| Input | Type | Default | Description | |
| 40 | +|---|---|---|---| |
| 41 | +| `dragAutoScrollMargin` | `number` | `50` | Edge zone height (px) that triggers scrolling | |
| 42 | +| `dragAutoScrollSpeed` | `number` | `10` | Maximum scroll speed (px/frame) | |
| 43 | +| `dragAutoScrollTolerance` | `number` | `50` | Horizontal tolerance (px) outside container bounds above which scrolling does not trigger | |
| 44 | +| `dragAutoScrollDisabled` | `boolean` | `false` | Disables auto-scroll behavior | |
| 45 | + |
| 46 | +### `dragAutoScrollMargin` |
| 47 | + |
| 48 | +Defines the height (in pixels) of the invisible zone at the top and bottom edges of the container. When the cursor enters this zone during a drag, scrolling begins. Larger values make it easier to trigger scrolling, smaller values require the cursor to be closer to the edge. |
| 49 | + |
| 50 | +### `dragAutoScrollSpeed` |
| 51 | + |
| 52 | +The maximum scroll speed in pixels per animation frame. The actual speed scales proportionally — when the cursor is at the very edge, it scrolls at full speed; at the outer boundary of the margin zone, it scrolls slowly. Increase for long lists where users need to scroll quickly. |
| 53 | + |
| 54 | +### `dragAutoScrollTolerance` |
| 55 | + |
| 56 | +How far (in pixels) the cursor can be **horizontally** outside the container's left/right edges before scrolling stops. This prevents accidental scrolling when the cursor drifts sideways during a drag. Set to `0` to require the cursor to be strictly inside the container horizontally. |
| 57 | + |
| 58 | +### `dragAutoScrollDisabled` |
| 59 | + |
| 60 | +When `true`, the directive stops responding to drag events entirely. Useful for conditionally disabling auto-scroll based on application state. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Custom Configuration |
| 65 | + |
| 66 | +Increase the edge zone and scroll speed for large lists: |
| 67 | + |
| 68 | +```html |
| 69 | +<div |
| 70 | + dragAutoScroll |
| 71 | + [dragAutoScrollMargin]="80" |
| 72 | + [dragAutoScrollSpeed]="15" |
| 73 | + style="overflow-y: auto; max-height: 600px" |
| 74 | +> |
| 75 | + <!-- draggable content --> |
| 76 | +</div> |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## How It Works |
| 82 | + |
| 83 | +1. The directive listens for `dragover` events on `document` (works even when a drag preview covers the container) |
| 84 | +2. It checks the cursor is horizontally within the container bounds (± tolerance) |
| 85 | +3. When the cursor enters the top or bottom margin zone, scrolling starts via `requestAnimationFrame` |
| 86 | +4. Scroll speed scales linearly — the closer to the edge, the faster it scrolls |
| 87 | +5. Scrolling stops on `drop`, `dragend`, or when the cursor moves to the center or outside bounds |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Live Demo |
| 92 | + |
| 93 | +{{ NgDocActions.demoPane("DragAutoScrollDemoComponent") }} |
0 commit comments