-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathTransitionablePortal.js
174 lines (143 loc) · 4.5 KB
/
TransitionablePortal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import Portal from '../Portal'
import Transition from '../../modules/Transition'
import { TRANSITION_STATUS_ENTERING } from '../../modules/Transition/utils/computeStatuses'
import { getUnhandledProps, makeDebugger, useForceUpdate } from '../../lib'
const debug = makeDebugger('transitionable_portal')
function usePortalState(props) {
const portalOpen = React.useRef(false)
const forceUpdate = useForceUpdate()
const setPortalOpen = React.useCallback((value) => {
portalOpen.current = value
forceUpdate()
}, [])
React.useEffect(() => {
if (!_.isUndefined(props.open)) {
portalOpen.current = props.open
}
}, [props.open])
if (_.isUndefined(props.open)) {
// This is definitely a hack :(
//
// It's coupled with handlePortalClose() for force set the state of `portalOpen` omitting
// props.open. It's related to implementation of the component itself as `onClose()` will be
// called after a transition will end.
// https://github.com/Semantic-Org/Semantic-UI-React/issues/2382
if (portalOpen.current === -1) {
return [false, setPortalOpen]
}
return [portalOpen.current, setPortalOpen]
}
return [props.open, setPortalOpen]
}
/**
* A sugar for `Portal` and `Transition`.
* @see Portal
* @see Transition
*/
function TransitionablePortal(props) {
const {
children,
transition = {
animation: 'scale',
duration: 400,
},
} = props
const [portalOpen, setPortalOpen] = usePortalState(props)
const [transitionVisible, setTransitionVisible] = React.useState(false)
const open = portalOpen || transitionVisible
// ----------------------------------------
// Callback handling
// ----------------------------------------
const handlePortalClose = () => {
debug('handlePortalClose()')
setPortalOpen(-1)
}
const handlePortalOpen = () => {
debug('handlePortalOpen()')
setPortalOpen(true)
}
const handleTransitionHide = (nothing, data) => {
debug('handleTransitionHide()')
setTransitionVisible(false)
props.onClose?.(null, { ...data, portalOpen: false, transitionVisible: false })
props.onHide?.(null, { ...data, portalOpen, transitionVisible: false })
}
const handleTransitionStart = (nothing, data) => {
debug('handleTransitionStart()')
const { status } = data
const nextTransitionVisible = status === TRANSITION_STATUS_ENTERING
props.onStart?.(null, {
...data,
portalOpen,
transitionVisible: nextTransitionVisible,
})
// Heads up! TransitionablePortal fires onOpen callback on the start of transition animation
if (!nextTransitionVisible) {
return
}
setTransitionVisible(nextTransitionVisible)
props.onOpen?.(null, {
...data,
transitionVisible: nextTransitionVisible,
portalOpen: true,
})
}
// ----------------------------------------
// Render
// ----------------------------------------
const rest = getUnhandledProps(TransitionablePortal, props)
return (
<Portal {...rest} open={open} onOpen={handlePortalOpen} onClose={handlePortalClose}>
<Transition
{...transition}
transitionOnMount
onStart={handleTransitionStart}
onHide={handleTransitionHide}
visible={portalOpen}
>
{children}
</Transition>
</Portal>
)
}
TransitionablePortal.displayName = 'TransitionablePortal'
TransitionablePortal.propTypes = {
/** Primary content. */
children: PropTypes.node.isRequired,
/**
* Called when a close event happens.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props and internal state.
*/
onClose: PropTypes.func,
/**
* Callback on each transition that changes visibility to hidden.
*
* @param {null}
* @param {object} data - All props with transition status and internal state.
*/
onHide: PropTypes.func,
/**
* Called when an open event happens.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props and internal state.
*/
onOpen: PropTypes.func,
/**
* Callback on animation start.
*
* @param {null}
* @param {object} data - All props with transition status and internal state.
*/
onStart: PropTypes.func,
/** Controls whether or not the portal is displayed. */
open: PropTypes.bool,
/** Transition props. */
transition: PropTypes.object,
}
export default TransitionablePortal