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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, ASPECT_RATIO_WIDE } from '../constants';
  6. /**
  7. * Determines whether a specific React {@code Component} decorated into an
  8. * {@link AspectRatioAware} has {@link ASPECT_RATIO_NARROW} as the value of its
  9. * {@code aspectRatio} React prop.
  10. *
  11. * @param {AspectRatioAware} component - An {@link AspectRatioAware} which may
  12. * have an {@code aspectRatio} React prop.
  13. * @returns {boolean}
  14. */
  15. export function isNarrowAspectRatio(component: React$Component<*>) {
  16. return component.props.aspectRatio === ASPECT_RATIO_NARROW;
  17. }
  18. /**
  19. * Decorates a specific React {@code Component} class into an
  20. * {@link AspectRatioAware} which provides the React prop {@code aspectRatio}
  21. * updated on each redux state change.
  22. *
  23. * @param {Class<React$Component>} WrappedComponent - A React {@code Component}
  24. * class to be wrapped.
  25. * @returns {AspectRatioAwareWrapper}
  26. */
  27. export function makeAspectRatioAware(
  28. WrappedComponent: Class<React$Component<*>>
  29. ): Class<React$Component<*>> {
  30. /**
  31. * Renders {@code WrappedComponent} with the React prop {@code aspectRatio}.
  32. */
  33. class AspectRatioAware extends Component<*> {
  34. /**
  35. * Properties of the aspect ratio aware wrapper.
  36. */
  37. static propTypes = {
  38. /**
  39. * Either {@link ASPECT_RATIO_NARROW} or {@link ASPECT_RATIO_WIDE}.
  40. */
  41. aspectRatio: PropTypes.oneOf([
  42. ASPECT_RATIO_NARROW,
  43. ASPECT_RATIO_WIDE
  44. ])
  45. }
  46. /**
  47. * Implement's React render method to wrap the nested component.
  48. *
  49. * @returns {React$Element}
  50. */
  51. render(): React$Element<*> {
  52. return <WrappedComponent { ...this.props } />;
  53. }
  54. }
  55. // $FlowFixMe
  56. return connect(_mapStateToProps)(AspectRatioAware);
  57. }
  58. /**
  59. * Maps (parts of) the redux state to {@link AspectRatioAware} props.
  60. *
  61. * @param {Object} state - The whole redux state.
  62. * @private
  63. * @returns {{
  64. * aspectRatio: Symbol
  65. * }}
  66. */
  67. function _mapStateToProps(state) {
  68. return {
  69. aspectRatio: state['features/base/responsive-ui'].aspectRatio
  70. };
  71. }