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

[SDP-1415] scroll to top when displaying a notification banner #187

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions src/helpers/scrollTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type toDirection = "top" | "bottom";

export const scrollTo = (direction: toDirection) => {
const scrollableContainer = document.querySelector(".InnerPage__container");
if (scrollableContainer) {
const scrollToOptions: ScrollToOptions = {
behavior: "smooth",
};

if (direction === "top") {
scrollToOptions.top = 0;
} else if (direction === "bottom") {
scrollToOptions.top = scrollableContainer.scrollHeight;
}

scrollableContainer.scrollTo(scrollToOptions);
}
};
Comment on lines +1 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, we shouldn't use DOM elements manually (document.querySelector). This might have some issues with React rendering. It's better to use useRef and scroll to the element we want to bring into the view.

Example of a hook that could handle it:

import { useEffect, useRef, useState } from "react";

export const useScrollIntoView = (
  isEnabled: boolean,
  scrollEl: React.MutableRefObject<HTMLDivElement | null>,
) => {
  useEffect(() => {
    if (isEnabled) {
      scrollEl?.current?.scrollIntoView({ behavior: "smooth" });
    }
  }, [isEnabled, scrollEl]);
};

Usage:

const scrollToEl = useRef<HTMLDivElement | null>(null);
// This can also be status from React Query
const [isScroll, setIsScroll] = useState(false);

useScrollIntoView(isScroll, scrollToEl);

31 changes: 24 additions & 7 deletions src/pages/DisbursementDraftDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { Badge, Heading, Link, Notification } from "@stellar/design-system";
import { useDispatch } from "react-redux";
Expand Down Expand Up @@ -30,6 +30,7 @@ import { DisbursementInviteMessage } from "components/DisbursementInviteMessage"
import { DisbursementInstructions } from "components/DisbursementInstructions";
import { DisbursementButtons } from "components/DisbursementButtons";
import { ErrorWithExtras } from "components/ErrorWithExtras";
import { scrollTo } from "helpers/scrollTo";

import { DisbursementDraft, DisbursementStep, hasWallet } from "types";

Expand Down Expand Up @@ -70,6 +71,26 @@ export const DisbursementDraftDetails = () => {
const apiError = disbursementDrafts.errorString;
const isLoading = disbursementDetails.status === "PENDING";

useEffect(() => {
if (!apiError && !isCsvUpdatedSuccess && !isResponseSuccess) return;

scrollTo("top");
}, [apiError, isCsvUpdatedSuccess, isResponseSuccess]);

const isFirstRender = useRef(true);
useEffect(() => {
if (!csvFile) return;

if (isFirstRender.current) {
isFirstRender.current = false;
return;
}

setTimeout(() => {
scrollTo("bottom");
}, 100);
}, [csvFile]);

const fetchedDisbursementDraft = disbursementDrafts.items.find(
(p) => p.details.id === draftId,
);
Expand Down Expand Up @@ -332,12 +353,7 @@ export const DisbursementDraftDetails = () => {
</div>

<div className="Notification__buttons">
<Link
role="button"
onClick={() => {
setIsCsvUpdatedSuccess(false);
}}
>
<Link role="button" onClick={() => setIsCsvUpdatedSuccess(false)}>
Dismiss
</Link>
</div>
Expand All @@ -359,6 +375,7 @@ export const DisbursementDraftDetails = () => {
variant={"preview"}
csvFile={csvFile}
onChange={(file) => {
setIsCsvUpdatedSuccess(false);
if (apiError) {
dispatch(clearDisbursementDraftsErrorAction());
}
Expand Down
7 changes: 7 additions & 0 deletions src/pages/DisbursementsNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { NotificationWithButtons } from "components/NotificationWithButtons";
import { InfoTooltip } from "components/InfoTooltip";
import { AccountBalances } from "components/AccountBalances";
import { ErrorWithExtras } from "components/ErrorWithExtras";
import { scrollTo } from "helpers/scrollTo";

import { Disbursement, DisbursementStep, hasWallet } from "types";

Expand Down Expand Up @@ -62,6 +63,12 @@ export const DisbursementsNew = () => {
const apiError =
disbursementDrafts.status === "ERROR" && disbursementDrafts.errorString;

useEffect(() => {
if (!apiError && !isSavedDraftMessageVisible && !isResponseSuccess) return;

scrollTo("top");
}, [isSavedDraftMessageVisible, apiError, isResponseSuccess]);

useEffect(() => {
if (
disbursementDrafts.newDraftId &&
Expand Down