File size: 3,841 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { css } from 'astroturf';
import React, { useEffect, useRef } from 'react';
import style from 'dom-helpers/css';

import Transition, {
  EXITED,
  ENTERED,
  ENTERING,
  EXITING,
} from '../../src/Transition';

const styles = css`
  .fade {
    opacity: 0;
    transition: opacity 0.5s linear;
  }
  .fade.in {
    opacity: 1;
  }

  .collapse {
    display: none;
  }

  .collapse.in {
    display: block;
  }

  .collapsing {
    position: relative;
    height: 0;
    overflow: hidden;
    transition: 0.35s ease;
    transition-property: height, visibility;
  }
`;

const fadeStyles = {
  [ENTERING]: styles.in,
  [ENTERED]: styles.in,
};

export function Fade(props) {
  const nodeRef = useRef();
  return (
    <Transition
      {...props}
      nodeRef={nodeRef}
      className={styles.fade}
      timeout={500}
    >
      {(status) => (
        <div
          ref={nodeRef}
          className={`${styles.fade} ${fadeStyles[status] || ''}`}
        >
          {props.children}
        </div>
      )}
    </Transition>
  );
}

function getHeight(elem) {
  let value = elem.offsetHeight;
  let margins = ['marginTop', 'marginBottom'];

  return (
    value +
    parseInt(style(elem, margins[0]), 10) +
    parseInt(style(elem, margins[1]), 10)
  );
}

const collapseStyles = {
  [EXITED]: styles.collapse,
  [EXITING]: styles.collapsing,
  [ENTERING]: styles.collapsing,
  [ENTERED]: `${styles.collapse} ${styles.in}`,
};

export class Collapse extends React.Component {
  nodeRef = React.createRef();

  /* -- Expanding -- */
  handleEnter = () => {
    this.nodeRef.current.style.height = '0';
  };

  handleEntering = () => {
    this.nodeRef.current.style.height = `${this.nodeRef.current.scrollHeight}px`;
  };

  handleEntered = () => {
    this.nodeRef.current.style.height = null;
  };

  /* -- Collapsing -- */
  handleExit = () => {
    this.nodeRef.current.style.height = getHeight(this.nodeRef.current) + 'px';
    this.nodeRef.current.offsetHeight; // eslint-disable-line no-unused-expressions
  };

  handleExiting = () => {
    this.nodeRef.current.style.height = '0';
  };

  render() {
    const { children, ...rest } = this.props;
    return (
      <Transition
        {...rest}
        nodeRef={this.nodeRef}
        timeout={350}
        onEnter={this.handleEnter}
        onEntering={this.handleEntering}
        onEntered={this.handleEntered}
        onExit={this.handleExit}
        onExiting={this.handleExiting}
      >
        {(state, props) => (
          <div ref={this.nodeRef} className={collapseStyles[state]} {...props}>
            {children}
          </div>
        )}
      </Transition>
    );
  }
}

export function FadeInnerRef(props) {
  const nodeRef = useMergedRef(props.innerRef);
  return (
    <Transition
      {...props}
      nodeRef={nodeRef}
      className={styles.fade}
      timeout={150}
    >
      {(status) => (
        <div
          ref={nodeRef}
          className={`${styles.fade} ${fadeStyles[status] || ''}`}
        >
          {props.children}
        </div>
      )}
    </Transition>
  );
}

export const FadeForwardRef = React.forwardRef((props, ref) => {
  return <FadeInnerRef {...props} innerRef={ref} />;
});

/**
 * Compose multiple refs, there may be different implementations
 * This one is derived from
 * e.g. https://github.com/react-restart/hooks/blob/ed37bf3dfc8fc1d9234a6d8fe0af94d69fad3b74/src/useMergedRefs.ts
 * Also here are good discussion about this
 * https://github.com/facebook/react/issues/13029
 * @param ref
 * @returns {React.MutableRefObject<undefined>}
 */
function useMergedRef(ref) {
  const nodeRef = React.useRef();
  useEffect(function () {
    if (ref) {
      if (typeof ref === 'function') {
        ref(nodeRef.current);
      } else {
        ref.current = nodeRef.current;
      }
    }
  });
  return nodeRef;
}