2015-06-03 19:53:08 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-06-03 19:53:08 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-06-03 19:53:08 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-06-03 19:53:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-26 20:31:04 +00:00
|
|
|
const BorderBox = require('BorderBox');
|
2018-05-10 22:44:52 +00:00
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
2018-06-26 20:31:04 +00:00
|
|
|
const View = require('View');
|
2015-06-03 19:53:08 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const flattenStyle = require('flattenStyle');
|
2018-06-26 20:31:04 +00:00
|
|
|
const resolveBoxStyle = require('resolveBoxStyle');
|
2015-06-03 19:53:08 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ElementBox extends React.Component<$FlowFixMeProps> {
|
2015-06-03 19:53:08 +00:00
|
|
|
render() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const style = flattenStyle(this.props.style) || {};
|
|
|
|
const margin = resolveBoxStyle('margin', style);
|
|
|
|
const padding = resolveBoxStyle('padding', style);
|
2018-06-26 20:31:04 +00:00
|
|
|
|
|
|
|
const frameStyle = {...this.props.frame};
|
|
|
|
const contentStyle = {
|
2015-06-03 19:53:08 +00:00
|
|
|
width: this.props.frame.width,
|
|
|
|
height: this.props.frame.height,
|
|
|
|
};
|
2018-06-26 20:31:04 +00:00
|
|
|
|
|
|
|
if (margin != null) {
|
|
|
|
frameStyle.top -= margin.top;
|
|
|
|
frameStyle.left -= margin.left;
|
|
|
|
frameStyle.height += margin.top + margin.bottom;
|
|
|
|
frameStyle.width += margin.left + margin.right;
|
|
|
|
|
|
|
|
if (margin.top < 0) {
|
|
|
|
contentStyle.height += margin.top;
|
|
|
|
}
|
|
|
|
if (margin.bottom < 0) {
|
|
|
|
contentStyle.height += margin.bottom;
|
|
|
|
}
|
|
|
|
if (margin.left < 0) {
|
|
|
|
contentStyle.width += margin.left;
|
|
|
|
}
|
|
|
|
if (margin.right < 0) {
|
|
|
|
contentStyle.width += margin.right;
|
|
|
|
}
|
2015-06-03 19:53:08 +00:00
|
|
|
}
|
2018-06-26 20:31:04 +00:00
|
|
|
|
|
|
|
if (padding != null) {
|
|
|
|
contentStyle.width -= padding.left + padding.right;
|
|
|
|
contentStyle.height -= padding.top + padding.bottom;
|
|
|
|
}
|
|
|
|
|
2015-06-03 19:53:08 +00:00
|
|
|
return (
|
|
|
|
<View style={[styles.frame, frameStyle]} pointerEvents="none">
|
|
|
|
<BorderBox box={margin} style={styles.margin}>
|
|
|
|
<BorderBox box={padding} style={styles.padding}>
|
|
|
|
<View style={[styles.content, contentStyle]} />
|
|
|
|
</BorderBox>
|
|
|
|
</BorderBox>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const styles = StyleSheet.create({
|
2015-06-03 19:53:08 +00:00
|
|
|
frame: {
|
|
|
|
position: 'absolute',
|
|
|
|
},
|
|
|
|
content: {
|
2018-06-26 20:31:04 +00:00
|
|
|
backgroundColor: 'rgba(200, 230, 255, 0.8)', // blue
|
2015-06-03 19:53:08 +00:00
|
|
|
},
|
|
|
|
padding: {
|
2018-06-26 20:31:04 +00:00
|
|
|
borderColor: 'rgba(77, 255, 0, 0.3)', // green
|
2015-06-03 19:53:08 +00:00
|
|
|
},
|
|
|
|
margin: {
|
2018-06-26 20:31:04 +00:00
|
|
|
borderColor: 'rgba(255, 132, 0, 0.3)', // orange
|
2015-06-03 19:53:08 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = ElementBox;
|