Guest User

Untitled

a guest
Apr 25th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import AnimatedValue from '../../AnimatedValue'
  4.  
  5. const check = value => value === 'auto'
  6. const convert = (acc, [name, value]) => ({
  7. ...acc,
  8. [name]: new AnimatedValue(value),
  9. })
  10. const overwrite = (width, height) => (acc, [name, value]) => ({
  11. ...acc,
  12. [name]: value === 'auto' ? (name === 'height' ? height : width) : value,
  13. })
  14.  
  15. export default function fixAuto(spring, props) {
  16. const { native, children, from, to } = props
  17.  
  18. // Dry-route props back if nothing's using 'auto' in there
  19. if (![...Object.values(from), ...Object.values(to)].some(check)) return props
  20.  
  21. return new Promise(res => {
  22. const forward = spring.getForwardProps(props)
  23. const allProps = Object.entries({ ...from, ...to })
  24. const portal = document.createElement('div')
  25. portal.style.cssText = 'position:static;visibility:hidden;'
  26. document.body.appendChild(portal)
  27.  
  28. // Collect to-state props
  29. const componentProps = native
  30. ? allProps.reduce(convert, forward)
  31. : { ...from, ...to, ...forward }
  32.  
  33. // Render to-state vdom to portal
  34. ReactDOM.render(
  35. <div
  36. ref={ref => {
  37. if (ref) {
  38. // Once it's rendered out, fetch bounds
  39. const height = ref.clientHeight
  40. const width = ref.clientWidth
  41.  
  42. // Remove portal and resolve promise with updated props
  43. document.body.removeChild(portal)
  44. res({
  45. ...props,
  46. from: Object.entries(from).reduce(overwrite(width, height), from),
  47. to: Object.entries(to).reduce(overwrite(width, height), to),
  48. })
  49. }
  50. }}>
  51. {children(componentProps)}
  52. </div>,
  53. portal
  54. )
  55. })
  56. }
Add Comment
Please, Sign In to add comment