-
-
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.
fix(axes): restore memoization (#1521)
Co-authored-by: Neil Kistner <[email protected]>
- Loading branch information
1 parent
5580e96
commit 729cd55
Showing
8 changed files
with
129 additions
and
115 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 |
---|---|---|
@@ -1,54 +1,57 @@ | ||
import React from 'react' | ||
import React, { memo } from 'react' | ||
import { Axis } from './Axis' | ||
import { positions } from '../props' | ||
import { AnyScale, AxisProps, AxisValue } from '../types' | ||
|
||
export const Axes = <X extends AxisValue, Y extends AxisValue>({ | ||
xScale, | ||
yScale, | ||
width, | ||
height, | ||
top, | ||
right, | ||
bottom, | ||
left, | ||
}: { | ||
xScale: AnyScale | ||
yScale: AnyScale | ||
width: number | ||
height: number | ||
top?: AxisProps<X> | ||
right?: AxisProps<Y> | ||
bottom?: AxisProps<X> | ||
left?: AxisProps<Y> | ||
}) => { | ||
const axes = { top, right, bottom, left } | ||
export const Axes = memo( | ||
<X extends AxisValue, Y extends AxisValue>({ | ||
xScale, | ||
yScale, | ||
width, | ||
height, | ||
top, | ||
right, | ||
bottom, | ||
left, | ||
}: { | ||
xScale: AnyScale | ||
yScale: AnyScale | ||
width: number | ||
height: number | ||
top?: AxisProps<X> | ||
right?: AxisProps<Y> | ||
bottom?: AxisProps<X> | ||
left?: AxisProps<Y> | ||
}) => { | ||
const axes = { top, right, bottom, left } | ||
|
||
return ( | ||
<> | ||
{positions.map(position => { | ||
const axis = axes[position] as typeof position extends 'bottom' | 'top' | ||
? AxisProps<X> | undefined | ||
: AxisProps<Y> | undefined | ||
return ( | ||
<> | ||
{positions.map(position => { | ||
const axis = axes[position] as typeof position extends 'bottom' | 'top' | ||
? AxisProps<X> | undefined | ||
: AxisProps<Y> | undefined | ||
|
||
if (!axis) return null | ||
if (!axis) return null | ||
|
||
const isXAxis = position === 'top' || position === 'bottom' | ||
const ticksPosition = position === 'top' || position === 'left' ? 'before' : 'after' | ||
const isXAxis = position === 'top' || position === 'bottom' | ||
const ticksPosition = | ||
position === 'top' || position === 'left' ? 'before' : 'after' | ||
|
||
return ( | ||
<Axis | ||
key={position} | ||
{...axis} | ||
axis={isXAxis ? 'x' : 'y'} | ||
x={position === 'right' ? width : 0} | ||
y={position === 'bottom' ? height : 0} | ||
scale={isXAxis ? xScale : yScale} | ||
length={isXAxis ? width : height} | ||
ticksPosition={ticksPosition} | ||
/> | ||
) | ||
})} | ||
</> | ||
) | ||
} | ||
return ( | ||
<Axis | ||
key={position} | ||
{...axis} | ||
axis={isXAxis ? 'x' : 'y'} | ||
x={position === 'right' ? width : 0} | ||
y={position === 'bottom' ? height : 0} | ||
scale={isXAxis ? xScale : yScale} | ||
length={isXAxis ? width : height} | ||
ticksPosition={ticksPosition} | ||
/> | ||
) | ||
})} | ||
</> | ||
) | ||
} | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
import React, { useMemo } from 'react' | ||
import React, { useMemo, memo } from 'react' | ||
import { GridLines } from './GridLines' | ||
import { computeGridLines } from '../compute' | ||
import { AnyScale, AxisValue } from '../types' | ||
|
||
export const Grid = <X extends AxisValue, Y extends AxisValue>({ | ||
width, | ||
height, | ||
xScale, | ||
yScale, | ||
xValues, | ||
yValues, | ||
}: { | ||
width: number | ||
height: number | ||
xScale?: AnyScale | ||
xValues?: number | X[] | ||
yScale?: AnyScale | ||
yValues?: number | Y[] | ||
}) => { | ||
const xLines = useMemo(() => { | ||
if (!xScale) return false | ||
export const Grid = memo( | ||
<X extends AxisValue, Y extends AxisValue>({ | ||
width, | ||
height, | ||
xScale, | ||
yScale, | ||
xValues, | ||
yValues, | ||
}: { | ||
width: number | ||
height: number | ||
xScale?: AnyScale | ||
xValues?: number | X[] | ||
yScale?: AnyScale | ||
yValues?: number | Y[] | ||
}) => { | ||
const xLines = useMemo(() => { | ||
if (!xScale) return false | ||
|
||
return computeGridLines({ | ||
width, | ||
height, | ||
scale: xScale, | ||
axis: 'x', | ||
values: xValues, | ||
}) | ||
}, [xScale, xValues, width, height]) | ||
return computeGridLines({ | ||
width, | ||
height, | ||
scale: xScale, | ||
axis: 'x', | ||
values: xValues, | ||
}) | ||
}, [xScale, xValues, width, height]) | ||
|
||
const yLines = useMemo(() => { | ||
if (!yScale) return false | ||
const yLines = useMemo(() => { | ||
if (!yScale) return false | ||
|
||
return computeGridLines({ | ||
width, | ||
height, | ||
scale: yScale, | ||
axis: 'y', | ||
values: yValues, | ||
}) | ||
}, [height, width, yScale, yValues]) | ||
return computeGridLines({ | ||
width, | ||
height, | ||
scale: yScale, | ||
axis: 'y', | ||
values: yValues, | ||
}) | ||
}, [height, width, yScale, yValues]) | ||
|
||
return ( | ||
<> | ||
{xLines && <GridLines lines={xLines} />} | ||
{yLines && <GridLines lines={yLines} />} | ||
</> | ||
) | ||
} | ||
return ( | ||
<> | ||
{xLines && <GridLines lines={xLines} />} | ||
{yLines && <GridLines lines={yLines} />} | ||
</> | ||
) | ||
} | ||
) |
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,19 +1,21 @@ | ||
import React from 'react' | ||
import React, { memo } from 'react' | ||
import { SpringValues, animated } from '@react-spring/web' | ||
import { useTheme } from '@nivo/core' | ||
|
||
export const GridLine = ({ | ||
animatedProps, | ||
}: { | ||
animatedProps: SpringValues<{ | ||
opacity: number | ||
x1: number | ||
x2: number | ||
y1: number | ||
y2: number | ||
}> | ||
}) => { | ||
const theme = useTheme() | ||
export const GridLine = memo( | ||
({ | ||
animatedProps, | ||
}: { | ||
animatedProps: SpringValues<{ | ||
opacity: number | ||
x1: number | ||
x2: number | ||
y1: number | ||
y2: number | ||
}> | ||
}) => { | ||
const theme = useTheme() | ||
|
||
return <animated.line {...animatedProps} {...(theme.grid.line as unknown)} /> | ||
} | ||
return <animated.line {...animatedProps} {...(theme.grid.line as unknown)} /> | ||
} | ||
) |
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