-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
99eb9f6
commit e1b4cf1
Showing
6 changed files
with
182 additions
and
2,039 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
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,57 @@ | ||
import dynamic from "next/dynamic"; | ||
import { Config, Layout } from "plotly.js"; | ||
import { | ||
forwardRef, | ||
useEffect, | ||
useId, | ||
useImperativeHandle, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
interface PlotlyProps { | ||
id?: string; | ||
className?: string; | ||
data: Plotly.Data[]; | ||
layout?: Partial<Layout>; | ||
config?: Partial<Config>; | ||
} | ||
type PlotlyHTML = Plotly.PlotlyHTMLElement & { align: string }; | ||
const Plotly = dynamic( | ||
() => | ||
import("plotly.js-dist-min").then(({ newPlot, purge }) => { | ||
const Plotly = forwardRef<HTMLDivElement, PlotlyProps>( | ||
({ className, config, data, id, layout }, ref) => { | ||
const originId = useId(); | ||
const realId = id || originId; | ||
const originRef = useRef(null); | ||
const [handle, setHandle] = useState< | ||
Plotly.PlotlyHTMLElement | undefined | ||
>(undefined); | ||
|
||
useEffect(() => { | ||
let instance: Plotly.PlotlyHTMLElement | undefined; | ||
if (originRef.current) { | ||
newPlot(originRef.current, data, layout, config).then((ref) => { | ||
setHandle((instance = ref)); | ||
}); | ||
} | ||
return () => { | ||
if (instance) { | ||
purge(instance); | ||
} | ||
}; | ||
}, [data, layout, config]); | ||
|
||
useImperativeHandle(ref, () => handle as PlotlyHTML, [handle]); | ||
|
||
return <div className={className} id={realId} ref={originRef} />; | ||
}, | ||
); | ||
Plotly.displayName = "Plotly"; | ||
return Plotly; | ||
}), | ||
{ ssr: false }, | ||
); | ||
|
||
export default Plotly; |
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.