1 import OpenSeadragon from 'openseadragon';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { createPortal } from 'react-dom';
6 import { useOpenSeadragon } from './OpenSeadragon';
8 const Overlay = ({ children, height, onClick, page, width, x, y }) => {
9 const { viewer } = useOpenSeadragon();
10 const [element] = React.useState(document.createElement('div'));
12 React.useEffect(() => {
15 if (width && height) {
18 new OpenSeadragon.Rect(x, y, width, height),
23 new OpenSeadragon.Point(x, y),
24 OpenSeadragon.Placement.CENTER,
28 new OpenSeadragon.MouseTracker({
30 clickHandler: onClick,
34 const addPage = () => {
35 if (viewer.currentPage() === page) {
39 if (typeof page !== 'undefined') {
40 viewer.addHandler('page', addPage);
42 viewer.removeHandler('page', addPage);
44 viewer.removeOverlay(element);
51 if (viewer.isOpen()) {
54 viewer.addHandler('open', add);
56 viewer.removeHandler('open', add);
58 viewer.removeOverlay(element);
65 }, [onClick, height, page, viewer, width, x, y]);
67 return createPortal(children, element);
71 children: PropTypes.node,
72 height: PropTypes.number,
73 onClick: PropTypes.func,
74 page: PropTypes.number,
75 width: PropTypes.number,
80 export default Overlay;