|
| 1 | +import * as React from 'react'; |
| 2 | +import Baba from '../../Baba'; |
| 3 | +import Move from '../Move'; |
| 4 | +import { CollectorChildrenAsFunction } from '../../Collector'; |
| 5 | +import SimpleReveal from '../SimpleReveal'; |
| 6 | +import { Duration } from '../types'; |
| 7 | + |
| 8 | +interface ReshapingContainerProps { |
| 9 | + /** |
| 10 | + * This should be a unique identifier across your whole app. |
| 11 | + */ |
| 12 | + id: string; |
| 13 | + |
| 14 | + children: CollectorChildrenAsFunction; |
| 15 | + |
| 16 | + /** |
| 17 | + * Defaults to "div". |
| 18 | + * Any valid HTML tag allowed. |
| 19 | + */ |
| 20 | + as: keyof JSX.IntrinsicElements; |
| 21 | + |
| 22 | + /** |
| 23 | + * Used the same as the CSS property. |
| 24 | + */ |
| 25 | + background?: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * Used the same as the CSS property. |
| 29 | + */ |
| 30 | + boxShadow?: string; |
| 31 | + |
| 32 | + /** |
| 33 | + * Padding. |
| 34 | + * Use only px values, otherwise same as the CSS property. |
| 35 | + */ |
| 36 | + padding?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * Used the same as the CSS property. |
| 40 | + */ |
| 41 | + maxWidth?: string; |
| 42 | + |
| 43 | + /** |
| 44 | + * Used the same as the CSS property. |
| 45 | + */ |
| 46 | + maxHeight?: string; |
| 47 | + |
| 48 | + /** |
| 49 | + * Used the same as the CSS property. |
| 50 | + */ |
| 51 | + minWidth?: string; |
| 52 | + |
| 53 | + /** |
| 54 | + * Used the same as the CSS property. |
| 55 | + */ |
| 56 | + minHeight?: string; |
| 57 | + |
| 58 | + /** |
| 59 | + * Used the same as the CSS property. |
| 60 | + */ |
| 61 | + margin?: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * Takes either "dynamic" or a number in ms. |
| 65 | + * How long the animation should take over {duration}ms. |
| 66 | + * Defaults to "dynamic". |
| 67 | + */ |
| 68 | + duration?: Duration; |
| 69 | + |
| 70 | + /** |
| 71 | + * Used the same as the CSS property. |
| 72 | + */ |
| 73 | + display?: string; |
| 74 | + |
| 75 | + /** |
| 76 | + * Timing function to be used in the transition. |
| 77 | + */ |
| 78 | + timingFunction?: string; |
| 79 | +} |
| 80 | + |
| 81 | +interface ReshapingContainerState { |
| 82 | + renderCount: number; |
| 83 | +} |
| 84 | + |
| 85 | +export default class ReshapingContainer extends React.PureComponent< |
| 86 | + ReshapingContainerProps, |
| 87 | + ReshapingContainerState |
| 88 | +> { |
| 89 | + static defaultProps = { |
| 90 | + as: 'div', |
| 91 | + }; |
| 92 | + |
| 93 | + state: ReshapingContainerState = { |
| 94 | + renderCount: 0, |
| 95 | + }; |
| 96 | + |
| 97 | + /** |
| 98 | + * Incremeent render count every time a render occurs. |
| 99 | + * We're abusing react "key" to trigger animations for now. |
| 100 | + */ |
| 101 | + static getDerivedStateFromProps(_, state) { |
| 102 | + return { |
| 103 | + renderCount: state.renderCount + 1, |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + componentDidMount() { |
| 108 | + if (this.props.padding.indexOf('em') >= 0 || this.props.padding.indexOf('%') >= 0) { |
| 109 | + throw new Error(`Only px values are supported for props.padding in ${this.displayName}`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * We're using this to increase the clip-path box of the Reveal animation so the children contents |
| 115 | + * line up with the parent container in this component. |
| 116 | + */ |
| 117 | + getInversePaddingParts() { |
| 118 | + const parts = this.props.padding.split(' ').map(p => -Number(p.replace('px', ''))); |
| 119 | + |
| 120 | + switch (parts.length) { |
| 121 | + case 1: |
| 122 | + parts.push(parts[0]); |
| 123 | + parts.push(parts[0]); |
| 124 | + parts.push(parts[0]); |
| 125 | + break; |
| 126 | + |
| 127 | + case 2: |
| 128 | + parts.push(parts[0]); |
| 129 | + parts.push(parts[1]); |
| 130 | + break; |
| 131 | + |
| 132 | + case 3: |
| 133 | + parts.push(parts[1]); |
| 134 | + break; |
| 135 | + |
| 136 | + case 4: |
| 137 | + default: |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + return parts; |
| 142 | + } |
| 143 | + |
| 144 | + render() { |
| 145 | + const { |
| 146 | + children, |
| 147 | + background, |
| 148 | + maxWidth, |
| 149 | + maxHeight, |
| 150 | + minWidth, |
| 151 | + minHeight, |
| 152 | + boxShadow, |
| 153 | + margin, |
| 154 | + padding, |
| 155 | + duration, |
| 156 | + id, |
| 157 | + display, |
| 158 | + timingFunction, |
| 159 | + ...rest |
| 160 | + } = this.props; |
| 161 | + |
| 162 | + return ( |
| 163 | + <Baba name={`${id}-container`} key={this.state.renderCount}> |
| 164 | + <Move duration={duration} timingFunction={timingFunction}> |
| 165 | + {baba => ( |
| 166 | + <rest.as |
| 167 | + ref={baba.ref} |
| 168 | + style={{ |
| 169 | + position: 'relative', |
| 170 | + minWidth, |
| 171 | + maxWidth, |
| 172 | + minHeight, |
| 173 | + maxHeight, |
| 174 | + margin, |
| 175 | + padding, |
| 176 | + display, |
| 177 | + }} |
| 178 | + > |
| 179 | + <div |
| 180 | + aria-hidden="true" |
| 181 | + className={baba.className} |
| 182 | + style={{ |
| 183 | + ...baba.style, |
| 184 | + background, |
| 185 | + boxShadow, |
| 186 | + position: 'absolute', |
| 187 | + top: 0, |
| 188 | + left: 0, |
| 189 | + right: 0, |
| 190 | + bottom: 0, |
| 191 | + pointerEvents: 'none', |
| 192 | + zIndex: -1, |
| 193 | + }} |
| 194 | + /> |
| 195 | + |
| 196 | + <Baba |
| 197 | + name={`${id}-children`} |
| 198 | + key={this.state.renderCount} |
| 199 | + timingFunction={timingFunction} |
| 200 | + > |
| 201 | + <SimpleReveal duration={duration} offset={this.getInversePaddingParts()}> |
| 202 | + {props => children(props)} |
| 203 | + </SimpleReveal> |
| 204 | + </Baba> |
| 205 | + </rest.as> |
| 206 | + )} |
| 207 | + </Move> |
| 208 | + </Baba> |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
0 commit comments