react-native-readium 2.0.0-rc.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import React, { useCallback, useState, useEffect, forwardRef } from 'react';
|
|
1
|
+
import React, { useCallback, useState, useEffect, forwardRef, useRef } from 'react';
|
|
2
2
|
import { View, Platform, findNodeHandle, StyleSheet } from 'react-native';
|
|
3
3
|
import { Settings } from '../interfaces';
|
|
4
4
|
import { createFragment, getWidthOrHeightValue as dimension } from '../utils';
|
|
5
5
|
import { BaseReadiumView } from './BaseReadiumView';
|
|
6
|
-
export const ReadiumView = forwardRef(({ onLocationChange: wrappedOnLocationChange, onTableOfContents: wrappedOnTableOfContents, settings: unmappedSettings, ...props },
|
|
6
|
+
export const ReadiumView = forwardRef(({ onLocationChange: wrappedOnLocationChange, onTableOfContents: wrappedOnTableOfContents, settings: unmappedSettings, ...props }, forwardedRef) => {
|
|
7
|
+
const defaultRef = useRef(null);
|
|
7
8
|
const [{ height, width }, setDimensions] = useState({
|
|
8
9
|
width: 0,
|
|
9
10
|
height: 0,
|
|
@@ -27,15 +28,24 @@ export const ReadiumView = forwardRef(({ onLocationChange: wrappedOnLocationChan
|
|
|
27
28
|
wrappedOnTableOfContents(toc);
|
|
28
29
|
}
|
|
29
30
|
}, [wrappedOnTableOfContents]);
|
|
31
|
+
// create the view fragment on android
|
|
30
32
|
useEffect(() => {
|
|
31
|
-
if (Platform.OS === 'android') {
|
|
32
|
-
|
|
33
|
-
const viewId = findNodeHandle(ref.current);
|
|
33
|
+
if (Platform.OS === 'android' && defaultRef.current) {
|
|
34
|
+
const viewId = findNodeHandle(defaultRef.current);
|
|
34
35
|
createFragment(viewId);
|
|
35
36
|
}
|
|
36
37
|
}, []);
|
|
38
|
+
// assign the forwarded ref
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (forwardedRef && 'current' in forwardedRef) {
|
|
41
|
+
forwardedRef.current = defaultRef.current;
|
|
42
|
+
}
|
|
43
|
+
else if (forwardedRef) {
|
|
44
|
+
forwardedRef(defaultRef);
|
|
45
|
+
}
|
|
46
|
+
}, [defaultRef.current !== null]);
|
|
37
47
|
return (<View style={styles.container} onLayout={onLayout}>
|
|
38
|
-
<BaseReadiumView height={height} width={width} {...props} onLocationChange={onLocationChange} onTableOfContents={onTableOfContents} settings={unmappedSettings ? Settings.map(unmappedSettings) : undefined} ref={
|
|
48
|
+
<BaseReadiumView height={height} width={width} {...props} onLocationChange={onLocationChange} onTableOfContents={onTableOfContents} settings={unmappedSettings ? Settings.map(unmappedSettings) : undefined} ref={defaultRef}/>
|
|
39
49
|
</View>);
|
|
40
50
|
});
|
|
41
51
|
const styles = StyleSheet.create({
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useCallback, useState, useEffect, forwardRef } from 'react';
|
|
1
|
+
import React, { useCallback, useState, useEffect, forwardRef, ForwardedRef, useRef, createRef } from 'react';
|
|
2
2
|
import { View, Platform, findNodeHandle, StyleSheet } from 'react-native';
|
|
3
3
|
|
|
4
4
|
import type { BaseReadiumViewProps, Dimensions } from '../interfaces';
|
|
@@ -13,11 +13,13 @@ export const ReadiumView: React.FC<ReadiumProps> = forwardRef(({
|
|
|
13
13
|
onTableOfContents: wrappedOnTableOfContents,
|
|
14
14
|
settings: unmappedSettings,
|
|
15
15
|
...props
|
|
16
|
-
},
|
|
16
|
+
}, forwardedRef) => {
|
|
17
|
+
const defaultRef = useRef<any>(null);
|
|
17
18
|
const [{ height, width }, setDimensions] = useState<Dimensions>({
|
|
18
19
|
width: 0,
|
|
19
20
|
height: 0,
|
|
20
21
|
});
|
|
22
|
+
|
|
21
23
|
// set the view dimensions on layout
|
|
22
24
|
const onLayout = useCallback(({ nativeEvent: { layout: { width, height } }}: any) => {
|
|
23
25
|
setDimensions({
|
|
@@ -25,6 +27,7 @@ export const ReadiumView: React.FC<ReadiumProps> = forwardRef(({
|
|
|
25
27
|
height: dimension(height),
|
|
26
28
|
});
|
|
27
29
|
}, []);
|
|
30
|
+
|
|
28
31
|
// wrap the native onLocationChange and extract the raw event value
|
|
29
32
|
const onLocationChange = useCallback((event: any) => {
|
|
30
33
|
if (wrappedOnLocationChange) {
|
|
@@ -39,13 +42,22 @@ export const ReadiumView: React.FC<ReadiumProps> = forwardRef(({
|
|
|
39
42
|
}
|
|
40
43
|
}, [wrappedOnTableOfContents]);
|
|
41
44
|
|
|
45
|
+
// create the view fragment on android
|
|
42
46
|
useEffect(() => {
|
|
43
|
-
if (Platform.OS === 'android') {
|
|
44
|
-
|
|
45
|
-
const viewId = findNodeHandle(ref.current);
|
|
47
|
+
if (Platform.OS === 'android' && defaultRef.current) {
|
|
48
|
+
const viewId = findNodeHandle(defaultRef.current);
|
|
46
49
|
createFragment(viewId);
|
|
47
50
|
}
|
|
48
|
-
}, [])
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
// assign the forwarded ref
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (forwardedRef && 'current' in forwardedRef) {
|
|
56
|
+
forwardedRef.current = defaultRef.current;
|
|
57
|
+
} else if (forwardedRef) {
|
|
58
|
+
forwardedRef(defaultRef);
|
|
59
|
+
}
|
|
60
|
+
}, [defaultRef.current !== null])
|
|
49
61
|
|
|
50
62
|
return (
|
|
51
63
|
<View
|
|
@@ -59,7 +71,7 @@ export const ReadiumView: React.FC<ReadiumProps> = forwardRef(({
|
|
|
59
71
|
onLocationChange={onLocationChange}
|
|
60
72
|
onTableOfContents={onTableOfContents}
|
|
61
73
|
settings={unmappedSettings ? Settings.map(unmappedSettings) : undefined}
|
|
62
|
-
ref={
|
|
74
|
+
ref={defaultRef}
|
|
63
75
|
/>
|
|
64
76
|
</View>
|
|
65
77
|
);
|