-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate victory-canvas to TypeScript (#2710)
- Loading branch information
1 parent
9dff1f3
commit b6ceae1
Showing
11 changed files
with
190 additions
and
263 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,5 @@ | ||
--- | ||
"victory-canvas": patch | ||
--- | ||
|
||
Migrate victory-canvas to TypeScript |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from "react"; | ||
import { | ||
LineHelpers, | ||
NumberOrCallback, | ||
StringOrCallback, | ||
VictoryCommonPrimitiveProps, | ||
} from "victory-core"; | ||
import { useCanvasContext } from "./hooks/use-canvas-context"; | ||
import { LineRadial } from "../../victory-vendor/d3-shape"; | ||
|
||
export interface CanvasCurveProps extends VictoryCommonPrimitiveProps { | ||
ariaLabel?: StringOrCallback; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
interpolation?: string | Function; | ||
openCurve?: boolean; | ||
tabIndex?: NumberOrCallback; | ||
} | ||
|
||
export const CanvasCurve = (props: CanvasCurveProps) => { | ||
const { canvasRef, clear, clip } = useCanvasContext(); | ||
const { style, data } = props; | ||
const { stroke, strokeWidth } = style; | ||
|
||
const draw = React.useCallback( | ||
(ctx: CanvasRenderingContext2D) => { | ||
const line = LineHelpers.getLineFunction(props) as LineRadial< | ||
[number, number] | ||
>; | ||
ctx.strokeStyle = stroke; | ||
ctx.lineWidth = strokeWidth; | ||
line.context(ctx)(data); | ||
ctx.stroke(); | ||
}, | ||
[data, props, stroke, strokeWidth], | ||
); | ||
|
||
React.useEffect(() => { | ||
const ctx = canvasRef.current?.getContext("2d"); | ||
if (!ctx) return; | ||
clear(ctx); | ||
draw(ctx); | ||
clip(ctx); | ||
}, [canvasRef, draw, clear, clip]); | ||
|
||
return null; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from "react"; | ||
import { CanvasContext } from "./hooks/use-canvas-context"; | ||
import { PaddingProps } from "victory-core"; | ||
|
||
export interface CanvasGroupProps { | ||
children?: React.ReactNode | React.ReactNode[]; | ||
clipWidth?: number; | ||
height?: number; | ||
padding?: PaddingProps; | ||
width?: number; | ||
} | ||
|
||
export const CanvasGroup = (props: CanvasGroupProps) => { | ||
const canvasRef = React.useRef<HTMLCanvasElement>(null); | ||
const { children, width = 0, height = 0, clipWidth, padding } = props; | ||
|
||
const clear = React.useCallback( | ||
(ctx: CanvasRenderingContext2D) => { | ||
return ctx.clearRect(0, 0, width, height); | ||
}, | ||
[width, height], | ||
); | ||
|
||
// This needs to be called in the child component to ensure it is called after the | ||
// shape is drawn | ||
const clip = React.useCallback( | ||
(ctx: CanvasRenderingContext2D) => { | ||
const paddingRight = | ||
typeof padding === "number" ? padding : padding?.right || 0; | ||
const paddingLeft = | ||
typeof padding === "number" ? padding : padding?.left || 0; | ||
|
||
const maxClipWidth = width - paddingRight - paddingLeft; | ||
ctx.clearRect( | ||
width - paddingRight, | ||
0, | ||
clipWidth ? (maxClipWidth - clipWidth) * -1 : 0, | ||
height, | ||
); | ||
}, | ||
[width, height, padding, clipWidth], | ||
); | ||
|
||
return ( | ||
<CanvasContext.Provider | ||
value={{ | ||
canvasRef, | ||
clear, | ||
clip, | ||
}} | ||
> | ||
<foreignObject width={width} height={height} x={0} y={0}> | ||
<canvas width={width} height={height} ref={canvasRef} /> | ||
</foreignObject> | ||
{children} | ||
</CanvasContext.Provider> | ||
); | ||
}; | ||
|
||
CanvasGroup.role = "container"; |
Oops, something went wrong.
b6ceae1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
victory – ./
victory-rose.vercel.app
victory-git-main-formidable-labs.vercel.app
victory-formidable-labs.vercel.app