The sophisticated Drag and Drop library you've been waiting for 🥳
$
prefix.type DFlexEvents =
| "$onDragOutContainer"
| "$onDragOutThreshold"
| "$onDragOver"
| "$onDragLeave"
| "$onLiftUpSiblings"
| "$onMoveDownSiblings";
Implementation like any other event:
import type { DFlexEvents } from "@dflex/dnd";
const onDFlexEvent = (e: DFlexEvents) => {
// Do something, or just don't.
};
// Later you can remove the listener with `removeEventListener`
document.addEventListener("$onDragLeave", onDFlexEvent);
LayoutState
.interface DFlexLayoutStateEvent {
type: "layoutState",
layoutState: "pending" | "ready" | "dragging" | "dragEnd" | "dragCancel";
}
Implementation from anywhere inside the app:
React.useEffect(() => {
const unsubscribe = store.listeners.subscribe((e) => {
console.info("new layout state", e);
}, "layoutState");
return () => {
unsubscribe();
};
}, []);
parentID
from registry input by checking branches internally (#564)interface RegisterInput{
id: string;
depth?: number;
readonly?: boolean;
};
const { id, depth, readonly } = registerInput;
React.useEffect(() => {
if (ref.current) {
store.register({ id, depth, readonly });
}
return () => {
store.unregister(id);
};
}, [ref.current]);
Playwright
(#557)id
and parentID
for registered elements (#555).playwright
to cover core cases (#556).Refactor meta extractor to distinguish between an empty and orphan case (#524)
Change margin when positioning from orphan to non-orphan container (#529)
Refactor test description for essential cases (#528)
Add unit test to meta extractor (#527)
Refactor unit test for DnD (#526)
Fix margin-bottom calculation taking into consideration the last occupied position (#525)
Upgrade to pnpm 7.
Add essential cases for obliquity and continuity including from/to empty container (#531)
Remove unused instances and methods not used in the store and refactor related tests (#533).
Add origin length to determine when to restore and when the container is expanding (#533).
Refactor unit test for meta extractor (#533).
Fix continuity in extending orphan container (#534)
Clear transition instances after each operation (#537)
Testing transform element to an into empty container (#538)
Enable transforming elements between containers (#539)
interface ContainersTransition {
/** Default=true */
enable: boolean;
/**
* Support orphan to orphan transformation.
* Default=10px
* */
margin: number;
}
Transformation depends on three cases:
Calculations are made to:
occupied-position
because can't move based on its own position so this will kill dealing with different heights/widths. So it has to move to a post-defined point. For transformation inside the list itself, it's not a problem. But for migration to a new container then the calculated value should be done before triggering any transformation. It's not a step-by-step regular movement.Three different phases for execution:
out-position= true
.Roadmap:
A- Handle the case where transformation between containers happens to the absent bottom (#493)
numberOfElementsTransformed
and updateNumOfElementsTransformed
from dragged and replace the use case with exicting indicators.B- Refactor utils to Core removing #getDiff
(#494)
interface INode extends ICore {
isConnected(): boolean;
isPositionedUnder(elmY: number): boolean;
isPositionedLeft(elmX: number): boolean;
getRectBottom(): number;
getRectRight(): number;
getRectDiff(elm: this, axis: Axis): number;
getDisplacement(elm: this, axis: Axis): number;
getDistance(elm: this, axis: Axis): number;
getOffset(): RectDimensions;
hasSamePosition(elm: this, axis: Axis): boolean;
}
C- Check the layout shift for vertically transforming elements with different heights inside the same container (#496)
Outside the container then insert: a. The dragged is bigger than the targeted element - moving without releasing. b. The dragged is smaller than the targeted element - moving without releasing. c. The dragged is smaller than the targeted element - move and release with iteration.
Inside the container: a. The dragged is bigger than the targeted element - moving without releasing. b. The dragged is bigger than the targeted element - move and release with iteration. c. The dragged is smaller than the targeted element - moving without releasing. d. The dragged is smaller than the targeted element - move and release with iteration.
D- Enable transformation for containers orphaned by migration (#497)
In this case, the origin container has two elements.
To solve it, when the container is receiving a new element. DFlex restores the preserved last element position and calculates the margin to guarantee the given scenario where there is no shifting in positions.
E- Enable transformation from origin higher than destination
One of the calculations DFlex has is defining a threshold for each layout (#418). Threshold tells the transformers when dragged is in/out its position or the container strict boundaries. This strict definition of threshold helps to improve user experience and the physical sense of elements' interactivity. It also plays role in detecting the active container when an element migrates from one to another. This definition prevents the transformation from a container having bigger height/width into another container having smaller boundaries. When leaving the position and entering a new one the transformer can't tell if the element is inside the less-boundaries container or not. To tackle this issue each depth must have a unified transforming boundary. So when the element is dragged at the same level horizontally or vertically it can be settled into the destination container and attached to the bottom which has the highest weight.
Steps to Solve it:
F- Dealing with multiple transformations without settling into a new position (#519)
This scenario requires checking all the containers that are affected by the transformation and rollback each last transformation accordingly. This is done by adding a unique id
to the migration instance instead of a central one created for each clickable operation. E.g. The user clicks and transforms then one id
is created for this operation. This allows to roll back each transformation connected to the same id
. But to achieve multiple undo between containers the id is shifted into migration.
interface MigrationPerContainer {
/** Last known index for draggable before transitioning. */
index: number;
/** Transition siblings key. */
SK: string;
/** Transition unique id. */
id: string;
}
G- Enable multiple steps of transformation (#520)
H- Add the ability to extend the transformation area between containers (#521). This allows accumulating all registered elements in one container.
setDistanceIndicators
calculations in one equation instead of if/else.build:w
and list length when migrated to a new container (#461)getDiff
to calculate difference in the space for : "offset" | "occupiedPosition" | "currentPosition"setDistanceIndicators
to setDistanceBtwPositions
core-instance
and Abstract Core and has all the utility methods for the core.interface ICoreUtils extends ICore {
isPositionedUnder(elmY: number): boolean;
isPositionedLeft(elmX: number): boolean;
getRectBottom(): number;
getRectRight(): number;
hasSamePosition(elm: this, axis: Axis): boolean;
}
Rect
option to keep the dimension on track. To do that efficiently it uses 2- CurrentPosition
. Presumably, width and height stay the same but the position keeps changing. So, to get the current offset we need getOffset
a new method introduced to the CoreUtils.
Migration
has now instances used for transition periods. Wich defined as the moment where the insertion is detected to the moment where the insertion is completed. To do that we have insertionTransform
and insertionOffset
. We have also the prev
method. Prev()
and latest()
returns easily the current and last migration instance. When migration is completed we set these instances to null
.handleElmMigration
to the Store. It helps update the boundaries for both new and original lists.newElmOffset
Check elmSyntheticOffset
to detect the right insertion.Create Container Instance which is similar to Core. But with the current edition, we now have multiple instances each one representing a different role.
interface IContainer {
/** Strict Rect for siblings containers. */
readonly boundaries: RectBoundaries;
/** Numbers of total columns and rows each container has. */
readonly grid: IPointNum;
/** Container scroll instance. */
scroll: IScroll;
setGrid(grid: IPointNum, rect: RectDimensions): void;
setBoundaries(rect: RectDimensions): void;
}
interface IDepth {
/** Grouping containers in the same level. */
readonly containers: {
[depth: number]: string[];
};
add(SK: string, depth: number): void;
getByDepth(depth: number): string[];
removeByDepth(depth: number): void;
removeAll(): void;
}
SK
generated by DOM-Gen
.enableContainersTransition
to opts
with a new playground /migration
(#475)Revert #446 approach Depends on getting parents/children for dragged migrations by enforcing parent registration if not exist in the store. It works fine but is not what it's intended to be.
DFlex main approach in the essential design depends on a flat hierarchy. Instead of getting each element and connecting it to the parent. The store already has all elements on the same level.
Supposed we have container -a
and container-b
. All elements in both containers are included in the registry. Both boundaries for siblings are calculated and can be added to the threshold instance since v3.3.1.
Regular Scenario: user click -> get element -> find the parent -> matching the parent overlay recursively -> matching the children overlay recursively -> remove node/append new node to new parent.
DFlex Implementation: user click -> get element -> matching siblings overlay -> add transformation for new positions.
1. Avoid recursively looking for parents.
2. Always allow elements from the same level to interact with each other.
3. Control element migration map. Registered/same level (siblingDepth
)
4. Insertion inside the containers already shipped. The scenario dragged outside the container and then moved inside. It's already done by getting the siblings' key SK
so it's somehow container agnostic.
Adding Migration
class (#450):
undo
multiple steps back when introducing the time travel
feature.interface IAbstract {
index: number;
key: string;
}
interface IMigration {
/** Get the latest migrations instance */
latest(): IAbstract;
/**
* We only update indexes considering migration definition when it happens
* outside container but not moving inside it.
* So we update an index but we add a key.
*/
setIndex(index: number): void;
/** When migration from one container to another. */
add(index: number, key: string): void;
}
Create an array for branches even if there's one child in (#457) to allow migration and flexible add/remove elements. Enhance actions by adding timeout this will cut off Cypress when there's more than one failing case.
setEffectedElemDirection
and effectedElemDirection
using the direction resulted in the update-element
depending on the increase/decreas parameter.setPosition
in the Core to include z axis
(#443)ESM
and CJS
and bundle them with esbuild
. (#459)yarn
to pnpm
. And Use Vite
instead of CRA
for playground. (#460)Full Changelog: https://github.com/dflex-js/dflex/compare/v3.3.2...v3.4.0
AxesCoordinates
to Point
For more readability.gridPlaceholder
for Dragged and update it for Core.updateElement
(#432)isLeavingFromHead
, public isLeavingFromTail
and mousePoints
(#435)