All files / src/hooks/common useScrollTopIcon.tsx

72.72% Statements 8/11
33.33% Branches 2/6
75% Functions 3/4
70% Lines 7/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54              121x   121x       121x                     121x 121x               121x             121x                          
import React, { useRef } from "react";
import { useInView } from "react-intersection-observer";
import { CustomTheme } from "@theme/v2/palette.interface";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { CaretUpIcon } from "@phosphor-icons/react";
 
export default function useScrollTopIcon() {
  const containerRef = useRef<HTMLDivElement | null>(null);
 
  const { ref, inView } = useInView({
    threshold: 0.1,
  });
 
  const scrollToTop = () => {
    const container = containerRef.current;
    if (container) {
      container.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    }
  };
 
  const scrollButton = (
    <GiveIconButton
      sx={(theme) => buttonStyle(!inView, theme)}
      onClick={scrollToTop}
      size="large"
      variant="filled"
      Icon={CaretUpIcon}
      data-testid="scroll-to-top-button-icon"
    />
  );
  return {
    scrollButton,
    containerRef,
    viewRef: ref,
  };
}
 
const buttonStyle = (visible: boolean, theme: CustomTheme) => ({
  bottom: "32px",
  right: "32px",
  zIndex: 99,
  backgroundColor: theme.palette?.primitive?.neutral["0"] as string,
  borderRadius: `${theme.customs?.radius?.large}px`,
  position: "absolute",
  display: "flex",
  opacity: visible ? 1 : 0,
  visibility: visible ? "visible" : "hidden",
  transition: "opacity 0.3s ease, visibility 0.3s ease",
  boxShadow: `0px 4px 8px 0px ${theme.palette?.primitive?.transparent["darken-10"]}`,
});