Skip to content

Commit 23179f2

Browse files
committed
added the right drawer
1 parent 8dc5fd4 commit 23179f2

File tree

2 files changed

+109
-20
lines changed

2 files changed

+109
-20
lines changed

src/components/Layout/Layout.jsx

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import styles from './styles';
1212
import AppBar from '../AppBar';
1313
import Footer from '../Footer';
1414

15-
// TODO create Layout Library
15+
// FIXME remove once material-ui drawer style is fixed
16+
const isDocked = type => type === 'permanent' || type === 'persistent';
1617

1718
class Layout extends React.PureComponent {
1819
static propTypes = {
@@ -21,16 +22,22 @@ class Layout extends React.PureComponent {
2122
children: PropTypes.element.isRequired,
2223
appBarPosition: PropTypes.string,
2324
stickyFooter: PropTypes.bool,
24-
footerContent: PropTypes.element,
2525
appBarContent: PropTypes.element,
26+
appBarProps: PropTypes.shape({}),
27+
footerContent: PropTypes.element,
28+
footerProps: PropTypes.shape({}),
2629
leftDrawerOpen: PropTypes.bool.isRequired,
2730
onLeftDrawerOpenChange: PropTypes.func,
2831
leftDrawerContent: PropTypes.element,
2932
leftDrawerType: PropTypes.string,
3033
leftDrawerUnder: PropTypes.bool,
31-
appBarProps: PropTypes.shape({}),
3234
leftDrawerProps: PropTypes.shape({}),
33-
footerProps: 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({}),
3441
width: PropTypes.string,
3542
};
3643

@@ -54,6 +61,18 @@ class Layout extends React.PureComponent {
5461
this.props.onLeftDrawerOpenChange(!this.props.leftDrawerOpen);
5562
};
5663

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);
74+
};
75+
5776
render() {
5877
const {
5978
classes: defaultClasses,
@@ -67,6 +86,11 @@ class Layout extends React.PureComponent {
6786
leftDrawerType,
6887
leftDrawerUnder,
6988
leftDrawerProps,
89+
rightDrawerContent,
90+
rightDrawerOpen,
91+
rightDrawerType,
92+
rightDrawerUnder,
93+
rightDrawerProps,
7094
footerContent,
7195
stickyFooter,
7296
footerProps,
@@ -79,37 +103,59 @@ class Layout extends React.PureComponent {
79103

80104
const smallScreen = isWidthDown('xs', width);
81105

82-
const mainShift =
106+
const mainLeftShift =
83107
!smallScreen &&
84108
(leftDrawerType === 'permanent' ||
85109
(leftDrawerOpen && leftDrawerType === 'persistent'));
86110

87-
const mainClassnames = classNames(
88-
classes.main,
89-
{ [`${classes.mainFixedAppBar}`]: appBarPosition === 'fixed' },
90-
{ [`${classes.mainStickyFooter}`]: stickyFooter },
91-
{ [`${classes.mainShift}`]: mainShift },
92-
);
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+
});
93124

94-
const appBarShift =
125+
const appBarLeftShift =
95126
!smallScreen &&
96127
(!leftDrawerUnder &&
97128
((leftDrawerOpen && leftDrawerType === 'persistent') ||
98129
leftDrawerType === 'permanent'));
99130

131+
const appBarRightShift =
132+
!smallScreen &&
133+
(!rightDrawerUnder &&
134+
((rightDrawerOpen && rightDrawerType === 'persistent') ||
135+
rightDrawerType === 'permanent'));
136+
100137
const appBarClassnames = classNames(classes.appBar, {
101-
[`${classes.appBarShift}`]: appBarShift,
138+
[`${classes.appBarShift}`]: appBarLeftShift || appBarRightShift,
139+
[`${classes.appBarLeftShift}`]: appBarLeftShift,
140+
[`${classes.appBarRightShift}`]: appBarRightShift,
141+
[`${classes.appBarLeftRightShift}`]: appBarLeftShift && appBarRightShift,
102142
});
103143

104-
const drawerPaperClassnames = classNames(classes.drawerPaper, {
144+
const leftDrawerPaperClassnames = classNames(classes.drawerPaper, {
105145
[`${classes.drawerPaperUnder}`]: !smallScreen && leftDrawerUnder,
106146
});
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+
});
107152

108153
return (
109154
<div className={classes.layout}>
110155
<AppBar
111156
position={appBarPosition}
112-
onIconClick={this.toggleLeftDrawer}
157+
toggleLeftDrawer={this.toggleLeftDrawer}
158+
toggleRightDrawer={this.toggleRightDrawer}
113159
className={appBarClassnames}
114160
{...appBarProps}
115161
>
@@ -120,7 +166,7 @@ class Layout extends React.PureComponent {
120166
open={leftDrawerOpen}
121167
onRequestClose={this.handleLeftDrawerClose}
122168
type={!smallScreen && leftDrawerType}
123-
classes={{ paper: drawerPaperClassnames }}
169+
classes={{ paper: leftDrawerPaperClassnames }}
124170
{...leftDrawerProps}
125171
>
126172
{!smallScreen && leftDrawerUnder ? (
@@ -129,6 +175,21 @@ class Layout extends React.PureComponent {
129175
{leftDrawerContent}
130176
</Drawer>
131177
) : 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}
191+
</Drawer>
192+
) : null}
132193
<main className={mainClassnames}>{children}</main>
133194
{footerContent ? (
134195
<Footer {...footerProps}>{footerContent}</Footer>
@@ -140,4 +201,5 @@ class Layout extends React.PureComponent {
140201

141202
export default controllable(compose(withStyles(styles), withWidth())(Layout), [
142203
'leftDrawerOpen',
204+
'rightDrawerOpen',
143205
]);

src/components/Layout/styles.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO curry drawerWidth?
12
const drawerWidth = 240;
23

34
const styles = theme => ({
@@ -13,13 +14,24 @@ const styles = theme => ({
1314
}),
1415
},
1516
appBarShift: {
16-
marginLeft: drawerWidth,
17-
width: `calc(100% - ${drawerWidth}px)`,
1817
transition: theme.transitions.create(['margin', 'width'], {
1918
easing: theme.transitions.easing.easeOut,
2019
duration: theme.transitions.duration.enteringScreen,
2120
}),
2221
},
22+
appBarLeftShift: {
23+
marginLeft: drawerWidth,
24+
width: `calc(100% - ${drawerWidth}px)`,
25+
},
26+
appBarRightShift: {
27+
marginRight: drawerWidth,
28+
width: `calc(100% - ${drawerWidth}px)`,
29+
},
30+
appBarLeftRightShift: {
31+
marginLeft: drawerWidth,
32+
marginRight: drawerWidth,
33+
width: `calc(100% - ${drawerWidth * 2}px)`,
34+
},
2335
main: {
2436
paddingTop: '0px',
2537
display: 'flex',
@@ -41,20 +53,35 @@ const styles = theme => ({
4153
flex: 1,
4254
},
4355
mainShift: {
44-
marginLeft: drawerWidth,
45-
width: `calc(100% - ${drawerWidth}px)`,
4656
transition: theme.transitions.create(['margin'], {
4757
easing: theme.transitions.easing.easeOut,
4858
duration: theme.transitions.duration.enteringScreen,
4959
}),
5060
},
61+
mainLeftShift: {
62+
marginLeft: drawerWidth,
63+
width: `calc(100% - ${drawerWidth}px)`,
64+
},
65+
mainRightShift: {
66+
marginRight: drawerWidth,
67+
width: `calc(100% - ${drawerWidth}px)`,
68+
},
69+
mainLeftRightShift: {
70+
marginRight: drawerWidth,
71+
marginLeft: drawerWidth,
72+
width: `calc(100% - ${drawerWidth * 2}px)`,
73+
},
5174
drawerPaper: {
5275
width: drawerWidth,
5376
},
5477
drawerPaperUnder: {
5578
zIndex: '1000',
5679
},
5780
drawerHeader: theme.mixins.toolbar,
81+
rightDrawerDockedFix: {
82+
borderLeft: `1px solid ${theme.palette.text.divider}`,
83+
borderRight: 'none',
84+
},
5885
});
5986

6087
export default styles;

0 commit comments

Comments
 (0)