Skip to content

Commit

Permalink
perf: use static EventListener options
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdyman committed Dec 20, 2020
1 parent 23b1dcb commit 9cab1ee
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/ReactCompareSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ interface UpdateInternalPositionProps
isOffset?: boolean;
}

const EVENT_PASSIVE_PARAMS = { passive: true };
const EVENT_CAPTURE_PARAMS = { capture: true, passive: false };

/** Root Comparison slider. */
export const ReactCompareSlider: React.FC<
ReactCompareSliderProps & React.HtmlHTMLAttributes<HTMLDivElement>
Expand Down Expand Up @@ -220,7 +223,7 @@ export const ReactCompareSlider: React.FC<
[onPositionChange]
);

// Update internal position if `position` prop changes.
// Update internal position when other user controllable props change.
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { width, height } = rootContainerRef.current!.getBoundingClientRect();
Expand Down Expand Up @@ -292,22 +295,10 @@ export const ReactCompareSlider: React.FC<
// Allow drag outside of container while pointer is still down.
useEffect(() => {
if (isDragging && !hasWindowBinding.current) {
window.addEventListener('mousemove', handlePointerMove, {
passive: true,
});

window.addEventListener('mouseup', handlePointerUp, {
passive: true,
});

window.addEventListener('touchmove', handlePointerMove, {
passive: true,
});

window.addEventListener('touchend', handlePointerUp, {
passive: true,
});

window.addEventListener('mousemove', handlePointerMove, EVENT_PASSIVE_PARAMS);
window.addEventListener('mouseup', handlePointerUp, EVENT_PASSIVE_PARAMS);
window.addEventListener('touchmove', handlePointerMove, EVENT_PASSIVE_PARAMS);
window.addEventListener('touchend', handlePointerUp, EVENT_PASSIVE_PARAMS);
hasWindowBinding.current = true;
}

Expand All @@ -325,17 +316,21 @@ export const ReactCompareSlider: React.FC<
// Bind resize observer to container.
useResizeObserver(rootContainerRef, handleResize);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
useEventListener('mousedown', handlePointerDown, interactiveTarget!, {
capture: true,
passive: false,
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
useEventListener('touchstart', handlePointerDown, interactiveTarget!, {
capture: true,
passive: false,
});
useEventListener(
'mousedown',
handlePointerDown,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
interactiveTarget!,
EVENT_CAPTURE_PARAMS
);

useEventListener(
'touchstart',
handlePointerDown,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
interactiveTarget!,
EVENT_CAPTURE_PARAMS
);

// Use custom handle if requested.
const Handle = handle || <ReactCompareSliderHandle portrait={portrait} />;
Expand Down

0 comments on commit 9cab1ee

Please sign in to comment.