Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import { Animated, StyleProp, ViewStyle } from 'react-native'
  3.  
  4. export type FadeDirection = 'up' | 'down'
  5.  
  6. interface FadeProps {
  7. visible?: boolean
  8. style?: StyleProp<ViewStyle>
  9. children: React.ReactNode
  10. direction?: FadeDirection
  11. }
  12.  
  13. interface FadeState {
  14. visible?: boolean
  15. }
  16.  
  17. export class Fade extends Component<FadeProps, FadeState> {
  18. static defaultProps = {
  19. direction: 'up',
  20. visible: true,
  21. }
  22.  
  23. private visibility: Animated.Value = new Animated.Value(0)
  24.  
  25. constructor(props: FadeProps) {
  26. super(props)
  27. this.state = {
  28. visible: props.visible || true,
  29. }
  30. }
  31.  
  32. componentWillReceiveProps({ visible }: FadeProps) {
  33. Animated.timing(this.visibility, {
  34. toValue: visible ? 1 : 0,
  35. duration: 200,
  36. }).start(() => !visible && this.setState({ visible }))
  37.  
  38. if (visible) this.setState({ visible })
  39. }
  40.  
  41. render(): JSX.Element {
  42. // prettier-ignore
  43. const {
  44. style,
  45. children,
  46. direction = 'down',
  47. ...rest
  48. } = this.props
  49. const { visible } = this.state
  50.  
  51. const directions = {
  52. up: [5, 0],
  53. down: [-5, 0],
  54. }
  55.  
  56. const test = this.visibility.interpolate({
  57. inputRange: [0, 1],
  58. outputRange: directions[direction] || [0, 0],
  59. })
  60.  
  61. const containerStyle = {
  62. opacity: this.visibility.interpolate({
  63. inputRange: [0, 1],
  64. outputRange: [0, 1],
  65. }),
  66. transform: [
  67. {
  68. translateY: test,
  69. },
  70. ],
  71. }
  72.  
  73. const combinedStyle = [containerStyle, style]
  74. return (
  75. <Animated.View style={visible ? combinedStyle : containerStyle} {...rest}>
  76. {visible ? children : null}
  77. </Animated.View>
  78. )
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement