Skip to content

Commit d258432

Browse files
committed
Refactor griditem styles
1 parent d8e2607 commit d258432

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

lib/GridItem.jsx

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ var GridItem = React.createClass({
1313
mixins: [PureDeepRenderMixin],
1414

1515
propTypes: {
16+
// Children must be only a single element
17+
children: React.PropTypes.element,
18+
1619
// General grid attributes
1720
cols: React.PropTypes.number.isRequired,
1821
containerWidth: React.PropTypes.number.isRequired,
@@ -148,6 +151,42 @@ var GridItem = React.createClass({
148151
return {w, h};
149152
},
150153

154+
/**
155+
* This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
156+
* well when server rendering, and the only way to do that properly is to use percentage width/left because
157+
* we don't know exactly what the browser viewport is.
158+
* Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
159+
* left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
160+
*
161+
* @param {Object} pos Position object with width, height, left, top.
162+
* @return {Object} Style object.
163+
*/
164+
createStyle(pos) {
165+
var style = {
166+
width: pos.width + 'px',
167+
height: pos.height + 'px',
168+
left: pos.left + 'px',
169+
top: pos.top + 'px',
170+
position: 'absolute'
171+
};
172+
173+
// This is used for server rendering.
174+
if (this.props.usePercentages) {
175+
pos.left = utils.perc(pos.left / this.props.containerWidth);
176+
style.left = pos.left;
177+
style.width = utils.perc(pos.width / this.props.containerWidth);
178+
}
179+
180+
// CSS Transforms support
181+
if (this.props.useCSSTransforms) {
182+
utils.setTransform(style, [pos.left, pos.top]);
183+
delete style.left;
184+
delete style.top;
185+
}
186+
187+
return style;
188+
},
189+
151190
/**
152191
* Mix a Draggable instance into a child.
153192
* @param {Element} child Child element.
@@ -262,42 +301,16 @@ var GridItem = React.createClass({
262301
pos.height = this.state.resizing.height;
263302
}
264303

265-
var child = cloneWithProps(React.Children.only(this.props.children), {
304+
// Create the child element. We clone the existing element but modify its className and style.
305+
var child = cloneWithProps(this.props.children, {
266306
// Munge a classname. Use passed in classnames and resizing.
267307
// React with merge the classNames.
268308
className: ['react-grid-item', this.props.className, this.state.resizing ? 'resizing' : '',
269309
this.props.useCSSTransforms ? 'cssTransforms' : ''].join(' '),
270310
// We can set the width and height on the child, but unfortunately we can't set the position.
271-
style: {
272-
width: pos.width + 'px',
273-
height: pos.height + 'px',
274-
left: pos.left + 'px',
275-
top: pos.top + 'px',
276-
position: 'absolute'
277-
}
311+
style: this.createStyle(pos)
278312
});
279313

280-
// This is where we set the grid item's absolute placement. It gets a little tricky because we want to do it
281-
// well when server rendering, and the only way to do that properly is to use percentage width/left because
282-
// we don't know exactly what the browser viewport is.
283-
//
284-
// Unfortunately, CSS Transforms, which are great for performance, break in this instance because a percentage
285-
// left is relative to the item itself, not its container! So we cannot use them on the server rendering pass.
286-
287-
// This is used for server rendering.
288-
if (this.props.usePercentages) {
289-
pos.left = utils.perc(pos.left / p.containerWidth);
290-
child.props.style.left = pos.left;
291-
child.props.style.width = utils.perc(pos.width / p.containerWidth);
292-
}
293-
294-
// CSS Transforms support
295-
if (this.props.useCSSTransforms) {
296-
utils.setTransform(child.props.style, [pos.left, pos.top]);
297-
delete child.props.style.left;
298-
delete child.props.style.top;
299-
}
300-
301314
// Resizable support. This is usually on but the user can toggle it off.
302315
if (this.props.isResizable) {
303316
child = this.mixinResizable(child, pos);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"dependencies": {
3535
"deep-equal": "^1.0.0",
3636
"object-assign": "^2.0.0",
37-
"react-resizable": "^0.2.3",
38-
"react-draggable": "strml/react-draggable#v0.3.1"
37+
"react-draggable": "strml/react-draggable#v0.3.1",
38+
"react-resizable": "^0.3.2"
3939
},
4040
"devDependencies": {
4141
"babel": "^5.0.1",

0 commit comments

Comments
 (0)