You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DimensionsDetector.native.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { StyleSheet, View } from 'react-native';
  4. type Props = {
  5. /**
  6. * The "onLayout" handler.
  7. */
  8. onDimensionsChanged: Function,
  9. /**
  10. * Any nested components.
  11. */
  12. children: React$Node
  13. };
  14. /**
  15. * A {@link View} which captures the 'onLayout' event and calls a prop with the
  16. * component size.
  17. */
  18. export default class DimensionsDetector extends PureComponent<Props> {
  19. /**
  20. * Initializes a new DimensionsDetector instance.
  21. *
  22. * @param {Object} props - The read-only properties with which the new
  23. * instance is to be initialized.
  24. */
  25. constructor(props: Object) {
  26. super(props);
  27. this._onLayout = this._onLayout.bind(this);
  28. }
  29. _onLayout: (Object) => void;
  30. /**
  31. * Handles the "on layout" View's event and calls the onDimensionsChanged
  32. * prop.
  33. *
  34. * @param {Object} event - The "on layout" event object/structure passed
  35. * by react-native.
  36. * @private
  37. * @returns {void}
  38. */
  39. _onLayout({ nativeEvent: { layout: { height, width } } }) {
  40. const { onDimensionsChanged } = this.props;
  41. onDimensionsChanged && onDimensionsChanged(width, height);
  42. }
  43. /**
  44. * Renders the root view and it's children.
  45. *
  46. * @returns {Component}
  47. */
  48. render() {
  49. return (
  50. <View
  51. onLayout = { this._onLayout }
  52. style = { StyleSheet.absoluteFillObject } >
  53. { this.props.children }
  54. </View>
  55. );
  56. }
  57. }