settingsNavItem.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {Fragment} from 'react';
  2. import {Link as RouterLink} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Badge from 'sentry/components/badge';
  5. import FeatureBadge from 'sentry/components/featureBadge';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import Tooltip from 'sentry/components/tooltip';
  8. import {t} from 'sentry/locale';
  9. import space from 'sentry/styles/space';
  10. type Props = {
  11. label: React.ReactNode;
  12. to: React.ComponentProps<RouterLink>['to'];
  13. badge?: string | number | null;
  14. id?: string;
  15. index?: boolean;
  16. onClick?: (e: React.MouseEvent) => void;
  17. };
  18. const SettingsNavItem = ({badge, label, index, id, ...props}: Props) => {
  19. const LabelHook = HookOrDefault({
  20. hookName: 'sidebar:item-label',
  21. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  22. });
  23. let renderedBadge: React.ReactNode;
  24. if (badge === 'new') {
  25. renderedBadge = <FeatureBadge type="new" />;
  26. } else if (badge === 'beta') {
  27. renderedBadge = <FeatureBadge type="beta" />;
  28. } else if (badge === 'warning') {
  29. renderedBadge = (
  30. <Tooltip title={t('This setting needs review')} position="right">
  31. <StyledBadge text={badge} type="warning" />
  32. </Tooltip>
  33. );
  34. } else {
  35. renderedBadge = <StyledBadge text={badge} />;
  36. }
  37. return (
  38. <StyledNavItem onlyActiveOnIndex={index} activeClassName="active" {...props}>
  39. <LabelHook id={id}>{label}</LabelHook>
  40. {badge ? renderedBadge : null}
  41. </StyledNavItem>
  42. );
  43. };
  44. const StyledNavItem = styled(RouterLink)`
  45. display: block;
  46. color: ${p => p.theme.gray300};
  47. font-size: 14px;
  48. line-height: 30px;
  49. position: relative;
  50. &.active {
  51. color: ${p => p.theme.textColor};
  52. &:before {
  53. background: ${p => p.theme.active};
  54. }
  55. }
  56. &:hover,
  57. &:focus,
  58. &:active {
  59. color: ${p => p.theme.textColor};
  60. outline: none;
  61. }
  62. &.focus-visible {
  63. outline: none;
  64. background: ${p => p.theme.backgroundSecondary};
  65. padding-left: 15px;
  66. margin-left: -15px;
  67. border-radius: 3px;
  68. &:before {
  69. left: -15px;
  70. }
  71. }
  72. &:before {
  73. position: absolute;
  74. content: '';
  75. display: block;
  76. top: 4px;
  77. left: -30px;
  78. height: 20px;
  79. width: 4px;
  80. background: transparent;
  81. border-radius: 0 2px 2px 0;
  82. }
  83. `;
  84. const StyledBadge = styled(Badge)`
  85. font-weight: 400;
  86. height: auto;
  87. line-height: 1;
  88. font-size: ${p => p.theme.fontSizeExtraSmall};
  89. padding: 3px ${space(0.75)};
  90. `;
  91. export default SettingsNavItem;