npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@browsepedia/drag-and-drop

v1.2.1

Published

An npm package which contains Drag and Drop functionality and supports nested dropzones.

Downloads

26

Readme

@browsepedia/drag-and-drop

An npm package which contains Drag and Drop functionality and supports nested dropzones.

Demo

You can find a demo on stackblitz here. You can also clone the repository and run ng serve demo

Installation

npm install --save @browsepedia/drag-and-drop

Usage

First off, you need to import the BpDragModule, preferably into your SharedModule.

Then, you have to use the provided directives to achieve drag the drag and drop functionality you want. There are 3 directives inside BpDragModule

  1. [bpDrag]="dragData" - defines that an element is draggable, it requires that you pass data to it - it is generic typed .
  2. bpDropzone - defines that an element is a dropzone.
  3. bpDropPlaceholder - defines that an element is the drop placeholder for a dropzone. MUST be a child of an element with the bpDropzone directive on it.

Simple example

<div class="draggable-item" [bpDrag]="data">Drag me</div>

<div class"dropzone" bpDropzone (bpDropped)="onItemDropped($event)">
    <div bpDropPlaceholder class="draggable-item"></div>
</div>

The good think about this library compared to the @angular/material drag and drop module is that it supports nested dropzones out of the box without collision due to mouse events propagation.

Nested dropzones example

<div class="draggable-item" [bpDrag]="data">Drag me</div>

<div class"dropzone" bpDropzone (bpDropped)="onItemDropped($event)">
   <div bpDropPlaceholder class="draggable-item"></div>

   <div class"dropzone" bpDropzone (bpDropped)="onItemDropped($event)">
       <div bpDropPlaceholder class="draggable-item"></div>
   </div>

   <div class"dropzone" bpDropzone (bpDropped)="onItemDropped($event)">
       <div bpDropPlaceholder class="draggable-item"></div>
   </div>
</div>

Properties

[bpDrag] directive

| Attribute | Description | Default | Required | | ------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------: | :------: | | @Input() bpDrag: T | This is also the directive name, but it requires an T value which is the object that will be emmited in the (bpDrop) @Output of the bpDropzone directive | undefned | yes | | @Input() bpDragDisabled: boolean | Wheather the item dragging is disabled | false | no | | @Input() bpDropEffect: BpDropEffect | Metadata to be attached to the (bpDrop) @Output() of bpDropzone when dropping an item. You can use it to differentiate between copy or move, for example. | 'move' | no | | @Output() bpDragStart: EventEmitter<MouseEvent> | Called when the item on which the bpDrag directive started t be dragged. Emmits the default JavaScript MouseEvent | n/a | no | | @Output() bpDragEnd: EventEmitter<void> | Called when the item on which the bpDrag directive stopped being dragged, either by being dropped or the mouse button being raised over a non bpDropzone Element | n/a | no |

[bpDropzone] directive

| Attribute | Description | Default | Required | | ------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----: | :------: | | @Input() bpDropDisabled: boolean | Wheather the dropping of items is allowed inside this container, this way you can conditionally allow or dissallow drops | false | no | | @Input() bpDropzoneIsHorizontal: boolean | Wheather the dropzone is horizontal or not. You still have to use the correct layouting (flex-row, or grid columns), this just tells the directive how to position the placeholder correctly. | false | no | | @Output() bpDrop: EventEmitter<BpDropEvent<T>> | Where T is the type of the value you set on [bpDrag] | n/a | yes |

[bpDropPlaceholder]

This directive has no inputs or outputs, it is mandatory you have one inside every [bpDropzone]. The element on which it is set will be used as a placeholder to show where the item you dragged will be placed.

Note that this library doesn't handle actually moving the element, that is business logic you have to take care of using the library. For example, when catching a (bpDrop) event you have to move the item where you need it.

The BpDropEvent<T> properties
interface BpDropEvent<T> {
  event: MouseEvent;
  data: T;
  targetIndex: number;
  dropEffect: BpDropEffect;
}

Getting the drag data

There are 2 ways to get the dragging state information. One for Angular 14+ and one fr Angular <14.

Angular 14

In Angular 14, we can now use the inject() function during the constructor phase. This opens up A LOT of posibilities. @browsepedia/drag-and-drop uses the inject() function and exposes 2 methods that depend on it in order to avoid the direct dependency on BpDragService.

  1. getIsDragging() which returns an Observable<boolean> that indicates weather an element is being dragged.
  2. getDraggingItemData() which returns an Observable<T> where T is the data type of the value passed to the [bpDrag] directive on the element that is being dragged.

Using the above functions you can declare members in your components directly, without needing the constructor. Example

    import { getIsDragging, getDraggingItemData } from '@browsepedia/drag-and-drop';

    @Component({...})
    export class MyComponent {
        protected isDragging$ = getIsDragging();
        protected draggedItemData = getDraggingItemData();
    }

Angular <13

For Angular version less that 14 you have to use the classic way of injecting BpDragService and accessing the 2 public properties.

  1. service.isDragging$, which is an Observable<boolean> that indicates weather an element is being dragged.
  2. service.data$ which is an Observable<T> where T is the data type of the value passed to the [bpDrag] directive on the element that is being dragged.

State classes

There are a few state classes that are automatically set on the [bpDropzone] element, depending on the state of the dragging mechanism.

  1. .bp-dragging-over is set on the container which you are currently dragging over and in which the placeholder is visible.
  2. .bp-dragging-active is set on all dropzones which are not disabled when an element is being dragged.