2017-10-21 01:55:17 +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.
|
2017-10-21 01:55:17 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-10-21 01:55:17 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2017-10-21 01:55:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
const {Dimensions, Text, View} = ReactNative;
|
2017-10-21 01:55:17 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
class DimensionsSubscription extends React.Component<
|
|
|
|
{dim: string},
|
|
|
|
{dims: Object},
|
|
|
|
> {
|
2017-10-21 01:55:17 +00:00
|
|
|
state = {
|
|
|
|
dims: Dimensions.get(this.props.dim),
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
Dimensions.addEventListener('change', this._handleDimensionsChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
Dimensions.removeEventListener('change', this._handleDimensionsChange);
|
|
|
|
}
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
_handleDimensionsChange = dimensions => {
|
2017-10-21 01:55:17 +00:00
|
|
|
this.setState({
|
|
|
|
dims: dimensions[this.props.dim],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{JSON.stringify(this.state.dims)}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.title = 'Dimensions';
|
|
|
|
exports.description = 'Dimensions of the viewport';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'window',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <DimensionsSubscription dim="window" />;
|
|
|
|
},
|
2017-10-21 01:55:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'screen',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <DimensionsSubscription dim="screen" />;
|
|
|
|
},
|
2017-10-21 01:55:17 +00:00
|
|
|
},
|
|
|
|
];
|