toastIndicator.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. import {motion} from 'framer-motion';
  5. import {Indicator} from 'sentry/actionCreators/indicator';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import {IconCheckmark, IconClose} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import space from 'sentry/styles/space';
  10. import testableTransition from 'sentry/utils/testableTransition';
  11. type Props = {
  12. indicator: Indicator;
  13. onDismiss: (indicator: Indicator, event: React.MouseEvent) => void;
  14. className?: string;
  15. };
  16. function ToastIndicator({indicator, onDismiss, className, ...props}: Props) {
  17. let icon: React.ReactNode;
  18. const {options, message, type} = indicator;
  19. const {undo, disableDismiss} = options || {};
  20. const showUndo = typeof undo === 'function';
  21. const handleClick = (e: React.MouseEvent) => {
  22. if (disableDismiss) {
  23. return;
  24. }
  25. if (typeof onDismiss === 'function') {
  26. onDismiss(indicator, e);
  27. }
  28. };
  29. if (type === 'success') {
  30. icon = <IconCheckmark size="lg" isCircled />;
  31. } else if (type === 'error') {
  32. icon = <IconClose size="lg" isCircled />;
  33. }
  34. // TODO(billy): Remove ref- className after removing usage from getsentry
  35. return (
  36. <Toast
  37. onClick={handleClick}
  38. data-test-id={type ? `toast-${type}` : 'toast'}
  39. className={classNames(className, 'ref-toast', `ref-${type}`)}
  40. {...props}
  41. >
  42. {type === 'loading' ? (
  43. <StyledLoadingIndicator mini />
  44. ) : (
  45. <Icon type={type}>{icon}</Icon>
  46. )}
  47. <Message>{message}</Message>
  48. {showUndo && <Undo onClick={undo}>{t('Undo')}</Undo>}
  49. </Toast>
  50. );
  51. }
  52. const Toast = styled(motion.div)`
  53. display: flex;
  54. align-items: center;
  55. height: 40px;
  56. padding: 0 15px 0 10px;
  57. margin-top: 15px;
  58. background: ${p => p.theme.inverted.background};
  59. color: ${p => p.theme.inverted.textColor};
  60. border-radius: 44px 7px 7px 44px;
  61. box-shadow: ${p => p.theme.dropShadowHeavy};
  62. position: relative;
  63. `;
  64. Toast.defaultProps = {
  65. initial: {
  66. opacity: 0,
  67. y: 70,
  68. },
  69. animate: {
  70. opacity: 1,
  71. y: 0,
  72. },
  73. exit: {
  74. opacity: 0,
  75. y: 70,
  76. },
  77. transition: testableTransition({
  78. type: 'spring',
  79. stiffness: 450,
  80. damping: 25,
  81. }),
  82. };
  83. const Icon = styled('div', {shouldForwardProp: p => p !== 'type'})<{type: string}>`
  84. margin-right: ${space(0.75)};
  85. svg {
  86. display: block;
  87. }
  88. color: ${p => (p.type === 'success' ? p.theme.green300 : p.theme.red300)};
  89. `;
  90. const Message = styled('div')`
  91. flex: 1;
  92. `;
  93. const Undo = styled('div')`
  94. display: inline-block;
  95. color: ${p => p.theme.inverted.subText};
  96. padding-left: ${space(2)};
  97. margin-left: ${space(2)};
  98. border-left: 1px solid ${p => p.theme.inverted.innerBorder};
  99. cursor: pointer;
  100. &:hover {
  101. color: ${p => p.theme.inverted.textColor};
  102. }
  103. `;
  104. const StyledLoadingIndicator = styled(LoadingIndicator)`
  105. .loading-indicator {
  106. border-color: ${p => p.theme.inverted.border};
  107. border-left-color: ${p => p.theme.inverted.purple300};
  108. }
  109. `;
  110. export default ToastIndicator;