detailPanel.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import {IconClose} from 'sentry/icons/iconClose';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import useKeyPress from 'sentry/utils/useKeyPress';
  8. import useOnClickOutside from 'sentry/utils/useOnClickOutside';
  9. import SlideOverPanel from 'sentry/views/starfish/components/slideOverPanel';
  10. type DetailProps = {
  11. children: React.ReactNode;
  12. detailKey?: string;
  13. onClose?: () => void;
  14. };
  15. type DetailState = {
  16. collapsed: boolean;
  17. };
  18. export default function Detail({children, detailKey, onClose}: DetailProps) {
  19. const [state, setState] = useState<DetailState>({collapsed: true});
  20. const escapeKeyPressed = useKeyPress('Escape');
  21. // Any time the key prop changes (due to user interaction), we want to open the panel
  22. useEffect(() => {
  23. if (detailKey) {
  24. setState({collapsed: false});
  25. }
  26. }, [detailKey]);
  27. const panelRef = useRef<HTMLDivElement>(null);
  28. useOnClickOutside(panelRef, () => {
  29. if (!state.collapsed) {
  30. onClose?.();
  31. setState({collapsed: true});
  32. }
  33. });
  34. useEffect(() => {
  35. if (escapeKeyPressed) {
  36. if (!state.collapsed) {
  37. onClose?.();
  38. setState({collapsed: true});
  39. }
  40. }
  41. // eslint-disable-next-line react-hooks/exhaustive-deps
  42. }, [escapeKeyPressed]);
  43. return (
  44. <SlideOverPanel collapsed={state.collapsed} ref={panelRef}>
  45. <CloseButtonWrapper>
  46. <CloseButton
  47. priority="link"
  48. size="zero"
  49. borderless
  50. aria-label={t('Close Details')}
  51. icon={<IconClose size="sm" />}
  52. onClick={() => {
  53. setState({collapsed: true});
  54. onClose?.();
  55. }}
  56. />
  57. </CloseButtonWrapper>
  58. <DetailWrapper>{children}</DetailWrapper>
  59. </SlideOverPanel>
  60. );
  61. }
  62. const CloseButton = styled(Button)`
  63. color: ${p => p.theme.gray300};
  64. &:hover {
  65. color: ${p => p.theme.gray400};
  66. }
  67. `;
  68. const CloseButtonWrapper = styled('div')`
  69. justify-content: flex-end;
  70. display: flex;
  71. padding: ${space(2)};
  72. `;
  73. const DetailWrapper = styled('div')`
  74. padding: 0 ${space(4)};
  75. `;