|
1 | 1 | import { createElement, useMemo } from 'react'
|
2 | 2 | import { useTransition } from '@react-spring/web'
|
3 | 3 | import { useMotionConfig } from '@nivo/core'
|
4 |
| -import { NetworkLink } from './NetworkLink' |
5 |
| -import { ComputedLink, NetworkInputNode } from './types' |
| 4 | +import { ComputedLink, NetworkInputNode, NetworkLinkComponent } from './types' |
6 | 5 |
|
7 | 6 | interface NetworkLinksProps<N extends NetworkInputNode> {
|
8 | 7 | links: ComputedLink<N>[]
|
9 |
| - linkThickness: (link: ComputedLink<N>) => number |
10 |
| - linkColor: (link: ComputedLink<N>) => string |
| 8 | + linkComponent: NetworkLinkComponent<N> |
11 | 9 | }
|
12 | 10 |
|
13 |
| -const getEnterTransition = <N extends NetworkInputNode>( |
14 |
| - linkColor: NetworkLinksProps<N>['linkColor'] |
15 |
| -) => (link: ComputedLink<N>) => ({ |
| 11 | +const getEnterTransition = <N extends NetworkInputNode>() => (link: ComputedLink<N>) => ({ |
16 | 12 | x1: link.source.x,
|
17 | 13 | y1: link.source.y,
|
18 | 14 | x2: link.source.x,
|
19 | 15 | y2: link.source.y,
|
20 |
| - color: linkColor(link), |
| 16 | + color: link.color, |
21 | 17 | opacity: 0,
|
22 | 18 | })
|
23 | 19 |
|
24 |
| -const getRegularTransition = <N extends NetworkInputNode>( |
25 |
| - linkColor: NetworkLinksProps<N>['linkColor'] |
26 |
| -) => (link: ComputedLink<N>) => ({ |
| 20 | +const getRegularTransition = <N extends NetworkInputNode>() => (link: ComputedLink<N>) => ({ |
27 | 21 | x1: link.source.x,
|
28 | 22 | y1: link.source.y,
|
29 | 23 | x2: link.target.x,
|
30 | 24 | y2: link.target.y,
|
31 |
| - color: linkColor(link), |
| 25 | + color: link.color, |
32 | 26 | opacity: 1,
|
33 | 27 | })
|
34 | 28 |
|
35 |
| -const getExitTransition = <N extends NetworkInputNode>( |
36 |
| - linkColor: NetworkLinksProps<N>['linkColor'] |
37 |
| -) => (link: ComputedLink<N>) => ({ |
| 29 | +const getExitTransition = <N extends NetworkInputNode>() => (link: ComputedLink<N>) => ({ |
38 | 30 | x1: link.source.x,
|
39 | 31 | y1: link.source.y,
|
40 | 32 | x2: link.source.x,
|
41 | 33 | y2: link.source.y,
|
42 |
| - color: linkColor(link), |
| 34 | + color: link.color, |
43 | 35 | opacity: 0,
|
44 | 36 | })
|
45 | 37 |
|
46 | 38 | export const NetworkLinks = <N extends NetworkInputNode>({
|
47 | 39 | links,
|
48 |
| - linkThickness, |
49 |
| - linkColor, |
| 40 | + linkComponent, |
50 | 41 | }: NetworkLinksProps<N>) => {
|
51 | 42 | const { animate, config: springConfig } = useMotionConfig()
|
52 | 43 |
|
53 | 44 | const [enterTransition, regularTransition, exitTransition] = useMemo(
|
54 |
| - () => [ |
55 |
| - getEnterTransition<N>(linkColor), |
56 |
| - getRegularTransition<N>(linkColor), |
57 |
| - getExitTransition<N>(linkColor), |
58 |
| - ], |
59 |
| - [linkColor] |
| 45 | + () => [getEnterTransition<N>(), getRegularTransition<N>(), getExitTransition<N>()], |
| 46 | + [] |
60 | 47 | )
|
61 | 48 |
|
62 | 49 | const transition = useTransition<
|
@@ -84,10 +71,9 @@ export const NetworkLinks = <N extends NetworkInputNode>({
|
84 | 71 | return (
|
85 | 72 | <>
|
86 | 73 | {transition((transitionProps, link) => {
|
87 |
| - return createElement(NetworkLink, { |
| 74 | + return createElement(linkComponent, { |
88 | 75 | key: link.id,
|
89 | 76 | link,
|
90 |
| - thickness: linkThickness(link), |
91 | 77 | animated: transitionProps,
|
92 | 78 | })
|
93 | 79 | })}
|
|
0 commit comments