CollapseToggle.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { css, cx } from '@emotion/css';
  2. import React, { FC, HTMLAttributes } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { IconSize, useStyles2, Button } from '@grafana/ui';
  5. interface Props extends HTMLAttributes<HTMLButtonElement> {
  6. isCollapsed: boolean;
  7. onToggle: (isCollapsed: boolean) => void;
  8. // Todo: this should be made compulsory for a11y purposes
  9. idControlled?: string;
  10. size?: IconSize;
  11. className?: string;
  12. text?: string;
  13. }
  14. export const CollapseToggle: FC<Props> = ({
  15. isCollapsed,
  16. onToggle,
  17. idControlled,
  18. className,
  19. text,
  20. size = 'xl',
  21. ...restOfProps
  22. }) => {
  23. const styles = useStyles2(getStyles);
  24. return (
  25. <Button
  26. type="button"
  27. fill="text"
  28. aria-expanded={!isCollapsed}
  29. aria-controls={idControlled}
  30. className={cx(styles.expandButton, className)}
  31. icon={isCollapsed ? 'angle-right' : 'angle-down'}
  32. onClick={() => onToggle(!isCollapsed)}
  33. {...restOfProps}
  34. >
  35. {text}
  36. </Button>
  37. );
  38. };
  39. export const getStyles = (theme: GrafanaTheme2) => ({
  40. expandButton: css`
  41. color: ${theme.colors.text.secondary};
  42. margin-right: ${theme.spacing(1)};
  43. `,
  44. });