Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

scale feature

Oleg Isonen18fc8435e23b
diff --git a/app/designer/designer.tsx b/app/designer/designer.tsx
index c3e9f7f887..8b7dbb5824 100644
--- a/app/designer/designer.tsx
+++ b/app/designer/designer.tsx
@@ -13,6 +13,7 @@ import {
useBreakpoints,
useIsPreviewMode,
useRootInstance,
+ useScale,
useSelectedBreakpoint,
useSelectedInstanceData,
useSyncStatus,
@@ -23,6 +24,7 @@ import { Breadcrumbs } from "./features/breadcrumbs";
import { TreePrevew } from "./features/tree-preview";
import { usePublishShortcuts } from "./shared/shortcuts/use-publish-shortcuts";
import { type SyncStatus } from "~/shared/sync";
+import { useIframeWidth } from "./features/breakpoints/use-iframe-width";
export const links = () => {
return [
@@ -114,12 +116,44 @@ const Main = ({ children }: { children: Array<JSX.Element> }) => (
direction="column"
css={{
gridArea: "main",
+ overflow: "hidden",
}}
>
{children}
</Flex>
);
+const Workspace = ({ children }: { children: JSX.Element }) => {
+ const [scale] = useScale();
+
+ return (
+ <Box
+ css={{
+ flexGrow: 1,
+ background: "$gray6",
+ overflow: "hidden",
+ }}
+ >
+ <Flex
+ direction="column"
+ align="center"
+ css={{
+ overscrollBehavior: "contain",
+ transformStyle: "preserve-3d",
+ transition: "transform 50ms",
+ height: "100%",
+ width: "100%",
+ }}
+ style={{
+ transform: `scale(${scale / 100})`,
+ }}
+ >
+ {children}
+ </Flex>
+ </Box>
+ );
+};
+
type ChromeWrapperProps = {
children: Array<JSX.Element>;
isPreviewMode: boolean;
@@ -171,6 +205,7 @@ export const Designer = ({ config, project }: DesignerProps) => {
const [isPreviewMode] = useIsPreviewMode();
const [isDragging, setIsDragging] = useIsDragging();
usePublishShortcuts(publish);
+ const iframeWidth = useIframeWidth();
return (
<DndProvider backend={HTML5Backend}>
@@ -189,13 +224,15 @@ export const Designer = ({ config, project }: DesignerProps) => {
publish={publish}
/>
<Main>
- <CanvasIframe
- ref={iframeRef}
- src={`${config.canvasPath}/${project.id}`}
- pointerEvents={isDragging ? "none" : "all"}
- title={project.title}
- css={{ height: "100%" }}
- />
+ <Workspace>
+ <CanvasIframe
+ ref={iframeRef}
+ src={`${config.canvasPath}/${project.id}`}
+ pointerEvents={isDragging ? "none" : "all"}
+ title={project.title}
+ css={{ height: "100%", width: iframeWidth }}
+ />
+ </Workspace>
<Breadcrumbs publish={publish} />
</Main>
<SidePanel
diff --git a/app/designer/features/topbar/breakpoints/breakpoints-editor.tsx b/app/designer/features/breakpoints/breakpoints-editor.tsx
similarity index 92%
rename from app/designer/features/topbar/breakpoints/breakpoints-editor.tsx
rename to app/designer/features/breakpoints/breakpoints-editor.tsx
index cc2e3571b9..164dbcfdf3 100644
--- a/app/designer/features/topbar/breakpoints/breakpoints-editor.tsx
+++ b/app/designer/features/breakpoints/breakpoints-editor.tsx
@@ -10,12 +10,10 @@ type BreakpointEditorItemProps = {
breakpoint: Breakpoint;
onChange: (breakpoint: Breakpoint) => void;
onDelete: (breakpoint: Breakpoint) => void;
- onFocus: () => void;
};
const BreakpointEditorItem = ({
breakpoint: initialBreakpoint,
- onFocus,
onChange,
onDelete,
}: BreakpointEditorItemProps) => {
@@ -45,13 +43,11 @@ const BreakpointEditorItem = ({
const data = new FormData(form);
const nextBreakpoint: Breakpoint = {
...breakpoint,
- // @todo if label changed, we want to generate a ref name
label: String(data.get("label")),
minWidth: Number(data.get("minWidth")),
};
setBreakpoint(nextBreakpoint);
}}
- onFocus={onFocus}
>
<Flex gap="1" css={{ paddingLeft: "$4", paddingRight: "$3" }}>
<TextField
@@ -89,13 +85,11 @@ const BreakpointEditorItem = ({
type BreakpointsEditorProps = {
breakpoints: Array<Breakpoint>;
- onSelect: (breakpoint: Breakpoint) => void;
publish: Publish;
};
export const BreakpointsEditor = ({
breakpoints: initialBreakpoints,
- onSelect,
publish,
}: BreakpointsEditorProps) => {
const [breakpoints, setBreakpoints] = useState(sort(initialBreakpoints));
@@ -134,11 +128,7 @@ export const BreakpointsEditor = ({
<BreakpointEditorItem
key={breakpoint.id}
breakpoint={breakpoint}
- onFocus={() => {
- onSelect(breakpoint);
- }}
onChange={(breakpoint) => {
- onSelect(breakpoint);
publish({ type: "breakpointChange", payload: breakpoint });
}}
onDelete={(breakpoint) => {
diff --git a/app/designer/features/topbar/breakpoints/breakpoints.tsx b/app/designer/features/breakpoints/breakpoints.tsx
similarity index 70%
rename from app/designer/features/topbar/breakpoints/breakpoints.tsx
rename to app/designer/features/breakpoints/breakpoints.tsx
index 1fbae901fc..1b864a8aac 100644
--- a/app/designer/features/topbar/breakpoints/breakpoints.tsx
+++ b/app/designer/features/breakpoints/breakpoints.tsx
@@ -7,31 +7,18 @@ import {
DropdownMenuTrigger,
DropdownMenuArrow,
DropdownMenuSeparator,
- Button,
Text,
- Paragraph,
- Box,
Flex,
} from "~/shared/design-system";
-import { type Publish } from "../../../shared/canvas-iframe";
+import { type Publish } from "../../shared/canvas-iframe";
import {
useBreakpoints,
useSelectedBreakpoint,
-} from "../../../shared/nano-values";
+} from "../../shared/nano-values";
import { BreakpointsEditor } from "./breakpoints-editor";
-
-const Hint = ({ breakpoint }: { breakpoint?: Breakpoint }) => {
- return (
- <Box css={{ px: "$3" }}>
- <Paragraph css={{ fontSize: "$1" }}>CSS Preview:</Paragraph>
- <Paragraph css={{ fontSize: "$1" }} variant="gray">
- {breakpoint === undefined
- ? "No breakpoint selected"
- : `@media (min-width: ${breakpoint.minWidth}px)`}
- </Paragraph>
- </Box>
- );
-};
+import { Hint } from "./hint";
+import { Scale } from "./scale";
+import { TriggerButton } from "./trigger-button";
const BreakpointSelectorItem = ({ breakpoint }: { breakpoint: Breakpoint }) => (
<Flex align="center" justify="between" gap="3" css={{ flexGrow: 1 }}>
@@ -47,7 +34,7 @@ const menuItemCss = {
gap: "$3",
justifyContent: "start",
flexGrow: 1,
- minWidth: 150,
+ minWidth: 180,
};
type BreakpointsProps = {
@@ -65,22 +52,11 @@ export const Breakpoints = ({ publish }: BreakpointsProps) => {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
- <Button css={{ gap: "$1" }} ghost aria-label="Breakpoints">
- <Text size="1">{selectedBreakpoint.label}</Text>
- {selectedBreakpoint.minWidth > 0 && (
- <Text size="1" variant="gray">
- {selectedBreakpoint.minWidth}
- </Text>
- )}
- </Button>
+ <TriggerButton breakpoint={selectedBreakpoint} />
</DropdownMenuTrigger>
<DropdownMenuContent>
{isEditing ? (
- <BreakpointsEditor
- breakpoints={breakpoints}
- onSelect={setBreakpointHint}
- publish={publish}
- />
+ <BreakpointsEditor breakpoints={breakpoints} publish={publish} />
) : (
sort(breakpoints).map((breakpoint) => {
return (
@@ -106,8 +82,14 @@ export const Breakpoints = ({ publish }: BreakpointsProps) => {
);
})
)}
- <DropdownMenuSeparator />
- <Hint breakpoint={breakpointHint} />
+ {isEditing === false && (
+ <>
+ <DropdownMenuSeparator />
+ <Scale />
+ <DropdownMenuSeparator />
+ <Hint breakpoint={breakpointHint} />
+ </>
+ )}
<DropdownMenuSeparator />
<DropdownMenuItem
css={{ justifyContent: "center" }}
diff --git a/app/designer/features/breakpoints/hint.tsx b/app/designer/features/breakpoints/hint.tsx
new file mode 100644
index 0000000000..4d444c989a
--- /dev/null
+++ b/app/designer/features/breakpoints/hint.tsx
@@ -0,0 +1,19 @@
+import { type Breakpoint } from "@webstudio-is/sdk";
+import { Paragraph, Flex, Text } from "~/shared/design-system";
+
+type HintProps = {
+ breakpoint?: Breakpoint;
+};
+
+export const Hint = ({ breakpoint }: HintProps) => {
+ return (
+ <Flex css={{ px: "$5" }} gap="1" direction="column">
+ <Text size="1">CSS Preview:</Text>
+ <Paragraph css={{ fontSize: "$1" }} variant="gray">
+ {breakpoint === undefined
+ ? "No breakpoint selected"
+ : `@media (min-width: ${breakpoint.minWidth}px)`}
+ </Paragraph>
+ </Flex>
+ );
+};
diff --git a/app/designer/features/topbar/breakpoints/index.ts b/app/designer/features/breakpoints/index.ts
similarity index 100%
rename from app/designer/features/topbar/breakpoints/index.ts
rename to app/designer/features/breakpoints/index.ts
diff --git a/app/designer/features/breakpoints/scale.tsx b/app/designer/features/breakpoints/scale.tsx
new file mode 100644
index 0000000000..ba5e7d83f4
--- /dev/null
+++ b/app/designer/features/breakpoints/scale.tsx
@@ -0,0 +1,20 @@
+import { useScale } from "~/designer/shared/nano-values";
+import { Slider, Text, Flex } from "~/shared/design-system";
+
+export const Scale = () => {
+ const [value, setValue] = useScale();
+ return (
+ <Flex css={{ px: "$5" }} gap="1" direction="column">
+ <Text size="1">Scale:</Text>
+ <Flex gap="3" align="center">
+ <Slider
+ defaultValue={[value]}
+ onValueChange={([value]) => {
+ setValue(value);
+ }}
+ />
+ <Text size="1">{value}%</Text>
+ </Flex>
+ </Flex>
+ );
+};
diff --git a/app/designer/features/breakpoints/trigger-button.tsx b/app/designer/features/breakpoints/trigger-button.tsx
new file mode 100644
index 0000000000..1e508078a4
--- /dev/null
+++ b/app/designer/features/breakpoints/trigger-button.tsx
@@ -0,0 +1,45 @@
+import { forwardRef, type ComponentProps, type ElementRef } from "react";
+import { type Breakpoint } from "@webstudio-is/sdk";
+import { useScale } from "~/designer/shared/nano-values";
+import { Button, Text } from "~/shared/design-system";
+
+const buildStatus = (breakpoint: Breakpoint, scale: number) => {
+ const status = [];
+ if (breakpoint.minWidth > 0) status.push(`${breakpoint.minWidth}px`);
+ if (scale < 100) {
+ if (status.length !== 0) status.push("/");
+ status.push(`${scale}%`);
+ }
+ return status.join(" ");
+};
+
+type TriggerButtonProps = {
+ breakpoint: Breakpoint;
+} & ComponentProps<typeof Button>;
+
+export const TriggerButton = forwardRef<
+ ElementRef<typeof Button>,
+ TriggerButtonProps
+>(({ breakpoint, ...props }, ref) => {
+ const [scale] = useScale();
+ const status = buildStatus(breakpoint, scale);
+ return (
+ <Button
+ {...props}
+ ref={ref}
+ css={{ gap: "$1" }}
+ ghost
+ aria-label="Show breakpoints"
+ >
+ <Text size="1">{breakpoint.label}</Text>
+
+ {status.length !== 0 && (
+ <Text size="1" variant="gray">
+ {status}
+ </Text>
+ )}
+ </Button>
+ );
+});
+
+TriggerButton.displayName = "TriggerButton";
diff --git a/app/designer/features/breakpoints/use-iframe-width.ts b/app/designer/features/breakpoints/use-iframe-width.ts
new file mode 100644
index 0000000000..6bfc3ad711
--- /dev/null
+++ b/app/designer/features/breakpoints/use-iframe-width.ts
@@ -0,0 +1,13 @@
+import { useSelectedBreakpoint } from "~/designer/shared/nano-values";
+
+// Doesn't make sense to allow resizing the canvas lower than this.
+const minWidth = 200;
+
+export const useIframeWidth = () => {
+ const [selectedBreakpoint] = useSelectedBreakpoint();
+ if (selectedBreakpoint === undefined || selectedBreakpoint.minWidth === 0) {
+ return "100%";
+ }
+
+ return Math.max(selectedBreakpoint.minWidth, minWidth);
+};
diff --git a/app/designer/features/topbar/topbar.tsx b/app/designer/features/topbar/topbar.tsx
index 81b0bfab5c..567c550c7d 100644
--- a/app/designer/features/topbar/topbar.tsx
+++ b/app/designer/features/topbar/topbar.tsx
@@ -6,7 +6,7 @@ import { Share } from "./share";
import { Publish } from "./publish";
import { SyncStatus } from "./sync-status";
import { Menu } from "./menu";
-import { Breakpoints } from "./breakpoints";
+import { Breakpoints } from "../breakpoints";
import { type Publish as PublishOnCanvas } from "../../shared/canvas-iframe";
type TopbarProps = {
diff --git a/app/designer/shared/nano-values/index.ts b/app/designer/shared/nano-values/index.ts
index 34a2b6e262..cf74dbe016 100644
--- a/app/designer/shared/nano-values/index.ts
+++ b/app/designer/shared/nano-values/index.ts
@@ -22,7 +22,7 @@ const isPublishDialogOpenContainer = createValueContainer<boolean>(false);
export const useIsPublishDialogOpen = () =>
useValue(isPublishDialogOpenContainer);
-export const selectedBreakpointContainer = createValueContainer<
+const selectedBreakpointContainer = createValueContainer<
Breakpoint | undefined
>();
export const useSelectedBreakpoint = () =>
@@ -31,5 +31,8 @@ export const useSelectedBreakpoint = () =>
export const breakpointsContainer = createValueContainer<Array<Breakpoint>>([]);
export const useBreakpoints = () => useValue(breakpointsContainer);
+const scaleContainer = createValueContainer<number>(100);
+export const useScale = () => useValue(scaleContainer);
+
const syncStatusContainer = createValueContainer<SyncStatus>("idle");
export const useSyncStatus = () => useValue(syncStatusContainer);
diff --git a/app/shared/db/tree.server.ts b/app/shared/db/tree.server.ts
index 5fe5fb6472..683205361a 100644
--- a/app/shared/db/tree.server.ts
+++ b/app/shared/db/tree.server.ts
@@ -7,6 +7,10 @@ const rootConfig = {
// @todo this should be part of a root primitive in primitives
component: "Box",
style: {
+ backgroundColor: {
+ type: "keyword",
+ value: "white",
+ },
fontFamily: {
type: "keyword",
value: "Arial",
diff --git a/app/shared/design-system/components/slider.tsx b/app/shared/design-system/components/slider.tsx
index b0db2ed15b..f2617c1f33 100644
--- a/app/shared/design-system/components/slider.tsx
+++ b/app/shared/design-system/components/slider.tsx
@@ -34,7 +34,6 @@ const SliderThumb = styled(SliderPrimitive.Thumb, {
width: 15,
height: 15,
outline: "none",
- opacity: "0",
backgroundColor: "white",
boxShadow: "0 0 1px rgba(0,0,0,.3), 0 1px 4px rgba(0,0,0,.15)",
borderRadius: "$round",
@@ -80,9 +79,6 @@ export const StyledSlider = styled(SliderPrimitive.Root, {
[`& ${SliderTrack}`]: {
backgroundColor: "$slate8",
},
- [`& ${SliderThumb}`]: {
- opacity: "1",
- },
},
},
});
diff --git a/app/shared/design-system/index.ts b/app/shared/design-system/index.ts
index d2a4c865dc..fbc3e7a0fa 100644
--- a/app/shared/design-system/index.ts
+++ b/app/shared/design-system/index.ts
@@ -60,3 +60,4 @@ export { Paragraph } from "./components/paragraph";
export { Link } from "./components/link";
export { Switch } from "./components/switch";
export { Panel } from "./components/panel";
+export * from "./components/slider";