Skip to content

Commit b47030c

Browse files
committed
feat: adjust zoom based on screen width
1 parent 0cd42ac commit b47030c

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

apps/dashboard/src/components/EditorToolbar.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ export function EditorToolbar() {
9999
</Card>
100100
<Card variant="classic">
101101
<Flex align="center" gap="4">
102-
<Tooltip content="Zoom Out">
102+
<Tooltip content="Zoom In">
103103
<IconButton
104104
color="gray"
105105
onClick={() => {
106-
zoomOut();
106+
zoomIn();
107107
}}
108108
variant="ghost"
109109
>
110-
<ZoomOutIcon height="1.4em" width="1.4em" />
110+
<ZoomInIcon height="1.4em" width="1.4em" />
111111
</IconButton>
112112
</Tooltip>
113113
<Separator orientation="vertical" />
@@ -116,15 +116,15 @@ export function EditorToolbar() {
116116
{zoom}%
117117
</Text>
118118
<Separator orientation="vertical" />
119-
<Tooltip content="Zoom In">
119+
<Tooltip content="Zoom Out">
120120
<IconButton
121121
color="gray"
122122
onClick={() => {
123-
zoomIn();
123+
zoomOut();
124124
}}
125125
variant="ghost"
126126
>
127-
<ZoomInIcon height="1.4em" width="1.4em" />
127+
<ZoomOutIcon height="1.4em" width="1.4em" />
128128
</IconButton>
129129
</Tooltip>
130130
</Flex>

apps/dashboard/src/components/OgEditor.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { useRouter } from "next/navigation";
55
import { toast } from "sonner";
66
import type { OGElement } from "../lib/types";
77
import { createDefaultElement, createElementId } from "../lib/elements";
8-
import { useZoomStore } from "../stores/zoomStore";
98
import { useElementsStore } from "../stores/elementsStore";
109
import { useImagesStore } from "../stores/imagesStore";
10+
import { useAdjustedZoom } from "../lib/hooks/useAdjustedZoom";
1111
import { Element } from "./Element";
1212
import { RightPanel } from "./RightPanel";
1313
import { LeftPanel } from "./LeftPanel";
@@ -25,7 +25,6 @@ let elementIdToCopy: string | undefined;
2525
export function OgEditor({ imageId, width, height }: OgProviderProps) {
2626
const router = useRouter();
2727
const rootRef = useRef<HTMLDivElement>(null);
28-
const zoom = useZoomStore((state) => state.zoom);
2928
const {
3029
selectedElementId,
3130
setSelectedElementId,
@@ -39,6 +38,7 @@ export function OgEditor({ imageId, width, height }: OgProviderProps) {
3938
const setSelectedImageId = useImagesStore(
4039
(state) => state.setSelectedImageId,
4140
);
41+
const adjustedZoom = useAdjustedZoom();
4242

4343
/**
4444
* When the editor image is updated or loaded for the first time, reset every
@@ -288,7 +288,7 @@ export function OgEditor({ imageId, width, height }: OgProviderProps) {
288288
style={{
289289
width,
290290
height,
291-
transform: `scale(${zoom / 100})`,
291+
transform: `scale(${adjustedZoom})`,
292292
boxShadow: "var(--shadow-3)",
293293
backgroundColor: "var(--gray-1)",
294294
}}
@@ -308,7 +308,7 @@ export function OgEditor({ imageId, width, height }: OgProviderProps) {
308308
style={{
309309
width,
310310
height,
311-
transform: `scale(${zoom / 100})`,
311+
transform: `scale(${adjustedZoom})`,
312312
border: "1px solid var(--gray-contrast)",
313313
}}
314314
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useEffect, useState } from "react";
2+
import { useZoomStore } from "../../stores/zoomStore";
3+
4+
export function useAdjustedZoom() {
5+
const zoom = useZoomStore((state) => state.zoom);
6+
const [zoomFactor, setZoomFactor] = useState(1);
7+
8+
useEffect(() => {
9+
function onResize() {
10+
const screenWidth = window.innerWidth;
11+
const editorWidth = 1200;
12+
const editorWidthWithPanels = editorWidth + 300 * 2;
13+
14+
if (screenWidth < editorWidthWithPanels) {
15+
setZoomFactor(screenWidth / (editorWidthWithPanels + 200));
16+
} else {
17+
setZoomFactor(1);
18+
}
19+
}
20+
21+
onResize();
22+
window.addEventListener("resize", onResize);
23+
24+
return () => {
25+
window.removeEventListener("resize", onResize);
26+
};
27+
}, []);
28+
29+
return (zoom / 100) * zoomFactor;
30+
}

0 commit comments

Comments
 (0)