Skip to content

Commit 972ee2e

Browse files
Georgios Andreadisfacebook-github-bot
Georgios Andreadis
authored andcommittedMar 13, 2019
Resolve relative size rendering error in inspector (#23804)
Summary: Currently, when relative sizes are given in margin or padding stylings (be it a percentage or an auto measure), the inspector crashes, due to frame rendering not properly handling those kinds of measurements. This PR adds a resolution step for them: * Percentages are evaluated relative to the window size. * I decided to simply not render `auto` margins/paddings, due to the complexities involved (e.g. when the margin is between multiple elements with relative sizes). Since the inspector does not crash anymore on relative sizes on paddings or margins, I believe that this addresses #17496. Fixes #17496 [General] [Fixed] - Fix inspector rendering of relative margins and paddings Pull Request resolved: #23804 Differential Revision: D14437273 Pulled By: cpojer fbshipit-source-id: c9f0f71a2e1b2399a2b2148cef2124787703ead3
1 parent fc3225d commit 972ee2e

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed
 

‎Libraries/Inspector/ElementBox.js

+54-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ const BorderBox = require('BorderBox');
1414
const React = require('React');
1515
const StyleSheet = require('StyleSheet');
1616
const View = require('View');
17+
const Dimensions = require('Dimensions');
1718

1819
const flattenStyle = require('flattenStyle');
1920
const resolveBoxStyle = require('resolveBoxStyle');
2021

2122
class ElementBox extends React.Component<$FlowFixMeProps> {
2223
render() {
2324
const style = flattenStyle(this.props.style) || {};
24-
const margin = resolveBoxStyle('margin', style);
25-
const padding = resolveBoxStyle('padding', style);
25+
let margin = resolveBoxStyle('margin', style);
26+
let padding = resolveBoxStyle('padding', style);
2627

2728
const frameStyle = {...this.props.frame};
2829
const contentStyle = {
@@ -31,6 +32,8 @@ class ElementBox extends React.Component<$FlowFixMeProps> {
3132
};
3233

3334
if (margin != null) {
35+
margin = resolveRelativeSizes(margin);
36+
3437
frameStyle.top -= margin.top;
3538
frameStyle.left -= margin.left;
3639
frameStyle.height += margin.top + margin.bottom;
@@ -51,6 +54,8 @@ class ElementBox extends React.Component<$FlowFixMeProps> {
5154
}
5255

5356
if (padding != null) {
57+
padding = resolveRelativeSizes(padding);
58+
5459
contentStyle.width -= padding.left + padding.right;
5560
contentStyle.height -= padding.top + padding.bottom;
5661
}
@@ -82,4 +87,51 @@ const styles = StyleSheet.create({
8287
},
8388
});
8489

90+
type Style = {
91+
top: number,
92+
right: number,
93+
bottom: number,
94+
left: number,
95+
};
96+
97+
/**
98+
* Resolves relative sizes (percentages and auto) in a style object.
99+
*
100+
* @param style the style to resolve
101+
* @return a modified copy
102+
*/
103+
function resolveRelativeSizes(style: $ReadOnly<Style>): Style {
104+
let resolvedStyle = Object.assign({}, style);
105+
resolveSizeInPlace(resolvedStyle, 'top', 'height');
106+
resolveSizeInPlace(resolvedStyle, 'right', 'width');
107+
resolveSizeInPlace(resolvedStyle, 'bottom', 'height');
108+
resolveSizeInPlace(resolvedStyle, 'left', 'width');
109+
return resolvedStyle;
110+
}
111+
112+
/**
113+
* Resolves the given size of a style object in place.
114+
*
115+
* @param style the style object to modify
116+
* @param direction the direction to resolve (e.g. 'top')
117+
* @param dimension the window dimension that this direction belongs to (e.g. 'height')
118+
*/
119+
function resolveSizeInPlace(
120+
style: Style,
121+
direction: string,
122+
dimension: string,
123+
) {
124+
if (style[direction] !== null && typeof style[direction] === 'string') {
125+
if (style[direction].indexOf('%') !== -1) {
126+
style[direction] =
127+
(parseFloat(style[direction]) / 100.0) *
128+
Dimensions.get('window')[dimension];
129+
}
130+
if (style[direction] === 'auto') {
131+
// Ignore auto sizing in frame drawing due to complexity of correctly rendering this
132+
style[direction] = 0;
133+
}
134+
}
135+
}
136+
85137
module.exports = ElementBox;

0 commit comments

Comments
 (0)
Please sign in to comment.