|
| 1 | +import * as React from 'react'; |
| 2 | +import { css } from 'emotion'; |
| 3 | +import Collector, { |
| 4 | + CollectorChildrenProps, |
| 5 | + AnimationCallback, |
| 6 | + CollectorActions, |
| 7 | +} from '../../Collector'; |
| 8 | +import { standard } from '../../lib/curves'; |
| 9 | +import { combine } from '../../lib/style'; |
| 10 | +import { dynamic } from '../../lib/duration'; |
| 11 | +import { Duration } from '../types'; |
| 12 | + |
| 13 | +export interface FocalRevealProps extends CollectorChildrenProps { |
| 14 | + /** |
| 15 | + * How long the animation should take over {duration}ms. |
| 16 | + */ |
| 17 | + duration: Duration; |
| 18 | + |
| 19 | + /** |
| 20 | + * zIndex to be applied to the moving element. |
| 21 | + */ |
| 22 | + zIndex?: number; |
| 23 | + |
| 24 | + /** |
| 25 | + * Timing function to be used in the transition. |
| 26 | + */ |
| 27 | + timingFunction: string; |
| 28 | + |
| 29 | + /** |
| 30 | + * Set to false to disable transforming the children to the X position of the focal element. |
| 31 | + * Defaults to true. |
| 32 | + */ |
| 33 | + childrenTransformX: boolean; |
| 34 | + |
| 35 | + /** |
| 36 | + * Set to false to disable transforming the children to the Y position of the focal element. |
| 37 | + * Defaults to `true`. |
| 38 | + */ |
| 39 | + childrenTransformY: boolean; |
| 40 | + |
| 41 | + /** |
| 42 | + * Will transition using clip-path over height and width. |
| 43 | + * This results in a more resilient transition since page flow isn't affected at the cost of browser support. |
| 44 | + * See: https://caniuse.com/#feat=css-clip-path |
| 45 | + */ |
| 46 | + useClipPath?: boolean; |
| 47 | +} |
| 48 | + |
| 49 | +export default class FocalReveal extends React.Component<FocalRevealProps> { |
| 50 | + static defaultProps = { |
| 51 | + duration: 'dynamic', |
| 52 | + timingFunction: standard(), |
| 53 | + childrenTransformX: true, |
| 54 | + childrenTransformY: true, |
| 55 | + }; |
| 56 | + |
| 57 | + beforeAnimate: AnimationCallback = (data, onFinish, setChildProps) => { |
| 58 | + if (!data.destination.focalTargetElementBoundingBox) { |
| 59 | + throw new Error(`yubaba |
| 60 | +<FocalTarget /> was not found, if you haven't defined one make sure to add one as a descendant of your target Baba.`); |
| 61 | + } |
| 62 | + |
| 63 | + const { childrenTransformX, childrenTransformY, useClipPath } = this.props; |
| 64 | + |
| 65 | + const offsetChildrenX = childrenTransformX |
| 66 | + ? data.destination.focalTargetElementBoundingBox.location.left - |
| 67 | + data.destination.elementBoundingBox.location.left |
| 68 | + : 0; |
| 69 | + const offsetChildrenY = childrenTransformY |
| 70 | + ? data.destination.focalTargetElementBoundingBox.location.top - |
| 71 | + data.destination.elementBoundingBox.location.top |
| 72 | + : 0; |
| 73 | + |
| 74 | + const revealStyles = useClipPath |
| 75 | + ? { |
| 76 | + clipPath: `inset(0 ${data.destination.elementBoundingBox.size.width - |
| 77 | + data.destination.focalTargetElementBoundingBox.size.width}px ${data.destination |
| 78 | + .elementBoundingBox.size.height - |
| 79 | + data.destination.focalTargetElementBoundingBox.size.height}px 0)`, |
| 80 | + } |
| 81 | + : { |
| 82 | + height: data.destination.focalTargetElementBoundingBox.size.height, |
| 83 | + width: data.destination.focalTargetElementBoundingBox.size.width, |
| 84 | + }; |
| 85 | + |
| 86 | + setChildProps({ |
| 87 | + style: prevStyles => |
| 88 | + data.destination.focalTargetElementBoundingBox |
| 89 | + ? { |
| 90 | + ...prevStyles, |
| 91 | + ...revealStyles, |
| 92 | + opacity: 1, |
| 93 | + visibility: 'visible', |
| 94 | + willChange: combine('height, width, clip-path')(prevStyles.willChange), |
| 95 | + overflow: 'hidden', |
| 96 | + } |
| 97 | + : undefined, |
| 98 | + className: () => |
| 99 | + data.destination.focalTargetElementBoundingBox |
| 100 | + ? css({ |
| 101 | + '> *': { |
| 102 | + transform: `translate3d(-${offsetChildrenX}px, -${offsetChildrenY}px, 0)`, |
| 103 | + }, |
| 104 | + }) |
| 105 | + : undefined, |
| 106 | + }); |
| 107 | + |
| 108 | + onFinish(); |
| 109 | + }; |
| 110 | + |
| 111 | + animate: AnimationCallback = (data, onFinish, setChildProps) => { |
| 112 | + const { timingFunction, duration, useClipPath } = this.props; |
| 113 | + const calculatedDuration = |
| 114 | + duration === 'dynamic' |
| 115 | + ? dynamic(data.origin.elementBoundingBox, data.destination.elementBoundingBox) |
| 116 | + : duration; |
| 117 | + |
| 118 | + const revealStyles = useClipPath |
| 119 | + ? { |
| 120 | + clipPath: 'inset(0 0 0 0)', |
| 121 | + } |
| 122 | + : { |
| 123 | + height: data.destination.elementBoundingBox.size.height, |
| 124 | + width: data.destination.elementBoundingBox.size.width, |
| 125 | + }; |
| 126 | + |
| 127 | + setChildProps({ |
| 128 | + style: prevStyles => ({ |
| 129 | + ...prevStyles, |
| 130 | + ...revealStyles, |
| 131 | + transition: combine( |
| 132 | + `height ${calculatedDuration}ms ${timingFunction}, width ${calculatedDuration}ms ${timingFunction}, clip-path ${calculatedDuration}ms ${timingFunction}` |
| 133 | + )(prevStyles.transition), |
| 134 | + }), |
| 135 | + className: () => |
| 136 | + css({ |
| 137 | + '> *': { |
| 138 | + transform: `translate3d(0, 0, 0)`, |
| 139 | + transition: `transform ${calculatedDuration}ms ${timingFunction}`, |
| 140 | + }, |
| 141 | + }), |
| 142 | + }); |
| 143 | + |
| 144 | + setTimeout(() => onFinish(), calculatedDuration); |
| 145 | + }; |
| 146 | + |
| 147 | + render() { |
| 148 | + const { children } = this.props; |
| 149 | + |
| 150 | + return ( |
| 151 | + <Collector |
| 152 | + data={{ |
| 153 | + action: CollectorActions.animation, |
| 154 | + payload: { |
| 155 | + beforeAnimate: this.beforeAnimate, |
| 156 | + animate: this.animate, |
| 157 | + }, |
| 158 | + }} |
| 159 | + > |
| 160 | + {children} |
| 161 | + </Collector> |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments