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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Compute selected breakpoint (#1023)
Bogdan Chadkineeb4130d9522
diff --git a/apps/designer/app/canvas/shared/use-shortcuts.ts b/apps/designer/app/canvas/shared/use-shortcuts.tsindex c8b3f65ad8..94a09e9263 100644--- a/apps/designer/app/canvas/shared/use-shortcuts.ts+++ b/apps/designer/app/canvas/shared/use-shortcuts.ts@@ -13,31 +13,13 @@ declare module "~/shared/pubsub" {export interface PubsubMap {cancelCurrentDrag: undefined;openBreakpointsMenu: undefined;- selectBreakpointFromShortcut: number;}}-type HandlerEvent = {- key?: string;- preventDefault?: () => void;-};-const togglePreviewMode = () => {isPreviewModeStore.set(!isPreviewModeStore.get());};-const publishSelectBreakpoint = ({ key }: HandlerEvent) => {- if (!key) {- throw new Error(- "`publishSelectBreakpoint` doesn't account for being called without a `key`"- );- }- publish({- type: "selectBreakpointFromShortcut",- payload: parseInt(key, 10),- });-};-const publishOpenBreakpointsMenu = () => {publish({ type: "openBreakpointsMenu" });};@@ -52,7 +34,6 @@ export const useShortcuts = () => {const shortcutHandlerMap = {preview: togglePreviewMode,breakpointsMenu: publishOpenBreakpointsMenu,- breakpoint: publishSelectBreakpoint,esc: publishCancelCurrentDrag,} as const;@@ -94,7 +75,6 @@ export const useShortcuts = () => {useHotkeys(shortcuts.preview, shortcutHandlerMap.preview, options, []);- useHotkeys(shortcuts.breakpoint, shortcutHandlerMap.breakpoint, options, []);useHotkeys(shortcuts.breakpointsMenu,shortcutHandlerMap.breakpointsMenu,@@ -105,7 +85,7 @@ export const useShortcuts = () => {useHotkeys(shortcuts.esc, shortcutHandlerMap.esc, options, []);// Shortcuts from the parent window- useSubscribe("shortcut", ({ name, key }) => {- shortcutHandlerMap[name]({ key });+ useSubscribe("shortcut", ({ name }) => {+ shortcutHandlerMap[name]();});};diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsxindex 16ca0fb7ef..379c108496 100644--- a/apps/designer/app/designer/designer.tsx+++ b/apps/designer/app/designer/designer.tsx@@ -20,10 +20,7 @@ import { Topbar } from "./features/topbar";import designerStyles from "./designer.css";import { Footer } from "./features/footer";import { TreePrevew } from "./features/tree-preview";-import {- useSubscribeBreakpoints,- useUpdateCanvasWidth,-} from "./features/breakpoints";+import { useUpdateCanvasWidth } from "./features/breakpoints";import {CanvasIframe,useReadCanvasRect,@@ -262,7 +259,6 @@ export const Designer = ({authPermit,}: DesignerProps) => {useSetAuthPermit(authPermit);- useSubscribeBreakpoints();useSetProject(project);useSetTreeId(treeId);useSetPages(pages);diff --git a/apps/designer/app/designer/features/breakpoints/breakpoints.tsx b/apps/designer/app/designer/features/breakpoints/breakpoints.tsxindex bba05e5ac6..01916c6b18 100644--- a/apps/designer/app/designer/features/breakpoints/breakpoints.tsx+++ b/apps/designer/app/designer/features/breakpoints/breakpoints.tsx@@ -3,6 +3,7 @@ import store from "immerhin";import type { Breakpoint } from "@webstudio-is/css-data";import { useSubscribe } from "~/shared/pubsub";import {+ theme,DropdownMenu,DropdownMenuCheckboxItem,DropdownMenuContent,@@ -12,13 +13,11 @@ import {DropdownMenuTrigger,DropdownMenuItemRightSlot,} from "@webstudio-is/design-system";-import { useSelectedBreakpoint } from "../../shared/nano-states";import { BreakpointsEditor } from "./breakpoints-editor";import { Preview } from "./preview";import { ZoomSetting } from "./zoom-setting";import { TriggerButton } from "./trigger-button";import { WidthSetting } from "./width-setting";-import { useSubscribeSelectBreakpointFromShortcut } from "./use-subscribe-shortcuts";import { ConfirmationDialog } from "./confirmation-dialog";import {breakpointsContainer,@@ -27,7 +26,11 @@ import {} from "~/shared/nano-states";import { utils } from "@webstudio-is/project";import { removeByMutable } from "~/shared/array-utils";-import { theme } from "@webstudio-is/design-system";+import { useStore } from "@nanostores/react";+import {+ selectedBreakpointIdStore,+ selectedBreakpointStore,+} from "~/shared/nano-states/breakpoints";export const Breakpoints = () => {const [view, setView] = useState<@@ -37,10 +40,9 @@ export const Breakpoints = () => {Breakpoint | undefined>();const [breakpoints] = useBreakpoints();- const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const [breakpointPreview, setBreakpointPreview] =useState(selectedBreakpoint);- useSubscribeSelectBreakpointFromShortcut();useEffect(() => {setBreakpointPreview(selectedBreakpoint);@@ -62,7 +64,7 @@ export const Breakpoints = () => {if (breakpointToDelete === undefined) {return;}- const [updatedBreakpoints] = store.createTransaction(+ store.createTransaction([breakpointsContainer, stylesStore],(breakpoints, styles) => {const breakpointId = breakpointToDelete.id;@@ -74,8 +76,8 @@ export const Breakpoints = () => {}});- if (breakpointToDelete === selectedBreakpoint) {- setSelectedBreakpoint(utils.breakpoints.sort(updatedBreakpoints)[0]);+ if (breakpointToDelete.id === selectedBreakpoint.id) {+ selectedBreakpointIdStore.set(undefined);}setBreakpointToDelete(undefined);@@ -144,7 +146,7 @@ export const Breakpoints = () => {setBreakpointPreview(selectedBreakpoint);}}onSelect={() => {- setSelectedBreakpoint(breakpoint);+ selectedBreakpointIdStore.set(breakpoint.id);}}>{breakpoint.label}diff --git a/apps/designer/app/designer/features/breakpoints/index.ts b/apps/designer/app/designer/features/breakpoints/index.tsindex 216d02cd97..2555fdd61d 100644--- a/apps/designer/app/designer/features/breakpoints/index.ts+++ b/apps/designer/app/designer/features/breakpoints/index.ts@@ -1,3 +1,2 @@export * from "./breakpoints";export * from "./use-update-canvas-width";-export * from "./use-subscribe-breakpoints";diff --git a/apps/designer/app/designer/features/breakpoints/trigger-button.tsx b/apps/designer/app/designer/features/breakpoints/trigger-button.tsxindex 688a0cda72..2928bf6698 100644--- a/apps/designer/app/designer/features/breakpoints/trigger-button.tsx+++ b/apps/designer/app/designer/features/breakpoints/trigger-button.tsx@@ -1,12 +1,12 @@import { forwardRef, type ComponentProps, type ElementRef } from "react";import { useStore } from "@nanostores/react";import { Button, Text } from "@webstudio-is/design-system";-import {- useCanvasWidth,- useSelectedBreakpoint,-} from "~/designer/shared/nano-states";+import { useCanvasWidth } from "~/designer/shared/nano-states";import { willRender } from "~/designer/shared/breakpoints";-import { zoomStore } from "~/shared/nano-states/breakpoints";+import {+ selectedBreakpointStore,+ zoomStore,+} from "~/shared/nano-states/breakpoints";type TriggerButtonProps = ComponentProps<typeof Button>;@@ -15,7 +15,7 @@ export const TriggerButton = forwardRef<TriggerButtonProps>((props, ref) => {const zoom = useStore(zoomStore);- const [breakpoint] = useSelectedBreakpoint();+ const breakpoint = useStore(selectedBreakpointStore);const [canvasWidth] = useCanvasWidth();if (breakpoint === undefined) {return null;diff --git a/apps/designer/app/designer/features/breakpoints/use-next-breakpoint.ts b/apps/designer/app/designer/features/breakpoints/use-next-breakpoint.tsindex 2229fddb12..2c72f94124 100644--- a/apps/designer/app/designer/features/breakpoints/use-next-breakpoint.ts+++ b/apps/designer/app/designer/features/breakpoints/use-next-breakpoint.ts@@ -1,13 +1,14 @@import { useMemo } from "react";-import { useSelectedBreakpoint } from "~/designer/shared/nano-states";import { useBreakpoints } from "~/shared/nano-states";import { utils } from "@webstudio-is/project";+import { useStore } from "@nanostores/react";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";/*** Return the next breakpoint from the currently selected one, sorted by the `sort()`*/export const useNextBreakpoint = () => {- const [selectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const [breakpoints] = useBreakpoints();const sortedBreakpoints = useMemo(diff --git a/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts b/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.tsdeleted file mode 100644index d8cc2e292c..0000000000--- a/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts+++ /dev/null@@ -1,29 +0,0 @@-import { useEffect } from "react";-import { useSelectedBreakpoint } from "~/designer/shared/nano-states";-import { useBreakpoints } from "~/shared/nano-states";-import { utils } from "@webstudio-is/project";--export const useSubscribeBreakpoints = () => {- const [breakpoints] = useBreakpoints();- const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();-- useEffect(() => {- // Set the initial selected breakpoint- if (selectedBreakpoint === undefined && breakpoints.length !== 0) {- setSelectedBreakpoint(utils.breakpoints.sort(breakpoints)[0]);- }-- // Breakpoints must have changed, lets update the selected breakpoint- if (- selectedBreakpoint !== undefined &&- breakpoints.includes(selectedBreakpoint) === false- ) {- const nextSelectedBreakpoint = breakpoints.find(- (breakpoint) => breakpoint.id === selectedBreakpoint.id- );- if (nextSelectedBreakpoint !== undefined) {- setSelectedBreakpoint(nextSelectedBreakpoint);- }- }- }, [breakpoints, selectedBreakpoint, setSelectedBreakpoint]);-};diff --git a/apps/designer/app/designer/features/breakpoints/use-subscribe-shortcuts.ts b/apps/designer/app/designer/features/breakpoints/use-subscribe-shortcuts.tsdeleted file mode 100644index da24cac328..0000000000--- a/apps/designer/app/designer/features/breakpoints/use-subscribe-shortcuts.ts+++ /dev/null@@ -1,15 +0,0 @@-import { useSubscribe } from "~/shared/pubsub";-import { useBreakpoints } from "~/shared/nano-states";-import { useSelectedBreakpoint } from "../../shared/nano-states";-import { utils } from "@webstudio-is/project";--export const useSubscribeSelectBreakpointFromShortcut = () => {- const [breakpoints] = useBreakpoints();- const [, setSelectedBreakpoint] = useSelectedBreakpoint();- useSubscribe("selectBreakpointFromShortcut", (index) => {- const breakpoint = utils.breakpoints.sort(breakpoints)[index - 1];- if (breakpoint) {- setSelectedBreakpoint(breakpoint);- }- });-};diff --git a/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts b/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.tsindex b716f9154b..6e162571af 100644--- a/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts+++ b/apps/designer/app/designer/features/breakpoints/use-update-canvas-width.ts@@ -1,13 +1,12 @@+import { useStore } from "@nanostores/react";import { useCallback, useEffect } from "react";-import {- useSelectedBreakpoint,- useCanvasWidth,-} from "~/designer/shared/nano-states";+import { useCanvasWidth } from "~/designer/shared/nano-states";import { useIsPreviewMode } from "~/shared/nano-states";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";import { minWidth } from "./width-setting";export const useUpdateCanvasWidth = () => {- const [selectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const [canvasWidth, setCanvasWidth] = useCanvasWidth();const [isPreviewMode] = useIsPreviewMode();diff --git a/apps/designer/app/designer/features/breakpoints/width-setting.tsx b/apps/designer/app/designer/features/breakpoints/width-setting.tsxindex af1fbf3361..c7a985088e 100644--- a/apps/designer/app/designer/features/breakpoints/width-setting.tsx+++ b/apps/designer/app/designer/features/breakpoints/width-setting.tsx@@ -1,11 +1,14 @@+import { useStore } from "@nanostores/react";import {- useCanvasWidth,- useSelectedBreakpoint,-} from "~/designer/shared/nano-states";-import { DeprecatedText2, Flex, Slider } from "@webstudio-is/design-system";+ theme,+ DeprecatedText2,+ Flex,+ Slider,+} from "@webstudio-is/design-system";import { useIsPreviewMode } from "~/shared/nano-states";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";+import { useCanvasWidth } from "~/designer/shared/nano-states";import { useNextBreakpoint } from "./use-next-breakpoint";-import { theme } from "@webstudio-is/design-system";// Doesn't make sense to allow resizing the canvas lower/higher than this.export const minWidth = 360;@@ -13,7 +16,7 @@ export const maxWidth = 3000;export const WidthSetting = () => {const [canvasWidth, setCanvasWidth] = useCanvasWidth();- const [selectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const nextBreakpoint = useNextBreakpoint();const [isPreviewMode] = useIsPreviewMode();diff --git a/apps/designer/app/designer/features/style-panel/shared/style-info.ts b/apps/designer/app/designer/features/style-panel/shared/style-info.tsindex 2973d0fe69..649efc7adf 100644--- a/apps/designer/app/designer/features/style-panel/shared/style-info.ts+++ b/apps/designer/app/designer/features/style-panel/shared/style-info.ts@@ -21,7 +21,7 @@ import {useBreakpoints,useRootInstance,} from "~/shared/nano-states";-import { useSelectedBreakpoint } from "~/designer/shared/nano-states";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";type CascadedValueInfo = {breakpointId: string;@@ -194,7 +194,7 @@ export const getInheritedInfo = (*/export const useStyleInfo = () => {const [breakpoints] = useBreakpoints();- const [selectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const selectedBreakpointId = selectedBreakpoint?.id;const selectedInstanceId = useStore(selectedInstanceIdStore);const selectedStyleSource = useStore(selectedStyleSourceStore);diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.tsindex 22fde1aa8f..c86d9a1ff4 100644--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts@@ -1,4 +1,5 @@import { useCallback } from "react";+import { useStore } from "@nanostores/react";import store from "immerhin";import warnOnce from "warn-once";import { getStyleDeclKey, type Instance } from "@webstudio-is/project-build";@@ -11,7 +12,7 @@ import {styleSourcesStore,stylesStore,} from "~/shared/nano-states";-import { useSelectedBreakpoint } from "~/designer/shared/nano-states";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";// @todo: must be removed, now it's only for compatibility with existing codeimport { parseCssValue } from "./parse-css-value";import { useStyleInfo } from "./style-info";@@ -49,7 +50,7 @@ export type CreateBatchUpdate = () => {};export const useStyleData = ({ selectedInstance, publish }: UseStyleData) => {- const [selectedBreakpoint] = useSelectedBreakpoint();+ const selectedBreakpoint = useStore(selectedBreakpointStore);const currentStyle = useStyleInfo();diff --git a/apps/designer/app/designer/features/style-panel/style-panel.tsx b/apps/designer/app/designer/features/style-panel/style-panel.tsxindex 696286c045..1985d602d5 100644--- a/apps/designer/app/designer/features/style-panel/style-panel.tsx+++ b/apps/designer/app/designer/features/style-panel/style-panel.tsx@@ -1,3 +1,4 @@+import { useStore } from "@nanostores/react";import { isFeatureEnabled } from "@webstudio-is/feature-flags";import {theme,@@ -7,13 +8,11 @@ import {} from "@webstudio-is/design-system";import type { Instance } from "@webstudio-is/project-build";import type { Publish } from "~/shared/pubsub";+import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";import { willRender } from "~/designer/shared/breakpoints";import { useStyleData } from "./shared/use-style-data";import { StyleSettings } from "./style-settings";-import {- useCanvasWidth,- useSelectedBreakpoint,-} from "~/designer/shared/nano-states";+import { useCanvasWidth } from "~/designer/shared/nano-states";import { StyleSourcesSection } from "./style-source-section";type StylePanelProps = {@@ -28,7 +27,7 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {publish,});- const [breakpoint] = useSelectedBreakpoint();+ const breakpoint = useStore(selectedBreakpointStore);const [canvasWidth] = useCanvasWidth();if (diff --git a/apps/designer/app/designer/shared/nano-states/index.ts b/apps/designer/app/designer/shared/nano-states/index.tsindex 2a07319a15..b5e703cdc8 100644--- a/apps/designer/app/designer/shared/nano-states/index.ts+++ b/apps/designer/app/designer/shared/nano-states/index.ts@@ -1,6 +1,5 @@import { atom, type WritableAtom } from "nanostores";import { useStore } from "@nanostores/react";-import type { Breakpoint } from "@webstudio-is/css-data";import type { Pages, Project } from "@webstudio-is/project";import type { AssetContainer, DeletingAssetContainer } from "../assets";@@ -16,10 +15,6 @@ const isPublishDialogOpenContainer = atom<boolean>(false);export const useIsPublishDialogOpen = () =>useValue(isPublishDialogOpenContainer);-const selectedBreakpointContainer = atom<Breakpoint | undefined>();-export const useSelectedBreakpoint = () =>- useValue(selectedBreakpointContainer);-const canvasWidthContainer = atom<number>(0);export const useCanvasWidth = () => useValue(canvasWidthContainer);diff --git a/apps/designer/app/designer/shared/shortcuts/use-publish-shortcuts.ts b/apps/designer/app/designer/shared/shortcuts/use-publish-shortcuts.tsindex 35e8a54db7..2a9af5840c 100644--- a/apps/designer/app/designer/shared/shortcuts/use-publish-shortcuts.ts+++ b/apps/designer/app/designer/shared/shortcuts/use-publish-shortcuts.ts@@ -4,7 +4,7 @@ import type { Publish } from "~/shared/pubsub";declare module "~/shared/pubsub" {export interface PubsubMap {- shortcut: { name: keyof typeof shortcuts; key?: string };+ shortcut: { name: keyof typeof shortcuts };}}@@ -19,10 +19,10 @@ export const usePublishShortcuts = (publish: Publish) => {// eslint-disable-next-line react-hooks/rules-of-hooksuseHotkeys(shortcuts[name],- (event) => {+ () => {publish({type: "shortcut",- payload: { name, key: event.key },+ payload: { name },});},options,diff --git a/apps/designer/app/shared/nano-states/breakpoints.ts b/apps/designer/app/shared/nano-states/breakpoints.tsindex 3621574f4c..1ee1ab805a 100644--- a/apps/designer/app/shared/nano-states/breakpoints.ts+++ b/apps/designer/app/shared/nano-states/breakpoints.ts@@ -1,4 +1,7 @@-import { atom } from "nanostores";+import { atom, computed } from "nanostores";+import type { Breakpoint } from "@webstudio-is/css-data";+import { utils } from "@webstudio-is/project";+import { breakpointsContainer } from "./nano-states";export const minZoom = 10;const maxZoom = 100;@@ -14,6 +17,35 @@ export const zoomOut = () => {zoomStore.set(Math.max(zoomStore.get() - zoomStep, minZoom));};+export const selectedBreakpointIdStore = atom<undefined | Breakpoint["id"]>(+ undefined+);++export const selectedBreakpointStore = computed(+ [breakpointsContainer, selectedBreakpointIdStore],+ (breakpoints, selectedBreakpointId) => {+ const matchedBreakpoint = breakpoints.find(+ (breakpoint) => breakpoint.id === selectedBreakpointId+ );+ // initially set first breakpoint as selected breakpoint+ const fallbackBreakpoint = utils.breakpoints.sort(breakpoints)[0];+ return matchedBreakpoint ?? fallbackBreakpoint;+ }+);++/**+ * order number starts with 1 and covers all existing breakpoints+ */+export const selectBreakpointByOrderNumber = (orderNumber: number) => {+ const breakpoints = breakpointsContainer.get();+ const index = orderNumber - 1;+ const breakpoint = utils.breakpoints.sort(breakpoints).at(index);+ if (breakpoint) {+ selectedBreakpointIdStore.set(breakpoint.id);+ }+};+export const synchronizedBreakpointsStores = [["zoomStore", zoomStore],+ ["selectedBreakpointId", selectedBreakpointIdStore],] as const;diff --git a/apps/designer/app/shared/shortcuts/shortcuts.ts b/apps/designer/app/shared/shortcuts/shortcuts.tsindex 08ad124356..e9015ac48a 100644--- a/apps/designer/app/shared/shortcuts/shortcuts.ts+++ b/apps/designer/app/shared/shortcuts/shortcuts.ts@@ -1,16 +1,17 @@import { Options, useHotkeys } from "react-hotkeys-hook";import store from "immerhin";import { selectedInstanceIdStore } from "../nano-states";-import { zoomIn, zoomOut } from "../nano-states/breakpoints";+import {+ zoomIn,+ zoomOut,+ selectBreakpointByOrderNumber,+} from "../nano-states/breakpoints";import { deleteInstance } from "../instance-utils";export const shortcuts = {esc: "esc",preview: "meta+shift+p, ctrl+shift+p",breakpointsMenu: "meta+b, ctrl+b",- breakpoint: Array.from(new Array(9))- .map((_, index) => `meta+${index + 1}, ctrl+${index + 1}`)- .join(", "),} as const;export const options: Options = {@@ -83,4 +84,16 @@ export const useSharedShortcuts = () => {{ enableOnFormTags: true, enableOnContentEditable: true },[]);++ const breakpointShortcuts = Array.from(new Array(9))+ .map((_, index) => `meta+${index + 1}, ctrl+${index + 1}`)+ .join(", ");+ useHotkeys(+ breakpointShortcuts,+ (event) => {+ selectBreakpointByOrderNumber(Number.parseInt(event.key, 10));+ },+ { enableOnFormTags: true, enableOnContentEditable: true },+ []+ );};