-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scatterplot): migrate package to new architecture
- Loading branch information
Raphaël Benitte
authored and
Raphaël Benitte
committed
Jun 8, 2019
1 parent
0ed7eb8
commit 4397dab
Showing
14 changed files
with
698 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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, { memo } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { TransitionMotion, spring } from 'react-motion' | ||
import { useMotionConfig } from '@nivo/core' | ||
import { NodePropType } from './props' | ||
import NodeWrapper from './NodeWrapper' | ||
|
||
const AnimatedNodes = ({ | ||
nodes, | ||
renderNode, | ||
isInteractive, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
onClick, | ||
tooltip, | ||
}) => { | ||
const { springConfig } = useMotionConfig() | ||
|
||
return ( | ||
<TransitionMotion | ||
styles={nodes.map(node => ({ | ||
key: node.id, | ||
data: node, | ||
style: { | ||
x: spring(node.x, springConfig), | ||
y: spring(node.y, springConfig), | ||
size: spring(node.size, springConfig), | ||
}, | ||
}))} | ||
> | ||
{interpolatedStyles => ( | ||
<> | ||
{interpolatedStyles.map(({ key, style, data: node }) => ( | ||
<NodeWrapper | ||
key={key} | ||
node={node} | ||
renderNode={renderNode} | ||
x={style.x} | ||
y={style.y} | ||
size={style.size} | ||
color={node.style.color} | ||
isInteractive={isInteractive} | ||
onMouseEnter={onMouseEnter} | ||
onMouseMove={onMouseMove} | ||
onMouseLeave={onMouseLeave} | ||
onClick={onClick} | ||
tooltip={tooltip} | ||
/> | ||
))} | ||
</> | ||
)} | ||
</TransitionMotion> | ||
) | ||
} | ||
|
||
AnimatedNodes.propTypes = { | ||
nodes: PropTypes.arrayOf(NodePropType).isRequired, | ||
renderNode: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.object, | ||
]).isRequired, | ||
|
||
isInteractive: PropTypes.bool.isRequired, | ||
onMouseEnter: PropTypes.func, | ||
onMouseMove: PropTypes.func, | ||
onMouseLeave: PropTypes.func, | ||
onClick: PropTypes.func, | ||
|
||
tooltip: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.object | ||
]).isRequired, | ||
} | ||
|
||
export default memo(AnimatedNodes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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, { memo, useCallback } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { useTooltip } from '@nivo/tooltip' | ||
import { NodePropType } from './props' | ||
|
||
const NodeWrapper = ({ | ||
node, | ||
renderNode: NodeComponent, | ||
x, | ||
y, | ||
size, | ||
color, | ||
isInteractive, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
onClick, | ||
tooltip, | ||
}) => { | ||
const { showTooltipFromEvent, hideTooltip } = useTooltip() | ||
|
||
const handleMouseEnter = useCallback( | ||
event => { | ||
showTooltipFromEvent(React.createElement(tooltip, { node }), event) | ||
onMouseEnter && onMouseEnter(node, event) | ||
}, | ||
[node, tooltip, showTooltipFromEvent, onMouseEnter] | ||
) | ||
|
||
const handleMouseMove = useCallback( | ||
event => { | ||
showTooltipFromEvent(React.createElement(tooltip, { node }), event) | ||
onMouseMove && onMouseMove(node, event) | ||
}, | ||
[node, tooltip, showTooltipFromEvent, onMouseMove] | ||
) | ||
|
||
const handleMouseLeave = useCallback( | ||
event => { | ||
hideTooltip() | ||
onMouseLeave && onMouseLeave(node, event) | ||
}, | ||
[node, hideTooltip, onMouseLeave] | ||
) | ||
|
||
const handleClick = useCallback( | ||
event => { | ||
onClick && onClick(node, event) | ||
}, | ||
[node, onClick] | ||
) | ||
|
||
return React.createElement(NodeComponent, { | ||
node, | ||
x, | ||
y, | ||
size, | ||
color, | ||
onMouseEnter: isInteractive ? handleMouseEnter : undefined, | ||
onMouseMove: isInteractive ? handleMouseMove : undefined, | ||
onMouseLeave: isInteractive ? handleMouseLeave : undefined, | ||
onClick: (isInteractive && onClick) ? handleClick : undefined, | ||
}) | ||
} | ||
|
||
NodeWrapper.propTypes = { | ||
node: NodePropType.isRequired, | ||
|
||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired, | ||
size: PropTypes.number.isRequired, | ||
color: PropTypes.string.isRequired, | ||
|
||
isInteractive: PropTypes.bool.isRequired, | ||
onMouseEnter: PropTypes.func, | ||
onMouseMove: PropTypes.func, | ||
onMouseLeave: PropTypes.func, | ||
onClick: PropTypes.func, | ||
|
||
tooltip: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.object | ||
]).isRequired, | ||
} | ||
|
||
export default memo(NodeWrapper) |
Oops, something went wrong.