Projekt

Obecné

Profil

Stáhnout (5 KB) Statistiky
| Větev: | Tag: | Revize:
1
import * as React from 'react'
2
import { styled, useTheme } from '@mui/material/styles'
3
import Box from '@mui/material/Box'
4
import Drawer from '@mui/material/Drawer'
5
import CssBaseline from '@mui/material/CssBaseline'
6
import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar'
7
import Toolbar from '@mui/material/Toolbar'
8
import Typography from '@mui/material/Typography'
9
import IconButton from '@mui/material/IconButton'
10
import MenuIcon from '@mui/icons-material/Menu'
11
import { Fragment, FunctionComponent } from 'react'
12
import NavigationMenu from './NavigationMenu'
13
import { Paper, Stack } from '@mui/material'
14
import { useDispatch, useSelector } from 'react-redux'
15
import { toggleTheme } from '../Theme/themeSlice'
16
import { RootState } from '../redux/store'
17

    
18
import DarkModeIcon from '@mui/icons-material/DarkMode'
19
import LightModeIcon from '@mui/icons-material/LightMode'
20
import { setOpen } from './navigationSlice'
21

    
22
const drawerWidth = 240
23

    
24
const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })<{
25
    open?: boolean
26
}>(({ theme, open }) => ({
27
    flexGrow: 1,
28
    // padding: theme.spacing(3),
29
    transition: theme.transitions.create('margin', {
30
        easing: theme.transitions.easing.sharp,
31
        duration: theme.transitions.duration.leavingScreen,
32
    }),
33
    marginLeft: `-${drawerWidth}px`,
34
    ...(open && {
35
        transition: theme.transitions.create('margin', {
36
            easing: theme.transitions.easing.easeOut,
37
            duration: theme.transitions.duration.enteringScreen,
38
        }),
39
        marginLeft: 0,
40
    }),
41
}))
42

    
43
interface AppBarProps extends MuiAppBarProps {
44
    open?: boolean
45
}
46

    
47
const AppBar = styled(MuiAppBar, {
48
    shouldForwardProp: (prop) => prop !== 'open',
49
})<AppBarProps>(({ theme, open }) => ({
50
    transition: theme.transitions.create(['margin', 'width'], {
51
        easing: theme.transitions.easing.sharp,
52
        duration: theme.transitions.duration.leavingScreen,
53
    }),
54
    ...(open && {
55
        width: `calc(100% - ${drawerWidth}px)`,
56
        marginLeft: `${drawerWidth}px`,
57
        transition: theme.transitions.create(['margin', 'width'], {
58
            easing: theme.transitions.easing.easeOut,
59
            duration: theme.transitions.duration.enteringScreen,
60
        }),
61
    }),
62
}))
63

    
64
const DrawerHeader = styled('div')(({ theme }) => ({
65
    display: 'flex',
66
    alignItems: 'center',
67
    padding: theme.spacing(0, 1),
68
    // necessary for content to be below app bar
69
    ...theme.mixins.toolbar,
70
    justifyContent: 'flex-end',
71
}))
72

    
73
export interface DrawerProps {
74
    children: React.ReactNode
75
}
76

    
77
const PersistentDrawerLeft: FunctionComponent<DrawerProps> = ({ children }) => {
78
    const open = useSelector((state: RootState) => state.navigation.open)
79

    
80
    const onOpenDrawer = () => {
81
        dispatch(setOpen(true))
82
    }
83

    
84
    const colorThemeMode = useSelector(
85
        (state: RootState) => state.theme.paletteMode
86
    )
87

    
88
    const dispatch = useDispatch()
89

    
90
    const onToggleTheme = () => {
91
        dispatch(toggleTheme())
92
    }
93

    
94
    return (
95
        <Fragment>
96
            <Paper style={{ minHeight: '100vh', borderRadius: 0}}>
97
                <Box sx={{ display: 'flex' }}>
98
                    {/* <CssBaseline /> */}
99
                    <AppBar position="fixed" open={open}>
100
                        <Toolbar>
101
                            <IconButton
102
                                color="inherit"
103
                                aria-label="open drawer"
104
                                onClick={onOpenDrawer}
105
                                edge="start"
106
                                sx={{ ...(open && { display: 'none' }) }}
107
                            >
108
                                <MenuIcon />
109
                            </IconButton>
110
                                <Typography variant="h6" noWrap component="div">
111
                                    Assyrian Toponyms App Prototype
112
                                </Typography>
113
                                <Stack sx={{ml: 'auto'}} alignItems="flex-end">
114
                                {colorThemeMode === 'dark' ? (
115
                                    <IconButton onClick={onToggleTheme}>
116
                                        <LightModeIcon />
117
                                    </IconButton>
118
                                ) : (
119
                                    <IconButton onClick={onToggleTheme}>
120
                                        <DarkModeIcon />
121
                                    </IconButton>
122
                                )}
123
                                </Stack>
124
                        </Toolbar>
125
                    </AppBar>
126
                    <NavigationMenu
127
                        open={open}
128
                        drawerWidth={drawerWidth}
129
                        setOpen={(open: boolean) => dispatch(setOpen(open))}
130
                    />
131
                    <Main open={open} sx={{ mt: 2 }}>
132
                        <DrawerHeader />
133
                        {children}
134
                    </Main>
135
                </Box>
136
            </Paper>
137
        </Fragment>
138
    )
139
}
140

    
141
export default PersistentDrawerLeft
(1-1/4)