EmptyAreaWithCTA.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { css } from '@emotion/css';
  2. import React, { ButtonHTMLAttributes, FC } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { Button, ButtonVariant, IconName, LinkButton, useStyles } from '@grafana/ui';
  5. import { EmptyArea } from './EmptyArea';
  6. export interface EmptyAreaWithCTAProps {
  7. buttonLabel: string;
  8. href?: string;
  9. onButtonClick?: ButtonHTMLAttributes<HTMLButtonElement>['onClick'];
  10. text: string;
  11. buttonIcon?: IconName;
  12. buttonSize?: 'xs' | 'sm' | 'md' | 'lg';
  13. buttonVariant?: ButtonVariant;
  14. showButton?: boolean;
  15. }
  16. export const EmptyAreaWithCTA: FC<EmptyAreaWithCTAProps> = ({
  17. buttonIcon,
  18. buttonLabel,
  19. buttonSize = 'lg',
  20. buttonVariant = 'primary',
  21. onButtonClick,
  22. text,
  23. href,
  24. showButton = true,
  25. }) => {
  26. const styles = useStyles(getStyles);
  27. const commonProps = {
  28. className: styles.button,
  29. icon: buttonIcon,
  30. size: buttonSize,
  31. variant: buttonVariant,
  32. };
  33. return (
  34. <EmptyArea>
  35. <>
  36. <p className={styles.text}>{text}</p>
  37. {showButton &&
  38. (href ? (
  39. <LinkButton href={href} type="button" {...commonProps}>
  40. {buttonLabel}
  41. </LinkButton>
  42. ) : (
  43. <Button onClick={onButtonClick} type="button" {...commonProps}>
  44. {buttonLabel}
  45. </Button>
  46. ))}
  47. </>
  48. </EmptyArea>
  49. );
  50. };
  51. const getStyles = (theme: GrafanaTheme) => {
  52. return {
  53. container: css`
  54. background-color: ${theme.colors.bg2};
  55. color: ${theme.colors.textSemiWeak};
  56. padding: ${theme.spacing.xl};
  57. text-align: center;
  58. `,
  59. text: css`
  60. margin-bottom: ${theme.spacing.md};
  61. `,
  62. button: css`
  63. margin: ${theme.spacing.md} 0 ${theme.spacing.sm};
  64. `,
  65. };
  66. };