Socket
Socket
Sign inDemoInstall

ngx-drag-to-select

Package Overview
Dependencies
8
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ngx-drag-to-select

A lightweight, fast, configurable and reactive drag-to-select component for Angular 5 and beyond


Version published
Weekly downloads
8.2K
decreased by-9.44%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

1.0.5 (2018-04-05)

Bug Fixes

  • lib: cross-browser scroll (5f02cf8)
  • lib: rework positioning of selection box (9a817df), closes #8

Readme

Source

ngx-drag-to-select

travis styled with prettier npm npm License

A lightweight, fast, configurable and reactive drag-to-select component for Angular 5 and beyond

Demo

Live Demo

Playground

You can also fiddle with the library using StackBlitz. Credits for the template go to Bram Borggreve.

Features

  • Drag to Select
  • Shortcuts
  • Customizable 💅
  • Lightweight
  • Easy to use
  • Ready for AoT and SSR
  • Complies with the Angular Package Format
  • Includes FESM2015, FESM5, and UMD bundles 📦
  • It's fast 🏎

Installation

npm install ngx-drag-to-select @angular/cdk

or

yarn add ngx-drag-to-select @angular/cdk

Setup

Setting up ngx-drag-to-select is easy, and it only takes a few steps!

Adding the CSS

The first step is to add the CSS and for that you have two options. Either you use the default styles or you can import the ngx-drag-to-select sass package directly. The latter gives you the option to override variables and customize for instance the look and feel of the selection rectangle.

Using the default styles

Copy ngx-drag-to-select.css to your project and add it as a style tag to your index.html.

If you are using sass you can import the css as follows:

@import "~ngx-drag-to-select/ngx-drag-to-select.css";

If you are using the Angular CLI you can add it to your .angular-cli.json

"styles": [
  "styles.scss",
  "../node_modules/ngx-drag-to-select/ngx-drag-to-select.css"
]

Using the sass package

If you're using sass you can simply import the sass package. This allows you to override the default variables to customize the library to your needs.

@import "~ngx-drag-to-select/scss/ngx-drag-to-select";

Adding the module

In your AppModule import DragToSelectModule from ngx-drag-to-select and add it to the module imports:

import { DragToSelectModule } from 'ngx-drag-to-select';

@NgModule({
  imports: [
    DragToSelectModule.forRoot()
  ]
})
export class AppModule { }

That's it. You are now ready to use this library in your project. Make sure to call forRoot() only once in your AppModule and for feature modules you simply add the DragToSelectModule as is without calling forRoot().

Usage

Once you have installed the library and added the DragToSelectModule to your application you are ready to go.

Anywhere in your template add the ngx-select-container component and wrap all items that you want to be selectable in this component.

Next, mark all selectable items with the selectItem directive. This connects each item with the container component.

Here's a complete example:

<ngx-select-container #container="ngx-select-container" [(selectedItems)]="selectedDocuments" (select)="someMethod($event)">
  <ul>
    <li [selectItem]="document" *ngFor="let document of documents">{{ document.name }}</li>
  </ul>
</ngx-select-container>

Configuration Options

This section gives you an overview of things you can customize and configure.

Overriding sass variables

You can override the following variables:

VariableTypeDefaultDescription
$select-box-colorColor#7ddafcColor of the selection rectangle
$select-box-border-sizeUnit2pxBorder size for the selection rectangle
$selected-item-borderBooleantrueWhether the selected item should get a border
$selected-item-border-colorColor#d2d2d2Border color of the selected item
$selected-item-border-sizeUnit1pxBorder size of the selected item
$box-shadowBooleantrueWhether the selected item should get a box shadow

If you wish to override one of these variables, make sure to do that before you import the sass package.

Example:

// Example for overriding the color of the selection retangle
$select-box-color: red;

@import "~ngx-drag-to-select/scss/ngx-drag-to-select";

Configuring DragToSelectModule

This library allows to you override certain options, such as

selectedClass (String)

Class that is added to an item when it's selected. The default class is selected. Note that if you override this option, you'll lose the default styling and must take care of this yourself.

shortcuts (Object)

ngx-drag-to-select supports a hand full of shortcuts to make our live easier when selecting items.

ShortcutDefaultDescription
disableSelectionaltDisable selection mode to allow selecting text on the screen within the drag area
toggleSingleItemmetaAdd or remove single item to / from selection
addToSelectionshiftAdd items to selection
removeFromSelectionshift + metaRemove items from selection

You can override these options by passing a configuration object to forRoot().

Here's an example:

import { DragToSelectModule } from 'ngx-drag-to-select';

@NgModule({
  imports: [
    DragToSelectModule.forRoot({
      selectedClass: 'my-selected-item',
      shortcuts: {
        disableSelection: ''
      }
    })
  ]
})
export class AppModule { }

