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.

AspectRatioDetector.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setAspectRatio } from '../actions';
  5. import DimensionsDetector from './DimensionsDetector';
  6. /**
  7. * AspectRatioDetector component's property types.
  8. */
  9. type Props = {
  10. /**
  11. * The "onDimensionsHandler" handler.
  12. */
  13. _onDimensionsChanged: Function,
  14. /**
  15. * Any nested components.
  16. */
  17. children: React$Node
  18. };
  19. /**
  20. * A root {@link View} which captures the 'onLayout' event and figures out
  21. * the aspect ratio of the app.
  22. */
  23. class AspectRatioDetector extends Component<Props> {
  24. /**
  25. * Renders the root view and it's children.
  26. *
  27. * @returns {Component}
  28. */
  29. render() {
  30. return (
  31. <DimensionsDetector
  32. onDimensionsChanged = { this.props._onDimensionsChanged } >
  33. { this.props.children }
  34. </DimensionsDetector>
  35. );
  36. }
  37. }
  38. /**
  39. * Maps dispatching of the aspect ratio actions to React component props.
  40. *
  41. * @param {Function} dispatch - Redux action dispatcher.
  42. * @private
  43. * @returns {{
  44. * _onDimensionsChanged: Function
  45. * }}
  46. */
  47. function _mapDispatchToProps(dispatch) {
  48. return {
  49. /**
  50. * Handles the "on dimensions changed" event and dispatches aspect ratio
  51. * changed action.
  52. *
  53. * @param {number} width - The new width for the component.
  54. * @param {number} height - The new height for the component.
  55. * @private
  56. * @returns {void}
  57. */
  58. _onDimensionsChanged(width: number, height: number) {
  59. dispatch(setAspectRatio(width, height));
  60. }
  61. };
  62. }
  63. export default connect(undefined, _mapDispatchToProps)(AspectRatioDetector);