-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: develop
Are you sure you want to change the base?
Conversation
stellar-disbursement-platform-frontend-preview is available here: |
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); | ||
} | ||
}; |
There was a problem hiding this comment.
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);
I'll hold off on this PR while I work on the v3.0 release. Marking it as DRAFT for now. |
What
Scroll to top when displaying a notification banner.
Why
Because it seems nothing happened, but there's actually a message at the top.
Address https://stellarorg.atlassian.net/browse/SDP-1415