banner.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {useState} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Button from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import {IconClose} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. const makeKey = (prefix: string) => `${prefix}-banner-dismissed`;
  10. function dismissBanner(bannerKey: string) {
  11. localStorage.setItem(makeKey(bannerKey), 'true');
  12. }
  13. function useDismissable(bannerKey: string) {
  14. const key = makeKey(bannerKey);
  15. const [value, setValue] = useState(localStorage.getItem(key));
  16. const dismiss = () => {
  17. setValue('true');
  18. dismissBanner(bannerKey);
  19. };
  20. return [value === 'true', dismiss] as const;
  21. }
  22. type BannerWrapperProps = {
  23. backgroundComponent?: React.ReactNode;
  24. backgroundImg?: string;
  25. };
  26. type Props = BannerWrapperProps & {
  27. className?: string;
  28. dismissKey?: string;
  29. isDismissable?: boolean;
  30. subtitle?: string;
  31. title?: string;
  32. };
  33. type BannerType = React.FC<Props> & {
  34. /**
  35. * Helper function to hide banners outside of their usage
  36. */
  37. dismiss: typeof dismissBanner;
  38. };
  39. const Banner: BannerType = ({
  40. title,
  41. subtitle,
  42. isDismissable = true,
  43. dismissKey = 'generic-banner',
  44. className,
  45. backgroundImg,
  46. backgroundComponent,
  47. children,
  48. }) => {
  49. const [dismissed, dismiss] = useDismissable(dismissKey);
  50. if (dismissed) {
  51. return null;
  52. }
  53. return (
  54. <BannerWrapper backgroundImg={backgroundImg} className={className}>
  55. {backgroundComponent}
  56. {isDismissable ? <CloseButton onClick={dismiss} aria-label={t('Close')} /> : null}
  57. <BannerContent>
  58. <BannerTitle>{title}</BannerTitle>
  59. <BannerSubtitle>{subtitle}</BannerSubtitle>
  60. <StyledButtonBar gap={1}>{children}</StyledButtonBar>
  61. </BannerContent>
  62. </BannerWrapper>
  63. );
  64. };
  65. Banner.dismiss = dismissBanner;
  66. const BannerWrapper = styled('div')<BannerWrapperProps>`
  67. ${p =>
  68. p.backgroundImg
  69. ? css`
  70. background: url(${p.backgroundImg});
  71. background-repeat: no-repeat;
  72. background-size: cover;
  73. background-position: center center;
  74. `
  75. : css`
  76. background-color: ${p.theme.gray500};
  77. `}
  78. display: flex;
  79. overflow: hidden;
  80. align-items: center;
  81. justify-content: center;
  82. position: relative;
  83. margin-bottom: ${space(2)};
  84. box-shadow: ${p => p.theme.dropShadowLight};
  85. border-radius: ${p => p.theme.borderRadius};
  86. height: 180px;
  87. color: ${p => p.theme.white};
  88. @media (min-width: ${p => p.theme.breakpoints.small}) {
  89. height: 220px;
  90. }
  91. `;
  92. const BannerContent = styled('div')`
  93. position: absolute;
  94. display: grid;
  95. justify-items: center;
  96. grid-template-rows: repeat(3, max-content);
  97. text-align: center;
  98. padding: ${space(4)};
  99. `;
  100. const BannerTitle = styled('h1')`
  101. margin: 0;
  102. @media (min-width: ${p => p.theme.breakpoints.small}) {
  103. font-size: 40px;
  104. }
  105. `;
  106. const BannerSubtitle = styled('div')`
  107. margin: 0;
  108. @media (min-width: ${p => p.theme.breakpoints.small}) {
  109. font-size: ${p => p.theme.fontSizeExtraLarge};
  110. }
  111. `;
  112. const StyledButtonBar = styled(ButtonBar)`
  113. margin-top: ${space(2)};
  114. width: fit-content;
  115. `;
  116. const CloseButton = styled(Button)`
  117. position: absolute;
  118. display: block;
  119. top: ${space(2)};
  120. right: ${space(2)};
  121. color: ${p => p.theme.white};
  122. cursor: pointer;
  123. z-index: 1;
  124. `;
  125. CloseButton.defaultProps = {
  126. icon: <IconClose />,
  127. ['aria-label']: t('Close'),
  128. priority: 'link',
  129. borderless: true,
  130. size: 'xs',
  131. };
  132. export default Banner;