/
withContainer.js
99 lines (93 loc) · 3.17 KB
/
withContainer.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
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React, { Component, useRef } from 'react'
import PropTypes from 'prop-types'
import { tooltipContext, useTooltipHandlers, TooltipWrapper } from '@nivo/tooltip'
import { ThemeProvider } from '../theming'
import { MotionConfigProvider } from '../motion'
const containerStyle = {
position: 'relative',
}
const Container = ({
theme,
renderWrapper = true,
children,
animate,
motionStiffness,
motionDamping,
motionConfig,
}) => {
const container = useRef(null)
const {
showTooltipAt,
showTooltipFromEvent,
hideTooltip,
isTooltipVisible,
tooltipContent,
tooltipPosition,
tooltipAnchor,
} = useTooltipHandlers(container)
return (
<ThemeProvider theme={theme}>
<MotionConfigProvider
animate={animate}
stiffness={motionStiffness}
damping={motionDamping}
config={motionConfig}
>
<tooltipContext.Provider
value={{ showTooltipAt, showTooltipFromEvent, hideTooltip }}
>
{/* we should not render the div element if using the HTTP API */}
{renderWrapper === true && (
<div style={containerStyle} ref={container}>
{children}
{isTooltipVisible && (
<TooltipWrapper position={tooltipPosition} anchor={tooltipAnchor}>
{tooltipContent}
</TooltipWrapper>
)}
</div>
)}
{renderWrapper !== true && children}
</tooltipContext.Provider>
</MotionConfigProvider>
</ThemeProvider>
)
}
Container.propTypes = {
children: PropTypes.node.isRequired,
theme: PropTypes.object,
animate: MotionConfigProvider.propTypes.animate,
motionStiffness: MotionConfigProvider.propTypes.stiffness,
motionDamping: MotionConfigProvider.propTypes.damping,
motionConfig: MotionConfigProvider.propTypes.config,
renderWrapper: PropTypes.bool,
}
export const withContainer = WrappedComponent => {
// eslint-disable-next-line react/display-name
return class extends Component {
render() {
// eslint-disable-next-line react/prop-types
const { theme, renderWrapper, ...childProps } = this.props
return (
<Container
theme={theme}
renderWrapper={renderWrapper}
animate={childProps.animate}
motionStiffness={childProps.motionStiffness}
motionDamping={childProps.motionDamping}
motionConfig={childProps.motionConfig}
>
<WrappedComponent {...childProps} />
</Container>
)
}
}
}