Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Jump links don't work in preview after first click #4714

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/builder/app/canvas/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const switchPageAndUpdateSystem = (href: string, formData?: FormData) => {
}
}
const search = Object.fromEntries(pageHref.searchParams);
$selectedPageHash.set(pageHref.hash);
$selectedPageHash.set({ hash: pageHref.hash });
selectPage(page.id);
updateSystem(page, { params, search });
savePathInHistory(page.id, pageHref.pathname);
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/app/shared/nano-states/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import type { Page, Pages } from "@webstudio-is/sdk";

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

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

export const $editingPageId = atom<undefined | Page["id"]>();
11 changes: 6 additions & 5 deletions apps/builder/app/shared/pages/use-switch-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const setPageStateFromUrl = () => {
findPageByIdOrPath(searchParams.get("pageId") ?? "", pages)?.id ??
pages.homePage.id;

$selectedPageHash.set(searchParams.get("pageHash") ?? "");
$selectedPageHash.set({ hash: searchParams.get("pageHash") ?? "" });
selectPage(pageId);
};

Expand Down Expand Up @@ -94,7 +94,7 @@ export const useSyncPageUrl = () => {
// Do not navigate on popstate change
if (
searchParamsPageId === page.id &&
searchParamsPageHash === pageHash &&
searchParamsPageHash === pageHash.hash &&
searParamsMode === builderMode
) {
return;
Expand All @@ -104,7 +104,7 @@ export const useSyncPageUrl = () => {
builderPath({
pageId: page.id === pages.homePage.id ? undefined : page.id,
authToken: $authToken.get(),
pageHash: pageHash === "" ? undefined : pageHash,
pageHash: pageHash.hash === "" ? undefined : pageHash.hash,
mode: builderMode === "design" ? undefined : builderMode,
})
);
Expand All @@ -118,13 +118,13 @@ export const useHashLinkSync = () => {
const pageHash = useStore($selectedPageHash);

useEffect(() => {
if (pageHash === "") {
if (pageHash.hash === "") {
// native browser behavior is to do nothing if hash is empty
// remix scroll to top, we emulate native
return;
}

let elementId = decodeURIComponent(pageHash);
let elementId = decodeURIComponent(pageHash.hash);
if (elementId.startsWith("#")) {
elementId = elementId.slice(1);
}
Expand All @@ -134,6 +134,7 @@ export const useHashLinkSync = () => {
if (element !== null) {
element.scrollIntoView();
}

// Remix scroll to top if element not found
// browser do nothing
}, [pageHash]);
Expand Down
Loading