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

@dflex/dnd

v3.10.6

Published

Modern drag and drop package for all JavaScript frameworks

Downloads

307

Readme

Installation

npm install @dflex/dnd

API

DFlex DnD depends on three principles to achieve DOM interactivity:

  • Register element in the store.
  • Start dragging when mouse is down.
  • End dragging to release element when mouse is up.
import { store, DnD } from "@dflex/dnd";

Register element

Each element should be registered in DFlex DnD Store in order to be active for drag and drop later.

store.register(RegisterInputOpts): void;

Where RegisterInputOpts is an object with the following properties:

  • id: string Targeted element-id.
  • depth?: number The depth of targeted element starting from zero (The default value is zero).
  • readonly?: boolean True for elements that won't be transformed during DnD but belongs to the same interactive container.

Create Drag and Drop Session

The responsive drag and drop session should be created when onmousedown is fired. So it can initialize the element and its siblings before start dragging.

const dflexDnD = new DnD(id, coordinate, opts);

Start responsive dragging

dflexDnD.dragAt(x, y);
  • x: number is event.clientX, the horizontal click coordinate.
  • y: number is event.clientY, the vertical click coordinate.

End Drag and Drop Session

dflexDnD.endDragging();

Cleanup element

It's necessary to cleanup the element from store when the element won't be used or will be removed/unmounted from the DOM to prevent any potential memory leaks.

store.unregister(id: string): void

Options

You can pass options when creating a DnD instance that controls each element individually. So your options can be different from each other.

Dragging Threshold

The threshold object defines when the dragging event should be fired and triggers the response of other sibling elements.

Threshold Interface

interface ThresholdPercentages {
  /** vertical threshold in percentage from 0-100 */
  vertical: number;

  /** horizontal threshold in percentage from 0-100 */
  horizontal: number;
}

Threshold Definition

interface DFlexDnDOpts {
  // ... other options.
  threshold?: Partial<ThresholdPercentages>;
}

Threshold Default Value

{
  "threshold": {
    "vertical": 60,
    "horizontal": 60
  }
}

Commit changes to DOM

DFlex is built to manipulate DOM elements with transformation indefinitely. This means you can always drag and drop elements without reconstruction of the DOM. Still, it comes with a reconciler that tracks elements' changes and only reconciles the elements that have changed their position from their origin.

Commit Interface

interface CommitInterface {
  enableAfterEndingDrag: boolean;
  enableForScrollOnly: boolean;
}

Commit Definition

interface DFlexDnDOpts {
  // ... other options.
  commit?: Partial<CommitInterface>;
}

Commit Default Value

{
  "commit": {
    "enableAfterEndingDrag": true,
    "enableForScrollOnly": true
  }
}

Dragging Restrictions

You can define the dragging restrictions for each element relative:

  1. Element position.
  2. Element parent container.
  3. Screen viewport (automatically enabled).

Restrictions Interface

interface Restrictions {
  self: {
    allowLeavingFromTop: boolean;
    allowLeavingFromBottom: boolean;
    allowLeavingFromLeft: boolean;
    allowLeavingFromRight: boolean;
  };
  container: {
    allowLeavingFromTop: boolean;
    allowLeavingFromBottom: boolean;
    allowLeavingFromLeft: boolean;
    allowLeavingFromRight: boolean;
  };
}

Restrictions Definition

interface DFlexDnDOpts {
  // ... other options.
  restrictions?: {
    self?: Partial<Restrictions["self"]>;
    container?: Partial<Restrictions["container"]>;
  };
}

Restrictions Default Value

{
  "restrictions": {
    "self": {
      "allowLeavingFromTop": true,
      "allowLeavingFromBottom": true,
      "allowLeavingFromLeft": true,
      "allowLeavingFromRight": true
    },
    "container": {
      "allowLeavingFromTop": true,
      "allowLeavingFromBottom": true,
      "allowLeavingFromLeft": true,
      "allowLeavingFromRight": true
    }
  }
}

Auto-Scroll

Auto-Scroll Interface

interface ScrollOptions {
  enable?: boolean;
  initialSpeed?: number;
  threshold?: Partial<ThresholdPercentages>;
}

Auto-Scroll Definition

interface DFlexDnDOpts {
  // ... other options.
  scroll?: Partial<ScrollOptions>;
}

Auto-Scroll Default Value

{
  "scroll": {
    "enable": true,
    "initialSpeed": 10,
    "threshold": {
      "vertical": 15,
      "horizontal": 15
    }
  }
}

Events

DFlex has three (3) types of custom events.

  1. DFlex Dragged Event.
  2. DFlex Interactivity Event.
  3. DFlex Siblings Event.

Event Usage

// DFlex event handler.
const onDFlexEvent = (e: DFlexEvents) => {
  // Do something.
  console.log(`onDFlexEvent: ${e.type}`, e.detail);
};

// Dragged Events.
const ON_OUT_CONTAINER = "$onDragOutContainer";
const ON_OUT_THRESHOLD = "$onDragOutThreshold";

//  Interactivity Events.
const ON_DRAG_OVER = "$onDragOver";
const ON_DRAG_LEAVE = "$onDragLeave";