When overriding the default shortcuts you can use the following modifier keys:

shift alt ctrl meta

When using meta, it will be substituted with ctrl (for Windows) and cmd (for Mac). This allows for cross-platform shortcuts.

Note: If you override one of the shortscut you have to make sure they do not interfear with one another to ensure a smooth selecting experience.

API

ngx-drag-to-select comes with two main building blocks, a ngx-select-container component and a selectItem directive.

ngx-select-container

Inputs

InputTypeDefaultDescription
selectedItemsArray/Collection of items that are currently selected
selectOnDragBooleantrueWhether items should be selected while dragging
disabledBooleanfalseDisable selection

Here's an example of both inputs in action:

<ngx-select-container [(selectedItems)]="selectedDocuments" [disabled]="false" [selectOnDrag]="selectOnDrag">
  ...
</ngx-select-container>
  • To get ahold of the selected items you can use a two-way data binding ([()]) aka banana-in-the-box syntax. This means that whenever the selection changes, your property is updated accordingly. It will always reflect the current selection.

  • Binding an expression to selectOnDrag will override the default value. When this option is set to false, it will increase the performance but you'll trade this for a slighly worse user experience.

Outputs

InputPayload TypeDescription
selectArrayEvent that is fired whenever the selection changes. The payload ($event) will be the list of selected items.

Example:

<ngx-select-container (select)="someMethod($event)">
  ...
</ngx-select-container>

Public Methods

MethodsDescription
selectAllSelect all items within the drag area
clearSelectionClear current selection

To access these methods on the container component you can either use the @ViewChild() decorator

import { Component, ViewChild } from '@angular/core';
import { SelectContainerComponent } from 'ngx-drag-to-select';

@Component({
  ...
})
export class AppComponent {
  @ViewChild(SelectContainerComponent) selectContainer: SelectContainerComponent;

  someMethod() {
    this.selectContainer.clearSelection();
  }
}

or use it within the template with a template reference variable

<button (click)="selectContainer.selectAll()">Select All</button>
<button (click)="selectContainer.clearSelection()">Clear Selection</button>

<ngx-select-container #selectContainer="ngx-select-container" [(selectedItems)]="selectedDocuments">
  ...
</ngx-select-container>

What if I want to use the @ViewChild() decorator but have multiple instances of the ngx-select-container in my template?

In that case I would recommend to add template reference variables to your select containers and query them one by one using the variable name.

Here's an example:

<ngx-select-container #documents>
  ...
</ngx-select-container>

...

<ngx-select-container #images>
  ...
</ngx-select-container>

In the component you can then query them one by one:

import { Component, ViewChild } from '@angular/core';
import { SelectContainerComponent } from 'ngx-drag-to-select';

@Component({
  ...
})
export class AppComponent {
  @ViewChild('documents') documentContainer: SelectContainerComponent;
  @ViewChild('images') imagesContainer: SelectContainerComponent;

  someMethod() {
    this.documentContainer.clearSelection();
  }

  someOtherMethod() {
    this.imagesContainer.selectAll();
  }
}

selectItem

The selectItem directive is used to mark DOM elements as selectable items. It takes an input to control the value that is used when the item was selected. If the input is not specified, it will use the directive instance as a default value.

Inputs

InputTypeDefaultDescription
selectItemanyDirective InstanceValue that is used when the item is selected

Example:

<ngx-select-container>
  <ul>
    <li [selectItem]="document" *ngFor="let document of documents">{{ document.name }}</li>
  </ul>
</ngx-select-container>

Public Properties

MethodsTypeDescription
selectedBooleanWhether the item is selected or not

You can access this property in a similar why you access methods on the select-container component using either a template reference variable or programmatically with the @ViewChild() decorator.

Example:

<ngx-select-container>
  <ul>
    <li [selectItem]="document" #item *ngFor="let document of documents">
      {{ document.name }}
      <i class="fa fa-check" *ngIf="item.selected"></i>
    </li>
  </ul>
</ngx-select-container>

Want to contribute?

If you want to file a bug, contribute some code, or improve our documentation, read up on our contributing guidelines and code of conduct, and check out open issues.

For developers

If you want to set up ngx-drag-to-select on your machine for development, head over to our developers guide and follow the described instructions.

Versioning

ngx-drag-to-select will be maintained under the Semantic Versioning guidelines. Releases are numbered with the following format:

<MAJOR>.<MINOR>.<PATCH>
  1. MAJOR versions indicate incompatible API changes,
  2. MINOR versions add functionality in a backwards-compatible manner, and
  3. PATCH versions introduce backwards-compatible bug fixes.

For more information on SemVer, please visit http://semver.org.

Licence

MIT © Dominic Elm

Keywords

FAQs

Last updated on 05 Apr 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc