aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/design-system/src/components/Badge.tsx
blob: abb8172d5be0111e1b6482898e51455559ebc6ff (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import styled from '@emotion/styled';
import tw from 'twin.macro';
import { themeColor, themeContrast } from '../helpers/theme';
import { ThemeColors } from '../types/theme';

type BadgeVariant = 'default' | 'new' | 'deleted' | 'counter';

const variantList: Record<BadgeVariant, ThemeColors> = {
  default: 'badgeDefault',
  new: 'badgeNew',
  deleted: 'badgeDeleted',
  counter: 'badgeCounter',
};

interface BadgeProps {
  children: string | number;
  className?: string;
  title?: string;
  variant?: BadgeVariant;
}

export function Badge({ className, children, title, variant = 'default' }: BadgeProps) {
  const commonProps = {
    'aria-label': title ?? children.toString(),
    className,
    role: 'status',
    title,
  };
  if (variant === 'counter') {
    return <StyledCounter {...commonProps}>{children}</StyledCounter>;
  }
  return (
    <StyledBadge variantInfo={variantList[variant]} {...commonProps}>
      {children}
    </StyledBadge>
  );
}

const StyledBadge = styled.span<{
  variantInfo: ThemeColors;
}>`
  ${tw`sw-text-[0.75rem]`};
  ${tw`sw-leading-[0.938rem]`};
  ${tw`sw-font-semibold`};
  ${tw`sw-inline-block`};
  ${tw`sw-whitespace-nowrap`};
  ${tw`sw-px-[0.125rem] sw-py-[0.03125rem]`};
  ${tw`sw-rounded-1/2`};

  color: ${({ variantInfo }) => themeContrast(variantInfo)};
  background-color: ${({ variantInfo }) => themeColor(variantInfo)};
  text-transform: uppercase;

  &:empty {
    ${tw`sw-hidden`}
  }

  .page-actions & {
    ${tw`sw-my-1`};
    ${tw`sw-mx-0`};
  }
`;

const StyledCounter = styled.span`
  ${tw`sw-text-[0.75rem]`};
  ${tw`sw-font-regular`};
  ${tw`sw-px-2`};
  ${tw`sw-inline-flex`};
  ${tw`sw-leading-[1.125rem]`};
  ${tw`sw-items-center sw-justify-center`};
  ${tw`sw-rounded-pill`};

  color: ${themeContrast('badgeCounter')};
  background-color: ${themeColor('badgeCounter')};

  &:empty {
    ${tw`sw-hidden`}
  }
`;