Skip to content

Commit 3e4ccd9

Browse files
fix(studio): a corner drag on a small element resizes it instead of starting a crop (#4730)
* fix(studio): a corner drag on a small element resizes it instead of starting a crop * refactor(studio): one crop gesture ending, edge layout in one helper
1 parent ac754b2 commit 3e4ccd9

4 files changed

Lines changed: 158 additions & 68 deletions

File tree

‎packages/studio/src/components/editor/DomEditCropHandles.test.tsx‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function makeEl(id: string, clip: string): HTMLElement {
3636
function render(
3737
el: HTMLElement,
3838
onStyleCommit: (property: string, value: string) => Promise<unknown> | void = () => undefined,
39+
rect: OverlayRect = overlayRect,
3940
): { root: Root; rerender: (next: HTMLElement) => void } {
4041
const host = document.createElement("div");
4142
document.body.append(host);
@@ -45,7 +46,7 @@ function render(
4546
root.render(
4647
<DomEditCropHandles
4748
selection={selectionFor(target)}
48-
overlayRect={overlayRect}
49+
overlayRect={rect}
4950
onStyleCommit={onStyleCommit}
5051
/>,
5152
);
@@ -155,3 +156,88 @@ describe("DomEditCropHandles clip lift/restore", () => {
155156
act(() => root.unmount());
156157
});
157158
});
159+
160+
describe("DomEditCropHandles leaves the corner resize dots free", () => {
161+
type Box = { left: number; top: number; width: number; height: number };
162+
const HIT = 16;
163+
const cornerSquares = (r: Box): Box[] =>
164+
[
165+
[r.left, r.top],
166+
[r.left + r.width, r.top],
167+
[r.left, r.top + r.height],
168+
[r.left + r.width, r.top + r.height],
169+
].map(([x, y]) => ({ left: x - HIT / 2, top: y - HIT / 2, width: HIT, height: HIT }));
170+
const overlaps = (a: Box, b: Box) =>
171+
a.left < b.left + b.width &&
172+
b.left < a.left + a.width &&
173+
a.top < b.top + b.height &&
174+
b.top < a.top + a.height;
175+
const handles = () =>
176+
[...document.querySelectorAll<HTMLElement>("[data-dom-edit-crop-handle]")].map((el) => ({
177+
label: el.getAttribute("aria-label"),
178+
box: {
179+
left: parseFloat(el.style.left),
180+
top: parseFloat(el.style.top),
181+
width: parseFloat(el.style.width),
182+
height: parseFloat(el.style.height),
183+
},
184+
}));
185+
const rectOf = (width: number, height: number): OverlayRect => ({
186+
left: 0,
187+
top: 0,
188+
width,
189+
height,
190+
editScaleX: 1,
191+
editScaleY: 1,
192+
});
193+
194+
it.each([
195+
{
196+
name: "29x14",
197+
size: [29, 14],
198+
clip: "",
199+
cropped: { left: 0, top: 0, width: 29, height: 14 },
200+
},
201+
{
202+
name: "40x40",
203+
size: [40, 40],
204+
clip: "",
205+
cropped: { left: 0, top: 0, width: 40, height: 40 },
206+
},
207+
{
208+
name: "29x14",
209+
size: [29, 14],
210+
clip: "inset(2px)",
211+
cropped: { left: 2, top: 2, width: 25, height: 10 },
212+
},
213+
{
214+
name: "200x100",
215+
size: [200, 100],
216+
clip: "inset(10px)",
217+
cropped: { left: 10, top: 10, width: 180, height: 80 },
218+
},
219+
])("no crop handle covers a corner of a $name box clipped '$clip'", (c) => {
220+
render(makeEl("a", c.clip), undefined, rectOf(c.size[0], c.size[1]));
221+
const found = handles();
222+
expect(found.length).toBeGreaterThan(0);
223+
for (const handle of found) {
224+
for (const corner of cornerSquares(c.cropped)) {
225+
expect(overlaps(handle.box, corner), `${handle.label} covers a corner`).toBe(false);
226+
}
227+
}
228+
});
229+
230+
it("keeps every handle where the edges have room, and drops only those with none", () => {
231+
render(makeEl("a", "inset(10px)"), undefined, rectOf(200, 100));
232+
expect(handles().map((h) => h.label)).toEqual([
233+
"Reposition crop",
234+
"Crop top",
235+
"Crop right",
236+
"Crop bottom",
237+
"Crop left",
238+
]);
239+
document.body.innerHTML = "";
240+
render(makeEl("b", ""), undefined, rectOf(29, 14));
241+
expect(handles().map((h) => h.label)).toEqual(["Crop top", "Crop bottom"]);
242+
});
243+
});

‎packages/studio/src/components/editor/DomEditCropHandles.tsx‎

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
22
import type { DomEditSelection } from "./domEditing";
3-
import type { OverlayRect } from "./domEditOverlayGeometry";
3+
import { type OverlayRect, RESIZE_HANDLE_HIT_PX } from "./domEditOverlayGeometry";
44
import {
55
type CropEdge,
66
cropRectFromInsets,
@@ -37,47 +37,53 @@ interface DomEditCropHandlesProps {
3737
// and centered inside the strip.
3838
const EDGE_HIT_THICKNESS = 12;
3939
const EDGE_HIT_LENGTH = 32;
40+
const EDGE_PILL_LENGTH = 24;
41+
const REPOSITION_HANDLE_PX = 22;
4042

41-
/** Place an edge handle's hit strip just OUTSIDE the given crop edge
42-
* (translate pushes it fully past the boundary). Keeps the element body free
43-
* for moving. Corners stay free for the selection's own resize handles. */
44-
function edgeHandlePlacement(
45-
edge: CropEdge,
46-
rect: { left: number; top: number; width: number; height: number },
47-
) {
48-
const cx = rect.left + rect.width / 2;
49-
const cy = rect.top + rect.height / 2;
50-
if (edge === "top") {
51-
return { left: cx, top: rect.top, transform: "translate(-50%, -100%)" };
52-
}
53-
if (edge === "bottom") {
54-
return { left: cx, top: rect.top + rect.height, transform: "translate(-50%, 0)" };
55-
}
56-
if (edge === "left") {
57-
return { left: rect.left, top: cy, transform: "translate(-100%, -50%)" };
58-
}
59-
return { left: rect.left + rect.width, top: cy, transform: "translate(0, -50%)" };
60-
}
43+
type Rect = { left: number; top: number; width: number; height: number };
6144

62-
const EDGES: CropEdge[] = ["top", "right", "bottom", "left"];
63-
64-
/** Hit-strip + pill dimensions for an edge handle, keyed on its orientation. */
65-
function edgeHandleMetrics(vertical: boolean): {
66-
hitWidth: number;
67-
hitHeight: number;
68-
cursor: string;
69-
pillWidth: number;
70-
pillHeight: number;
71-
} {
45+
/** An edge handle's hit strip, just OUTSIDE the crop edge so the body stays free
46+
* for moving, and short of the corner squares the resize dots own, with its
47+
* cursor and pill. Null when the edge is too short to leave any room. */
48+
function edgeHandleLayout(edge: CropEdge, rect: Rect) {
49+
const vertical = edge === "left" || edge === "right";
50+
const length = Math.min(
51+
EDGE_HIT_LENGTH,
52+
(vertical ? rect.height : rect.width) - RESIZE_HANDLE_HIT_PX,
53+
);
54+
if (length <= 0) return null;
55+
const pill = Math.min(EDGE_PILL_LENGTH, length);
56+
if (vertical) {
57+
return {
58+
hit: {
59+
left: edge === "left" ? rect.left - EDGE_HIT_THICKNESS : rect.left + rect.width,
60+
top: rect.top + (rect.height - length) / 2,
61+
width: EDGE_HIT_THICKNESS,
62+
height: length,
63+
},
64+
cursor: "ew-resize",
65+
pill: { width: 4, height: pill },
66+
};
67+
}
7268
return {
73-
hitWidth: vertical ? EDGE_HIT_THICKNESS : EDGE_HIT_LENGTH,
74-
hitHeight: vertical ? EDGE_HIT_LENGTH : EDGE_HIT_THICKNESS,
75-
cursor: vertical ? "ew-resize" : "ns-resize",
76-
pillWidth: vertical ? 4 : 24,
77-
pillHeight: vertical ? 24 : 4,
69+
hit: {
70+
left: rect.left + (rect.width - length) / 2,
71+
top: edge === "top" ? rect.top - EDGE_HIT_THICKNESS : rect.top + rect.height,
72+
width: length,
73+
height: EDGE_HIT_THICKNESS,
74+
},
75+
cursor: "ns-resize",
76+
pill: { width: pill, height: 4 },
7877
};
7978
}
8079

80+
/** The reposition handle's size: shrunk until its box clears the corner squares. */
81+
function repositionHandleSize(rect: Rect): number {
82+
return Math.min(REPOSITION_HANDLE_PX, Math.max(rect.width, rect.height) - RESIZE_HANDLE_HIT_PX);
83+
}
84+
85+
const EDGES: CropEdge[] = ["top", "right", "bottom", "left"];
86+
8187
/**
8288
* Always-on crop, integrated with the selection (no crop "mode"): while a
8389
* croppable element is selected its clip is lifted so the FULL content shows and
@@ -167,6 +173,7 @@ export function DomEditCropHandles({
167173
frame.scaleX,
168174
frame.scaleY,
169175
);
176+
const repositionSize = repositionHandleSize(cropRect);
170177

171178
const startCropGesture = (edge: CropEdge | "move", event: ReactPointerEvent<HTMLElement>) => {
172179
if (!onStyleCommit) return;
@@ -214,13 +221,19 @@ export function DomEditCropHandles({
214221
setState((prev) => ({ ...prev, insets: nextInsets }));
215222
};
216223

217-
const finishCropGesture = (event: ReactPointerEvent<HTMLElement>) => {
224+
const endCropGesture = (event: ReactPointerEvent<HTMLElement>) => {
218225
const gesture = gestureRef.current;
219-
if (!gesture || gesture.pointerId !== event.pointerId) return;
226+
if (!gesture || gesture.pointerId !== event.pointerId) return null;
220227
event.preventDefault();
221228
event.stopPropagation();
222229
gestureRef.current = null;
223230
setDragging(false);
231+
return gesture;
232+
};
233+
234+
const finishCropGesture = (event: ReactPointerEvent<HTMLElement>) => {
235+
const gesture = endCropGesture(event);
236+
if (!gesture) return;
224237
if (!gesture.didMove) return;
225238
// Commit to the file. The commit path re-applies the value to the live
226239
// element synchronously, so re-lift in the same turn to keep showing the full
@@ -250,12 +263,8 @@ export function DomEditCropHandles({
250263
};
251264

252265
const cancelCropGesture = (event: ReactPointerEvent<HTMLElement>) => {
253-
const gesture = gestureRef.current;
254-
if (!gesture || gesture.pointerId !== event.pointerId) return;
255-
event.preventDefault();
256-
event.stopPropagation();
257-
gestureRef.current = null;
258-
setDragging(false);
266+
const gesture = endCropGesture(event);
267+
if (!gesture) return;
259268
// Clip stays lifted; the dim follows the reset insets.
260269
setState((prev) => ({ ...prev, insets: gesture.startInsets }));
261270
};
@@ -317,19 +326,18 @@ export function DomEditCropHandles({
317326
{/* Reposition handle — a center circle shown only once cropped. Drag it to
318327
pan the crop window (which part of the element shows) without resizing
319328
the crop. It's a small, discrete target, so a body drag still MOVES. */}
320-
{hasCrop && (
329+
{hasCrop && repositionSize > 0 && (
321330
<button
322331
type="button"
323332
aria-label="Reposition crop"
324333
title="Reposition crop"
325334
data-dom-edit-crop-handle="true"
326335
className="pointer-events-auto absolute rounded-full border-2 border-studio-accent bg-studio-accent/30 shadow-[0_0_0_1px_rgba(0,0,0,0.4)]"
327336
style={{
328-
left: cropRect.left + cropRect.width / 2,
329-
top: cropRect.top + cropRect.height / 2,
330-
width: 22,
331-
height: 22,
332-
transform: "translate(-50%, -50%)",
337+
left: cropRect.left + (cropRect.width - repositionSize) / 2,
338+
top: cropRect.top + (cropRect.height - repositionSize) / 2,
339+
width: repositionSize,
340+
height: repositionSize,
333341
cursor: "move",
334342
touchAction: "none",
335343
}}
@@ -340,16 +348,15 @@ export function DomEditCropHandles({
340348
/>
341349
)}
342350
{/* Edge handles — drag a side to crop it. Positioned just OUTSIDE the crop
343-
edge (via edgeHandlePlacement) so they never overlap the element body:
351+
edge (via edgeHandleLayout) so they never overlap the element body:
344352
dragging the body always MOVES, only a handle crops. The pill is
345353
hover-revealed (or shown while dragging / once a crop exists) so the
346354
resting selection chrome stays uncluttered; the hit strip is always
347355
live, and the title names the affordance. */}
348356
{EDGES.map((edge) => {
349-
const vertical = edge === "left" || edge === "right";
350-
const place = edgeHandlePlacement(edge, cropRect);
357+
const layout = edgeHandleLayout(edge, cropRect);
358+
if (!layout) return null;
351359
const revealed = dragging || hasCrop || hotEdge === edge;
352-
const m = edgeHandleMetrics(vertical);
353360
return (
354361
<button
355362
key={edge}
@@ -359,12 +366,8 @@ export function DomEditCropHandles({
359366
data-dom-edit-crop-handle="true"
360367
className="pointer-events-auto absolute flex items-center justify-center border-0 bg-transparent p-0"
361368
style={{
362-
left: place.left,
363-
top: place.top,
364-
width: m.hitWidth,
365-
height: m.hitHeight,
366-
transform: place.transform,
367-
cursor: m.cursor,
369+
...layout.hit,
370+
cursor: layout.cursor,
368371
touchAction: "none",
369372
}}
370373
onPointerEnter={() => setHotEdge(edge)}
@@ -377,8 +380,7 @@ export function DomEditCropHandles({
377380
<span
378381
className="pointer-events-none rounded-full bg-studio-accent/90 shadow-[0_0_0_1px_rgba(0,0,0,0.4)] transition-opacity duration-100"
379382
style={{
380-
width: m.pillWidth,
381-
height: m.pillHeight,
383+
...layout.pill,
382384
opacity: revealed ? 1 : 0,
383385
}}
384386
/>

‎packages/studio/src/components/editor/DomEditSelectionChrome.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { RefObject } from "react";
22
import { type DomEditSelection } from "./domEditing";
3-
import type { GroupOverlayItem, OverlayRect } from "./domEditOverlayGeometry";
3+
import {
4+
type GroupOverlayItem,
5+
type OverlayRect,
6+
RESIZE_HANDLE_HIT_PX,
7+
} from "./domEditOverlayGeometry";
48
import type { ResizeHandle } from "./domEditOverlayGestures";
59
import type { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures";
610
import { DomEditCropHandles } from "./DomEditCropHandles";
@@ -23,10 +27,6 @@ const RESIZE_HANDLE_DEFS: Array<{
2327
{ handle: "se", cursor: "nwse-resize", x: "right", y: "bottom" },
2428
];
2529

26-
// Visible dot is 9px; the pointer target is a 16px invisible square centered
27-
// on the corner so click targets don't shrink with the smaller dot.
28-
const RESIZE_HANDLE_HIT_PX = 16;
29-
3030
type CropInset = { top: number; right: number; bottom: number; left: number };
3131
const NO_CROP_INSET: CropInset = { top: 0, right: 0, bottom: 0, left: 0 };
3232

‎packages/studio/src/components/editor/domEditOverlayGeometry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { hugRectForElement } from "./domEditOverlayCrop";
99
import { composeElementTransform, type PlanarTransformOps } from "./domEditOverlayTransform";
1010
import { type OverlayMeasurePass, readThroughPass } from "./domEditOverlayMeasurePass";
1111

12+
export const RESIZE_HANDLE_HIT_PX = 16;
13+
1214
export interface OverlayRect {
1315
left: number;
1416
top: number;

0 commit comments

Comments
 (0)