Skip to content

Commit

Permalink
feat(viewer): add viewerRef prop to InsightViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
LTakhyunKim committed Dec 1, 2022
1 parent 7cc19d5 commit 698297e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libs/insight-viewer/src/Viewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function InsightViewer({
interaction,
onViewportChange,
children,
viewerRef,
}: WithChildren<ViewerProp>): JSX.Element {
const elRef = useRef<HTMLDivElement>(null)
const viewportRef = useRef<Partial<Viewport>>(viewport ?? {}) // viewport props
Expand Down Expand Up @@ -48,6 +49,14 @@ export function InsightViewer({
if (viewport) viewportRef.current = viewport
}, [viewport])

/**
* This useEffect is intended to update the viewerRef in an external app
* This is a useEffect that runs only once when it is first loaded.
*/
useEffect(() => {
if (elRef && viewerRef) viewerRef.current = elRef.current
}, [elRef, viewerRef])

return (
<ViewerWrapper ref={elRef} Progress={Progress} onViewportChange={onViewportChange} imageEnabled={!!image}>
<OverlayContextProvider image={image} element={elRef.current} imageEnabled={!!image} viewport={viewport}>
Expand Down
3 changes: 3 additions & 0 deletions libs/insight-viewer/src/Viewer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ProgressComponent, Viewport, OnViewportChange } from '../types'
import { SetFrame } from '../hooks/useMultipleImages/useFrame'
import { Interaction } from '../hooks/useInteraction/types'

import type { MutableRefObject } from 'react'

export type Image = (CornerstoneImage & { _imageSeriesKey?: string; data: DataSet }) | undefined
export type ImageWithoutKey = CornerstoneImage & { data: DataSet }

Expand All @@ -15,4 +17,5 @@ export type ViewerProp = {
onViewportChange?: OnViewportChange
onFrameChange?: SetFrame
interaction?: Interaction
viewerRef?: MutableRefObject<HTMLDivElement | null>
}

0 comments on commit 698297e

Please sign in to comment.