Skip to content

Commit 1e2b5f3

Browse files
only21milcodex
andcommitted
fix(web): preserve typed dimensions with the aspect ratio locked
Keep the edited dimension as a draft while deriving the other axis. Normalize the completed size through the existing viewport clamp before saving or rotating it. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent a8bb769 commit 1e2b5f3

2 files changed

Lines changed: 96 additions & 9 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import type { PreviewViewportSetting } from "@t3tools/contracts";
2+
import { useState } from "react";
3+
import { expect, it } from "vite-plus/test";
4+
import { userEvent } from "vite-plus/test/browser";
5+
6+
import { render } from "../../test/render";
7+
import { BrowserDeviceToolbar } from "./BrowserDeviceToolbar";
8+
9+
function Viewport({ width = 1920, height = 1080 }: { width?: number; height?: number }) {
10+
const [setting, setSetting] = useState<PreviewViewportSetting>({
11+
_tag: "freeform",
12+
width,
13+
height,
14+
});
15+
const [aspectRatio, setAspectRatio] = useState<number | null>(width / height);
16+
if (setting._tag === "fill") throw new Error("Expected fixed viewport");
17+
return (
18+
<>
19+
<BrowserDeviceToolbar
20+
setting={setting}
21+
width={800}
22+
aspectRatio={aspectRatio}
23+
onAspectRatioChange={setAspectRatio}
24+
onChange={async (next) => setSetting(next)}
25+
/>
26+
<output aria-label="Saved viewport">
27+
{setting.width} × {setting.height}
28+
</output>
29+
<button type="button">Leave toolbar</button>
30+
</>
31+
);
32+
}
33+
34+
it.each([
35+
{ width: 1920, height: 1080, axis: "width", typed: "2560", expected: "2560 × 1440" },
36+
{ width: 390, height: 844, axis: "height", typed: "2532", expected: "1170 × 2532" },
37+
])("keeps each typed $axis digit until the locked size is committed", async (testCase) => {
38+
await using screen = await render(<Viewport {...testCase} />);
39+
const input = screen.getByRole("spinbutton", { name: `Viewport ${testCase.axis}` });
40+
await userEvent.clear(input);
41+
await userEvent.type(input, testCase.typed);
42+
await expect.element(input).toHaveValue(Number(testCase.typed));
43+
await screen.getByRole("button", { name: "Leave toolbar" }).click();
44+
await expect
45+
.element(screen.getByRole("status", { name: "Saved viewport" }))
46+
.toHaveTextContent(testCase.expected);
47+
});
48+
49+
it("clamps a completed width below the locked minimum when leaving the toolbar", async () => {
50+
await using screen = await render(<Viewport />);
51+
const input = screen.getByRole("spinbutton", { name: "Viewport width" });
52+
await userEvent.clear(input);
53+
await userEvent.type(input, "256");
54+
await expect.element(input).toHaveValue(256);
55+
await screen.getByRole("button", { name: "Leave toolbar" }).click();
56+
await expect
57+
.element(screen.getByRole("status", { name: "Saved viewport" }))
58+
.toHaveTextContent("427 × 240");
59+
});
60+
61+
it("still limits a completed locked size to the viewport area cap", async () => {
62+
await using screen = await render(<Viewport width={1920} height={1920} />);
63+
const input = screen.getByRole("spinbutton", { name: "Viewport width" });
64+
await userEvent.clear(input);
65+
await userEvent.type(input, "3840");
66+
await expect.element(input).toHaveValue(3840);
67+
await screen.getByRole("button", { name: "Leave toolbar" }).click();
68+
await expect
69+
.element(screen.getByRole("status", { name: "Saved viewport" }))
70+
.toHaveTextContent("2880 × 2880");
71+
});

‎apps/web/src/browser/BrowserDeviceToolbar.tsx‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function BrowserDeviceToolbar({
5151
const [customSize, setCustomSize] = useState<{
5252
readonly width: string;
5353
readonly height: string;
54+
readonly axis?: "width" | "height";
5455
} | null>(null);
5556
const presentedSize = customSize ?? {
5657
width: String(setting.width),
@@ -61,15 +62,25 @@ export function BrowserDeviceToolbar({
6162
PREVIEW_VIEWPORT_PRESETS.some((preset) => preset.id === setting.presetId)
6263
? setting.presetId
6364
: RESPONSIVE_VALUE;
64-
const customWidth = Number(presentedSize.width);
65-
const customHeight = Number(presentedSize.height);
65+
const typedWidth = Number(presentedSize.width);
66+
const typedHeight = Number(presentedSize.height);
67+
const { width: customWidth, height: customHeight } =
68+
aspectRatio === null || customSize?.axis === undefined
69+
? { width: typedWidth, height: typedHeight }
70+
: resizeFreeformViewport(
71+
{ width: typedWidth, height: typedHeight },
72+
{ x: 0, y: 0 },
73+
1,
74+
customSize?.axis === "height" ? "south" : "east",
75+
aspectRatio,
76+
);
6677
const customValid =
67-
Number.isInteger(customWidth) &&
68-
Number.isInteger(customHeight) &&
69-
customWidth >= PREVIEW_VIEWPORT_MIN_DIMENSION &&
70-
customWidth <= PREVIEW_VIEWPORT_MAX_DIMENSION &&
71-
customHeight >= PREVIEW_VIEWPORT_MIN_DIMENSION &&
72-
customHeight <= PREVIEW_VIEWPORT_MAX_DIMENSION &&
78+
Number.isInteger(typedWidth) &&
79+
Number.isInteger(typedHeight) &&
80+
typedWidth >= PREVIEW_VIEWPORT_MIN_DIMENSION &&
81+
typedWidth <= PREVIEW_VIEWPORT_MAX_DIMENSION &&
82+
typedHeight >= PREVIEW_VIEWPORT_MIN_DIMENSION &&
83+
typedHeight <= PREVIEW_VIEWPORT_MAX_DIMENSION &&
7384
customWidth * customHeight <= PREVIEW_VIEWPORT_MAX_AREA;
7485

7586
const apply = (next: PreviewViewportSetting, nextAspectRatio = aspectRatio) => {
@@ -96,6 +107,7 @@ export function BrowserDeviceToolbar({
96107
const next = {
97108
width: axis === "width" ? value : (current?.width ?? String(setting.width)),
98109
height: axis === "height" ? value : (current?.height ?? String(setting.height)),
110+
axis,
99111
};
100112
const numeric = Number(value);
101113
if (
@@ -115,7 +127,11 @@ export function BrowserDeviceToolbar({
115127
axis === "width" ? "east" : "south",
116128
aspectRatio,
117129
);
118-
return { width: String(resized.width), height: String(resized.height) };
130+
return {
131+
...next,
132+
width: axis === "width" ? value : String(resized.width),
133+
height: axis === "height" ? value : String(resized.height),
134+
};
119135
});
120136
};
121137

0 commit comments

Comments
 (0)