Skip to content

Commit 88d4aec

Browse files
authored
fix: Jump links don't work in preview after first click (#4714)
## Description closes #4691 ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 069376f commit 88d4aec

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

apps/builder/app/canvas/interceptor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const switchPageAndUpdateSystem = (href: string, formData?: FormData) => {
5959
}
6060
}
6161
const search = Object.fromEntries(pageHref.searchParams);
62-
$selectedPageHash.set(pageHref.hash);
62+
$selectedPageHash.set({ hash: pageHref.hash });
6363
selectPage(page.id);
6464
updateSystem(page, { params, search });
6565
savePathInHistory(page.id, pageHref.pathname);

apps/builder/app/shared/nano-states/pages.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import type { Page, Pages } from "@webstudio-is/sdk";
33

44
export const $pages = atom<undefined | Pages>(undefined);
55

6-
export const $selectedPageHash = atom<string>("");
6+
export const $selectedPageHash = atom<{ hash: string }>({ hash: "" });
77

88
export const $editingPageId = atom<undefined | Page["id"]>();

apps/builder/app/shared/pages/use-switch-page.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const setPageStateFromUrl = () => {
3636
findPageByIdOrPath(searchParams.get("pageId") ?? "", pages)?.id ??
3737
pages.homePage.id;
3838

39-
$selectedPageHash.set(searchParams.get("pageHash") ?? "");
39+
$selectedPageHash.set({ hash: searchParams.get("pageHash") ?? "" });
4040
selectPage(pageId);
4141
};
4242

@@ -94,7 +94,7 @@ export const useSyncPageUrl = () => {
9494
// Do not navigate on popstate change
9595
if (
9696
searchParamsPageId === page.id &&
97-
searchParamsPageHash === pageHash &&
97+
searchParamsPageHash === pageHash.hash &&
9898
searParamsMode === builderMode
9999
) {
100100
return;
@@ -104,7 +104,7 @@ export const useSyncPageUrl = () => {
104104
builderPath({
105105
pageId: page.id === pages.homePage.id ? undefined : page.id,
106106
authToken: $authToken.get(),
107-
pageHash: pageHash === "" ? undefined : pageHash,
107+
pageHash: pageHash.hash === "" ? undefined : pageHash.hash,
108108
mode: builderMode === "design" ? undefined : builderMode,
109109
})
110110
);
@@ -118,13 +118,13 @@ export const useHashLinkSync = () => {
118118
const pageHash = useStore($selectedPageHash);
119119

120120
useEffect(() => {
121-
if (pageHash === "") {
121+
if (pageHash.hash === "") {
122122
// native browser behavior is to do nothing if hash is empty
123123
// remix scroll to top, we emulate native
124124
return;
125125
}
126126

127-
let elementId = decodeURIComponent(pageHash);
127+
let elementId = decodeURIComponent(pageHash.hash);
128128
if (elementId.startsWith("#")) {
129129
elementId = elementId.slice(1);
130130
}
@@ -134,6 +134,7 @@ export const useHashLinkSync = () => {
134134
if (element !== null) {
135135
element.scrollIntoView();
136136
}
137+
137138
// Remix scroll to top if element not found
138139
// browser do nothing
139140
}, [pageHash]);

0 commit comments

Comments
 (0)