All files / Breadcrumbs/BreadcrumbsItem BreadcrumbsItem.tsx

100% Statements 49/49
100% Branches 28/28
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264  2x 2x 2x 2x         2x 2x   2x 28x 27x 27x 27x 27x 27x 27x   27x 27x   27x 4x 4x         27x                             27x                             2x   2x           27x             27x                                             2x             27x         2x           27x   15x 14x                       1x                                   4x 3x                       1x                                   4x 3x                       1x                                   4x 3x                       1x                                     2x 27x   3x         21x         3x            
import * as React from "react";
import Link from "../../Link/Link";
import Text from "../../Text/Text";
import colorTokens from "../../../tokens/colors.json";
import styled from "styled-components";
import {
  BreadcrumbsItemProps,
  BreadcrumbsItemStyledProps,
} from "./BreadcrumbsItem.types";
import { useTheme } from "../../ThemeProvider/ThemeProvider";
import { getColorPalette } from "../../../helpers/helpers";
 
const BreadcrumbsItem: React.FunctionComponent<BreadcrumbsItemProps> = ({
  link,
  title,
  color = colorTokens.default.primary.main,
  size = "medium",
  variant = "contained",
  "data-testid": dataTestId,
  ...props
}: BreadcrumbsItemProps) => {
  const theme = useTheme().theme;
  const colorPalette = getColorPalette(theme, color);
 
  const getPreciseColor = () => {
    if (colorPalette)
      return variant === "contained"
        ? colorPalette[color].accentContrast
        : colorPalette[color].accentScale[10];
  };
 
  const BreadcrumbsItemContent = link ? (
    <Link
      size={size}
      preciseColor={getPreciseColor()}
      variant="hover"
      href={link}
    >
      {title}
    </Link>
  ) : (
    <Text disableColor size={size} variant="span">
      {title}
    </Text>
  );
 
  return (
    <StyledBreadcrumbsItem
      $variant={variant}
      $color={color}
      $colorPalette={colorPalette}
      $size={size}
      $isSelectable={!!link}
      data-testid={dataTestId}
      aria-current={link ? "page" : undefined}
      {...props}
    >
      {BreadcrumbsItemContent}
    </StyledBreadcrumbsItem>
  );
};
export default BreadcrumbsItem;
 
