Skip to content

Commit

Permalink
Fix to close modals/dropdowns on graph events
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-phillips-18 committed Jan 14, 2020
1 parent 69ab2c5 commit 9ce9d56
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions frontend/packages/topology/src/behavior/usePanZoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const ZOOM_EXTENT: [number, number] = [0.25, 4];

export type PanZoomRef = (node: SVGGElement | null) => void;

// Used to send events prevented by d3.zoom to the document allowing modals, dropdowns, etc, to close
const propagatePanZoomMouseEvent = (e: Event): void => {
document.dispatchEvent(new MouseEvent(e.type, e));
};

export const usePanZoom = (zoomExtent: [number, number] = ZOOM_EXTENT): PanZoomRef => {
const element = React.useContext(ElementContext);
if (!isGraph(element)) {
Expand All @@ -31,6 +36,9 @@ export const usePanZoom = (zoomExtent: [number, number] = ZOOM_EXTENT): PanZoomR
if (node) {
// TODO fix any type
const $svg = d3.select(node.ownerSVGElement) as any;
if (node && node.ownerSVGElement) {
node.ownerSVGElement.addEventListener('mousedown', propagatePanZoomMouseEvent);
}
const zoom = d3
.zoom()
.scaleExtent(zoomExtent)
Expand Down Expand Up @@ -77,6 +85,9 @@ export const usePanZoom = (zoomExtent: [number, number] = ZOOM_EXTENT): PanZoomR
if (node) {
// remove all zoom listeners
d3.select(node.ownerSVGElement).on('.zoom', null);
if (node.ownerSVGElement) {
node.ownerSVGElement.removeEventListener('mousedown', propagatePanZoomMouseEvent);
}
}
};
},
Expand Down

0 comments on commit 9ce9d56

Please sign in to comment.