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

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