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

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