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

drag-to-blank

v1.1.0

Published

DragToBlank class built with TypeScript for front end use. Meant to be extended and used as dependencies.

Downloads

8

Readme

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. API
  5. Packages extending DragToBlank
  6. License

DragToBlank

DragToBlank is a simple, lightweight TypeScript NPM library that provides a foundation for adding drag interactions to your web applications. By extending the DragToBlank class and overriding the mouseDown, dragStart, dragMove, and dragEnd methods, you can easily customize the behavior of your DragToBlank elements.

Installation

npm install -D drag-to-blank

Usage

First, import the DragToBlank class from the package:

import { DragToBlank } from 'drag-to-blank';

Next, extend the DragToBlank class and override or add the necessary methods and properties:

export class MyDragToBlank extends DragToBlank {
	//...custom class properties
	protected override defaultClassName = 'my-DragToBlank';
	private options?: MyDragToBlankOptions;

	//... custom constructor
	constructor(
		element: HTMLElement,
		options?: MyDragToBlankOptions,
	) {
		super(element);
		this.options = options;
	}

	protected mouseDown(event: MouseEvent): void {
		// Custom mouse down behavior
	}

	protected dragStart(event: MouseEvent): void {
		// Custom drag start behavior
	}

	protected dragMove(event: MouseEvent): void {
		// Custom drag move behavior
	}

	protected dragEnd(event: MouseEvent): void {
		// Custom drag end behavior
	}

	//...custom class methods
}

Finally, instantiate your DragToBlank element by passing in the element to be dragged or by using the apply method to apply the DragToBlank behavior to all elements with the specified class name (defaults to DragToBlank if not overridden) or a specified class name:

import { MyDragToBlank } from 'my-DragToBlank';

const DragToBlank = new MyDragToBlank(
	document.getElementById('my-DragToBlank-element'),
);
// or
MyDragToBlank.apply();
// or
MyDragToBlank.apply('my-DragToBlank-class');

API

Mouse Data

To access mouse data, use the mouseData property of the DragToBlank class. Its get() method retrieves the mouse data for a given mouse data type. If no data is found for the key, it returns undefined. The keys are as follows:

  • 'mouseDown'
  • 'dragStart'
  • 'dragMove'
  • 'dragEnd'
const mouseDownData = this.mouseData.get('mouseDown');

All mouse data is stored as an object of type StampedPosition which has the following properties:

| Property | Type | Description | | ----------- | -------------------------- | --------------------------------- | | timestamp | number | The timestamp of the mouse event. | | position | {x : number, y : number} | The position of the mouse event. |

The mouse data of dragMove is stored as a LinkedStampedPosition object. It extends the StampedPosition type. Its additional properties are:

| Property | Type | Description | | -------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------- | | prev | StampedPosition | The previous mouse move StampedPosition. On initializing the first node, the value of dragStart is assigned to prev. | | next | StampedPosition | The next mouse move StampedPosition. On the latest node, this is undefined. |

Static Properties

| Property | Type | Access | Description | | ------------------ | --------------- | ---------------- | -------------------------------------------------------------------------------------------------------- | | defaultClassName | string | protected static | The default class name to use when applying DragToBlank behavior to elements (defaults to DragToBlank) | | instances | DragToBlank[] | static | An array to hold all instances of the DragToBlank class |

Methods

| Method | Description | | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | apply(className?: string) | Applies DragToBlank behavior to all elements with either the specified className if provided or the defaultClassName property value | | mouseDown(event: MouseEvent) | This method is called when the mouse button is pressed down on the DragToBlank element. Override this method to implement custom behavior if desired. | | dragStart(event: MouseEvent) | This method is called a single time on the first mouse movement event following the mouseDown() method. Override this method to implement custom behavior if desired. | | dragMove(event: MouseEvent) | This method is called on every mouse movement event after the dragStart() method. Override this method to implement custom behavior if desired. | | dragEnd(event: MouseEvent) | This method is called when the mouse button is released after the dragStart() method. Override this method to implement custom behavior if desired. |

Example

This example shows how to extend the DragToBlank class to implement custom behavior. In this example, we will log a message, the original event and the mouse down data to the console on each mouse move event that occurs after the mouse is down event occured on the target element.

import { DragToBlank } from 'DragToBlank';

export class MyDragToBlank extends DragToBlank {
	protected override defaultClassName = 'my-DragToBlank';

	constructor(element: HTMLElement) {
		super(element);
	}

	protected override dragMove(event: MouseEvent): void {
		let mouseDownData = this.mouseData.get('mouseDown');
		console.log('my mouse move', event, mouseDownData);
	}
}

Packages extending DragToBlank

This section includes links to packages that extend the DragToBlank base class. If you've created a package that extends DragToBlank, feel free to add it here.

  1. DragToScroll: Allows you to drag with the mouse to scroll a page or any elements with scrollbars.
  2. DragToTranslate: Allows you to drag with the mouse to translate an element.

To add your extension to this list, please submit a pull request with the changes to this README file.

License

MIT