aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/SlideInPanel.tsx
blob: 2c189b5bc083c885aa3a4a990f11cda81e477e75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import Dialog from '@mui/material/Dialog';
import IconButton from '@mui/material/IconButton';
import Paper from '@mui/material/Paper';
import Slide from '@mui/material/Slide';
import { styled } from '@mui/material/styles';
import React, { useCallback, useId, useState } from 'react';

import SlideInDialog from './SlideInDialog';

const SlideInPanelRoot = styled('div', {
  name: 'SlideInPanel-Root',
  shouldForwardProp: (propName) => propName !== 'anchor',
})<{ anchor: 'left' | 'right' }>(({ theme, anchor }) => ({
  position: 'absolute',
  padding: theme.spacing(1),
  top: 0,
  [anchor]: 0,
  maxHeight: '100%',
  maxWidth: '100%',
  overflow: 'hidden',
  display: 'flex',
  flexDirection: 'column',
  alignItems: anchor === 'left' ? 'start' : 'end',
  '.SlideInPanel-drawer': {
    overflow: 'hidden',
    display: 'flex',
    maxWidth: '100%',
    margin: theme.spacing(1),
  },
}));

export default function SlideInPanel({
  anchor,
  dialog,
  title,
  icon,
  iconLabel,
  buttons,
  children,
}: {
  anchor: 'left' | 'right';
  dialog: boolean;
  title: string;
  icon: (show: boolean) => React.ReactNode;
  iconLabel: string;
  buttons: React.ReactNode | ((close: () => void) => React.ReactNode);
  children?: React.ReactNode;
}): JSX.Element {
  const id = useId();
  const [show, setShow] = useState(false);
  const close = useCallback(() => setShow(false), []);

  return (
    <SlideInPanelRoot anchor={anchor}>
      <IconButton
        role="switch"
        aria-checked={show}
        aria-controls={dialog ? undefined : id}
        aria-label={iconLabel}
        onClick={() => setShow(!show)}
      >
        {icon(show)}
      </IconButton>
      {dialog ? (
        <Dialog open={show} onClose={close} maxWidth="xl">
          <SlideInDialog close={close} dialog title={title} buttons={buttons}>
            {children}
          </SlideInDialog>
        </Dialog>
      ) : (
        <Slide
          direction={anchor === 'left' ? 'right' : 'left'}
          in={show}
          id={id}
          mountOnEnter
          unmountOnExit
        >
          <Paper className="SlideInPanel-drawer" elevation={4}>
            <SlideInDialog close={close} title={title} buttons={buttons}>
              {children}
            </SlideInDialog>
          </Paper>
        </Slide>
      )}
    </SlideInPanelRoot>
  );
}

SlideInPanel.defaultProps = {
  children: undefined,
};