alertLink.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import omit from 'lodash/omit';
  4. import ExternalLink from 'app/components/links/externalLink';
  5. import Link from 'app/components/links/link';
  6. import {IconChevron} from 'app/icons';
  7. import space from 'app/styles/space';
  8. type Size = 'small' | 'normal';
  9. type Priority = 'info' | 'warning' | 'success' | 'error' | 'muted';
  10. type LinkProps = React.ComponentPropsWithoutRef<typeof Link>;
  11. type OtherProps = {
  12. ['data-test-id']?: string;
  13. icon?: string | React.ReactNode;
  14. onClick?: (e: React.MouseEvent) => void;
  15. children?: React.ReactNode;
  16. };
  17. type DefaultProps = {
  18. size: Size;
  19. priority: Priority;
  20. withoutMarginBottom: boolean;
  21. openInNewTab: boolean;
  22. href?: string;
  23. };
  24. type Props = OtherProps & Partial<DefaultProps> & Partial<Pick<LinkProps, 'to'>>;
  25. type StyledLinkProps = DefaultProps &
  26. Partial<Pick<LinkProps, 'to'>> &
  27. Omit<LinkProps, 'to' | 'size'>;
  28. function AlertLink({
  29. size = 'normal',
  30. priority = 'warning',
  31. icon,
  32. children,
  33. onClick,
  34. withoutMarginBottom = false,
  35. openInNewTab = false,
  36. to,
  37. href,
  38. ['data-test-id']: dataTestId,
  39. }: Props) {
  40. return (
  41. <StyledLink
  42. data-test-id={dataTestId}
  43. to={to}
  44. href={href}
  45. onClick={onClick}
  46. size={size}
  47. priority={priority}
  48. withoutMarginBottom={withoutMarginBottom}
  49. openInNewTab={openInNewTab}
  50. >
  51. {icon && <IconWrapper>{icon}</IconWrapper>}
  52. <AlertLinkText>{children}</AlertLinkText>
  53. <IconLink>
  54. <IconChevron direction="right" />
  55. </IconLink>
  56. </StyledLink>
  57. );
  58. }
  59. export default AlertLink;
  60. const StyledLink = styled(({openInNewTab, to, href, ...props}: StyledLinkProps) => {
  61. const linkProps = omit(props, ['withoutMarginBottom', 'priority', 'size']);
  62. if (href) {
  63. return <ExternalLink {...linkProps} href={href} openInNewTab={openInNewTab} />;
  64. }
  65. return <Link {...linkProps} to={to || ''} />;
  66. })`
  67. display: flex;
  68. background-color: ${p => p.theme.alert[p.priority].backgroundLight};
  69. color: ${p => p.theme.textColor};
  70. border: 1px dashed ${p => p.theme.alert[p.priority].border};
  71. padding: ${p => (p.size === 'small' ? `${space(1)} ${space(1.5)}` : space(2))};
  72. margin-bottom: ${p => (p.withoutMarginBottom ? 0 : space(3))};
  73. border-radius: 0.25em;
  74. transition: 0.2s border-color;
  75. &.focus-visible {
  76. outline: none;
  77. box-shadow: ${p => p.theme.alert[p.priority].border}7f 0 0 0 2px;
  78. }
  79. `;
  80. const IconWrapper = styled('span')`
  81. display: flex;
  82. margin: ${space(0.5)} ${space(1.5)} ${space(0.5)} 0;
  83. `;
  84. const IconLink = styled(IconWrapper)`
  85. margin: ${space(0.5)} 0;
  86. `;
  87. const AlertLinkText = styled('div')`
  88. line-height: 1.5;
  89. flex-grow: 1;
  90. `;