PanelHeaderMenuItem.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { css } from '@emotion/css';
  2. import React, { FC, useState } from 'react';
  3. import { PanelMenuItem } from '@grafana/data';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { Icon, IconName, useTheme } from '@grafana/ui';
  6. interface Props {
  7. children?: any;
  8. }
  9. export const PanelHeaderMenuItem: FC<Props & PanelMenuItem> = (props) => {
  10. const [ref, setRef] = useState<HTMLLIElement | null>(null);
  11. const isSubMenu = props.type === 'submenu';
  12. const isDivider = props.type === 'divider';
  13. const theme = useTheme();
  14. const menuIconClassName = css`
  15. margin-right: ${theme.spacing.sm};
  16. a::after {
  17. display: none;
  18. }
  19. `;
  20. const shortcutIconClassName = css`
  21. position: absolute;
  22. top: 7px;
  23. right: ${theme.spacing.xs};
  24. color: ${theme.colors.textWeak};
  25. `;
  26. return isDivider ? (
  27. <li className="divider" />
  28. ) : (
  29. <li className={isSubMenu ? `dropdown-submenu ${getDropdownLocationCssClass(ref)}` : undefined} ref={setRef}>
  30. <a onClick={props.onClick} href={props.href}>
  31. {props.iconClassName && <Icon name={props.iconClassName as IconName} className={menuIconClassName} />}
  32. <span className="dropdown-item-text" aria-label={selectors.components.Panels.Panel.headerItems(props.text)}>
  33. {props.text}
  34. {isSubMenu && <Icon name="angle-right" className={shortcutIconClassName} />}
  35. </span>
  36. {props.shortcut && (
  37. <span className="dropdown-menu-item-shortcut">
  38. <Icon name="keyboard" className={menuIconClassName} /> {props.shortcut}
  39. </span>
  40. )}
  41. </a>
  42. {props.children}
  43. </li>
  44. );
  45. };
  46. function getDropdownLocationCssClass(element: HTMLElement | null) {
  47. if (!element) {
  48. return 'invisible';
  49. }
  50. const wrapperPos = element.parentElement!.getBoundingClientRect();
  51. const pos = element.getBoundingClientRect();
  52. if (pos.width === 0) {
  53. return 'invisible';
  54. }
  55. if (wrapperPos.right + pos.width + 10 > window.innerWidth) {
  56. return 'pull-left';
  57. } else {
  58. return 'pull-right';
  59. }
  60. }