All files / client/src/components ScrollToTopButton.tsx

95.83% Statements 23/24
100% Branches 2/2
100% Functions 14/14
95.65% Lines 22/23

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70            356x 1516x 1516x   1516x 479x 279x   479x 479x     1516x 1314x                   1x                 175x   175x             175x         175x 175x       175x       1x 96x 96x   96x 96x      
import { ReactElement, useEffect, useState } from "react";
import styled from "styled-components";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useTranslation } from "react-i18next";
import { scrollToTop } from "client/utils/scrollToTop";
 
export const ScrollToTopButton = (): ReactElement | null => {
  const { t } = useTranslation();
  const [visible, setVisible] = useState(false);
 
  useEffect(() => {
    const onScroll = (): void => {
      setVisible(window.scrollY > 0);
    };
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
 
  if (!visible) {
    return null;
  }
 
  return (
    <FloatingButton aria-label={t("button.scrollToTop")} onClick={scrollToTop}>
      <Icon aria-hidden="true" icon="chevron-up" />
    </FloatingButton>
  );
};
 
const FloatingButton = styled.button`
  position: fixed;
  bottom: 20px;
  right: 20px;
  border-radius: 100px;
  border: none;
  cursor: pointer;
  width: 48px;
  height: 48px;
  box-shadow: ${(props) => props.theme.shadowHigher};
 
  @media (max-width: ${(props) => props.theme.breakpointPhone}) {
    width: 36px;
    height: 36px;
    bottom: 12px;
    right: 12px;
  }
 
  background: ${(props) => props.theme.buttonPrimaryBackground};
  z-index: 2;
 
  &:hover,
  &:focus-visible {
    background: ${(props) => props.theme.buttonPrimaryHover};
    color: ${(props) => props.theme.textMain};
  }
 
  &:active {
    background: ${(props) => props.theme.buttonPrimaryClicked};
  }
`;
 
const Icon = styled(FontAwesomeIcon)`
  color: ${(props) => props.theme.buttonPrimaryText};
  font-size: ${(props) => props.theme.iconSizeNormal};
 
  @media (max-width: ${(props) => props.theme.breakpointPhone}) {
    font-size: ${(props) => props.theme.iconSizeSmall};
  }
`;