ac2073019d6cce5e7950dc6f92580c38fb1310e5
[arvados-workbench2.git] / src / components / side-panel / side-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { ReactElement } from 'react';
7 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
8 import List from "@material-ui/core/List/List";
9 import ListItem from "@material-ui/core/ListItem/ListItem";
10 import ListItemText from "@material-ui/core/ListItemText/ListItemText";
11 import ListItemIcon from '@material-ui/core/ListItemIcon';
12 import Collapse from "@material-ui/core/Collapse/Collapse";
13
14 import { Typography } from '@material-ui/core';
15
16 export interface SidePanelItem {
17     id: string;
18     name: string;
19     icon: string;
20     active?: boolean;
21     open?: boolean;
22     margin?: boolean;
23     openAble?: boolean;
24 }
25
26 interface SidePanelProps {
27     toggleOpen: (id: string) => void;
28     toggleActive: (id: string) => void;
29     sidePanelItems: SidePanelItem[];
30 }
31
32 class SidePanel extends React.Component<SidePanelProps & WithStyles<CssRules>> {
33     render(): ReactElement<any> {
34         const { classes, toggleOpen, toggleActive, sidePanelItems, children } = this.props;
35         const { listItemText, leftSidePanelContainer, row, list, icon, projectIconMargin, active, activeArrow, inactiveArrow, arrowTransition, arrowRotate } = classes;
36         return (
37             <div className={leftSidePanelContainer}>
38                 <List>
39                     {sidePanelItems.map(it => (
40                         <span key={it.name}>
41                             <ListItem button className={list} onClick={() => toggleActive(it.id)}>
42                                 <span className={row}>
43                                     {it.openAble ? <i onClick={() => toggleOpen(it.id)} className={`${it.active ? activeArrow : inactiveArrow} 
44                                         ${it.open ? `fas fa-caret-down ${arrowTransition}` : `fas fa-caret-down ${arrowRotate}`}`} /> : null}
45                                     <ListItemIcon className={it.active ? active : ''}>
46                                         <i className={`${it.icon} ${icon} ${it.margin ? projectIconMargin : ''}`} />
47                                     </ListItemIcon>
48                                     <ListItemText className={listItemText} primary={<Typography className={it.active ? active : ''}>{it.name}</Typography>} />
49                                 </span>
50                             </ListItem>
51                             {it.openAble ? (
52                                 <Collapse in={it.open} timeout="auto" unmountOnExit>
53                                     {children}
54                                 </Collapse>) : null}
55                         </span>
56                     ))}
57                 </List>
58             </div>
59         );
60     }
61 }
62
63 type CssRules = 'active' | 'listItemText' | 'row' | 'leftSidePanelContainer' | 'list' | 'icon' | 'projectIconMargin' |
64     'activeArrow' | 'inactiveArrow' | 'arrowRotate' | 'arrowTransition';
65
66 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
67     active: {
68         color: '#4285F6',
69     },
70     listItemText: {
71         padding: '0px',
72     },
73     row: {
74         display: 'flex',
75         alignItems: 'center',
76     },
77     activeArrow: {
78         color: '#4285F6',
79         position: 'absolute',
80     },
81     inactiveArrow: {
82         position: 'absolute',
83     },
84     arrowTransition: {
85         transition: 'all 0.1s ease',
86     },
87     arrowRotate: {
88         transition: 'all 0.1s ease',
89         transform: 'rotate(-90deg)',
90     },
91     leftSidePanelContainer: {
92         overflowY: 'auto',
93         minWidth: '240px',
94         whiteSpace: 'nowrap',
95         marginTop: '38px',
96         display: 'flex',
97         flexGrow: 1,
98     },
99     list: {
100         paddingBottom: '5px',
101         paddingTop: '5px',
102         paddingLeft: '14px',
103         minWidth: '240px',
104     },
105     icon: {
106         minWidth: '20px',
107     },
108     projectIconMargin: {
109         marginLeft: '17px',
110     }
111 });
112
113 export default withStyles(styles)(SidePanel);