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);
43 viewer.removeOverlay(element);
46 if (viewer.isOpen()) {
49 viewer.addHandler('open', add);
51 viewer.removeHandler('open', add);
52 viewer.removeOverlay(element);
55 }, [onClick, height, page, viewer, width, x, y]);
57 return createPortal(children, element);
61 children: PropTypes.node,
62 height: PropTypes.number,
63 onClick: PropTypes.func,
64 page: PropTypes.number,
65 width: PropTypes.number,
70 export default Overlay;