2018-02-02 12:21:14 +00:00
|
|
|
// @flow
|
|
|
|
|
2019-11-08 16:42:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-06-02 09:03:17 +00:00
|
|
|
import { StyleSheet, View } from 'react-native';
|
2018-02-02 12:21:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The "onLayout" handler.
|
|
|
|
*/
|
|
|
|
onDimensionsChanged: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Any nested components.
|
|
|
|
*/
|
|
|
|
children: React$Node
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A {@link View} which captures the 'onLayout' event and calls a prop with the
|
|
|
|
* component size.
|
|
|
|
*/
|
2019-11-08 16:42:39 +00:00
|
|
|
export default class DimensionsDetector extends PureComponent<Props> {
|
2018-02-02 12:21:14 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new DimensionsDetector instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._onLayout = this._onLayout.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLayout: (Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the "on layout" View's event and calls the onDimensionsChanged
|
|
|
|
* prop.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The "on layout" event object/structure passed
|
|
|
|
* by react-native.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onLayout({ nativeEvent: { layout: { height, width } } }) {
|
|
|
|
const { onDimensionsChanged } = this.props;
|
|
|
|
|
|
|
|
onDimensionsChanged && onDimensionsChanged(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the root view and it's children.
|
|
|
|
*
|
|
|
|
* @returns {Component}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
onLayout = { this._onLayout }
|
2020-06-02 09:03:17 +00:00
|
|
|
style = { StyleSheet.absoluteFillObject } >
|
2018-02-02 12:21:14 +00:00
|
|
|
{ this.props.children }
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|