All files / Avatar Avatar.tsx

98% Statements 49/50
96.29% Branches 26/27
100% Functions 7/7
97.77% Lines 44/45

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    2x 2x 2x 2x 2x   2x 11x 11x 11x 11x 11x 11x 11x 11x     11x 11x   11x 11x 11x 11x   9x   2x 2x       11x                             2x         11x 11x                                 2x             11x           2x 11x   10x       1x         2x 11x   1x         8x         1x         1x           2x         11x   10x             1x                                     2x  
import * as React from "react";
import { AvatarProps, AvatarStyledProps } from "./Avatar.types";
import styled from "styled-components";
import colorTokens from "../../tokens/colors.json";
import Text from "../Text/Text";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import { getColorPalette } from "../../helpers/helpers";
 
const Avatar: React.FunctionComponent<AvatarProps> = ({
  src,
  alt,
  initials,
  variant = "contained",
  size = "medium",
  shape = "rounded",
  type = "image",
  color = colorTokens.default.primary.main,
 
  // Test props
  "data-testid": dataTestId,
  ...props
}) => {
  const theme = useTheme().theme;
  const colorPalette = getColorPalette(theme, color);
  const AvatarContent = () => {
    switch (type) {
      case "image":
        return <img src={src} alt={alt} />;
      case "initials":
        const _size = size === "xlarge" || size === "medium" ? "large" : "medium";
        return <Text size={_size} disableColor variant="span">{initials}</Text>;
    }
  };
 
  return (
    <StyledImgContainer
      $shape={shape}
      $size={size}
      $variant={variant}
      $colorPalette={colorPalette}
      $color={color}
      data-testid={dataTestId}
      {...props}
    >
      <AvatarContent />
    </StyledImgContainer>
  );
};
 
const StyledImgContainer = styled.div<AvatarStyledProps>`
  display: flex;
  justify-content: center;
  align-items: center;
  ${(props) => {
    const properties = styleHandler(props.$size, props.$shape, props.$variant, props.$colorPalette, props.$color);
    return `
      border-radius: ${properties.borderRadius};
      width: ${properties.width};
      height: ${properties.height};
      background-color: ${properties.backgroundColor};
      box-shadow: ${properties.boxShadow};
      color: ${properties.color};
      outline: ${properties.outline};
      & img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: ${properties.borderRadius};
      }
    `;
  }}
`;
const styleHandler = (
  size: AvatarStyledProps["$size"],
  shape: AvatarStyledProps["$shape"],
  variant: AvatarStyledProps["$variant"],
  colorPalette: any,
  color: AvatarStyledProps["$color"]
) => {
  return {
    ...sizeHandler(size),
    ...shapeHandler(shape),
    ...variantHandler(variant, colorPalette, color),
  };
};
const shapeHandler = (shape: AvatarStyledProps["$shape"]) => {
  switch (shape) {
    case "rounded":
      return {
        borderRadius: "50%",
      };
    case "square":
      return {
        borderRadius: "8px",
      };
  }
};
const sizeHandler = (size: AvatarStyledProps["$size"]) => {
  switch (size) {
    case "small":
      return {
        width: "3rem",
        height: "3rem",
      };
    case "medium":
      return {
        width: "5rem",
        height: "5rem",
      };
    case "large":
      return {
        width: "7rem",
        height: "7rem",
      };
    case "xlarge":
      return {
        width: "10rem",
        height: "10rem",
      };
  }
};
const variantHandler = (
  variant: AvatarStyledProps["$variant"],
  colorPalette: AvatarStyledProps["$colorPalette"],
  color: AvatarStyledProps["$color"]
) => {
  switch (variant) {
    case "contained":
      return {
        backgroundColor: colorPalette[color].accentScale[8],
        color: colorPalette[color].accentContrast,
        outline: "none" ,
        boxShadow: "none",
      };
    case "outlined":
      return {
        backgroundColor: "transparent",
        color: colorPalette[color].accentScale[10],
        outline: `1px solid ${colorPalette[color].accentScale[8]}`,
        boxShadow: "none",
      };
    case "neumorph":
      return {
        backgroundColor: "transparent",
        color: colorPalette[color].accentScale[10],
        boxShadow: `-6px -6px 14px rgba(255, 255, 255, .7),
          -6px -6px 10px rgba(255, 255, 255, .5),
          6px 6px 8px rgba(255, 255, 255, .075),
          6px 6px 10px rgba(0, 0, 0, .15)`,
        outline: "none",
      }
  }
};
 
export default Avatar;