2023-06-22 10:52:02 -05:00
|
|
|
import React, { useState, useMemo, useEffect } from 'react'
|
2023-06-21 21:17:07 -05:00
|
|
|
|
2023-06-22 10:52:02 -05:00
|
|
|
/**
|
|
|
|
* A context for managing zoom state of a {@see PanZoomPattern}
|
|
|
|
* Allows transform handlers to be available in components outside of the TransformWrapper without a bunch of prop drilling
|
|
|
|
* */
|
2023-06-21 21:17:07 -05:00
|
|
|
export const PanZoomContext = React.createContext({})
|
|
|
|
|
2023-06-22 10:52:02 -05:00
|
|
|
/** Provider for the {@see PanZoomContext} */
|
2023-06-21 21:17:07 -05:00
|
|
|
export const PanZoomContextProvider = ({ children }) => {
|
|
|
|
const [zoomed, setZoomed] = useState(false)
|
|
|
|
const [_zoomFunctions, setZoomFunctions] = useState(false)
|
|
|
|
|
|
|
|
const value = useMemo(() => {
|
|
|
|
const onTransformed = (_ref, state) => {
|
|
|
|
setZoomed(state.scale !== 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
setZoomed(false)
|
|
|
|
_zoomFunctions.resetTransform()
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
zoomed,
|
|
|
|
zoomFunctions: _zoomFunctions ? { ..._zoomFunctions, reset } : false,
|
|
|
|
setZoomFunctions,
|
|
|
|
onTransformed,
|
|
|
|
}
|
|
|
|
}, [zoomed, setZoomed, _zoomFunctions, setZoomFunctions])
|
|
|
|
|
|
|
|
return <PanZoomContext.Provider value={value}>{children}</PanZoomContext.Provider>
|
|
|
|
}
|
2023-06-22 10:52:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A component to capture the zoom functions and set them on the zoom context
|
|
|
|
* Place this inside of a TransformWrapper child function.
|
|
|
|
* See {@see PanZoomPattern} for an example
|
|
|
|
* */
|
|
|
|
export const PanZoomCapture = ({ resetTransform, zoomIn, zoomOut, setZoomFunctions }) => {
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof setZoomFunctions === 'function') {
|
|
|
|
setZoomFunctions({ resetTransform, zoomIn, zoomOut })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return <></>
|
|
|
|
}
|