Skip to content

Commit

Permalink
feat(network): replace react-motion with react-spring and use the sam…
Browse files Browse the repository at this point in the history
…e component for static and animated variants
  • Loading branch information
plouc committed Dec 31, 2021
1 parent fa152f5 commit 82a2d05
Show file tree
Hide file tree
Showing 24 changed files with 482 additions and 432 deletions.
4 changes: 2 additions & 2 deletions packages/network/package.json
Expand Up @@ -33,9 +33,9 @@
"dependencies": {
"@nivo/colors": "0.76.0",
"@nivo/tooltip": "0.76.0",
"@react-spring/web": "9.3.1",
"d3-force": "^2.0.1",
"lodash": "^4.17.21",
"react-motion": "^0.5.2"
"lodash": "^4.17.21"
},
"devDependencies": {
"@nivo/core": "0.76.0"
Expand Down
63 changes: 0 additions & 63 deletions packages/network/src/AnimatedLinks.tsx

This file was deleted.

77 changes: 0 additions & 77 deletions packages/network/src/AnimatedNodes.tsx

This file was deleted.

27 changes: 0 additions & 27 deletions packages/network/src/Link.tsx

This file was deleted.

122 changes: 77 additions & 45 deletions packages/network/src/Network.tsx
@@ -1,44 +1,47 @@
import { createElement, Fragment, useCallback } from 'react'
import { withContainer, useDimensions, SvgWrapper, useTheme, useMotionConfig } from '@nivo/core'
import { Fragment, ReactNode, useCallback } from 'react'
import { Container, useDimensions, SvgWrapper, useTheme } from '@nivo/core'
import { useInheritedColor } from '@nivo/colors'
import { useTooltip } from '@nivo/tooltip'
import { NetworkDefaultProps } from './props'
import { svgDefaultProps } from './props'
import { useNetwork, useNodeColor, useLinkThickness } from './hooks'
import AnimatedNodes from './AnimatedNodes'
import StaticNodes from './StaticNodes'
import AnimatedLinks from './AnimatedLinks'
import StaticLinks from './StaticLinks'
import { NetworkNodes } from './NetworkNodes'
import { NetworkLinks } from './NetworkLinks'
import NetworkNodeTooltip from './NetworkNodeTooltip'
import { NetworkLayerId, NetworkSvgProps } from './types'

const Network = props => {
type InnerNetworkProps = Omit<
NetworkSvgProps,
'animate' | 'motionConfig' | 'renderWrapper' | 'theme'
>

const InnerNetwork = (props: InnerNetworkProps) => {
const {
width,
height,
margin: partialMargin,

nodes: rawNodes,
links: rawLinks,
data: { nodes: rawNodes, links: rawLinks },

linkDistance,
repulsivity,
distanceMin,
distanceMax,
iterations,
linkDistance = svgDefaultProps.linkDistance,
repulsivity = svgDefaultProps.repulsivity,
distanceMin = svgDefaultProps.distanceMin,
distanceMax = svgDefaultProps.distanceMax,
iterations = svgDefaultProps.iterations,

layers,
layers = svgDefaultProps.layers,

nodeColor,
nodeBorderWidth,
nodeBorderColor,
nodeColor = svgDefaultProps.nodeColor,
nodeBorderWidth = svgDefaultProps.nodeBorderWidth,
nodeBorderColor = svgDefaultProps.nodeBorderColor,

linkThickness,
linkColor,
linkThickness = svgDefaultProps.linkThickness,
linkColor = svgDefaultProps.linkColor,

tooltip,
isInteractive,
tooltip = svgDefaultProps.tooltip,
isInteractive = svgDefaultProps.isInteractive,
onClick,

role,
role = svgDefaultProps.role,
} = props

const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions(
Expand All @@ -47,7 +50,6 @@ const Network = props => {
partialMargin
)

const { animate } = useMotionConfig()
const theme = useTheme()
const getColor = useNodeColor(nodeColor)
const getBorderColor = useInheritedColor(nodeBorderColor, theme)
Expand Down Expand Up @@ -78,23 +80,36 @@ const Network = props => {
hideTooltip()
}, [hideTooltip])

const layerById = {
links: createElement(animate === true ? AnimatedLinks : StaticLinks, {
key: 'links',
links,
linkThickness: getLinkThickness,
linkColor: getLinkColor,
}),
nodes: createElement(animate === true ? AnimatedNodes : StaticNodes, {
key: 'nodes',
nodes,
color: getColor,
borderWidth: nodeBorderWidth,
borderColor: getBorderColor,
handleNodeClick: isInteractive ? onClick : undefined,
handleNodeHover: isInteractive ? handleNodeHover : undefined,
handleNodeLeave: isInteractive ? handleNodeLeave : undefined,
}),
const layerById: Record<NetworkLayerId, ReactNode> = {
links: null,
nodes: null,
}

if (layers.includes('links')) {
layerById.links = (
<NetworkLinks
key="links"
links={links}
linkThickness={getLinkThickness}
linkColor={getLinkColor}
/>
)
}

if (layers.includes('crap')) {
layerById.nodes = (
<NetworkNodes
key="nodes"
nodes={nodes}
color={getColor}
borderWidth={nodeBorderWidth}
borderColor={getBorderColor}
onClick={isInteractive ? onClick : undefined}
onMouseEnter={isInteractive ? handleNodeHover : undefined}
onMouseMove={isInteractive ? handleNodeHover : undefined}
onMouseLeave={isInteractive ? handleNodeLeave : undefined}
/>
)
}

return (
Expand All @@ -120,6 +135,23 @@ const Network = props => {
)
}

Network.defaultProps = NetworkDefaultProps

export default withContainer(Network)
export const Network = ({
isInteractive = svgDefaultProps.isInteractive,
animate = svgDefaultProps.animate,
motionConfig = svgDefaultProps.motionConfig,
theme,
renderWrapper,
...otherProps
}: NetworkSvgProps) => (
<Container
{...{
animate,
isInteractive,
motionConfig,
renderWrapper,
theme,
}}
>
<InnerNetwork isInteractive={isInteractive} {...otherProps} />
</Container>
)

0 comments on commit 82a2d05

Please sign in to comment.