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.

Breakpoints stage 7 (#1477)

Oleg Isonen759c54e37b5e
diff --git a/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts b/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
index 325bb4b477..87a31e4f71 100644
--- a/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
+++ b/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
@@ -11,6 +11,7 @@ import {
selectedBreakpointIdStore,
} from "~/shared/nano-states";
import { findInitialWidth } from "./find-initial-width";
+import { groupBreakpoints, isBaseBreakpoint } from "~/shared/breakpoints";
// Set canvas width based on workspace width, breakpoints and passed breakpoint id.
export const useSetInitialCanvasWidth = () => {
@@ -37,31 +38,42 @@ export const useSetInitialCanvasWidth = () => {
export const useSetInitialCanvasWidthOnce = () => {
const isDone = useRef(false);
- const [, setWidth] = useCanvasWidth();
+ const [, setCanvasWidth] = useCanvasWidth();
const workspaceRect = useStore(workspaceRectStore);
const breakpoints = useStore(breakpointsStore);
// Set it initially once.
useEffect(() => {
- if (isDone.current) {
- return;
- }
- if (workspaceRect === undefined) {
+ if (isDone.current || workspaceRect === undefined) {
return;
}
- const width = workspaceRect.width;
+ isDone.current = true;
+
+ const breakpointValues = Array.from(breakpoints.values());
+ const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);
+ // When there is base breakpoint, we want to find the lowest possible size
+ // that is bigger than all max breakpoints and smaller than all min breakpoints.
+ if (baseBreakpoint) {
+ const grouped = groupBreakpoints(breakpointValues);
+ const baseIndex = grouped.findIndex(isBaseBreakpoint);
+ const nextAfterBase = grouped[baseIndex + 1];
+ if (nextAfterBase?.maxWidth !== undefined) {
+ const width = nextAfterBase.maxWidth + 1;
+ setCanvasWidth(width);
+ return;
+ }
+ }
+
+ // Find the breakpoint that applies according to the workspace width.
const applicableBreakpoint = findApplicableMedia(
- Array.from(breakpoints.values()),
- width
+ breakpointValues,
+ workspaceRect.width
);
if (applicableBreakpoint) {
selectedBreakpointIdStore.set(applicableBreakpoint.id);
}
-
- setWidth(width);
-
- isDone.current = true;
- }, [breakpoints, setWidth, workspaceRect]);
+ setCanvasWidth(workspaceRect.width);
+ }, [breakpoints, setCanvasWidth, workspaceRect]);
};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
index eed3e5f07a..0e43d01f14 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
@@ -1,6 +1,6 @@
import { useStore } from "@nanostores/react";
import type { Publish } from "~/shared/pubsub";
-import { Box } from "@webstudio-is/design-system";
+import { css } from "@webstudio-is/design-system";
import { PlacementIndicator } from "@webstudio-is/design-system";
import {
useIsPreviewMode,
@@ -16,14 +16,18 @@ import { useSubscribeDragAndDropState } from "./use-subscribe-drag-drop-state";
import { ResizeHandles } from "./resize-handles";
import { MediaBadge } from "./media-badge";
-const toolsStyle = {
+const containerStyle = css({
position: "absolute",
- top: 0,
- right: 0,
- bottom: 0,
- left: 0,
+ inset: 0,
pointerEvents: "none",
-};
+ variants: {
+ overflow: {
+ hidden: {
+ overflow: "hidden",
+ },
+ },
+ },
+});
type CanvasToolsProps = {
publish: Publish;
@@ -46,7 +50,7 @@ export const CanvasTools = ({ publish }: CanvasToolsProps) => {
const { dropTarget, placementIndicator } = dragAndDropState;
const dropTargetInstance = instances.get(dropTarget.itemSelector[0]);
return dropTargetInstance ? (
- <Box css={toolsStyle}>
+ <div className={containerStyle({ overflow: "hidden" })}>
<Outline rect={placementIndicator.parentRect}>
<Label
instance={dropTargetInstance}
@@ -56,17 +60,23 @@ export const CanvasTools = ({ publish }: CanvasToolsProps) => {
{placementIndicator !== undefined && (
<PlacementIndicator placement={placementIndicator} />
)}
- </Box>
+ </div>
) : null;
}
return (
- <Box css={toolsStyle}>
+ <div className={containerStyle()}>
<MediaBadge />
<ResizeHandles />
- {isPreviewMode === false && <SelectedInstanceOutline />}
- {isPreviewMode === false && <HoveredInstanceOutline />}
- {isPreviewMode === false && <TextToolbar publish={publish} />}
- </Box>
+ {isPreviewMode === false && (
+ <>
+ <div className={containerStyle({ overflow: "hidden" })}>
+ <SelectedInstanceOutline />
+ <HoveredInstanceOutline />
+ </div>
+ <TextToolbar publish={publish} />
+ </>
+ )}
+ </div>
);
};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
index b8b11c5b71..5f2cb7c501 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
@@ -1,7 +1,7 @@
import { useStore } from "@nanostores/react";
import { findApplicableMedia } from "@webstudio-is/css-engine";
import { css, numericScrubControl, theme } from "@webstudio-is/design-system";
-import { useEffect, useRef, useState } from "react";
+import { useEffect, useRef } from "react";
import {
canvasWidthStore,
isCanvasPointerEventsEnabledStore,
@@ -13,7 +13,7 @@ import {
selectedBreakpointIdStore,
} from "~/shared/nano-states";
-const handleStyle = css({
+const handlesContainerStyle = css({
position: "absolute",
top: 0,
width: 4,
@@ -34,20 +34,24 @@ const handleStyle = css({
transform: "translateX(100%)",
color: theme.colors.foregroundSubtle,
},
- "&[data-state=hovering]": {
- "&::before, & svg": {
- color: theme.colors.backgroundPrimaryLight,
- },
- },
"&[data-align=left]": {
left: 0,
},
"&[data-align=right]": {
right: 0,
},
- "&[data-state=dragging]::before": {
+ "&[data-state=resizing]::before": {
display: "none",
},
+ "&:hover, &:has(+ &:hover), &:hover+&": {
+ "&::before, & svg": {
+ color: theme.colors.backgroundPrimaryLight,
+ },
+ },
+ // A little specificity hack to override the previou selector
+ "&&[data-state=resizing] svg": {
+ color: theme.colors.foregroundSubtle,
+ },
});
const handleIcon = (
@@ -127,47 +131,34 @@ const useScrub = ({ side }: { side: "right" | "left" }) => {
};
const useResize = () => {
- const isDragging = useStore(isCanvasPointerEventsEnabledStore) === false;
- const [isHovering, setIsHovering] = useState(false);
+ const isResizing = useStore(isResizingCanvasStore);
const leftRef = useScrub({ side: "left" });
const rightRef = useScrub({ side: "right" });
-
- const handleMouseEnter = () => setIsHovering(true);
-
- const handleMouseLeave = () => setIsHovering(false);
-
- const state = isDragging ? "dragging" : isHovering ? "hovering" : "idle";
+ const state = isResizing ? "resizing" : "idle";
return {
state,
leftRef,
rightRef,
- handleMouseEnter,
- handleMouseLeave,
};
};
export const ResizeHandles = () => {
- const { state, leftRef, rightRef, handleMouseEnter, handleMouseLeave } =
- useResize();
+ const { state, leftRef, rightRef } = useResize();
return (
<>
<div
ref={leftRef}
- onMouseEnter={handleMouseEnter}
- onMouseOut={handleMouseLeave}
data-state={state}
data-align="left"
- className={handleStyle()}
+ className={handlesContainerStyle()}
/>
<div
ref={rightRef}
- onMouseEnter={handleMouseEnter}
- onMouseLeave={handleMouseLeave}
data-state={state}
data-align="right"
- className={handleStyle()}
+ className={handlesContainerStyle()}
>
{handleIcon}
</div>