2017-10-13 16:13:46 +00:00
|
|
|
// @flow
|
2017-11-03 20:14:38 +00:00
|
|
|
|
2017-10-13 16:13:46 +00:00
|
|
|
import React, { Component } from 'react';
|
2019-03-21 16:38:29 +00:00
|
|
|
|
|
|
|
import { connect } from '../../redux';
|
2017-10-13 16:13:46 +00:00
|
|
|
|
2017-11-03 20:14:38 +00:00
|
|
|
import { ASPECT_RATIO_NARROW, ASPECT_RATIO_WIDE } from '../constants';
|
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link AspectRatioAware}.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
aspectRatio: ASPECT_RATIO_NARROW | ASPECT_RATIO_WIDE
|
|
|
|
};
|
|
|
|
|
2017-11-03 20:14:38 +00:00
|
|
|
/**
|
2017-11-07 14:28:08 +00:00
|
|
|
* Determines whether a specific React {@code Component} decorated into an
|
|
|
|
* {@link AspectRatioAware} has {@link ASPECT_RATIO_NARROW} as the value of its
|
|
|
|
* {@code aspectRatio} React prop.
|
2017-11-03 20:14:38 +00:00
|
|
|
*
|
2017-11-07 14:28:08 +00:00
|
|
|
* @param {AspectRatioAware} component - An {@link AspectRatioAware} which may
|
|
|
|
* have an {@code aspectRatio} React prop.
|
2017-11-03 20:14:38 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isNarrowAspectRatio(component: React$Component<*>) {
|
|
|
|
return component.props.aspectRatio === ASPECT_RATIO_NARROW;
|
|
|
|
}
|
2017-10-13 16:13:46 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-03 20:14:38 +00:00
|
|
|
* Decorates a specific React {@code Component} class into an
|
|
|
|
* {@link AspectRatioAware} which provides the React prop {@code aspectRatio}
|
|
|
|
* updated on each redux state change.
|
2017-10-13 16:13:46 +00:00
|
|
|
*
|
2017-11-03 20:14:38 +00:00
|
|
|
* @param {Class<React$Component>} WrappedComponent - A React {@code Component}
|
|
|
|
* class to be wrapped.
|
2017-10-13 16:13:46 +00:00
|
|
|
* @returns {AspectRatioAwareWrapper}
|
|
|
|
*/
|
2017-11-03 20:14:38 +00:00
|
|
|
export function makeAspectRatioAware(
|
|
|
|
WrappedComponent: Class<React$Component<*>>
|
|
|
|
): Class<React$Component<*>> {
|
|
|
|
/**
|
|
|
|
* Renders {@code WrappedComponent} with the React prop {@code aspectRatio}.
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class AspectRatioAware extends Component<Props> {
|
2017-11-03 20:14:38 +00:00
|
|
|
/**
|
|
|
|
* Implement's React render method to wrap the nested component.
|
|
|
|
*
|
|
|
|
* @returns {React$Element}
|
|
|
|
*/
|
|
|
|
render(): React$Element<*> {
|
|
|
|
return <WrappedComponent { ...this.props } />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return connect(_mapStateToProps)(AspectRatioAware);
|
2017-10-13 16:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-07 14:28:08 +00:00
|
|
|
* Maps (parts of) the redux state to {@link AspectRatioAware} props.
|
2017-10-13 16:13:46 +00:00
|
|
|
*
|
2017-11-07 14:28:08 +00:00
|
|
|
* @param {Object} state - The whole redux state.
|
|
|
|
* @private
|
2017-10-13 16:13:46 +00:00
|
|
|
* @returns {{
|
2018-02-06 18:14:05 +00:00
|
|
|
* aspectRatio: Symbol
|
2017-10-13 16:13:46 +00:00
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state) {
|
|
|
|
return {
|
2018-02-06 18:14:05 +00:00
|
|
|
aspectRatio: state['features/base/responsive-ui'].aspectRatio
|
2017-10-13 16:13:46 +00:00
|
|
|
};
|
|
|
|
}
|