Skip to content

Commit bfea0e5

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Fix Element Inspector w/ Start + End Styles
Summary: Fixes how the element inspector displays start and end styles (e.g. `marginStart` or `paddingEnd`). @public Reviewed By: TheSavior Differential Revision: D8607581 fbshipit-source-id: e949927fd86e9b21a8ecf26fbf3e4863ad1486bd
1 parent 537a995 commit bfea0e5

File tree

1 file changed

+88
-33
lines changed

1 file changed

+88
-33
lines changed

Libraries/Inspector/resolveBoxStyle.js

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,103 @@
1010

1111
'use strict';
1212

13+
const I18nManager = require('I18nManager');
14+
1315
/**
14-
* Resolve a style property into it's component parts, e.g.
16+
* Resolve a style property into its component parts.
17+
*
18+
* For example:
1519
*
16-
* resolveProperties('margin', {margin: 5, marginBottom: 10})
17-
* ->
18-
* {top: 5, left: 5, right: 5, bottom: 10}
20+
* > resolveProperties('margin', {margin: 5, marginBottom: 10})
21+
* {top: 5, left: 5, right: 5, bottom: 10}
1922
*
20-
* If none are set, returns false.
23+
* If no parts exist, this returns null.
2124
*/
22-
function resolveBoxStyle(prefix: string, style: Object): ?Object {
23-
const res = {};
24-
const subs = ['top', 'left', 'bottom', 'right'];
25-
let set = false;
26-
subs.forEach(sub => {
27-
res[sub] = style[prefix] || 0;
28-
});
29-
if (style[prefix]) {
30-
set = true;
31-
}
32-
if (style[prefix + 'Vertical']) {
33-
res.top = res.bottom = style[prefix + 'Vertical'];
34-
set = true;
25+
function resolveBoxStyle(
26+
prefix: string,
27+
style: Object,
28+
): ?$ReadOnly<{|
29+
bottom: number,
30+
left: number,
31+
right: number,
32+
top: number,
33+
|}> {
34+
let hasParts = false;
35+
const result = {
36+
bottom: 0,
37+
left: 0,
38+
right: 0,
39+
top: 0,
40+
};
41+
42+
// TODO: Fix issues with multiple properties affecting the same side.
43+
44+
const styleForAll = style[prefix];
45+
if (styleForAll != null) {
46+
for (const key of Object.keys(result)) {
47+
result[key] = styleForAll;
48+
}
49+
hasParts = true;
3550
}
36-
if (style[prefix + 'Horizontal']) {
37-
res.left = res.right = style[prefix + 'Horizontal'];
38-
set = true;
51+
52+
const styleForHorizontal = style[prefix + 'Horizontal'];
53+
if (styleForHorizontal != null) {
54+
result.left = styleForHorizontal;
55+
result.right = styleForHorizontal;
56+
hasParts = true;
57+
} else {
58+
const styleForLeft = style[prefix + 'Left'];
59+
if (styleForLeft != null) {
60+
result.left = styleForLeft;
61+
hasParts = true;
62+
}
63+
64+
const styleForRight = style[prefix + 'Right'];
65+
if (styleForRight != null) {
66+
result.right = styleForRight;
67+
hasParts = true;
68+
}
69+
70+
const styleForEnd = style[prefix + 'End'];
71+
if (styleForEnd != null) {
72+
if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) {
73+
result.left = styleForEnd;
74+
} else {
75+
result.right = styleForEnd;
76+
}
77+
hasParts = true;
78+
}
79+
const styleForStart = style[prefix + 'Start'];
80+
if (styleForStart != null) {
81+
if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) {
82+
result.right = styleForStart;
83+
} else {
84+
result.left = styleForStart;
85+
}
86+
hasParts = true;
87+
}
3988
}
40-
subs.forEach(sub => {
41-
const val = style[prefix + capFirst(sub)];
42-
if (val) {
43-
res[sub] = val;
44-
set = true;
89+
90+
const styleForVertical = style[prefix + 'Vertical'];
91+
if (styleForVertical != null) {
92+
result.bottom = styleForVertical;
93+
result.top = styleForVertical;
94+
hasParts = true;
95+
} else {
96+
const styleForBottom = style[prefix + 'Bottom'];
97+
if (styleForBottom != null) {
98+
result.bottom = styleForBottom;
99+
hasParts = true;
100+
}
101+
102+
const styleForTop = style[prefix + 'Top'];
103+
if (styleForTop != null) {
104+
result.top = styleForTop;
105+
hasParts = true;
45106
}
46-
});
47-
if (!set) {
48-
return;
49107
}
50-
return res;
51-
}
52108

53-
function capFirst(text) {
54-
return text[0].toUpperCase() + text.slice(1);
109+
return hasParts ? result : null;
55110
}
56111

57112
module.exports = resolveBoxStyle;

0 commit comments

Comments
 (0)