Skip to content

Commit

Permalink
feat(circle-packing): memoize labels transition phases
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc authored and wyze committed Apr 26, 2021
1 parent 138eafb commit d917057
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions packages/circle-packing/src/Labels.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useMemo } from 'react'
import { useTransition } from 'react-spring'
import { useMotionConfig } from '@nivo/core'
import { CirclePackingCommonProps, ComputedDatum, LabelComponent, ComputedLabel } from './types'
Expand All @@ -14,6 +14,30 @@ interface CirclesProps<RawDatum> {
component: LabelComponent<RawDatum>
}

const getTransitionPhases = <RawDatum,>() => ({
enter: (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 0,
}),
update: (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 1,
}),
leave: (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 0,
}),
})

export const Labels = <RawDatum,>({
nodes,
label,
Expand All @@ -32,29 +56,7 @@ export const Labels = <RawDatum,>({
textColor,
})

const enter = (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 0,
})

const update = (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 1,
})

const leave = (label: ComputedLabel<RawDatum>) => ({
x: label.node.x,
y: label.node.y,
radius: label.node.radius,
textColor: label.textColor,
opacity: 0,
})
const transitionPhases = useMemo(() => getTransitionPhases<RawDatum>(), [])

const transition = useTransition<
ComputedLabel<RawDatum>,
Expand All @@ -67,11 +69,11 @@ export const Labels = <RawDatum,>({
}
>(labels, {
key: label => label.node.id,
initial: update,
from: enter,
enter: update,
update,
leave,
initial: transitionPhases.update,
from: transitionPhases.enter,
enter: transitionPhases.update,
update: transitionPhases.update,
leave: transitionPhases.leave,
config: springConfig,
immediate: !animate,
})
Expand Down

0 comments on commit d917057

Please sign in to comment.