Skip to content

Commit

Permalink
chore: custom snap modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
henrynoowah committed Jan 8, 2025
1 parent 62895f1 commit b51a890
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CSSProperties } from "react";

interface InputSizeProps {
value:
| string
| number
| undefined
| CSSProperties["height"]
| CSSProperties["width"];
onChange: (value: string | number | undefined) => void;
}

const InputSize = ({ value, onChange }: InputSizeProps) => {
return (
<input
type="text"
value={value ? parseInt(value.toString().replace(/[^0-9]/g, "")) : ""}
className="nwcb-flex-1 nwcb-rounded nwcb-border nwcb-px-2 nwcb-py-1"
onChange={(e) => {
const value = e.target.value;
if (!value) {
onChange?.(undefined);
return;
}
const unit =
typeof value === "string" && value.includes("%") ? "%" : "px";
onChange?.(`${value}${unit}`);
}}
/>
);
};

export default InputSize;
63 changes: 53 additions & 10 deletions packages/ui/src/components/editor/section-editor/SectionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DragEndEvent,
DragOverlay,
DragStartEvent,
Modifier,
PointerSensor,
rectIntersection,
UniqueIdentifier,
Expand All @@ -16,13 +17,7 @@ import {
// SortableContext,
useSortable,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import {
Block,
Page,
Section,
SectionEditor as SectionEditorContext,
} from "../../../types";
import { CSS, getEventCoordinates } from "@dnd-kit/utilities";
import React, {
forwardRef,
Fragment,
Expand All @@ -33,6 +28,12 @@ import React, {
} from "react";
import { useFieldArray, useFormContext } from "react-hook-form";
import { convertJSONToCSS } from "../../../lib";
import {
Block,
Page,
Section,
SectionEditor as SectionEditorContext,
} from "../../../types";

// import { snapCenterToCursor } from "@dnd-kit/modifiers";

Expand All @@ -59,13 +60,14 @@ const SectionEditor = forwardRef<SectionEditorContext, SectionEditorProps>(
// active,
setNodeRef,
transition,
transform,
} = useSortable({
id: section.id,
});

const style = {
transition,
// transform: CSS.Translate.toString(transform),
transform: CSS.Translate.toString(transform),
opacity: isDragging ? 0.4 : 1,
};

Expand Down Expand Up @@ -149,6 +151,32 @@ const SectionEditor = forwardRef<SectionEditorContext, SectionEditorProps>(
return rectIntersection(updated);
};
const sectionRef = useRef<HTMLDivElement | null>(null);

const customSnapModifier: Modifier = ({
activatorEvent,
draggingNodeRect,
transform,
}) => {
if (draggingNodeRect && activatorEvent) {
const activatorCoordinates = getEventCoordinates(activatorEvent);

if (!activatorCoordinates) {
return transform;
}

const initialOffsetX = activatorCoordinates.x - draggingNodeRect.left;
const initialOffsetY = activatorCoordinates.y - draggingNodeRect.top;

return {
...transform,
x: transform.x + initialOffsetX,
y: transform.y + initialOffsetY,
};
}

return transform;
};

return (
<div
ref={(ref) => {
Expand Down Expand Up @@ -179,6 +207,15 @@ const SectionEditor = forwardRef<SectionEditorContext, SectionEditorProps>(
collisionDetection={fixCursorSnapOffset}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
// measuring={{
// draggable: {
// measure: (node) => {
// console.log("the measured node: ", node);
// console.log("children: ", node.children);
// return node.getBoundingClientRect();
// },
// },
// }}
>
{blocks?.map((block, i) => {
const css = `
Expand Down Expand Up @@ -240,12 +277,18 @@ const SectionEditor = forwardRef<SectionEditorContext, SectionEditorProps>(
);
})}

<DragOverlay>
<DragOverlay
modifiers={[customSnapModifier]}
// dropAnimation={{
// duration: 1000,
// easing: "cubic-bezier(0.18, 0.67, 0.6, 1.22)",
// }}
>
{activeId ? (
<div
id={`${activeId}`}
style={{ opacity: 0.4 }}
className="nwcb-ring-2 nwcb-ring-blue-400"
className="nwcb-ring-2 nwcb-ring-blue-400 nwcb-flex nwcb-items-center nwcb-justify-center"
>
<BlockEditor
section={section}
Expand Down

0 comments on commit b51a890

Please sign in to comment.