Files
readest/apps/readest-app/src/app/reader/hooks/useFoliateEvents.ts
T
2024-10-31 17:21:10 +01:00

25 lines
749 B
TypeScript

import { useEffect } from 'react';
import { FoliateView } from '@/app/reader/components/FoliateViewer';
type FoliateEventHandler = {
onLoad?: (event: Event) => void;
onRelocate?: (event: Event) => void;
};
export const useFoliateEvents = (view: FoliateView | null, handlers?: FoliateEventHandler) => {
const onLoad = handlers?.onLoad;
const onRelocate = handlers?.onRelocate;
useEffect(() => {
if (!view) return;
if (onLoad) view.addEventListener('load', onLoad);
if (onRelocate) view.addEventListener('relocate', onRelocate);
return () => {
if (onLoad) view.removeEventListener('load', onLoad);
if (onRelocate) view.removeEventListener('relocate', onRelocate);
};
}, [view, onLoad, onRelocate]);
};