All files / Drawer Drawer.tsx

100% Statements 86/86
98.11% Branches 52/53
100% Functions 16/16
100% Lines 74/74

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1922x 2x   2x 2x 2x 2x 2x 2x 2x             2x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x 18x 18x 18x   18x 18x 18x 18x   18x 12x   12x 2x 2x 2x       12x   12x 12x       18x 18x   18x 1x 1x 1x         18x               18x                       18x                         18x 17x                                                         2x   2x 17x   1x   12x   4x     2x 11x   1x   9x   1x       2x 14x     14x 14x   14x 14x   14x 14x       2x         2x             11x   2x           14x    
import React from "react";
import styled from "styled-components";
import { DrawerProps, DrawerStyleProps } from "./Drawer.types";
import Text from "../Text/Text";
import IconButton from "../IconButton/IconButton";
import { IconX } from "@tabler/icons-react";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import { useDebounce } from "../../helpers/CustomHooks";
import { getColorPalette, useWindowSize } from "../../helpers/helpers";
import colorTokens from "../../tokens/colors.json";
// import ClickAwayListener from "../ClickAwayListener/ClickAwayListener";
 
type DrawerBackdropProps = {
  $backdropStrength: DrawerProps["backdropStrength"];
};
 
const Drawer = ({
  children,
  open = false,
  position = "left",
  size = "medium",
  className,
  style,
  hideOnOutsideClick = true,
  zIndex = 1000,
  backdropStrength = "normal",
  title,
  hasCloseIcon = true,
  color = colorTokens.default.primary.main,
  // Events
  onClose,
  "data-testid": dataTestId,
  "data-testid-backdrop": dataTestIdBackdrop,
  ...props
}: DrawerProps) => {
  const [isOpen, setIsOpen] = React.useState(open);
  const debouncedOpen = useDebounce(open, 300);
  const windowSize = useWindowSize();
  const isMobile = windowSize.width < 480;
 
  React.useEffect(() => {
    setIsOpen(open);
 
    const handleEscapeKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") {
        setIsOpen(false);
        onClose && onClose(e);
      }
    };
 
    if (open) document.addEventListener("keydown", handleEscapeKey);
 
    return () => {
      document.removeEventListener("keydown", handleEscapeKey);
    };
  }, [open, onClose]);
 
  const theme = useTheme().theme;
  const colorPalette = getColorPalette(theme, color);
 
  const _onOutsideClick = (e: React.MouseEvent) => {
    if (hideOnOutsideClick) {
      setIsOpen(false);
      onClose && onClose(e);
    }
  };
 
  const Title =
    title && typeof title === "string" ? (
      <Text color={color} style={{ flexGrow: 1 }} variant="h5">
        {title}
      </Text>
    ) : (
      title
    );
  const CloseIcon = (
    <span style={{ float: "right" }}>
      <IconButton
        alt="Close"
        size="small"
        variant="outlined"
        color={color}
        icon={<IconX />}
        onClick={_onOutsideClick}
      />
    </span>
  );
 
  const headerDrawer = (title || hasCloseIcon) && (
    <StyledHeader
      $color={color}
      $size={size}
      $isMobile={isMobile}
      $colorPalette={colorPalette}
      $backdropStrength={backdropStrength}
    >
      <div style={{ flexGrow: 1 }}>{Title}</div>
      {CloseIcon}
    </StyledHeader>
  );
 
  if (!open && !debouncedOpen) return null;
  return (
    <>
      <StyledDrawer
        $open={isOpen}
        $colorPalette={colorPalette}
        $color={color}
        $position={position}
        $size={size}
        $isMobile={isMobile}
        $backdropStrength={backdropStrength}
        className={className}
        style={style}
        $zIndex={zIndex}
        data-testid={dataTestId}
        {...props}
      >
        {headerDrawer}
        <StyledDrawerContent>{children}</StyledDrawerContent>
      </StyledDrawer>
      {isOpen && (
        <StyledBackdrop
          data-testid={dataTestIdBackdrop}
          $backdropStrength={backdropStrength}
          onClick={_onOutsideClick}
        />
      )}
    </>
  );
};
export default Drawer;
 
const getSize = (size: DrawerStyleProps["$size"]) => {
  switch (size) {
    case "small":
      return 320;
    case "medium":
      return 480;
    case "large":
      return 640;
  }
};
const getBackdropStrength = (strength: DrawerProps["backdropStrength"]) => {
  switch (strength) {
    case "weak":
      return "rgba(0, 0, 0, 0.1)";
    case "normal":
      return "rgba(0, 0, 0, 0.3)";
    case "strong":
      return "rgba(0, 0, 0, 0.5)";
  }
};
 
const StyledDrawer = styled.div<DrawerStyleProps>`
  width: ${(props) => props.$isMobile ? "100%" : `${getSize(props.$size)}px`};
  position: fixed;
  top: 0;
  ${(props) => props.$position}: ${(props) =>
    props.$open ? 0 : `${-1 * getSize(props.$size)}px`};
  height: 100%;
  background-color: ${(props) => props.$colorPalette[props.$color].background};
  color: ${(props) => props.$colorPalette[props.$color].grayScale[11]};
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  transition: ${(props) => `${props.$position} 0.3s ease-in-out;`}  
  z-index: ${(props) => props.$zIndex};
  display: flex;
  flex-direction: column;
`;
const StyledDrawerContent = styled.div`
  padding: 1rem;
  flex-grow: 1;
  overflow-y: auto;
`;
const StyledBackdrop = styled.div<DrawerBackdropProps>`
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 999;
  background-color: ${(props) => getBackdropStrength(props.$backdropStrength)};
`;
const StyledHeader = styled.div<DrawerStyleProps>`
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: ${(props) =>
    `1px solid ${props.$colorPalette[props.$color].grayScale[5]}`};
`;