Skip to content

Commit 5860284

Browse files
Merge pull request SupremeTechnopriest#4 from yrokhlin/offcanvasfix
Refactored OffCanvas to use flexbox vs floats and padding.
2 parents 9f155bf + 30c60dd commit 5860284

File tree

2 files changed

+212
-94
lines changed

2 files changed

+212
-94
lines changed

lib/OffCanvas/OffCanvas.js

+106-49
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ var _radium = require('radium');
1717

1818
var _radium2 = _interopRequireDefault(_radium);
1919

20-
var _reactMotion = require('react-motion');
21-
2220
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2321

2422
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -58,73 +56,93 @@ var OffCanvas = (0, _radium2.default)(_class = (_temp = _class2 = function (_Com
5856
var style = _props.style;
5957
var leftStyle = _props.leftStyle;
6058
var rightStyle = _props.rightStyle;
59+
var leftType = _props.leftType;
60+
var rightType = _props.rightType;
6161
var _state = this.state;
6262
var openLeft = _state.openLeft;
6363
var openRight = _state.openRight;
6464
var expanded = _state.expanded;
6565

66-
var sidebarStyles = function sidebarStyles(width) {
67-
return [styles.sidebar, { width: width }, leftStyle];
68-
};
69-
var mainStyles = function mainStyles(paddingLeft, paddingRight) {
70-
return [styles.content, { paddingLeft: paddingLeft, paddingRight: paddingRight }, style];
71-
};
72-
var auxbarStyles = function auxbarStyles(width) {
73-
return [styles.auxbar, { width: width }, rightStyle];
74-
};
66+
var leftPushStyles = [leftType === 'push' && styles.push, { left: 0 }];
7567

76-
var defaults = {
77-
leftWidth: this._calculateLeft('width'),
78-
rightWidth: this._calculateRight('width'),
79-
paddingLeft: this._calculateLeft('padding'),
80-
paddingRight: this._calculateRight('padding')
81-
};
68+
var rightPushStyles = [rightType === 'push' && styles.push, { right: 0 }];
8269

83-
var springs = {
84-
leftWidth: (0, _reactMotion.spring)(this._calculateLeft('width')),
85-
rightWidth: (0, _reactMotion.spring)(this._calculateRight('width')),
86-
paddingLeft: (0, _reactMotion.spring)(this._calculateLeft('padding')),
87-
paddingRight: (0, _reactMotion.spring)(this._calculateRight('padding'))
70+
var containerStyles = function containerStyles(marginLeft) {
71+
return [styles.container, { marginLeft: marginLeft }];
72+
},
73+
sidebarStyles = function sidebarStyles(width) {
74+
return [styles.sidebar, { width: width }, leftType === 'overlay' && styles.overlay, leftPushStyles, leftStyle];
75+
},
76+
mainStyles = [styles.content, style],
77+
auxbarStyles = function auxbarStyles(width) {
78+
return [styles.auxbar, { width: width }, rightType === 'overlay' && styles.overlay, rightPushStyles, rightStyle];
8879
};
8980

9081
return _react2.default.createElement(
9182
'div',
92-
{ style: styles.container },
83+
{ style: containerStyles(this._calculatePush()) },
9384
_react2.default.createElement(
9485
'div',
9586
{ style: sidebarStyles(this._calculateLeft('width')) },
9687
this.props.leftSidebar
9788
),
9889
_react2.default.createElement(
9990
'div',
100-
{ style: auxbarStyles(this._calculateRight('width')) },
101-
this.props.rightSidebar
91+
{ style: mainStyles },
92+
this.props.children
10293
),
10394
_react2.default.createElement(
10495
'div',
105-
{ style: mainStyles(this._calculateLeft('padding'), this._calculateRight('padding')) },
106-
this.props.children
96+
{ style: auxbarStyles(this._calculateRight('width')) },
97+
this.props.rightSidebar
10798
)
10899
);
109100
}
110101
}, {
111102
key: 'toggleLeft',
112103
value: function toggleLeft() {
113-
this.setState({ openLeft: !this.state.openLeft });
104+
var _props2 = this.props;
105+
var leftType = _props2.leftType;
106+
var rightType = _props2.rightType;
107+
108+
if (leftType === 'push' && rightType === 'push') {
109+
this.setState({ openLeft: !this.state.openLeft, openRight: false });
110+
} else {
111+
this.setState({ openLeft: !this.state.openLeft });
112+
}
114113
}
115114
}, {
116115
key: 'toggleRight',
117116
value: function toggleRight() {
117+
var _props3 = this.props;
118+
var leftType = _props3.leftType;
119+
var rightType = _props3.rightType;
118120

119-
if (this.state.openRight) {
120-
this.setState({
121-
openRight: false,
122-
expanded: false
123-
});
121+
if (leftType === 'push' && rightType === 'push') {
122+
123+
if (this.state.openRight) {
124+
this.setState({
125+
openRight: false,
126+
expanded: false
127+
});
128+
} else {
129+
this.setState({
130+
openRight: true,
131+
openLeft: false
132+
});
133+
}
124134
} else {
125-
this.setState({
126-
openRight: true
127-
});
135+
136+
if (this.state.openRight) {
137+
this.setState({
138+
openRight: false,
139+
expanded: false
140+
});
141+
} else {
142+
this.setState({
143+
openRight: true
144+
});
145+
}
128146
}
129147
}
130148
}, {
@@ -151,6 +169,29 @@ var OffCanvas = (0, _radium2.default)(_class = (_temp = _class2 = function (_Com
151169
value: function closeRight() {
152170
this.setState({ openRight: false });
153171
}
172+
}, {
173+
key: '_calculatePush',
174+
value: function _calculatePush() {
175+
var _props4 = this.props;
176+
var rightType = _props4.rightType;
177+
var leftType = _props4.leftType;
178+
var _state2 = this.state;
179+
var openLeft = _state2.openLeft;
180+
var openRight = _state2.openRight;
181+
var expanded = _state2.expanded;
182+
183+
if (openLeft && leftType === 'push') {
184+
185+
return this.props.leftWidth;
186+
} else if (openRight && rightType === 'push') {
187+
188+
if (this.state.expanded) {
189+
return this.props.expandedWidth * -1;
190+
}
191+
192+
return this.props.rightWidth * -1;
193+
}
194+
}
154195
}, {
155196
key: '_calculateRight',
156197
value: function _calculateRight(type) {
@@ -159,10 +200,13 @@ var OffCanvas = (0, _radium2.default)(_class = (_temp = _class2 = function (_Com
159200
case 'squeeze':
160201
if (this.state.expanded) return this.props.expandedWidth;
161202
return this.props.rightWidth;
203+
204+
case 'push':
205+
if (this.state.expanded) return this.props.expandedWidth;
206+
return this.props.rightWidth;
207+
162208
case 'overlay':
163-
if (this.state.expanded) {
164-
return type === 'padding' ? 0 : this.props.expandedWidth;
165-
}
209+
if (this.state.expanded) return type === 'padding' ? 0 : this.props.expandedWidth;
166210
return type === 'padding' ? 0 : this.props.rightWidth;
167211
}
168212
} else {
@@ -176,6 +220,8 @@ var OffCanvas = (0, _radium2.default)(_class = (_temp = _class2 = function (_Com
176220
switch (this.props.leftType) {
177221
case 'squeeze':
178222
return this.props.leftWidth;
223+
case 'push':
224+
return this.props.leftWidth;
179225
case 'overlay':
180226
return type === 'padding' ? 0 : this.props.leftWidth;
181227
}
@@ -216,35 +262,46 @@ var OffCanvas = (0, _radium2.default)(_class = (_temp = _class2 = function (_Com
216262
exports.default = OffCanvas;
217263

218264
var styles = {
219-
container: { height: '100%' },
265+
container: {
266+
height: '100%',
267+
width: '100%',
268+
display: 'flex',
269+
flexDirection: 'row',
270+
flex: 1,
271+
transition: 'margin 0.4s ease-out'
272+
},
220273
sidebar: {
221274
width: 0,
222275
height: '100%',
223-
position: 'fixed',
276+
position: 'relative',
224277
paddingLeft: 0,
225-
float: 'left',
226278
zIndex: 0,
227279
overflow: 'hidden',
228-
transition: 'width 0.5s ease'
280+
transition: 'width 0.4s ease-out'
229281
},
230282
auxbar: {
231283
width: 0,
284+
position: 'relative',
232285
height: '100%',
233-
position: 'fixed',
234286
right: 0,
235-
float: 'left',
236287
zIndex: 0,
237288
overflow: 'hidden',
238-
transition: 'width 0.5s ease'
289+
transition: 'width 0.4s ease-out'
290+
},
291+
overlay: {
292+
position: 'absolute',
293+
zIndex: 2
294+
},
295+
push: {
296+
position: 'absolute'
239297
},
240298
content: {
241299
width: 'auto',
242300
height: '100%',
243301
paddingLeft: 0,
244302
zIndex: 1,
245-
overflowX: 'hidden',
303+
flex: 1,
246304
position: 'relative',
247-
pointerEvents: 'none',
248-
transition: 'padding 0.5s ease'
305+
pointerEvents: 'none'
249306
}
250307
};

0 commit comments

Comments
 (0)