const StyledBreadcrumbsItem = styled.div<BreadcrumbsItemStyledProps>`
  display: inline-flex;
  justify-content: center;
  align-items: center;
  border-radius: 0.5rem;
  ${(props) => {
    const properties = styledHandler(
      props.$variant,
      props.$color,
      props.$colorPalette,
      props.$isSelectable,
      props.$size
    );
    return `
      background-color: ${properties.backgroundColor.default};
      color: ${properties.color.default};
      border: ${properties.border.default};
      padding: ${properties.padding};
      margin: ${properties.margin};
      ${
        props.$isSelectable
          ? `&:hover {
          background-color: ${properties.backgroundColor.hover};
          color: ${properties.color.hover};
          border: ${properties.border.hover};
          }
          &:active {
          background-color: ${properties.backgroundColor.active};
          color: ${properties.color.active};
          border: ${properties.border.active};
          `
          : ""
      }
      `;
  }}
`;
const styledHandler = (
  variant: BreadcrumbsItemStyledProps["$variant"],
  color: BreadcrumbsItemStyledProps["$color"],
  colorPalette: BreadcrumbsItemStyledProps["$colorPalette"],
  isSelectable: BreadcrumbsItemStyledProps["$isSelectable"],
  size: BreadcrumbsItemStyledProps["$size"]
) => {
  return {
    ...variantHandler(variant, color, colorPalette, isSelectable),
    ...sizeHandler(size),
  };
};
const variantHandler = (
  variant: BreadcrumbsItemStyledProps["$variant"],
  color: BreadcrumbsItemStyledProps["$color"],
  colorPalette: BreadcrumbsItemStyledProps["$colorPalette"],
  isSelectable: BreadcrumbsItemStyledProps["$isSelectable"]
) => {
  switch (variant) {
    case "contained":
      if (!isSelectable) {
        return {
          backgroundColor: {
            default: colorPalette[color].grayScale[8],
          },
          color: {
            default: colorPalette[color].accentContrast,
          },
          border: {
            default: `1px solid ${colorPalette[color].grayScale[8]}`,
          },
        };
      }
      return {
        backgroundColor: {
          default: colorPalette[color].accentScale[8],
          hover: colorPalette[color].accentScale[9],
          active: colorPalette[color].accentScale[9],
        },
        color: {
          default: colorPalette[color].accentContrast,
          hover: colorPalette[color].accentContrast,
          active: colorPalette[color].accentContrast,
        },
        border: {
          default: `1px solid ${colorPalette[color].accentScale[8]}`,
          hover: `1px solid ${colorPalette[color].accentScale[9]}`,
          active: `1px solid ${colorPalette[color].accentScale[9]}`,
        },
      };
    case "outlined":
      if (!isSelectable) {
        return {
          backgroundColor: {
            default: "transparent",
          },
          color: {
            default: colorPalette[color].grayScale[10],
          },
          border: {
            default: `1px solid ${colorPalette[color].grayScale[8]}`,
          },
        };
      }
      return {
        backgroundColor: {
          default: "transparent",
          hover: "transparent",
          active: "transparent",
        },
        color: {
          default: colorPalette[color].accentScale[10],
          hover: colorPalette[color].accentScale[10],
          active: colorPalette[color].accentScale[10],
        },
        border: {
          default: `1px solid ${colorPalette[color].accentScale[6]}`,
          hover: `1px solid ${colorPalette[color].accentScale[7]}`,
          active: `1px solid ${colorPalette[color].accentScale[7]}`,
        },
      };
    case "soft":
      if (!isSelectable) {
        return {
          backgroundColor: {
            default: colorPalette[color].grayScale[3],
          },
          color: {
            default: colorPalette[color].grayScale[10],
          },
          border: {
            default: `1px solid ${colorPalette[color].grayScale[3]}`,
          },
        };
      }
      return {
        backgroundColor: {
          default: colorPalette[color].accentScale[2],
          hover: colorPalette[color].accentScale[3],
          active: colorPalette[color].accentScale[3],
        },
        color: {
          default: colorPalette[color].accentScale[10],
          hover: colorPalette[color].accentScale[10],
          active: colorPalette[color].accentScale[10],
        },
        border: {
          default: `1px solid ${colorPalette[color].accentScale[2]}`,
          hover: `1px solid ${colorPalette[color].accentScale[3]}`,
          active: `1px solid ${colorPalette[color].accentScale[3]}`,
        },
      };
    case "text":
      if (!isSelectable) {
        return {
          backgroundColor: {
            default: "transparent",
          },
          color: {
            default: colorPalette[color].grayScale[10],
          },
          border: {
            default: `1px solid transparent`,
          },
        };
      }
      return {
        backgroundColor: {
          default: "transparent",
          hover: "transparent",
          active: "transparent",
        },
        color: {
          default: colorPalette[color].accentScale[10],
          hover: colorPalette[color].accentScale[11],
          active: colorPalette[color].accentScale[11],
        },
        border: {
          default: "1px solid transparent",
          hover: "1px solid transparent",
          active: "1px solid transparent",
        },
      };
  }
};
const sizeHandler = (size: BreadcrumbsItemStyledProps["$size"]) => {
  switch (size) {
    case "small":
      return {
        margin: "0rem 0.25rem",
        padding: "0.25rem 0.5rem",
      };
    case "medium":
      return {
        margin: "0rem 0.5rem",
        padding: "0.25rem 0.5rem",
      };
    case "large":
      return {
        margin: "0rem 0.5rem",
        padding: "0.25rem 0.5rem",
      };
  }
};