From d9170572150330632747b2ce3ce6ca50e15a0444 Mon Sep 17 00:00:00 2001 From: plouc Date: Wed, 23 Dec 2020 08:42:38 +0900 Subject: [PATCH] feat(circle-packing): memoize labels transition phases --- packages/circle-packing/src/Labels.tsx | 60 +++++++++++++------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/circle-packing/src/Labels.tsx b/packages/circle-packing/src/Labels.tsx index 9db3fec76..08f18c572 100644 --- a/packages/circle-packing/src/Labels.tsx +++ b/packages/circle-packing/src/Labels.tsx @@ -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' @@ -14,6 +14,30 @@ interface CirclesProps { component: LabelComponent } +const getTransitionPhases = () => ({ + enter: (label: ComputedLabel) => ({ + x: label.node.x, + y: label.node.y, + radius: label.node.radius, + textColor: label.textColor, + opacity: 0, + }), + update: (label: ComputedLabel) => ({ + x: label.node.x, + y: label.node.y, + radius: label.node.radius, + textColor: label.textColor, + opacity: 1, + }), + leave: (label: ComputedLabel) => ({ + x: label.node.x, + y: label.node.y, + radius: label.node.radius, + textColor: label.textColor, + opacity: 0, + }), +}) + export const Labels = ({ nodes, label, @@ -32,29 +56,7 @@ export const Labels = ({ textColor, }) - const enter = (label: ComputedLabel) => ({ - x: label.node.x, - y: label.node.y, - radius: label.node.radius, - textColor: label.textColor, - opacity: 0, - }) - - const update = (label: ComputedLabel) => ({ - x: label.node.x, - y: label.node.y, - radius: label.node.radius, - textColor: label.textColor, - opacity: 1, - }) - - const leave = (label: ComputedLabel) => ({ - x: label.node.x, - y: label.node.y, - radius: label.node.radius, - textColor: label.textColor, - opacity: 0, - }) + const transitionPhases = useMemo(() => getTransitionPhases(), []) const transition = useTransition< ComputedLabel, @@ -67,11 +69,11 @@ export const Labels = ({ } >(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, })