-
-
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(pie): restore feature parity for slices
- Loading branch information
Showing
8 changed files
with
187 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './motion' | ||
export * from './types' | ||
export * from './useAnimatedArc' | ||
export * from './useArcGenerator' | ||
export * from './useArcsTransition' |
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,115 @@ | ||
import { to, SpringValue, useSpring, useTransition } from 'react-spring' | ||
import { useMotionConfig } from '@nivo/core' | ||
import { Arc, ArcGenerator, DatumWithArc } from './types' | ||
|
||
/** | ||
* Directly animating paths for arcs leads to sub-optimal results | ||
* as the interpolation is gonna be linear while we deal with polar coordinates, | ||
* this interpolator is gonna generate proper arc transitions. | ||
* It should be used with the `useAnimatedArc` or `useArcsTransition` hooks. | ||
*/ | ||
export const interpolateArc = ( | ||
startAngleValue: SpringValue<number>, | ||
endAngleValue: SpringValue<number>, | ||
innerRadiusValue: SpringValue<number>, | ||
outerRadiusValue: SpringValue<number>, | ||
arcGenerator: ArcGenerator | ||
) => | ||
to( | ||
[startAngleValue, endAngleValue, innerRadiusValue, outerRadiusValue], | ||
(startAngle, endAngle, innerRadius, outerRadius) => { | ||
return arcGenerator({ | ||
startAngle, | ||
endAngle, | ||
innerRadius, | ||
outerRadius, | ||
}) | ||
} | ||
) | ||
|
||
/** | ||
* This hook can be used to animate a single arc, | ||
* if you want to animate a group of arcs, | ||
* please have a look at the `useArcsTransition` hook. | ||
*/ | ||
export const useAnimatedArc = (datumWithArc: { arc: Arc }, arcGenerator: ArcGenerator) => { | ||
const { animate, config: springConfig } = useMotionConfig() | ||
|
||
const animatedValues = useSpring({ | ||
startAngle: datumWithArc.arc.startAngle, | ||
endAngle: datumWithArc.arc.endAngle, | ||
innerRadius: datumWithArc.arc.innerRadius, | ||
outerRadius: datumWithArc.arc.outerRadius, | ||
config: springConfig, | ||
immediate: !animate, | ||
}) | ||
|
||
return { | ||
...animatedValues, | ||
path: interpolateArc( | ||
animatedValues.startAngle, | ||
animatedValues.endAngle, | ||
animatedValues.innerRadius, | ||
animatedValues.outerRadius, | ||
arcGenerator | ||
), | ||
} | ||
} | ||
|
||
/** | ||
* This hook can be used to animate a group of arcs, | ||
* if you want to animate a single arc, | ||
* please have a look at the `useAnimatedArc` hook. | ||
*/ | ||
export const useArcsTransition = <Datum extends DatumWithArc>(data: Datum[]) => { | ||
const { animate, config: springConfig } = useMotionConfig() | ||
|
||
const transition = useTransition< | ||
Datum, | ||
{ | ||
startAngle: number | ||
endAngle: number | ||
innerRadius: number | ||
outerRadius: number | ||
} | ||
>(data, { | ||
key: datum => datum.id, | ||
initial: datum => ({ | ||
startAngle: datum.arc.startAngle, | ||
endAngle: datum.arc.endAngle, | ||
innerRadius: datum.arc.innerRadius, | ||
outerRadius: datum.arc.outerRadius, | ||
}), | ||
from: datum => ({ | ||
startAngle: datum.arc.startAngle + (datum.arc.endAngle - datum.arc.startAngle) / 2, | ||
endAngle: datum.arc.startAngle + (datum.arc.endAngle - datum.arc.startAngle) / 2, | ||
innerRadius: datum.arc.innerRadius, | ||
outerRadius: datum.arc.outerRadius, | ||
}), | ||
enter: datum => ({ | ||
startAngle: datum.arc.startAngle, | ||
endAngle: datum.arc.endAngle, | ||
innerRadius: datum.arc.innerRadius, | ||
outerRadius: datum.arc.outerRadius, | ||
}), | ||
update: datum => ({ | ||
startAngle: datum.arc.startAngle, | ||
endAngle: datum.arc.endAngle, | ||
innerRadius: datum.arc.innerRadius, | ||
outerRadius: datum.arc.outerRadius, | ||
}), | ||
leave: datum => ({ | ||
startAngle: datum.arc.startAngle + (datum.arc.endAngle - datum.arc.startAngle) / 2, | ||
endAngle: datum.arc.startAngle + (datum.arc.endAngle - datum.arc.startAngle) / 2, | ||
innerRadius: datum.arc.innerRadius, | ||
outerRadius: datum.arc.outerRadius, | ||
}), | ||
config: springConfig, | ||
immediate: !animate, | ||
}) | ||
|
||
return { | ||
transition, | ||
interpolate: interpolateArc, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.