Stirling-PDF/frontend/src/core/hooks/useWheelZoom.ts
Reece Browne aa20dbb7a6
Feature/v2/selected pageeditor rework (#4756)
# Description of Changes

<!--
Please provide a summary of the changes, including:

- What was changed
- Why the change was made
- Any challenges encountered

Closes #(issue_number)
-->

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.

---------

Co-authored-by: James Brunton <jbrunton96@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-13 12:53:57 +00:00

84 lines
2.1 KiB
TypeScript

import { RefObject, useEffect } from 'react';
interface UseWheelZoomOptions {
/**
* Element the wheel listener should be bound to.
*/
ref: RefObject<Element | null>;
/**
* Callback executed when the hook decides to zoom in.
*/
onZoomIn: () => void;
/**
* Callback executed when the hook decides to zoom out.
*/
onZoomOut: () => void;
/**
* Whether the wheel listener should be active.
*/
enabled?: boolean;
/**
* How much delta needs to accumulate before a zoom action is triggered.
* Defaults to 10 which matches the previous implementations.
*/
threshold?: number;
/**
* Whether a Ctrl/Cmd modifier is required for zooming. Defaults to true so
* we only react to pinch gestures and intentional ctrl+wheel zooming.
*/
requireModifierKey?: boolean;
}
/**
* Shared hook for handling wheel-based zoom across components.
* It normalises accumulated delta behaviour, prevents default scrolling when
* zoom is triggered, and keeps the handler detached when disabled.
*/
export function useWheelZoom({
ref,
onZoomIn,
onZoomOut,
enabled = true,
threshold = 10,
requireModifierKey = true,
}: UseWheelZoomOptions) {
useEffect(() => {
if (!enabled) {
return;
}
const element = ref.current;
if (!element) {
return;
}
let accumulator = 0;
const handleWheel = (event: Event) => {
const wheelEvent = event as WheelEvent;
const hasModifier = wheelEvent.ctrlKey || wheelEvent.metaKey;
if (requireModifierKey && !hasModifier) {
return;
}
wheelEvent.preventDefault();
wheelEvent.stopPropagation();
accumulator += wheelEvent.deltaY;
if (accumulator <= -threshold) {
onZoomIn();
accumulator = 0;
} else if (accumulator >= threshold) {
onZoomOut();
accumulator = 0;
}
};
element.addEventListener('wheel', handleWheel, { passive: false });
return () => {
element.removeEventListener('wheel', handleWheel);
};
}, [ref, onZoomIn, onZoomOut, enabled, threshold, requireModifierKey]);
}