1 | import React, {Children, cloneElement, Component} from 'react';
|
2 |
|
3 | import Header from './header';
|
4 | import Content from './content';
|
5 |
|
6 | const TITLE_RESIZE_END = 20;
|
7 |
|
8 | export default function adaptiveIslandHOC(ComposedComponent) {
|
9 |
|
10 | return class AdaptiveIsland extends Component {
|
11 | static propTypes = ComposedComponent.propTypes;
|
12 |
|
13 | state = {
|
14 | phase: 0
|
15 | };
|
16 |
|
17 | onContentScroll = ({scrollTop}) => {
|
18 | const phase = Math.min(1, scrollTop / TITLE_RESIZE_END);
|
19 | this.setState({phase});
|
20 | };
|
21 |
|
22 | addResizingProps(children) {
|
23 | return Children.map(children, child => {
|
24 | if (!child) {
|
25 | return child;
|
26 | }
|
27 | let props;
|
28 | const {phase} = this.state;
|
29 |
|
30 | if (child.type === Content) {
|
31 | props = {onScroll: this.onContentScroll, bottomBorder: true};
|
32 | }
|
33 |
|
34 | if (child.type === Header) {
|
35 | props = {phase};
|
36 | }
|
37 |
|
38 | return props ? cloneElement(child, props) : child;
|
39 | });
|
40 | }
|
41 |
|
42 | render() {
|
43 | const {children, ...restProps} = this.props;
|
44 |
|
45 | return (
|
46 | <ComposedComponent {...restProps}>
|
47 | {this.addResizingProps(children)}
|
48 | </ComposedComponent>
|
49 | );
|
50 | }
|
51 | };
|
52 | }
|