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(plus): scroll to anchor hook #10364

Merged
merged 1 commit into from
Jan 23, 2024
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
6 changes: 4 additions & 2 deletions client/src/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@
}
}

:target {
:target,
section[id] {
scroll-margin-top: var(--sticky-header-with-actions-height);
}

.sticky-header-container.without-actions ~ * :target {
.sticky-header-container.without-actions ~ * :target,
.sticky-header-container.without-actions ~ * section[id] {
scroll-margin-top: var(--sticky-header-without-actions-height);
}

Expand Down
14 changes: 14 additions & 0 deletions client/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,17 @@ export function useIsIntersecting(
}, [node, options]);
return isIntersectingState;
}

export const useScrollToAnchor = () => {
const scrolledRef = React.useRef(false);
const { hash } = useLocation();
React.useEffect(() => {
if (hash && !scrolledRef.current) {
const element = document.getElementById(hash.replace("#", ""));
if (element) {
element.scrollIntoView({ behavior: "instant" });
scrolledRef.current = true;
}
}
});
};
2 changes: 2 additions & 0 deletions client/src/plus/offer-overview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useScrollToAnchor } from "../../hooks";
import OfferHero from "./offer-hero";
import OfferOverviewFeatures from "./offer-overview-feature";
import OfferOverviewSubscribe from "./offer-overview-subscribe";

function OfferOverview() {
useScrollToAnchor();
return (
<div className="offer-overview">
<OfferHero />
Expand Down
2 changes: 2 additions & 0 deletions client/src/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import "./index.scss";
import { Manage } from "./manage";
import Newsletter from "./newsletter";
import { ManageAIHelp } from "./ai-help";
import { useScrollToAnchor } from "../hooks";

const OfflineSettings = React.lazy(() => import("./offline-settings"));

export function Settings() {
const pageTitle = "Settings";
useScrollToAnchor();
return (
<>
<OfflineStatusBar />
Expand Down
Loading