|
1 | 1 | import _ from 'lodash' |
2 | 2 | import PropTypes from 'prop-types' |
3 | | -import React, { Component } from 'react' |
| 3 | +import React from 'react' |
4 | 4 |
|
5 | 5 | import Portal from '../Portal' |
6 | 6 | import Transition from '../../modules/Transition' |
7 | 7 | import { TRANSITION_STATUS_ENTERING } from '../../modules/Transition/utils/computeStatuses' |
8 | | -import { getUnhandledProps, makeDebugger } from '../../lib' |
| 8 | +import { getUnhandledProps, makeDebugger, useForceUpdate } from '../../lib' |
9 | 9 |
|
10 | 10 | const debug = makeDebugger('transitionable_portal') |
11 | 11 |
|
12 | | -/** |
13 | | - * A sugar for `Portal` and `Transition`. |
14 | | - * @see Portal |
15 | | - * @see Transition |
16 | | - */ |
17 | | -export default class TransitionablePortal extends Component { |
18 | | - state = {} |
| 12 | +function usePortalState(props) { |
| 13 | + const portalOpen = React.useRef(false) |
| 14 | + const forceUpdate = useForceUpdate() |
19 | 15 |
|
20 | | - // ---------------------------------------- |
21 | | - // Lifecycle |
22 | | - // ---------------------------------------- |
| 16 | + const setPortalOpen = React.useCallback((value) => { |
| 17 | + portalOpen.current = value |
| 18 | + forceUpdate() |
| 19 | + }, []) |
23 | 20 |
|
24 | | - static getDerivedStateFromProps(props, state) { |
| 21 | + React.useEffect(() => { |
| 22 | + if (!_.isUndefined(props.open)) { |
| 23 | + portalOpen.current = props.open |
| 24 | + } |
| 25 | + }, [props.open]) |
| 26 | + |
| 27 | + if (_.isUndefined(props.open)) { |
25 | 28 | // This is definitely a hack :( |
26 | 29 | // |
27 | 30 | // It's coupled with handlePortalClose() for force set the state of `portalOpen` omitting |
28 | 31 | // props.open. It's related to implementation of the component itself as `onClose()` will be |
29 | 32 | // called after a transition will end. |
30 | 33 | // https://github.com/Semantic-Org/Semantic-UI-React/issues/2382 |
31 | | - if (state.portalOpen === -1) { |
32 | | - return { portalOpen: false } |
33 | | - } |
34 | | - |
35 | | - if (_.isUndefined(props.open)) { |
36 | | - return null |
| 34 | + if (portalOpen.current === -1) { |
| 35 | + return [false, setPortalOpen] |
37 | 36 | } |
38 | 37 |
|
39 | | - return { portalOpen: props.open } |
| 38 | + return [portalOpen.current, setPortalOpen] |
40 | 39 | } |
41 | 40 |
|
| 41 | + return [props.open, setPortalOpen] |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * A sugar for `Portal` and `Transition`. |
| 46 | + * @see Portal |
| 47 | + * @see Transition |
| 48 | + */ |
| 49 | +function TransitionablePortal(props) { |
| 50 | + const { children, transition } = props |
| 51 | + |
| 52 | + const [portalOpen, setPortalOpen] = usePortalState(props) |
| 53 | + const [transitionVisible, setTransitionVisible] = React.useState(false) |
| 54 | + |
| 55 | + const open = portalOpen || transitionVisible |
| 56 | + |
42 | 57 | // ---------------------------------------- |
43 | 58 | // Callback handling |
44 | 59 | // ---------------------------------------- |
45 | 60 |
|
46 | | - handlePortalClose = () => { |
| 61 | + const handlePortalClose = () => { |
47 | 62 | debug('handlePortalClose()') |
48 | | - |
49 | | - this.setState({ portalOpen: -1 }) |
| 63 | + setPortalOpen(-1) |
50 | 64 | } |
51 | 65 |
|
52 | | - handlePortalOpen = () => { |
| 66 | + const handlePortalOpen = () => { |
53 | 67 | debug('handlePortalOpen()') |
54 | | - |
55 | | - this.setState({ portalOpen: true }) |
| 68 | + setPortalOpen(true) |
56 | 69 | } |
57 | 70 |
|
58 | | - handleTransitionHide = (nothing, data) => { |
| 71 | + const handleTransitionHide = (nothing, data) => { |
59 | 72 | debug('handleTransitionHide()') |
60 | | - const { portalOpen } = this.state |
61 | 73 |
|
62 | | - this.setState({ transitionVisible: false }) |
63 | | - _.invoke(this.props, 'onClose', null, { ...data, portalOpen: false, transitionVisible: false }) |
64 | | - _.invoke(this.props, 'onHide', null, { ...data, portalOpen, transitionVisible: false }) |
| 74 | + setTransitionVisible(false) |
| 75 | + _.invoke(props, 'onClose', null, { ...data, portalOpen: false, transitionVisible: false }) |
| 76 | + _.invoke(props, 'onHide', null, { ...data, portalOpen, transitionVisible: false }) |
65 | 77 | } |
66 | 78 |
|
67 | | - handleTransitionStart = (nothing, data) => { |
| 79 | + const handleTransitionStart = (nothing, data) => { |
68 | 80 | debug('handleTransitionStart()') |
69 | | - const { portalOpen } = this.state |
70 | 81 | const { status } = data |
71 | | - const transitionVisible = status === TRANSITION_STATUS_ENTERING |
| 82 | + const nextTransitionVisible = status === TRANSITION_STATUS_ENTERING |
72 | 83 |
|
73 | | - _.invoke(this.props, 'onStart', null, { ...data, portalOpen, transitionVisible }) |
| 84 | + _.invoke(props, 'onStart', null, { |
| 85 | + ...data, |
| 86 | + portalOpen, |
| 87 | + transitionVisible: nextTransitionVisible, |
| 88 | + }) |
74 | 89 |
|
75 | 90 | // Heads up! TransitionablePortal fires onOpen callback on the start of transition animation |
76 | | - if (!transitionVisible) return |
| 91 | + if (!nextTransitionVisible) { |
| 92 | + return |
| 93 | + } |
77 | 94 |
|
78 | | - this.setState({ transitionVisible }) |
79 | | - _.invoke(this.props, 'onOpen', null, { ...data, transitionVisible, portalOpen: true }) |
| 95 | + setTransitionVisible(nextTransitionVisible) |
| 96 | + _.invoke(props, 'onOpen', null, { |
| 97 | + ...data, |
| 98 | + transitionVisible: nextTransitionVisible, |
| 99 | + portalOpen: true, |
| 100 | + }) |
80 | 101 | } |
81 | 102 |
|
82 | 103 | // ---------------------------------------- |
83 | 104 | // Render |
84 | 105 | // ---------------------------------------- |
85 | 106 |
|
86 | | - render() { |
87 | | - debug('render()', this.state) |
88 | | - |
89 | | - const { children, transition } = this.props |
90 | | - const { portalOpen, transitionVisible } = this.state |
91 | | - |
92 | | - const open = portalOpen || transitionVisible |
93 | | - const rest = getUnhandledProps(TransitionablePortal, this.props) |
94 | | - |
95 | | - return ( |
96 | | - <Portal {...rest} open={open} onOpen={this.handlePortalOpen} onClose={this.handlePortalClose}> |
97 | | - <Transition |
98 | | - {...transition} |
99 | | - transitionOnMount |
100 | | - onStart={this.handleTransitionStart} |
101 | | - onHide={this.handleTransitionHide} |
102 | | - visible={portalOpen} |
103 | | - > |
104 | | - {children} |
105 | | - </Transition> |
106 | | - </Portal> |
107 | | - ) |
108 | | - } |
| 107 | + const rest = getUnhandledProps(TransitionablePortal, props) |
| 108 | + |
| 109 | + return ( |
| 110 | + <Portal {...rest} open={open} onOpen={handlePortalOpen} onClose={handlePortalClose}> |
| 111 | + <Transition |
| 112 | + {...transition} |
| 113 | + transitionOnMount |
| 114 | + onStart={handleTransitionStart} |
| 115 | + onHide={handleTransitionHide} |
| 116 | + visible={portalOpen} |
| 117 | + > |
| 118 | + {children} |
| 119 | + </Transition> |
| 120 | + </Portal> |
| 121 | + ) |
109 | 122 | } |
110 | 123 |
|
| 124 | +TransitionablePortal.displayName = 'TransitionablePortal' |
111 | 125 | TransitionablePortal.propTypes = { |
112 | 126 | /** Primary content. */ |
113 | 127 | children: PropTypes.node.isRequired, |
@@ -157,3 +171,5 @@ TransitionablePortal.defaultProps = { |
157 | 171 | duration: 400, |
158 | 172 | }, |
159 | 173 | } |
| 174 | + |
| 175 | +export default TransitionablePortal |
0 commit comments