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.

Fix hash scroll (#1861)

Ivan Starkov7632b05fe77e
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index 8b46369bcd..3f57671a4c 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -20,7 +20,8 @@ const workspaceStyle = css({
flexGrow: 1,
background: theme.colors.backgroundCanvas,
position: "relative",
- overflow: "hidden",
+ // Prevent scrollIntoView from scrolling the whole page
+ overflow: "clip",
});
const canvasContainerStyle = css({
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index db86925e94..3eb46435f5 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -42,6 +42,7 @@ import { setDataCollapsed, subscribeCollapsedToPubSub } from "./collapsed";
import { useWindowResizeDebounced } from "~/shared/dom-hooks";
import { subscribeInstanceSelection } from "./instance-selection";
import { subscribeInstanceHovering } from "./instance-hovering";
+import { useHashLinkSync } from "~/shared/pages";
registerContainers();
@@ -190,6 +191,8 @@ export const Canvas = ({ params }: CanvasProps): JSX.Element | null => {
useEffect(subscribeCollapsedToPubSub, []);
+ useHashLinkSync();
+
const elements = useElementsTree(components, params);
return (
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index d8dace2bf6..ec7c847496 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -19,6 +19,7 @@ import {
} from "@webstudio-is/react-sdk";
import {
instancesStore,
+ isPreviewModeStore,
selectedInstanceRenderStateStore,
selectedInstanceSelectorStore,
selectedStyleSourceSelectorStore,
@@ -131,6 +132,7 @@ export const WebstudioComponentDev = ({
selectedInstanceSelector,
instanceSelector
);
+ const isPreviewMode = useStore(isPreviewModeStore);
// Scroll the selected instance into view when selected from navigator.
useEffect(() => {
@@ -157,7 +159,8 @@ export const WebstudioComponentDev = ({
});
const readonlyProps =
- instance.component === "Input" || instance.component === "Textarea"
+ isPreviewMode === false &&
+ (instance.component === "Input" || instance.component === "Textarea")
? { readOnly: true }
: undefined;
diff --git a/apps/builder/app/shared/pages/use-switch-page.ts b/apps/builder/app/shared/pages/use-switch-page.ts
index f26ed55ad1..21cefd21a1 100644
--- a/apps/builder/app/shared/pages/use-switch-page.ts
+++ b/apps/builder/app/shared/pages/use-switch-page.ts
@@ -104,3 +104,31 @@ export const useSyncPageUrl = () => {
);
}, [isPreviewMode, navigate, page, pageHash]);
};
+
+/**
+ * Synchronize pageHash with scrolling position
+ */
+export const useHashLinkSync = () => {
+ const pageHash = useStore(selectedPageHashStore);
+
+ useEffect(() => {
+ if (pageHash === "") {
+ // native browser behavior is to do nothing if hash is empty
+ // remix scroll to top, we emulate native
+ return;
+ }
+
+ let elementId = decodeURIComponent(pageHash);
+ if (elementId.startsWith("#")) {
+ elementId = elementId.slice(1);
+ }
+
+ // Try find element to scroll to
+ const element = document.getElementById(elementId);
+ if (element !== null) {
+ element.scrollIntoView();
+ }
+ // Remix scroll to top if element not found
+ // browser do nothing
+ }, [pageHash]);
+};
diff --git a/packages/sdk-components-react-remix/src/shared/remix-link.tsx b/packages/sdk-components-react-remix/src/shared/remix-link.tsx
index 0782b49f80..583feb42c9 100644
--- a/packages/sdk-components-react-remix/src/shared/remix-link.tsx
+++ b/packages/sdk-components-react-remix/src/shared/remix-link.tsx
@@ -27,7 +27,7 @@ export const wrapLinkComponent = (BaseLink: typeof Link) => {
to = urlTo.pathname;
if (href.hash !== undefined) {
- urlTo.hash = href.hash;
+ urlTo.hash = encodeURIComponent(href.hash);
to = `${urlTo.pathname}${urlTo.hash}`;
}
diff --git a/packages/sdk-components-react/src/link.tsx b/packages/sdk-components-react/src/link.tsx
index 95788bb895..fde4ac4f1a 100644
--- a/packages/sdk-components-react/src/link.tsx
+++ b/packages/sdk-components-react/src/link.tsx
@@ -25,7 +25,7 @@ export const Link = forwardRef<HTMLAnchorElement, Props>((props, ref) => {
url = urlTo.pathname;
if (href.hash !== undefined) {
- urlTo.hash = href.hash;
+ urlTo.hash = encodeURIComponent(href.hash);
url = `${urlTo.pathname}${urlTo.hash}`;
}
break;