// Sibling Events.
const ON_LIFT_UP = "$onLiftUpSiblings";
const ON_MOVE_DOWN = "$onMoveDownSiblings";

// Capture DFlex event.
document.addEventListener(
  ON_OUT_CONTAINER /** or another event */,
  onDFlexEvent
);

// Remove it later when dragging is done.
document.removeEventListener(
  ON_OUT_CONTAINER /** or another event */,
  onDFlexEvent
);

Dragged Event

It's an event related to capturing dragged positions. This event is fired when the dragged is out of its threshold position $onDragOutContainer or out of its container $onDragOutThreshold.

DraggedEvent interface

interface PayloadDraggedEvent {
  /** Returns element id in the registry  */
  id: string;

  /** Returns dragged temp index */
  index: number;
}

/** For dragged out of threshold or container event. */
type DFlexDraggedEvent = CustomEvent<PayloadDraggedEvent>;

Interactivity Event

It's an event related to capturing dragged interactions with other elements. This event is fired when the dragged is over another element $onDragOver or when the dragged is leaving the occupied position $onDragLeave.

InteractivityEvent interface
interface PayloadInteractivityEvent {
  /** Returns element id in the registry  */
  id: string;

  /** Returns element current index */
  index: number;

  /** Returns the element that triggered the event  */
  target: HTMLElement;
}

/** For dragged over an element or leaving an element. */
type DFlexInteractivityEvent = CustomEvent<PayloadInteractivityEvent>;

Siblings Event

It's an event related to capturing siblings' positions. This event is fired when the siblings are lifting up $onLiftUpSiblings or moving down $onMoveDownSiblings

SiblingsEvent interface
interface PayloadSiblingsEvent {
  /** Returns the index where the dragged left  */
  from: number;

  /** Returns the last index effected of the dragged leaving/entering  */
  to: number;

  /** Returns an array of sibling ids in order  */
  siblings: string[];
}

/** When dragged movement triggers the siblings up/down. */
type DFlexSiblingsEvent = CustomEvent<PayloadSiblingsEvent>;

Listeners

DFlex listeners are more generic than the custom events and responsible for monitoring the entire layout and reporting back to you.

DFlex has two (2) types of listeners:

  1. Layout state listener.
  2. Mutation listener.

Listener Usage

// app/index.js

const unsubscribeLayout = store.listeners.subscribe((e) => {
  console.info("new layout state", e);
}, "layoutState");

// call it later for clear listeners from memory.
unsubscribeLayout();

const unsubscribeMutation = store.listeners.subscribe((e) => {
  console.info("new mutation state", e);
}, "mutation");

// call it later for clear listeners from memory.
unsubscribeMutation();

Layout state listener

Responsible for monitoring any change that happens to layout interactivity.

Layout state listener interface

type LayoutState =
  | "pending" // when DnD is initiated but not activated yet.
  | "ready" // When clicking over the registered element. The element is ready but not being dragged.
  | "dragging" // as expected.
  | "dragEnd" // as expected.
  | "dragCancel"; // When releasing the drag without settling in the new position.

interface DFlexLayoutStateEvent {
  type: "layoutState";
  status: LayoutState;
}

Mutation listener

Responsible for monitoring DOM mutation that happens during reconciliation.

Mutation listener interface

type ElmMutationType = "committed";

interface DFlexElmMutationEvent {
  type: "mutation";
  status: ElmMutationType;
  payload: {
    target: HTMLElement; // HTML element container.
    ids: string[]; // Committed Elements' id in order.
  };
}

Advanced

getSerializedElm

DFlex elements are serialized and exported accordingly.

store.getSerializedElm(elmID: string): DFlexSerializedElement | null

type DFlexSerializedElement = {
  type: string;
  version: number;
  id: string;
  translate: PointNum | null;
  grid: PointNum;
  order: DFlexDOMGenOrder;
  initialPosition: AxesPoint;
  rect: BoxRectAbstract;
  hasTransformedFromOrigin: boolean;
  hasPendingTransformation: boolean;
  isVisible: boolean;
};

getSerializedScrollContainer

DFlex scroll containers are serialized and exported accordingly. You can get any scroll container for any registered element id.

store.getSerializedScrollContainer(elmID: string): DFlexSerializedScroll | null

type DFlexSerializedScroll = {
  type: string;
  version: number;
  key: string;
  hasOverFlow: AxesPoint<boolean>;
  hasDocumentAsContainer: boolean;
  scrollRect: AbstractBox;
  scrollContainerRect: AbstractBox;
  invisibleDistance: AbstractBox;
  visibleScreen: Dimensions;
};

commit

Commit changes to the DOM. commit will always do surgical reconciliation. and it's the same function that's used in the options

store.commit(): void

isLayoutAvailable

True when DFlex is not transforming any elements and not executing any task.

isLayoutAvailable(): boolean

unregister

safely removing element from store.

store.unregister(id: string): void

destroy

To destroy all DFlex instances. This is what you should do when you are done with DnD completely and your app is about to be closed.

store.destroy(): void;

Documentation 📖

For documentation, more information about DFlex and a live demo, be sure to visit the DFlex website https://www.dflex.dev/

License 🤝

DFlex is MIT License.