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

Conversation

marcelosalloum
Copy link
Collaborator

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

@stellar-jenkins
Copy link

stellar-disbursement-platform-frontend-preview is available here:
http://sdp-frontend-pr187.previews.kube001.services.stellar-ops.com/

Comment on lines +1 to +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);
}
};
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);

@marcelosalloum
Copy link
Collaborator Author

I'll hold off on this PR while I work on the v3.0 release. Marking it as DRAFT for now.

@marcelosalloum marcelosalloum marked this pull request as draft November 25, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants