Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/create-draggable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createEffect,
createSignal,
createMemo,
onCleanup,
onMount,
Setter,
Expand Down Expand Up @@ -42,10 +43,10 @@ const createDraggable = (id: Id, data: Record<string, any> = {}): Draggable => {
});
onCleanup(() => removeDraggable(id));

const isActiveDraggable = () => state.active.draggableId === id;
const transform = () => {
const isActiveDraggable = createMemo(() => state.active.draggableId === id);
const transform = createMemo(() => {
return state.draggables[id]?.transform || noopTransform();
};
});

const draggable = Object.defineProperties(
(element: HTMLElement, accessor?: () => { skipTransform?: boolean }) => {
Expand Down Expand Up @@ -96,9 +97,7 @@ const createDraggable = (id: Id, data: Record<string, any> = {}): Draggable => {
},
dragActivators: {
enumerable: true,
get: () => {
return draggableActivators(id, true);
},
get: createMemo(() => draggableActivators(id, true)),
},
transform: {
enumerable: true,
Expand Down
7 changes: 4 additions & 3 deletions src/create-droppable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
createEffect,
createMemo,
createSignal,
onCleanup,
onMount,
Expand Down Expand Up @@ -40,10 +41,10 @@ const createDroppable = (id: Id, data: Record<string, any> = {}): Droppable => {
});
onCleanup(() => removeDroppable(id));

const isActiveDroppable = () => state.active.droppableId === id;
const transform = () => {
const isActiveDroppable = createMemo(() => state.active.droppableId === id);
const transform = createMemo(() => {
return state.droppables[id]?.transform || noopTransform();
};
});
const droppable = Object.defineProperties(
(element: HTMLElement, accessor?: () => { skipTransform?: boolean }) => {
const config = accessor ? accessor() : {};
Expand Down
4 changes: 2 additions & 2 deletions src/create-pointer-sensor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onCleanup, onMount } from "solid-js";
import { createMemo, onCleanup, onMount } from "solid-js";

import {
Coordinates,
Expand Down Expand Up @@ -32,7 +32,7 @@ const createPointerSensor = (id: Id = "pointer-sensor"): void => {
removeSensor(id);
});

const isActiveSensor = () => state.active.sensorId === id;
const isActiveSensor = createMemo(() => state.active.sensorId === id);

const initialCoordinates: Coordinates = { x: 0, y: 0 };

Expand Down
18 changes: 11 additions & 7 deletions src/create-sortable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEffect, onCleanup, onMount } from "solid-js";
import { createEffect, createMemo, onCleanup, onMount } from "solid-js";

import { createDraggable } from "./create-draggable";
import { createDroppable } from "./create-droppable";
Expand Down Expand Up @@ -30,12 +30,16 @@ const createSortable = (id: Id, data: Record<string, any> = {}): Sortable => {
const droppable = createDroppable(id, data);
const setNode = combineRefs(draggable.ref, droppable.ref);

const initialIndex = (): number => sortableState.initialIds.indexOf(id);
const currentIndex = (): number => sortableState.sortedIds.indexOf(id);
const initialIndex = createMemo((): number =>
sortableState.initialIds.indexOf(id)
);
const currentIndex = createMemo((): number =>
sortableState.sortedIds.indexOf(id)
);
const layoutById = (id: Id): Layout | null =>
dndState.droppables[id]?.layout || null;

const sortedTransform = (): Transform => {
const sortedTransform = createMemo((): Transform => {
const delta = noopTransform();
const resolvedInitialIndex = initialIndex();
const resolvedCurrentIndex = currentIndex();
Expand All @@ -53,7 +57,7 @@ const createSortable = (id: Id, data: Record<string, any> = {}): Sortable => {
}

return delta;
};
});

const transformer: Transformer = {
id: "sortableOffset",
Expand All @@ -67,13 +71,13 @@ const createSortable = (id: Id, data: Record<string, any> = {}): Sortable => {
onMount(() => addTransformer("droppables", id, transformer));
onCleanup(() => removeTransformer("droppables", id, transformer.id));

const transform = (): Transform => {
const transform = createMemo((): Transform => {
return (
(id === dndState.active.draggableId && !dndState.active.overlay
? dndState.draggables[id]?.transform
: dndState.droppables[id]?.transform) || noopTransform()
);
};
});

const sortable = Object.defineProperties(
(element: HTMLElement) => {
Expand Down
7 changes: 3 additions & 4 deletions src/drag-overlay.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Portal } from "solid-js/web";
import { Component, JSX, Show } from "solid-js";
import { Component, JSX, Show, createMemo } from "solid-js";

import { Draggable, useDragDropContext } from "./drag-drop-context";
import { transformStyle } from "./style";
Expand Down Expand Up @@ -39,7 +39,7 @@ const DragOverlay: Component<DragOverlayProps> = (props) => {

onDragEnd(() => queueMicrotask(clearOverlay));

const style = (): JSX.CSSProperties => {
const style = createMemo((): JSX.CSSProperties => {
const overlay = state.active.overlay;
const draggable = state.active.draggable;
if (!overlay || !draggable) return {};
Expand All @@ -54,7 +54,7 @@ const DragOverlay: Component<DragOverlayProps> = (props) => {
...transformStyle(overlay.transform),
...props.style,
};
};
});

return (
<Portal mount={document.body}>
Expand All @@ -70,4 +70,3 @@ const DragOverlay: Component<DragOverlayProps> = (props) => {
};

export { DragOverlay };