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.6KB

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