All files / NavigationBar/NavigationBarGroupItem NavigationBarGroupItem.tsx

100% Statements 48/48
100% Branches 31/31
100% Functions 10/10
100% Lines 44/44

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 1262x         2x 2x 2x 2x 2x   2x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x   64x 64x 64x   64x   64x 40x   64x 64x   64x 3x 3x 3x   64x 2x 2x       64x                                                       2x 2x   2x                     60x       60x         60x           60x         60x               60x           2x    
import React from "react";
import {
  NavigationBarGroupItemProps,
  NavigationBarGroupItemStyleProps,
} from "./NavigationBarGroupItem.types";
import styled from "styled-components";
import Text from "../../Text/Text";
import colorTokens from "../../../tokens/colors.json";
import { useTheme } from "../../ThemeProvider/ThemeProvider";
import { getColorPalette } from "../../../helpers/helpers";
 
const NavigationBarGroupItem = ({
  _index,
  value,
  children,
  leadingIcon,
  trailingIcon,
  className,
  style,
  size,
  selected,
  tabIndex = 0,
  color = colorTokens.default.primary.main,
  // Events
  _getSelectedIndex,
  onClick,
  "data-testid": dataTestId,
}: NavigationBarGroupItemProps) => {
  const [_selected, setSelected] = React.useState(selected);
 
  React.useEffect(() => {
    setSelected(selected);
  }, [selected]);
  const theme = useTheme().theme;
  const colorPalette = getColorPalette(theme, color);
 
  const _onClick = (e: React.MouseEvent | React.KeyboardEvent) => {
    setSelected(true);
    _getSelectedIndex && _getSelectedIndex(_index, value);
    onClick && onClick(e);
  };
  const _handleKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === "Enter" || e.key === " ") {
      _onClick(e);
    }
  };
 
  return (
    <StyledNavBarGroupItem
      tabIndex={tabIndex}
      $selected={_selected}
      $colorPalette={colorPalette}
      $color={color}
      className={className}
      style={style}
      onClick={_onClick}
      onKeyDown={_handleKeyDown}
      data-testid={dataTestId}
    >
      {leadingIcon && (
        <StyledNavBarGrpItemIcon>{leadingIcon}</StyledNavBarGrpItemIcon>
      )}
      {typeof children === "string" ? (
        <Text color={color} variant="span" size={size}>
          {children}
        </Text>
      ) : (
        children
      )}
      {trailingIcon && (
        <StyledNavBarGrpItemIcon>{trailingIcon}</StyledNavBarGrpItemIcon>
      )}
    </StyledNavBarGroupItem>
  );
};
NavigationBarGroupItem.displayName = "NavigationBarGroupItem";
export default NavigationBarGroupItem;
 
const StyledNavBarGroupItem = styled.li<NavigationBarGroupItemStyleProps>`
  display: flex;
  position: relative;
  flex-direction: row;
  gap: 1rem;
  align-items: center;
  cursor: pointer;
  border-radius: 0.5rem;
  padding: 0.4rem 0 0.4rem 1.75rem;
  margin: 0 0.5rem;
  color: ${(props) =>
    props.$selected
      ? props.$colorPalette[props.$color].accentScale[10]
      : props.$colorPalette[props.$color].grayScale[11]};
  background-color: ${(props) =>
    props.$selected
      ? props.$colorPalette[props.$color].accentScale[2]
      : "transparent"};
  &:hover {
    background-color: ${(props) =>
      props.$selected
        ? props.$colorPalette[props.$color].accentScale[3]
        : props.$colorPalette[props.$color].grayScale[1]};
  }
  &:active {
    background-color: ${(props) =>
      props.$selected
        ? props.$colorPalette[props.$color].accentScale[4]
        : props.$colorPalette[props.$color].grayScale[2]};
  }
  &:focus-visible {
    outline: 1px solid ${(props) => props.$colorPalette[props.$color].accentScale[5]};
  }
  &:after {
    content: "";
    position: absolute;
    height: 100%;
    margin-left: 0.43rem;
    border-left: 1px solid
      ${(props) => props.$selected
        ? props.$colorPalette[props.$color].accentScale[5] : props.$colorPalette[props.$color].grayScale[5]};
    top: 0;
    left: 0;
  }
`;
const StyledNavBarGrpItemIcon = styled.span`
  margin-left: auto;
`;