From 972ee2edbd4e1c4201da1606bf5a4c5add9f0083 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Wed, 13 Mar 2019 02:00:55 -0700 Subject: [PATCH] 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: https://github.com/facebook/react-native/pull/23804 Differential Revision: D14437273 Pulled By: cpojer fbshipit-source-id: c9f0f71a2e1b2399a2b2148cef2124787703ead3 --- Libraries/Inspector/ElementBox.js | 56 +++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/Libraries/Inspector/ElementBox.js b/Libraries/Inspector/ElementBox.js index d360485bcbe5f5..36327fb31ab57b 100644 --- a/Libraries/Inspector/ElementBox.js +++ b/Libraries/Inspector/ElementBox.js @@ -14,6 +14,7 @@ const BorderBox = require('BorderBox'); const React = require('React'); const StyleSheet = require('StyleSheet'); const View = require('View'); +const Dimensions = require('Dimensions'); const flattenStyle = require('flattenStyle'); const resolveBoxStyle = require('resolveBoxStyle'); @@ -21,8 +22,8 @@ const resolveBoxStyle = require('resolveBoxStyle'); class ElementBox extends React.Component<$FlowFixMeProps> { render() { const style = flattenStyle(this.props.style) || {}; - const margin = resolveBoxStyle('margin', style); - const padding = resolveBoxStyle('padding', style); + let margin = resolveBoxStyle('margin', style); + let padding = resolveBoxStyle('padding', style); const frameStyle = {...this.props.frame}; const contentStyle = { @@ -31,6 +32,8 @@ class ElementBox extends React.Component<$FlowFixMeProps> { }; if (margin != null) { + margin = resolveRelativeSizes(margin); + frameStyle.top -= margin.top; frameStyle.left -= margin.left; frameStyle.height += margin.top + margin.bottom; @@ -51,6 +54,8 @@ class ElementBox extends React.Component<$FlowFixMeProps> { } if (padding != null) { + padding = resolveRelativeSizes(padding); + contentStyle.width -= padding.left + padding.right; contentStyle.height -= padding.top + padding.bottom; } @@ -82,4 +87,51 @@ const styles = StyleSheet.create({ }, }); +type Style = { + top: number, + right: number, + bottom: number, + left: number, +}; + +/** + * Resolves relative sizes (percentages and auto) in a style object. + * + * @param style the style to resolve + * @return a modified copy + */ +function resolveRelativeSizes(style: $ReadOnly