import React, { useCallback, useEffect, useState, forwardRef, useRef, useImperativeHandle, } from 'react'; import { View, StyleSheet } from 'react-native'; import { callback } from 'react-native-nitro-modules'; import type { Dimensions } from '../interfaces'; import type { PublicationReadyEvent as SpecPublicationReadyEvent } from '../specs/ReadiumView.nitro'; import { buildLinkTree } from '../utils/buildLinkTree'; import { NitroReadiumView } from './NitroReadiumView'; export type { ReadiumViewRef, ReadiumProps } from './ReadiumView.types'; import type { ReadiumViewRef, ReadiumProps } from './ReadiumView.types'; export const ReadiumView = forwardRef( ( { onLocationChange, onPublicationReady, onDecorationActivated, onSelectionChange, onSelectionAction, preferences, decorations, selectionActions, ...props }, forwardedRef ) => { const hybridRef = useRef(null); const [{ height, width }, setDimensions] = useState({ width: 0, height: 0, }); const onLayout = useCallback( ({ nativeEvent: { layout: { width: layoutWidth, height: layoutHeight }, }, }: any) => { setDimensions({ width: layoutWidth, height: layoutHeight, }); }, [] ); const noop = () => {}; const handlePublicationReady = useCallback( (event: SpecPublicationReadyEvent) => { if (!onPublicationReady) return; onPublicationReady({ ...event, tableOfContents: buildLinkTree(event.tableOfContents), }); }, [onPublicationReady] ); useImperativeHandle( forwardedRef, () => ({ goTo: (locator) => hybridRef.current?.goTo(locator), goForward: () => hybridRef.current?.goForward(), goBackward: () => hybridRef.current?.goBackward(), }), [] ); useEffect(() => { return () => { hybridRef.current?.destroy(); }; }, []); const isReady = width > 0 && height > 0; return ( {isReady && ( { hybridRef.current = ref; })} /> )} ); } ); const styles = StyleSheet.create({ container: { width: '100%', height: '100%' }, });