-
-
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): use generic ArcsLayer from the arcs package
- Loading branch information
Showing
5 changed files
with
207 additions
and
137 deletions.
There are no files selected for viewing
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,60 @@ | ||
import React, { useCallback } from 'react' | ||
import { SpringValue, Interpolation, animated } from 'react-spring' | ||
import { DatumWithArcAndColor } from './types' | ||
|
||
export type ArcMouseHandler<Datum extends DatumWithArcAndColor> = ( | ||
datum: Datum, | ||
event: React.MouseEvent<SVGPathElement> | ||
) => void | ||
|
||
export interface ArcShapeProps<Datum extends DatumWithArcAndColor> { | ||
data: Datum | ||
style: { | ||
opacity: SpringValue<number> | ||
color: SpringValue<string> | ||
borderWidth: number | ||
borderColor: SpringValue<string> | ||
path: Interpolation<string> | ||
} | ||
onClick?: ArcMouseHandler<Datum> | ||
onMouseEnter?: ArcMouseHandler<Datum> | ||
onMouseMove?: ArcMouseHandler<Datum> | ||
onMouseLeave?: ArcMouseHandler<Datum> | ||
} | ||
|
||
/** | ||
* A simple arc component to be used typically with an `ArcsLayer`. | ||
* | ||
* Please note that the component accepts `SpringValue`s instead of | ||
* regular values to support animations. | ||
*/ | ||
export const ArcShape = <Datum extends DatumWithArcAndColor>({ | ||
data, | ||
style, | ||
onClick, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
}: ArcShapeProps<Datum>) => { | ||
const handleClick = useCallback(event => onClick?.(data, event), [onClick, data]) | ||
|
||
const handleMouseEnter = useCallback(event => onMouseEnter?.(data, event), [onMouseEnter, data]) | ||
|
||
const handleMouseMove = useCallback(event => onMouseMove?.(data, event), [onMouseMove, data]) | ||
|
||
const handleMouseLeave = useCallback(event => onMouseLeave?.(data, event), [onMouseLeave, data]) | ||
|
||
return ( | ||
<animated.path | ||
d={style.path} | ||
opacity={style.opacity} | ||
// fill={datum.fill || color} | ||
fill={style.color} | ||
stroke={style.borderColor} | ||
onClick={onClick ? handleClick : undefined} | ||
onMouseEnter={onMouseEnter ? handleMouseEnter : undefined} | ||
onMouseMove={onMouseMove ? handleMouseMove : undefined} | ||
onMouseLeave={onMouseLeave ? handleMouseLeave : undefined} | ||
/> | ||
) | ||
} |
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,93 @@ | ||
import React, { createElement } from 'react' | ||
import { useTheme } from '@nivo/core' | ||
import { InheritedColorConfig, useInheritedColor } from '@nivo/colors' | ||
import { DatumWithArcAndColor, ArcGenerator } from './types' | ||
import { useArcsTransition } from './useArcsTransition' | ||
import { ArcTransitionMode } from './arcTransitionMode' | ||
import { ArcMouseHandler, ArcShape, ArcShapeProps } from './ArcShape' | ||
|
||
type ArcComponent<Datum extends DatumWithArcAndColor> = (props: ArcShapeProps<Datum>) => JSX.Element | ||
|
||
interface ArcsLayerProps<Datum extends DatumWithArcAndColor> { | ||
center: [number, number] | ||
data: Datum[] | ||
arcGenerator: ArcGenerator | ||
borderWidth: number | ||
borderColor: InheritedColorConfig<Datum> | ||
onClick?: ArcMouseHandler<Datum> | ||
onMouseEnter?: ArcMouseHandler<Datum> | ||
onMouseMove?: ArcMouseHandler<Datum> | ||
onMouseLeave?: ArcMouseHandler<Datum> | ||
transitionMode: ArcTransitionMode | ||
component?: ArcComponent<Datum> | ||
} | ||
|
||
export const ArcsLayer = <Datum extends DatumWithArcAndColor>({ | ||
center, | ||
data, | ||
arcGenerator, | ||
borderWidth, | ||
borderColor, | ||
onClick, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
transitionMode, | ||
component = ArcShape, | ||
}: ArcsLayerProps<Datum>) => { | ||
const theme = useTheme() | ||
const getBorderColor = useInheritedColor<Datum>(borderColor, theme) | ||
|
||
const { transition, interpolate } = useArcsTransition< | ||
Datum, | ||
{ | ||
opacity: number | ||
color: string | ||
borderColor: string | ||
} | ||
>(data, transitionMode, { | ||
enter: datum => ({ | ||
opacity: 0, | ||
color: datum.color, | ||
borderColor: getBorderColor(datum), | ||
}), | ||
update: datum => ({ | ||
opacity: 1, | ||
color: datum.color, | ||
borderColor: getBorderColor(datum), | ||
}), | ||
leave: datum => ({ | ||
opacity: 0, | ||
color: datum.color, | ||
borderColor: getBorderColor(datum), | ||
}), | ||
}) | ||
|
||
const Arc: ArcComponent<Datum> = component | ||
|
||
return ( | ||
<g transform={`translate(${center[0]},${center[1]})`}> | ||
{transition((transitionProps, datum) => { | ||
return createElement(Arc, { | ||
key: datum.id, | ||
data: datum, | ||
style: { | ||
...transitionProps, | ||
borderWidth, | ||
path: interpolate( | ||
transitionProps.startAngle, | ||
transitionProps.endAngle, | ||
transitionProps.innerRadius, | ||
transitionProps.outerRadius, | ||
arcGenerator | ||
), | ||
}, | ||
onClick, | ||
onMouseEnter, | ||
onMouseMove, | ||
onMouseLeave, | ||
}) | ||
})} | ||
</g> | ||
) | ||
} |
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 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