@@ -3,14 +3,17 @@ import PropTypes from 'prop-types';
3
3
import { withStyles } from 'material-ui/styles' ;
4
4
import classNames from 'classnames' ;
5
5
import Drawer from 'material-ui/Drawer' ;
6
+ import compose from 'recompose/compose' ;
7
+ import withWidth , { isWidthDown } from 'material-ui/utils/withWidth' ;
6
8
import controllable from 'react-controllables' ;
7
9
8
10
import styles from './styles' ;
9
11
10
12
import AppBar from '../AppBar' ;
11
13
import Footer from '../Footer' ;
12
14
13
- // TODO create Layout Library
15
+ // FIXME remove once material-ui drawer style is fixed
16
+ const isDocked = type => type === 'permanent' || type === 'persistent' ;
14
17
15
18
class Layout extends React . PureComponent {
16
19
static propTypes = {
@@ -19,73 +22,184 @@ class Layout extends React.PureComponent {
19
22
children : PropTypes . element . isRequired ,
20
23
appBarPosition : PropTypes . string ,
21
24
stickyFooter : PropTypes . bool ,
22
- footerContent : PropTypes . element ,
23
25
appBarContent : PropTypes . element ,
24
- drawerOpen : PropTypes . bool . isRequired ,
25
- onDrawerOpenChange : PropTypes . func ,
26
- drawerContent : PropTypes . element ,
27
26
appBarProps : PropTypes . shape ( { } ) ,
28
- drawerProps : PropTypes . shape ( { } ) ,
27
+ footerContent : PropTypes . element ,
29
28
footerProps : PropTypes . shape ( { } ) ,
29
+ leftDrawerOpen : PropTypes . bool . isRequired ,
30
+ onLeftDrawerOpenChange : PropTypes . func ,
31
+ leftDrawerContent : PropTypes . element ,
32
+ leftDrawerType : PropTypes . string ,
33
+ leftDrawerUnder : PropTypes . bool ,
34
+ leftDrawerProps : PropTypes . shape ( { } ) ,
35
+ rightDrawerOpen : PropTypes . bool . isRequired ,
36
+ onRightDrawerOpenChange : PropTypes . func ,
37
+ rightDrawerContent : PropTypes . element ,
38
+ rightDrawerType : PropTypes . string ,
39
+ rightDrawerUnder : PropTypes . bool ,
40
+ rightDrawerProps : PropTypes . shape ( { } ) ,
41
+ width : PropTypes . string ,
30
42
} ;
31
43
32
44
static defaultProps = {
33
45
title : '' ,
34
- appBarPosition : 'default ' ,
46
+ appBarPosition : 'fixed ' ,
35
47
stickyFooter : false ,
36
- drawerOpen : false ,
48
+ leftDrawerOpen : false ,
49
+ leftDrawerUnder : false ,
37
50
} ;
38
51
39
- handleDrawerClose = ( ) => {
40
- if ( ! this . props . onDrawerOpenChange ) return ;
52
+ handleLeftDrawerClose = ( ) => {
53
+ if ( ! this . props . onLeftDrawerOpenChange ) return ;
41
54
42
- this . props . onDrawerOpenChange ( false ) ;
55
+ this . props . onLeftDrawerOpenChange ( false ) ;
43
56
} ;
44
57
45
- toggleDrawer = ( ) => {
46
- if ( ! this . props . onDrawerOpenChange ) return ;
58
+ toggleLeftDrawer = ( ) => {
59
+ if ( ! this . props . onLeftDrawerOpenChange ) return ;
47
60
48
- this . props . onDrawerOpenChange ( ! this . props . drawerOpen ) ;
61
+ this . props . onLeftDrawerOpenChange ( ! this . props . leftDrawerOpen ) ;
62
+ } ;
63
+
64
+ handleRightDrawerClose = ( ) => {
65
+ if ( ! this . props . onRightDrawerOpenChange ) return ;
66
+
67
+ this . props . onRightDrawerOpenChange ( false ) ;
68
+ } ;
69
+
70
+ toggleRightDrawer = ( ) => {
71
+ if ( ! this . props . onRightDrawerOpenChange ) return ;
72
+
73
+ this . props . onRightDrawerOpenChange ( ! this . props . rightDrawerOpen ) ;
49
74
} ;
50
75
51
76
render ( ) {
52
77
const {
53
78
classes : defaultClasses ,
54
79
overrideClasses,
55
80
children,
56
- appBarPosition,
57
- stickyFooter,
58
- footerContent,
59
81
appBarContent,
60
- drawerContent,
61
- drawerOpen,
82
+ appBarPosition,
62
83
appBarProps,
63
- drawerProps,
84
+ leftDrawerContent,
85
+ leftDrawerOpen,
86
+ leftDrawerType,
87
+ leftDrawerUnder,
88
+ leftDrawerProps,
89
+ rightDrawerContent,
90
+ rightDrawerOpen,
91
+ rightDrawerType,
92
+ rightDrawerUnder,
93
+ rightDrawerProps,
94
+ footerContent,
95
+ stickyFooter,
64
96
footerProps,
97
+ width,
65
98
} = this . props ;
66
99
100
+ // TODO change the way to overrideClasses
101
+ // use classes insted of overrideClasses as material-ui
67
102
const classes = { ...defaultClasses , ...overrideClasses } ;
68
103
69
- const mainClassnames = classNames (
70
- classes . main ,
71
- { [ `${ classes . mainFixedAppBar } ` ] : appBarPosition === 'fixed' } ,
72
- { [ `${ classes . mainStickyFooter } ` ] : stickyFooter } ,
73
- ) ;
104
+ const smallScreen = isWidthDown ( 'xs' , width ) ;
105
+
106
+ const mainLeftShift =
107
+ ! smallScreen &&
108
+ ( leftDrawerType === 'permanent' ||
109
+ ( leftDrawerOpen && leftDrawerType === 'persistent' ) ) ;
110
+
111
+ const mainRightShift =
112
+ ! smallScreen &&
113
+ ( rightDrawerType === 'permanent' ||
114
+ ( rightDrawerOpen && rightDrawerType === 'persistent' ) ) ;
115
+
116
+ const mainClassnames = classNames ( classes . main , {
117
+ [ `${ classes . mainFixedAppBar } ` ] : appBarPosition === 'fixed' ,
118
+ [ `${ classes . mainStickyFooter } ` ] : stickyFooter ,
119
+ [ `${ classes . mainShift } ` ] : mainLeftShift || mainRightShift ,
120
+ [ `${ classes . mainLeftShift } ` ] : mainLeftShift ,
121
+ [ `${ classes . mainRightShift } ` ] : mainRightShift ,
122
+ [ `${ classes . mainLeftRightShift } ` ] : mainLeftShift && mainRightShift ,
123
+ } ) ;
124
+
125
+ const appBarLeftShift =
126
+ ! smallScreen &&
127
+ ( ! leftDrawerUnder &&
128
+ ( ( leftDrawerOpen && leftDrawerType === 'persistent' ) ||
129
+ leftDrawerType === 'permanent' ) ) ;
130
+
131
+ const appBarRightShift =
132
+ ! smallScreen &&
133
+ ( ! rightDrawerUnder &&
134
+ ( ( rightDrawerOpen && rightDrawerType === 'persistent' ) ||
135
+ rightDrawerType === 'permanent' ) ) ;
136
+
137
+ const appBarClassnames = classNames ( classes . appBar , {
138
+ [ `${ classes . appBarShift } ` ] : appBarLeftShift || appBarRightShift ,
139
+ [ `${ classes . appBarLeftShift } ` ] : appBarLeftShift ,
140
+ [ `${ classes . appBarRightShift } ` ] : appBarRightShift ,
141
+ [ `${ classes . appBarLeftRightShift } ` ] : appBarLeftShift && appBarRightShift ,
142
+ } ) ;
143
+
144
+ const leftDrawerPaperClassnames = classNames ( classes . drawerPaper , {
145
+ [ `${ classes . drawerPaperUnder } ` ] : ! smallScreen && leftDrawerUnder ,
146
+ } ) ;
147
+ console . log ( isDocked ( rightDrawerType ) ) ;
148
+ const rightDrawerPaperClassnames = classNames ( classes . drawerPaper , {
149
+ [ `${ classes . drawerPaperUnder } ` ] : ! smallScreen && rightDrawerUnder ,
150
+ [ `${ classes . rightDrawerDockedFix } ` ] : isDocked ( rightDrawerType ) , // FIXME remove once material-ui drawer style is fixed
151
+ } ) ;
152
+
74
153
return (
75
154
< div className = { classes . layout } >
76
- < AppBar { ...appBarProps } position = { appBarPosition } onIconClick = { this . toggleDrawer } >
155
+ < AppBar
156
+ position = { appBarPosition }
157
+ toggleLeftDrawer = { this . toggleLeftDrawer }
158
+ toggleRightDrawer = { this . toggleRightDrawer }
159
+ className = { appBarClassnames }
160
+ { ...appBarProps }
161
+ >
77
162
{ appBarContent }
78
163
</ AppBar >
79
- { drawerContent ? (
80
- < Drawer { ...drawerProps } open = { drawerOpen } onRequestClose = { this . handleDrawerClose } >
81
- { drawerContent }
164
+ { leftDrawerContent ? (
165
+ < Drawer
166
+ open = { leftDrawerOpen }
167
+ onRequestClose = { this . handleLeftDrawerClose }
168
+ type = { ! smallScreen && leftDrawerType }
169
+ classes = { { paper : leftDrawerPaperClassnames } }
170
+ { ...leftDrawerProps }
171
+ >
172
+ { ! smallScreen && leftDrawerUnder ? (
173
+ < div className = { classes . drawerHeader } />
174
+ ) : null }
175
+ { leftDrawerContent }
176
+ </ Drawer >
177
+ ) : null }
178
+ { rightDrawerContent ? (
179
+ < Drawer
180
+ anchor = "right"
181
+ open = { rightDrawerOpen }
182
+ onRequestClose = { this . handleRightDrawerClose }
183
+ type = { ! smallScreen && rightDrawerType }
184
+ classes = { { paper : rightDrawerPaperClassnames } }
185
+ { ...rightDrawerProps }
186
+ >
187
+ { ! smallScreen && rightDrawerUnder ? (
188
+ < div className = { classes . drawerHeader } />
189
+ ) : null }
190
+ { rightDrawerContent }
82
191
</ Drawer >
83
192
) : null }
84
193
< main className = { mainClassnames } > { children } </ main >
85
- { footerContent ? < Footer { ...footerProps } > { footerContent } </ Footer > : null }
194
+ { footerContent ? (
195
+ < Footer { ...footerProps } > { footerContent } </ Footer >
196
+ ) : null }
86
197
</ div >
87
198
) ;
88
199
}
89
200
}
90
201
91
- export default controllable ( withStyles ( styles ) ( Layout ) , [ 'drawerOpen' ] ) ;
202
+ export default controllable ( compose ( withStyles ( styles ) , withWidth ( ) ) ( Layout ) , [
203
+ 'leftDrawerOpen' ,
204
+ 'rightDrawerOpen' ,
205
+ ] ) ;
0 commit comments