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

Listen to pathname changes. #17

Open
JoshuaKirby88 opened this issue Aug 15, 2024 · 2 comments
Open

Listen to pathname changes. #17

JoshuaKirby88 opened this issue Aug 15, 2024 · 2 comments

Comments

@JoshuaKirby88
Copy link

Currently, navigating away from the path where the highlighted element is mounted has no effect on the pointerPosition.
Listening to pathname changes can hide the Onborda overlay, and reveal it once navigating back to the correct pathname.

Change 1:

  • Return null if element is falsy.
  const getElementPosition = (element: Element) => {
    if (!element) return null

    const { top, left, width, height } = element.getBoundingClientRect();
    const scrollTop = window.scrollY || document.documentElement.scrollTop;
    const scrollLeft = window.scrollX || document.documentElement.scrollLeft;
    return {
      x: left + scrollLeft,
      y: top + scrollTop,
      width,
      height,
    };
  };

Change 2:

  • Added pathname to the useEffect dependency array.
  import { usePathname } from "next/navigation";
  const pathname = usePathname();

  useEffect(() => {
    if (isOnbordaVisible && currentTourSteps) {
      console.log("Onborda: Current Step Changed");
      const step = currentTourSteps[currentStep];
      if (step) {
        const element = document.querySelector(step.selector) as Element | null;
        if (element) {
          setPointerPosition(getElementPosition(element));
          currentElementRef.current = element;
          setElementToScroll(element);

          const rect = element.getBoundingClientRect();
          const isInViewportWithOffset =
            rect.top >= -offset && rect.bottom <= window.innerHeight + offset;

          if (!isInView || !isInViewportWithOffset) {
            element.scrollIntoView({ behavior: "smooth", block: "center" });
          }
        }
      }
    }
  }, [currentStep, currentTourSteps, isInView, offset, isOnbordaVisible, pathname]);
@uixmat
Copy link
Owner

uixmat commented Aug 15, 2024

I'll consider adding an option @JoshuaKirby88 to do this on a per step basis. The reason i don't want to do this, is i want the tip to transition between elements across route changes - particularly on SaaS products where the pathname may change but much of the layout remains the same / in the viewport

@JoshuaKirby88
Copy link
Author

Right, that makes sense, sorry I was just sharing some things that I was configuring.
Onborda is great by the way.

Just one small thing, I think you forgot to add transition to the border radius of the child motion.div that highlights the viewport.
You can either remove it from the "style" prop and add it to the "initial" and "animate" prop,
or add "transition-[border-radius]" to the className prop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants