Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address dnd issues in array fields #864

Merged
merged 7 commits into from
Jan 30, 2025
Merged
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
1 change: 0 additions & 1 deletion apps/demo/config/blocks/Hero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export type HeroProps = {
label: string;
href: string;
variant?: "primary" | "secondary";
more?: { text: string }[];
}[];
};

Expand Down
341 changes: 180 additions & 161 deletions packages/core/components/AutoField/fields/ArrayField/index.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
transition: none;
}

.ArrayField-inner {
margin-top: -1px;
}

/**
* ArrayFieldItem
*/
Expand All @@ -67,15 +71,12 @@
position: relative;
}

/* Pseudo-border so we can remove border whilst dragging. Last item will be 1px larger, but it's imperceptible. */
.ArrayFieldItem:not(.ArrayFieldItem--isDragging):not(:first-of-type)::before {
background-color: var(--puck-color-grey-09);
position: absolute;
width: 100%;
height: 1px;
content: "";
z-index: 1;
top: -1px;
.ArrayFieldItem {
border-top: 1px solid var(--puck-color-grey-09);
}

.ArrayFieldItem--isDragging {
border-top: transparent;
}

.ArrayFieldItem--isExpanded::before {
Expand All @@ -90,8 +91,7 @@
}

.ArrayFieldItem--isDragging {
box-shadow: color-mix(in srgb, var(--puck-color-grey-06) 25%, transparent) 0
3px 6px 0;
outline: 2px var(--puck-color-azure-09) solid !important;
}

.ArrayFieldItem--isDragging .ArrayFieldItem-summary:active {
Expand All @@ -114,7 +114,6 @@
font-size: var(--puck-font-size-xxs);
list-style: none;
padding: 12px 15px;
padding-bottom: 13px; /* Add one additional px to account for pseudo border */
position: relative;
overflow: hidden;
transition: background-color 50ms ease-in;
Expand All @@ -126,13 +125,16 @@
}

.ArrayField--addDisabled
> .ArrayField-inner
> .ArrayFieldItem:last-of-type:not(.ArrayFieldItem--isExpanded)
> .ArrayFieldItem-summary {
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
}

.ArrayField--addDisabled > .ArrayFieldItem--isExpanded:last-of-type {
.ArrayField--addDisabled
> .ArrayField-inner
> .ArrayFieldItem--isExpanded:last-of-type {
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
}
Expand Down
30 changes: 2 additions & 28 deletions packages/core/components/DragDropContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ import { insertComponent } from "../../lib/insert-component";
import { useDebouncedCallback } from "use-debounce";
import { CollisionMap } from "../../lib/dnd/collision/dynamic";
import { ComponentDndData } from "../DraggableComponent";
import { isElement } from "@dnd-kit/dom/utilities";

import { PointerSensor } from "../../lib/dnd/PointerSensor";
import { collisionStore } from "../../lib/dnd/collision/dynamic/store";
import { generateId } from "../../lib/generate-id";
import { createStore } from "zustand";
import { getDeepDir } from "../../lib/get-deep-dir";
import { useSensors } from "../../lib/dnd/use-sensors";

const DEBUG = false;

Expand Down Expand Up @@ -272,32 +271,7 @@ const DragDropContextClient = ({
),
]);

const [sensors] = useState(() => [
PointerSensor.configure({
activationConstraints(event, source) {
const { pointerType, target } = event;

if (
pointerType === "mouse" &&
isElement(target) &&
(source.handle === target || source.handle?.contains(target))
) {
return undefined;
}

const delay = { value: 200, tolerance: 10 };

if (pointerType === "touch") {
return { delay };
}

return {
delay,
distance: { value: 5 },
};
},
}),
]);
const sensors = useSensors();

const [dragListeners, setDragListeners] = useState<DragCbs>({});

Expand Down
31 changes: 19 additions & 12 deletions packages/core/components/ExternalInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,28 @@ export const ExternalInput = ({
Object.keys(filterFields).map((fieldName) => {
const filterField = filterFields[fieldName];
return (
<AutoFieldPrivate
<div
className={getClassNameModal("field")}
key={fieldName}
field={filterField}
name={fieldName}
id={`external_field_${fieldName}_filter`}
label={filterField.label || fieldName}
value={filters[fieldName]}
onChange={(value) => {
const newFilters = { ...filters, [fieldName]: value };
>
<AutoFieldPrivate
field={filterField}
name={fieldName}
id={`external_field_${fieldName}_filter`}
label={filterField.label || fieldName}
value={filters[fieldName]}
onChange={(value) => {
const newFilters = {
...filters,
[fieldName]: value,
};

setFilters(newFilters);
setFilters(newFilters);

search(searchQuery, newFilters);
}}
/>
search(searchQuery, newFilters);
}}
/>
</div>
);
})}
</div>
Expand Down
7 changes: 7 additions & 0 deletions packages/core/components/ExternalInput/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,10 @@
font-size: 14px;
text-align: right;
}

.ExternalInputModal-field {
color: var(--puck-color-grey-04);
margin: 16px;
margin-bottom: 12px;
display: block;
}
72 changes: 58 additions & 14 deletions packages/core/components/Sortable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,81 @@
import { DragDropProvider } from "@dnd-kit/react";
import { PropsWithChildren, ReactNode, useState } from "react";
import { useSortableSafe } from "../../lib/dnd/dnd-kit/safe";
import { useSensors } from "../../lib/dnd/use-sensors";
import {
CollisionMap,
createDynamicCollisionDetector,
} from "../../lib/dnd/collision/dynamic";
import { RestrictToElement } from "@dnd-kit/dom/modifiers";

export const SortableProvider = ({
children,
onDragStart,
onDragEnd,
onMove,
}: PropsWithChildren<{
onDragStart: () => void;
onDragEnd: () => void;
onMove: (moveData: { source: number; target: number }) => void;
}>) => {
const [move, setMove] = useState({ source: -1, target: -1 });

const sensors = useSensors();

return (
<DragDropProvider
onDragOver={(event) => {
sensors={sensors}
modifiers={[
RestrictToElement.configure({
element() {
return document.querySelector("[data-dnd-container]");
},
}),
]}
onDragStart={onDragStart}
onDragOver={(event, manager) => {
const { operation } = event;
const { source, target } = operation;

if (!source || !target) return;

let sourceIndex = source.data.index;
let targetIndex = target.data.index;

if (operation.source && operation.target) {
const newMove = {
source: operation.source.data.index,
target: operation.target.data.index,
};
const collisionData = (
manager.dragOperation.data?.collisionMap as CollisionMap
)?.[target.id];

if (sourceIndex !== targetIndex && source.id !== target.id) {
const collisionPosition =
collisionData?.direction === "up" ? "before" : "after";

if (targetIndex >= sourceIndex) {
targetIndex = targetIndex - 1;
}

if (newMove.source !== newMove.target) {
setMove({
source: operation.source.data.index,
target: operation.target.data.index,
});
if (collisionPosition === "after") {
targetIndex = targetIndex + 1;
}

setMove({
source: sourceIndex,
target: targetIndex,
});
}
}}
onDragEnd={() => {
if (move.source !== -1 && move.target !== -1) {
onMove(move);
}
setTimeout(() => {
if (move.source !== -1 && move.target !== -1) {
// Delay until animation finished
// TODO use this in onDragOver instead of optimistic rendering once re-renders reduced to polish out edge cases
onMove(move);
}

onDragEnd();
}, 250);

setMove({ source: -1, target: -1 });
}}
>
{children}
Expand Down Expand Up @@ -62,6 +105,7 @@ export const Sortable = ({
index,
disabled,
data: { index },
collisionDetector: createDynamicCollisionDetector("y"),
});

return children({ status, ref: sortableRef });
Expand Down
34 changes: 34 additions & 0 deletions packages/core/lib/dnd/use-sensors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useState } from "react";
import { PointerSensor } from "./PointerSensor";
import { isElement } from "@dnd-kit/dom/utilities";

export const useSensors = () => {
const [sensors] = useState(() => [
PointerSensor.configure({
activationConstraints(event, source) {
const { pointerType, target } = event;

if (
pointerType === "mouse" &&
isElement(target) &&
(source.handle === target || source.handle?.contains(target))
) {
return undefined;
}

const delay = { value: 200, tolerance: 10 };

if (pointerType === "touch") {
return { delay };
}

return {
delay,
distance: { value: 5 },
};
},
}),
]);

return sensors;
};
Loading