All files / Banner Banner.tsx

100% Statements 51/51
100% Branches 34/34
100% Functions 6/6
100% Lines 46/46

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 2082x 2x 2x 2x 2x   2x   2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x   13x 13x   12x                           12x               12x               13x 13x   1x     13x                               2x   2x                   13x 13x                   2x         13x   2x         13x   10x                             1x                             1x                             1x                                 2x         2x             2x               2x                
import * as React from "react";
import Text from "../Text/Text";
import styled from "styled-components";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import colorTokens from "../../tokens/colors.json";
import { BannerProps, BannerStyleProps } from "./Banner.types";
import { getColorPalette } from "../../helpers/helpers";
 
const Banner: React.FunctionComponent<BannerProps> = ({
  title,
  titleIcon,
  description,
  leadingIcon,
  color = colorTokens.default.primary.main,
  variant = "contained",
  width = "400px",
  style,
  className,
  "data-testid": dataTestId,
  "data-leading-icon": dataLeadingIcon,
  ...props
}: BannerProps) => {
  const theme = useTheme().theme;
  const colorPalette = getColorPalette(theme,color);
 
  const Content = React.useCallback(() => {
    if (!title && !description) return null;
 
    const Title = title ? (
      <StyledTitleContainer>
        {titleIcon && <StyledTitleIcon>{titleIcon}</StyledTitleIcon>}
        {typeof title === "string" ? (
          <Text disableColor variant="span" style={{ marginTop: "0.2rem" }}>
            {title}
          </Text>
        ) : (
          title
        )}
      </StyledTitleContainer>
    ) : null;
 
    const Description =
      description && typeof description === "string" ? (
        <Text disableColor variant="paragraph" size="small">
          {description}
        </Text>
      ) : (
        description
      );
 
    return (
      <StyledContentContainer>
        {Title}
        {Description}
      </StyledContentContainer>
    );
  }, [title, description, titleIcon]);
 
  const LeadingIcon = React.useCallback(() => {
    if (!leadingIcon) return null;
 
    return <StyledIcon data-testid={dataLeadingIcon}>{leadingIcon}</StyledIcon>;
  }, [leadingIcon, dataLeadingIcon]);
 
  return (
    <StyledBannerContainer
      style={style}
      className={className}
      $width={width}
      $color={color}
      $variant={variant}
      $colorPalette={colorPalette}
      data-testid={dataTestId}
      {...props}
    >
      <LeadingIcon />
      <Content />
    </StyledBannerContainer>
  );
};
export default Banner;
 
const StyledBannerContainer = styled.div<BannerStyleProps>`
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
  gap: 1rem;
  justify-content: flex-start;
  align-items: center;
  padding: 0.75rem 1rem 1rem 1rem;
  border-radius: 0.8rem;
  ${props => {
    const properties = propsBannerContainerHandler(props.$variant, props.$color, props.$colorPalette);
    return `
      background-color: ${properties.background.default};
      border: ${properties.border.default};
      border-left: ${properties.borderLeft.default};
      color: ${properties.color.default};
      width: ${props.$width};
      max-width: 100%;
    `;
  }}
`;
const propsBannerContainerHandler = (
  variant: BannerStyleProps["$variant"],
  color: BannerStyleProps["$color"],
  colorPalette: BannerStyleProps["$colorPalette"]
) => {
  return { ...getVariantProps(variant, color, colorPalette) };
};
const getVariantProps = (
  variant: BannerStyleProps["$variant"],
  color: BannerStyleProps["$color"],
  colorPalette: BannerStyleProps["$colorPalette"]
) => {
  switch (variant) {
    case "contained":
      return {
        background: {
          default: colorPalette[color].accentScale[8],
        },
        border: {
          default: `2px solid ${colorPalette[color].accentScale[10]}`,
        },
        borderLeft: {
          default: `0.5rem solid ${colorPalette[color].accentScale[10]}`,
        },
        color: {
          default: colorPalette[color].accentContrast,
        }
      };
    case "outlined":
      return {
        background: {
          default: "transparent",
        },
        border: {
          default: `2px solid ${colorPalette[color].accentScale[8]}`,
        },
        borderLeft: {
          default: `0.5rem solid ${colorPalette[color].accentScale[8]}`,
        },
        color: {
          default: colorPalette[color].accentScale[11],
        }
      };
    case "soft":
      return {
        background: {
          default: colorPalette[color].accentScale[2],
        },
        border: {
          default: `2px solid ${colorPalette[color].accentScale[2]}`,
        },
        borderLeft: {
          default: `0.5rem solid ${colorPalette[color].accentScale[6]}`,
        },
        color: {
          default: colorPalette[color].accentScale[11],
        }
      };
    case "outlined-soft":
      return {
        background: {
          default: colorPalette[color].accentScale[2],
        },
        border: {
          default: `2px solid ${colorPalette[color].accentScale[8]}`,
        },
        borderLeft: {
          default: `0.5rem solid ${colorPalette[color].accentScale[8]}`,
        },
        color: {
          default: colorPalette[color].accentScale[11],
        }
      };
  }
}
 
const StyledContentContainer = styled.div`
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
`;
const StyledTitleContainer = styled.div`
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  flex: 1 1 auto;
`;
const StyledTitleIcon = styled.div`
  width: 18px;
  height: 18px;
  & svg {
    width: 100%;
    height: 100%;
  }
`;
const StyledIcon = styled.div`
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  & svg {
    width: 100%;
    height: 100%;
  }
`;