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.

feat: Builder loading delight (#3526)

Oleg Isonend2a301523943
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 75bbd7ae24..d98b69de40 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -4,7 +4,14 @@ import { TooltipProvider } from "@radix-ui/react-tooltip";
import { usePublish, $publisher } from "~/shared/pubsub";
import type { Build } from "@webstudio-is/project-build";
import type { Project } from "@webstudio-is/project";
-import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
+import {
+ theme,
+ Box,
+ type CSS,
+ Flex,
+ Grid,
+ Progress,
+} from "@webstudio-is/design-system";
import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";
import { createImageLoader } from "@webstudio-is/image";
import { registerContainers, useBuilderStore } from "~/shared/sync";
@@ -33,6 +40,8 @@ import {
$publisherHost,
$imageLoader,
$textEditingInstanceSelector,
+ $selectedInstanceRenderState,
+ $canvasIframeState,
} from "~/shared/nano-states";
import { type Settings } from "./shared/client-settings";
import { getBuildUrl } from "~/shared/router-utils";
@@ -50,9 +59,14 @@ import type { TokenPermissions } from "@webstudio-is/authorization-token";
import { useToastErrors } from "~/shared/error/toast-error";
import { canvasApi } from "~/shared/canvas-api";
import { loadBuilderData, setBuilderData } from "~/shared/builder-data";
+import { WebstudioIcon } from "@webstudio-is/icons";
+import { atom, computed } from "nanostores";
+import { useInterval } from "~/shared/hook-utils/use-interval";
registerContainers();
+const $dataLoadingState = atom<"idle" | "loading" | "loaded">("idle");
+
const useSetWindowTitle = () => {
const project = useStore($project);
useEffect(() => {
@@ -77,6 +91,7 @@ const SidePanel = ({
<Box
as="aside"
css={{
+ position: "relative",
isolation: "isolate",
gridArea,
display: isPreviewMode ? "none" : "flex",
@@ -87,11 +102,11 @@ const SidePanel = ({
//overflowY: "auto",
bc: theme.colors.backgroundPanel,
height: "100%",
- ...css,
"&:last-of-type": {
// Ensure content still has full width, avoid subpixels give layout round numbers
boxShadow: `inset 1px 0 0 0 ${theme.colors.borderMain}`,
},
+ ...css,
}}
>
{children}
@@ -183,11 +198,13 @@ const ChromeWrapper = ({ children, isPreviewMode }: ChromeWrapperProps) => {
type NavigatorPanelProps = {
isPreviewMode: boolean;
navigatorLayout: "docked" | "undocked";
+ css: CSS;
};
const NavigatorPanel = ({
isPreviewMode,
navigatorLayout,
+ css,
}: NavigatorPanelProps) => {
if (navigatorLayout === "docked") {
return;
@@ -200,6 +217,7 @@ const NavigatorPanel = ({
borderRight: `1px solid ${theme.colors.borderMain}`,
width: theme.spacing[30],
height: "100%",
+ ...css,
}}
>
<NavigatorContent isClosable={false} />
@@ -208,6 +226,112 @@ const NavigatorPanel = ({
);
};
+const revealAnimation = ({
+ show,
+ backgroundColor,
+}: {
+ show: boolean;
+ backgroundColor: string;
+}): CSS => ({
+ position: "relative",
+ "> ::after": {
+ content: "",
+ position: "absolute",
+ inset: 0,
+ zIndex: 1,
+ transitionDuration: "300ms",
+ pointerEvents: "none",
+ transitionProperty: "opacity",
+ backgroundColor,
+ opacity: show ? 0 : 1,
+ },
+});
+
+const $loadingState = computed(
+ [
+ $dataLoadingState,
+ $selectedInstanceRenderState,
+ $canvasIframeState,
+ $isPreviewMode,
+ ],
+ (
+ dataLoadingState,
+ selectedInstanceRenderState,
+ canvasIframeState,
+ isPreviewMode
+ ) => {
+ type State =
+ | "dataLoadingState"
+ | "selectedInstanceRenderState"
+ | "canvasIframeState";
+
+ const readyStates = new Map<State, boolean>([
+ ["dataLoadingState", dataLoadingState === "loaded"],
+ [
+ "selectedInstanceRenderState",
+ selectedInstanceRenderState === "mounted" || isPreviewMode,
+ ],
+ ["canvasIframeState", canvasIframeState === "ready"],
+ ]);
+ const readyCount = Array.from(readyStates.values()).filter(Boolean).length;
+ const progress = Math.round((readyCount / readyStates.size) * 100);
+ const state = readyCount === readyStates.size ? "ready" : "loading";
+
+ return { state, progress, readyStates };
+ }
+);
+
+const ProgressIndicator = ({ value }: { value: number }) => {
+ const [fakeValue, setFakeValue] = useState(value);
+ // A maximum fake value we can grow to if the value is still 0, so that we don't get stuck at 0%.
+ const defaultFakeValueLimit = 50;
+
+ // This is an approximation of a real progress that should come from "value".
+ useInterval((timerId) => {
+ if (value >= 100) {
+ clearInterval(timerId);
+ return;
+ }
+ setFakeValue((fakeValue) => {
+ fakeValue++;
+ // - When real value is 0, we don't want to use it, because we don't want to get stuck at 0.
+ // - When real value is smaller than fake value, we don't want to use it because that would jump the progress back.
+ const minFakeValue =
+ value === 0 || value < fakeValue ? defaultFakeValueLimit : value;
+ return Math.min(fakeValue, minFakeValue);
+ });
+ }, 50);
+
+ if (value >= 100) {
+ return;
+ }
+
+ return (
+ <Flex
+ direction="column"
+ gap="3"
+ css={{
+ position: "absolute",
+ inset: 0,
+ justifyContent: "center",
+ alignItems: "center",
+ zIndex: 1,
+ }}
+ >
+ <WebstudioIcon
+ size={60}
+ style={{
+ filter: `
+ drop-shadow(3px 3px 6px rgba(0, 0, 0, 0.7))
+ brightness(${fakeValue}%)
+ `,
+ }}
+ />
+ <Progress value={fakeValue} />
+ </Flex>
+ );
+};
+
export type BuilderProps = {
project: Project;
publisherHost: string;
@@ -229,7 +353,6 @@ export const Builder = ({
userPlanFeatures,
authTokenPermissions,
}: BuilderProps) => {
- const [isDataLoaded, setIsDataLoaded] = useState(false);
useMount(() => {
// additional data stores
$project.set(project);
@@ -241,6 +364,7 @@ export const Builder = ({
$authTokenPermissions.set(authTokenPermissions);
const controller = new AbortController();
+ $dataLoadingState.set("loading");
loadBuilderData({ projectId: project.id, signal: controller.signal })
.then((data) => {
setBuilderData(data);
@@ -254,10 +378,14 @@ export const Builder = ({
// render canvas only after all data is loaded
// so builder is started listening for connect event
// when canvas is rendered
- setIsDataLoaded(true);
+ $dataLoadingState.set("loaded");
})
- .catch(() => {});
+ .catch(() => {
+ // @todo make needs error handling and error state? e.g. a toast
+ $dataLoadingState.set("idle");
+ });
return () => {
+ $dataLoadingState.set("idle");
controller.abort("unmount");
};
});
@@ -298,6 +426,8 @@ export const Builder = ({
);
const navigatorLayout = useNavigatorLayout();
+ const dataLoadingState = useStore($dataLoadingState);
+ const loadingState = useStore($loadingState);
const canvasUrl = getBuildUrl({
project,
@@ -347,22 +477,30 @@ export const Builder = ({
<ChromeWrapper isPreviewMode={isPreviewMode}>
<ProjectSettings />
<Topbar
- gridArea="header"
project={project}
hasProPlan={userPlanFeatures.hasProPlan}
+ css={{
+ gridArea: "header",
+ ...revealAnimation({
+ // Looks nicer when topbar is already visible earlier, so user has more sense of progress.
+ show: loadingState.readyStates.get("dataLoadingState") ?? false,
+ backgroundColor: theme.colors.backgroundTopbar,
+ }),
+ }}
/>
<Main>
- <Workspace onTransitionEnd={onTransitionEnd}>
- {isDataLoaded && (
+ <Workspace
+ onTransitionEnd={onTransitionEnd}
+ css={revealAnimation({
+ show: loadingState.state === "ready",
+ backgroundColor: theme.colors.backgroundCanvas,
+ })}
+ >
+ {dataLoadingState === "loaded" && (
<CanvasIframe
ref={iframeRefCallback}
src={canvasUrl}
title={project.title}
- css={{
- height: "100%",
- width: "100%",
- backgroundColor: "#fff",
- }}
/>
)}
</Workspace>
@@ -371,14 +509,30 @@ export const Builder = ({
<NavigatorPanel
isPreviewMode={isPreviewMode}
navigatorLayout={navigatorLayout}
+ css={revealAnimation({
+ show: loadingState.state === "ready",
+ backgroundColor: theme.colors.backgroundPanel,
+ })}
/>
- <SidePanel gridArea="sidebar">
+ <SidePanel
+ gridArea="sidebar"
+ css={revealAnimation({
+ show: loadingState.state === "ready",
+ backgroundColor: theme.colors.backgroundPanel,
+ })}
+ >
<SidebarLeft publish={publish} />
</SidePanel>
<SidePanel
gridArea="inspector"
isPreviewMode={isPreviewMode}
- css={{ overflow: "hidden" }}
+ css={{
+ overflow: "hidden",
+ ...revealAnimation({
+ show: loadingState.state === "ready",
+ backgroundColor: theme.colors.backgroundPanel,
+ }),
+ }}
>
<Inspector navigatorLayout={navigatorLayout} />
</SidePanel>
@@ -390,6 +544,7 @@ export const Builder = ({
project={project}
/>
</ChromeWrapper>
+ <ProgressIndicator value={loadingState.progress} />
</div>
</TooltipProvider>
);
diff --git a/apps/builder/app/builder/features/topbar/topbar.tsx b/apps/builder/app/builder/features/topbar/topbar.tsx
index b10913996c..d87bff6ee2 100644
--- a/apps/builder/app/builder/features/topbar/topbar.tsx
+++ b/apps/builder/app/builder/features/topbar/topbar.tsx
@@ -7,6 +7,7 @@ import {
ToolbarToggleGroup,
ToolbarButton,
Text,
+ type CSS,
} from "@webstudio-is/design-system";
import type { Project } from "@webstudio-is/project";
import { $pages, $selectedPage } from "~/shared/nano-states";
@@ -58,15 +59,15 @@ const topbarContainerStyle = css({
});
type TopbarProps = {
- gridArea: string;
+ css: CSS;
project: Project;
hasProPlan: boolean;
};
-export const Topbar = ({ gridArea, project, hasProPlan }: TopbarProps) => {
+export const Topbar = ({ css, project, hasProPlan }: TopbarProps) => {
const pages = useStore($pages);
return (
- <nav className={topbarContainerStyle({ css: { gridArea } })}>
+ <nav className={topbarContainerStyle({ css })}>
<Flex grow={false} shrink={false}>
<Menu />
</Flex>
diff --git a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
index 3f46b9b368..e70010e4fe 100644
--- a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
@@ -1,6 +1,5 @@
import { forwardRef } from "react";
import {
- type CSS,
css,
canvasPointerEventsPropertyName,
} from "@webstudio-is/design-system";
@@ -10,20 +9,21 @@ import { $canvasIframeState } from "~/shared/nano-states";
const iframeStyle = css({
border: "none",
pointerEvents: `var(${canvasPointerEventsPropertyName})`,
+ height: "100%",
+ width: "100%",
+ backgroundColor: "#fff",
});
-type CanvasIframeProps = {
- css: CSS;
-} & JSX.IntrinsicElements["iframe"];
+type CanvasIframeProps = JSX.IntrinsicElements["iframe"];
export const CanvasIframe = forwardRef<HTMLIFrameElement, CanvasIframeProps>(
- ({ css, ...rest }, ref) => {
+ (props, ref) => {
useUnmount(() => {
// Unmount does't work inside iframe.
$canvasIframeState.set("idle");
});
- return <iframe {...rest} ref={ref} className={iframeStyle({ css })} />;
+ return <iframe {...props} ref={ref} className={iframeStyle()} />;
}
);
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index 6fad4a4439..05c08df0be 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -1,6 +1,6 @@
import { useEffect, useRef, type ReactNode } from "react";
import { useStore } from "@nanostores/react";
-import { theme, Toaster, css } from "@webstudio-is/design-system";
+import { theme, Toaster, css, type CSS } from "@webstudio-is/design-system";
import {
$canvasWidth,
$scale,
@@ -93,9 +93,14 @@ const useOutlineStyle = () => {
type WorkspaceProps = {
children: ReactNode;
onTransitionEnd: () => void;
+ css: CSS;
};
-export const Workspace = ({ children, onTransitionEnd }: WorkspaceProps) => {
+export const Workspace = ({
+ children,
+ onTransitionEnd,
+ css,
+}: WorkspaceProps) => {
const canvasStyle = useCanvasStyle();
const outlineStyle = useOutlineStyle();
const workspaceRef = useMeasureWorkspace();
@@ -107,7 +112,7 @@ export const Workspace = ({ children, onTransitionEnd }: WorkspaceProps) => {
return (
<div
- className={workspaceStyle()}
+ className={workspaceStyle({ css })}
onClick={handleWorkspaceClick}
ref={workspaceRef}
>
diff --git a/apps/builder/app/shared/hook-utils/use-interval.ts b/apps/builder/app/shared/hook-utils/use-interval.ts
new file mode 100644
index 0000000000..26575d630a
--- /dev/null
+++ b/apps/builder/app/shared/hook-utils/use-interval.ts
@@ -0,0 +1,18 @@
+import { useEffect, useRef } from "react";
+
+type Timeout = ReturnType<typeof setTimeout>;
+
+export const useInterval = (callback: (id: Timeout) => void, delay: number) => {
+ const savedCallback = useRef(callback);
+
+ useEffect(() => {
+ savedCallback.current = callback;
+ });
+
+ useEffect(() => {
+ const id = setInterval(() => {
+ savedCallback.current(id);
+ }, delay);
+ return () => clearInterval(id);
+ }, [delay]);
+};
diff --git a/packages/design-system/src/components/progress.tsx b/packages/design-system/src/components/progress.tsx
new file mode 100644
index 0000000000..813864410b
--- /dev/null
+++ b/packages/design-system/src/components/progress.tsx
@@ -0,0 +1,39 @@
+import { Root, Indicator } from "@radix-ui/react-progress";
+import { css, theme } from "../stitches.config";
+
+const rootStyle = css({
+ width: 200,
+ height: 2,
+ overflow: "hidden",
+ borderRadius: 9999,
+ background: "#fff",
+ boxShadow: "0 0 32px #4a4efa80",
+});
+
+const indicatorStyle = css({
+ width: "100%",
+ height: "100%",
+ background: theme.colors.brandBorderNavbar,
+ transitionDuration: "1000ms",
+ transitionProperty: "transform",
+});
+
+export const Progress = ({
+ value,
+ transitionDuration,
+}: {
+ value: number;
+ transitionDuration?: string;
+}) => {
+ return (
+ <Root value={value} className={rootStyle()}>
+ <Indicator
+ className={indicatorStyle()}
+ style={{
+ transform: `translateX(-${100 - value}%)`,
+ transitionDuration,
+ }}
+ />
+ </Root>
+ );
+};
diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.ts
index a8f131f33e..4d9204d1b0 100644
--- a/packages/design-system/src/index.ts
+++ b/packages/design-system/src/index.ts
@@ -52,6 +52,7 @@ export * as Collapsible from "@radix-ui/react-collapsible";
export { AccessibleIcon } from "@radix-ui/react-accessible-icon";
export * from "./components/toggle-group";
export * from "./components/progress-radial";
+export * from "./components/progress";
export { SearchField } from "./components/search-field";
export { Tabs, TabsContent, TabsList, TabsTrigger } from "@radix-ui/react-tabs";
export { Card } from "./components/card";