-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
394 additions
and
166 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
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,10 @@ | ||
import React from 'react'; | ||
import Axis, { AxisProps } from './Axis'; | ||
import AnimatedTicksRenderer from './AnimatedTicksRenderer'; | ||
import { AxisScale } from '../types'; | ||
|
||
export default function AnimatedAxis<Scale extends AxisScale>( | ||
axisProps: Omit<AxisProps<Scale>, 'ticksComponent'>, | ||
) { | ||
return <Axis {...axisProps} ticksComponent={AnimatedTicksRenderer} />; | ||
} |
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,62 @@ | ||
import React from 'react'; | ||
import { animated, useTransition, interpolate } from 'react-spring'; | ||
import cx from 'classnames'; | ||
import { Text } from '@vx/text'; | ||
|
||
import Orientation from '../../constants/orientation'; | ||
import { TicksRendererProps, AxisScale } from '../../types'; | ||
import useTickTransitionConfig from './useTickTransitionConfig'; | ||
|
||
export default function TicksRenderer<Scale extends AxisScale>({ | ||
hideTicks, | ||
horizontal, | ||
orientation, | ||
scale, | ||
tickClassName, | ||
tickLabelProps: allTickLabelProps, | ||
tickStroke = '#222', | ||
tickTransform, | ||
ticks, | ||
}: TicksRendererProps<Scale>) { | ||
const transitionConfig = useTickTransitionConfig({ horizontal, scale }); | ||
const animatedTicks = useTransition(ticks, tick => `${tick.value}-${horizontal}`, { | ||
unique: true, | ||
...transitionConfig, | ||
}); | ||
|
||
return animatedTicks.map(({ item, key, props }, index) => { | ||
// @ts-ignore react-spring types don't handle fromX, etc. | ||
const { fromX, toX, fromY, toY, opacity } = props; | ||
const tickLabelProps = allTickLabelProps[index] ?? allTickLabelProps[0] ?? {}; | ||
return ( | ||
<animated.g key={key} className={cx('vx-axis-tick', tickClassName)} transform={tickTransform}> | ||
{!hideTicks && ( | ||
<animated.line | ||
x1={fromX} | ||
x2={toX} | ||
y1={fromY} | ||
y2={toY} | ||
stroke={tickStroke} | ||
strokeLinecap="square" | ||
strokeOpacity={opacity} | ||
/> | ||
)} | ||
{/** animate the group, not the Text */} | ||
<animated.g | ||
key={index} | ||
transform={interpolate( | ||
[toX, toY], | ||
(interpolatedX, interpolatedY) => | ||
`translate(${interpolatedX},${interpolatedY + | ||
(orientation === Orientation.bottom && typeof tickLabelProps.fontSize === 'number' | ||
? tickLabelProps.fontSize ?? 10 | ||
: 0)})`, | ||
)} | ||
opacity={opacity} | ||
> | ||
<Text {...tickLabelProps}>{item.formattedValue}</Text> | ||
</animated.g> | ||
</animated.g> | ||
); | ||
}); | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/vx-axis/src/axis/AnimatedTicksRenderer/useTickTransitionConfig.ts
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,54 @@ | ||
import { useMemo } from 'react'; | ||
import { coerceNumber } from '@vx/scale'; | ||
import { AxisScale, ComputedTick, TicksRendererProps } from '../../types'; | ||
|
||
function enterUpdate<Scale extends AxisScale>({ from, to }: ComputedTick<Scale>) { | ||
return { | ||
fromX: from.x, | ||
toX: to.x, | ||
fromY: from.y, | ||
toY: to.y, | ||
opacity: 1, | ||
}; | ||
} | ||
|
||
export default function useTickTransitionConfig<Scale extends AxisScale>({ | ||
horizontal, | ||
scale, | ||
}: Pick<TicksRendererProps<Scale>, 'scale' | 'horizontal'>) { | ||
return useMemo(() => { | ||
const [a, b] = scale.range(); | ||
const isDescending = b != null && a != null && b < a; | ||
const [minPosition, maxPosition] = isDescending ? [b, a] : [a, b]; | ||
const scaleLength = b != null && a != null ? Math.abs(coerceNumber(b) - coerceNumber(a)) : 0; | ||
|
||
const fromLeave = ({ from, to, value }: ComputedTick<Scale>) => { | ||
const scaledValue = scale(value) ?? 0; | ||
|
||
return { | ||
fromX: horizontal | ||
? // for top/bottom scales, enter from left or right based on value | ||
scaledValue < scaleLength / 2 | ||
? minPosition | ||
: maxPosition | ||
: // for left/right scales, don't animate x | ||
from.x, | ||
// same logic as above for the `to` Point | ||
toX: horizontal ? (scaledValue < scaleLength / 2 ? minPosition : maxPosition) : to.x, | ||
// for top/bottom scales, don't animate y | ||
fromY: horizontal | ||
? // for top/bottom scales, don't animate y | ||
from.y | ||
: // for left/right scales, animate from top or bottom based on value | ||
scaledValue < scaleLength / 2 | ||
? minPosition | ||
: maxPosition, | ||
// same logic as above for the `to` Point | ||
toY: horizontal ? to.y : scaledValue < scaleLength / 2 ? minPosition : maxPosition, | ||
opacity: 0, | ||
}; | ||
}; | ||
|
||
return { from: fromLeave, leave: fromLeave, enter: enterUpdate, update: enterUpdate }; | ||
}, [horizontal, scale]); | ||
} |
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
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
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.