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.

AspectRatioAware.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { ASPECT_RATIO_NARROW } from '../constants';
  6. /**
  7. * Decorates given React component class into {@link AspectRatioAwareWrapper}
  8. * which provides the <tt>aspectRatio</tt> property updated on each Redux state
  9. * change.
  10. *
  11. * @param {ReactClass} WrapperComponent - A React component class to be wrapped.
  12. * @returns {AspectRatioAwareWrapper}
  13. */
  14. export function AspectRatioAware(
  15. WrapperComponent: ReactClass<*>): ReactClass<*> {
  16. return connect(_mapStateToProps)(
  17. class AspectRatioAwareWrapper extends Component {
  18. /**
  19. * Properties of the aspect ratio aware wrapper.
  20. */
  21. static propTypes = {
  22. /**
  23. * Either {@link ASPECT_RATIO_NARROW} or
  24. * {@link ASPECT_RATIO_WIDE}.
  25. */
  26. aspectRatio: PropTypes.symbol
  27. }
  28. /**
  29. * Implement's React render method to wrap the nested component.
  30. *
  31. * @returns {XML}
  32. */
  33. render(): React$Element<*> {
  34. return <WrapperComponent { ...this.props } />;
  35. }
  36. });
  37. }
  38. /**
  39. * Maps Redux state to {@link AspectRatioAwareWrapper} properties.
  40. *
  41. * @param {Object} state - The Redux whole state.
  42. * @returns {{
  43. * aspectRatio: Symbol
  44. * }}
  45. * @private
  46. */
  47. function _mapStateToProps(state) {
  48. return {
  49. aspectRatio: state['features/base/aspect-ratio'].aspectRatio
  50. };
  51. }
  52. /**
  53. * Checks if given React component decorated in {@link AspectRatioAwareWrapper}
  54. * has currently the {@link ASPECT_RATIO_NARROW} set in the aspect ratio
  55. * property.
  56. *
  57. * @param {AspectRatioAwareWrapper} component - A
  58. * {@link AspectRatioAwareWrapper} which has <tt>aspectRation</tt> property.
  59. * @returns {boolean}
  60. */
  61. export function isNarrowAspectRatio(component: ReactClass<*>) {
  62. return component.props.aspectRatio === ASPECT_RATIO_NARROW;
  63. }