@granite-js/react-native 0.0.1

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.
Files changed (189) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/LICENSE +202 -0
  3. package/README.md +24 -0
  4. package/bin/cli.js +3 -0
  5. package/cli.d.ts +1 -0
  6. package/cli.js +4 -0
  7. package/config.d.ts +2 -0
  8. package/config.js +5 -0
  9. package/dist/app/App/index.android.d.ts +2 -0
  10. package/dist/app/App/index.ios.d.ts +6 -0
  11. package/dist/app/Granite.d.ts +60 -0
  12. package/dist/app/index.d.ts +2 -0
  13. package/dist/app/registerPage.d.ts +20 -0
  14. package/dist/async-bridges.d.ts +2 -0
  15. package/dist/constant-bridges.d.ts +1 -0
  16. package/dist/dev-entrypoint/index.d.ts +2 -0
  17. package/dist/event/abstract.d.ts +42 -0
  18. package/dist/event/index.d.ts +2 -0
  19. package/dist/event/useGraniteEvent.d.ts +14 -0
  20. package/dist/impression-area/ImpressionArea.d.ts +231 -0
  21. package/dist/impression-area/index.d.ts +1 -0
  22. package/dist/index.d.ts +17 -0
  23. package/dist/initial-props/InitialProps.d.ts +127 -0
  24. package/dist/initial-props/index.d.ts +1 -0
  25. package/dist/intersection-observer/IOContext.d.ts +10 -0
  26. package/dist/intersection-observer/IOFlatList.d.ts +55 -0
  27. package/dist/intersection-observer/IOManager.d.ts +24 -0
  28. package/dist/intersection-observer/IOScrollView.d.ts +59 -0
  29. package/dist/intersection-observer/InView.d.ts +107 -0
  30. package/dist/intersection-observer/IntersectionObserver.d.ts +67 -0
  31. package/dist/intersection-observer/index.d.ts +8 -0
  32. package/dist/intersection-observer/withIO.d.ts +20 -0
  33. package/dist/jest/index.d.ts +1 -0
  34. package/dist/jest/index.js +32 -0
  35. package/dist/keyboard/KeyboardAboveView.d.ts +40 -0
  36. package/dist/keyboard/index.d.ts +1 -0
  37. package/dist/keyboard/useKeyboardAnimatedHeight.d.ts +20 -0
  38. package/dist/native-event-emitter/eventEmitters/index.d.ts +2 -0
  39. package/dist/native-event-emitter/eventEmitters/types.d.ts +4 -0
  40. package/dist/native-event-emitter/eventEmitters/visibilityChanged.d.ts +10 -0
  41. package/dist/native-event-emitter/index.d.ts +1 -0
  42. package/dist/native-event-emitter/nativeEventEmitter.d.ts +15 -0
  43. package/dist/native-modules/core/GraniteCoreModule.d.ts +8 -0
  44. package/dist/native-modules/index.d.ts +3 -0
  45. package/dist/native-modules/natives/GraniteModule.d.ts +7 -0
  46. package/dist/native-modules/natives/closeView.d.ts +21 -0
  47. package/dist/native-modules/natives/getSchemeUri.d.ts +23 -0
  48. package/dist/native-modules/natives/index.d.ts +3 -0
  49. package/dist/native-modules/natives/openURL.d.ts +36 -0
  50. package/dist/react/index.d.ts +1 -0
  51. package/dist/react/useWaitForReturnNavigator.d.ts +39 -0
  52. package/dist/rn-polyfills/index.d.ts +1 -0
  53. package/dist/rn-polyfills/symbol-asynciterator/index.d.ts +9 -0
  54. package/dist/rn-polyfills/url/index.d.ts +1 -0
  55. package/dist/router/Router.d.ts +59 -0
  56. package/dist/router/components/BackButton.d.ts +7 -0
  57. package/dist/router/components/CanGoBackGuard.d.ts +6 -0
  58. package/dist/router/components/RouterBackButton.d.ts +9 -0
  59. package/dist/router/components/StackNavigator.d.ts +54 -0
  60. package/dist/router/constants.d.ts +2 -0
  61. package/dist/router/createRoute.d.ts +39 -0
  62. package/dist/router/createRoute.test-d.d.ts +9 -0
  63. package/dist/router/hooks/useInitialRouteName.d.ts +1 -0
  64. package/dist/router/hooks/useIsInitialScreen.d.ts +1 -0
  65. package/dist/router/hooks/useRouterControls.d.ts +11 -0
  66. package/dist/router/index.d.ts +3 -0
  67. package/dist/router/types/RequireContext.d.ts +7 -0
  68. package/dist/router/types/RouteScreen.d.ts +16 -0
  69. package/dist/router/types/Screen.d.ts +23 -0
  70. package/dist/router/types/index.d.ts +3 -0
  71. package/dist/router/types/screen-option.d.ts +4 -0
  72. package/dist/router/utils/createParentRouteScreenMap.d.ts +8 -0
  73. package/dist/router/utils/defaultParserParams.d.ts +9 -0
  74. package/dist/router/utils/index.d.ts +2 -0
  75. package/dist/router/utils/matchers.d.ts +2 -0
  76. package/dist/router/utils/mergeParentLayoutScreen.d.ts +18 -0
  77. package/dist/router/utils/path.d.ts +53 -0
  78. package/dist/router/utils/screen.d.ts +53 -0
  79. package/dist/scroll-view-inertial-background/ScrollViewInertialBackground.d.ts +49 -0
  80. package/dist/scroll-view-inertial-background/index.d.ts +1 -0
  81. package/dist/types/global.d.ts +18 -0
  82. package/dist/use-back-event/index.d.ts +1 -0
  83. package/dist/use-back-event/useBackEvent.d.ts +135 -0
  84. package/dist/utils/noop.d.ts +1 -0
  85. package/dist/utils/usePreservedCallback.d.ts +1 -0
  86. package/dist/visibility/VisibilityProvider.d.ts +27 -0
  87. package/dist/visibility/index.d.ts +6 -0
  88. package/dist/visibility/react-navigation/index.d.ts +2 -0
  89. package/dist/visibility/react-navigation/useIsFocusedSafely.d.ts +20 -0
  90. package/dist/visibility/react-navigation/useNavigationSafely.d.ts +19 -0
  91. package/dist/visibility/useIsAppForeground.d.ts +39 -0
  92. package/dist/visibility/useVisibility.d.ts +35 -0
  93. package/dist/visibility/useVisibilityChange.d.ts +51 -0
  94. package/dist/visibility/useVisibilityChanged.d.ts +41 -0
  95. package/dist/visibility/utils/usePrevious.d.ts +15 -0
  96. package/jest.d.ts +1 -0
  97. package/package.json +92 -0
  98. package/presets.d.ts +1 -0
  99. package/src/app/App/index.android.tsx +6 -0
  100. package/src/app/App/index.d.ts +6 -0
  101. package/src/app/App/index.ios.tsx +13 -0
  102. package/src/app/Granite.tsx +130 -0
  103. package/src/app/index.ts +2 -0
  104. package/src/app/registerPage.ts +29 -0
  105. package/src/async-bridges.ts +2 -0
  106. package/src/constant-bridges.ts +1 -0
  107. package/src/dev-entrypoint/index.tsx +21 -0
  108. package/src/event/abstract.ts +130 -0
  109. package/src/event/index.ts +2 -0
  110. package/src/event/useGraniteEvent.ts +34 -0
  111. package/src/impression-area/ImpressionArea.tsx +341 -0
  112. package/src/impression-area/index.ts +1 -0
  113. package/src/index.ts +19 -0
  114. package/src/initial-props/InitialProps.ts +144 -0
  115. package/src/initial-props/index.ts +1 -0
  116. package/src/intersection-observer/IOContext.ts +16 -0
  117. package/src/intersection-observer/IOFlatList.ts +72 -0
  118. package/src/intersection-observer/IOManager.ts +73 -0
  119. package/src/intersection-observer/IOScrollView.ts +69 -0
  120. package/src/intersection-observer/InView.tsx +205 -0
  121. package/src/intersection-observer/IntersectionObserver.ts +212 -0
  122. package/src/intersection-observer/index.ts +24 -0
  123. package/src/intersection-observer/withIO.tsx +151 -0
  124. package/src/jest/index.ts +1 -0
  125. package/src/keyboard/KeyboardAboveView.tsx +62 -0
  126. package/src/keyboard/index.ts +1 -0
  127. package/src/keyboard/useKeyboardAnimatedHeight.tsx +81 -0
  128. package/src/native-event-emitter/eventEmitters/index.ts +3 -0
  129. package/src/native-event-emitter/eventEmitters/types.ts +4 -0
  130. package/src/native-event-emitter/eventEmitters/visibilityChanged.ts +11 -0
  131. package/src/native-event-emitter/index.ts +1 -0
  132. package/src/native-event-emitter/nativeEventEmitter.ts +18 -0
  133. package/src/native-modules/core/GraniteCoreModule.ts +9 -0
  134. package/src/native-modules/index.ts +3 -0
  135. package/src/native-modules/natives/GraniteModule.ts +8 -0
  136. package/src/native-modules/natives/closeView.ts +25 -0
  137. package/src/native-modules/natives/getSchemeUri.ts +27 -0
  138. package/src/native-modules/natives/index.ts +3 -0
  139. package/src/native-modules/natives/openURL.ts +40 -0
  140. package/src/react/index.ts +1 -0
  141. package/src/react/useWaitForReturnNavigator.ts +75 -0
  142. package/src/rn-polyfills/index.ts +7 -0
  143. package/src/rn-polyfills/symbol-asynciterator/index.ts +15 -0
  144. package/src/rn-polyfills/url/index.ts +1 -0
  145. package/src/router/Router.tsx +164 -0
  146. package/src/router/components/BackButton.tsx +58 -0
  147. package/src/router/components/CanGoBackGuard.tsx +31 -0
  148. package/src/router/components/RouterBackButton.tsx +32 -0
  149. package/src/router/components/StackNavigator.tsx +12 -0
  150. package/src/router/constants.ts +3 -0
  151. package/src/router/createRoute.test-d.ts +52 -0
  152. package/src/router/createRoute.ts +161 -0
  153. package/src/router/hooks/useInitialRouteName.tsx +22 -0
  154. package/src/router/hooks/useIsInitialScreen.ts +7 -0
  155. package/src/router/hooks/useRouterControls.tsx +72 -0
  156. package/src/router/index.ts +3 -0
  157. package/src/router/types/RequireContext.ts +7 -0
  158. package/src/router/types/RouteScreen.ts +17 -0
  159. package/src/router/types/Screen.tsx +24 -0
  160. package/src/router/types/index.ts +3 -0
  161. package/src/router/types/screen-option.ts +23 -0
  162. package/src/router/utils/createParentRouteScreenMap.spec.ts +166 -0
  163. package/src/router/utils/createParentRouteScreenMap.ts +136 -0
  164. package/src/router/utils/defaultParserParams.spec.ts +46 -0
  165. package/src/router/utils/defaultParserParams.ts +19 -0
  166. package/src/router/utils/index.ts +2 -0
  167. package/src/router/utils/matchers.ts +5 -0
  168. package/src/router/utils/mergeParentLayoutScreen.spec.tsx +112 -0
  169. package/src/router/utils/mergeParentLayoutScreen.tsx +43 -0
  170. package/src/router/utils/path.spec.ts +135 -0
  171. package/src/router/utils/path.ts +105 -0
  172. package/src/router/utils/screen.tsx +111 -0
  173. package/src/scroll-view-inertial-background/ScrollViewInertialBackground.tsx +99 -0
  174. package/src/scroll-view-inertial-background/index.ts +1 -0
  175. package/src/types/global.ts +31 -0
  176. package/src/use-back-event/index.ts +1 -0
  177. package/src/use-back-event/useBackEvent.tsx +260 -0
  178. package/src/utils/noop.ts +1 -0
  179. package/src/utils/usePreservedCallback.ts +16 -0
  180. package/src/visibility/VisibilityProvider.tsx +36 -0
  181. package/src/visibility/index.ts +6 -0
  182. package/src/visibility/react-navigation/index.ts +2 -0
  183. package/src/visibility/react-navigation/useIsFocusedSafely.tsx +58 -0
  184. package/src/visibility/react-navigation/useNavigationSafely.tsx +30 -0
  185. package/src/visibility/useIsAppForeground.tsx +73 -0
  186. package/src/visibility/useVisibility.tsx +54 -0
  187. package/src/visibility/useVisibilityChange.ts +69 -0
  188. package/src/visibility/useVisibilityChanged.tsx +69 -0
  189. package/src/visibility/utils/usePrevious.tsx +24 -0
@@ -0,0 +1,341 @@
1
+ import { debounce } from 'es-toolkit';
2
+ import { PropsWithChildren, ReactElement, ReactNode, useContext, useEffect, useMemo, useState } from 'react';
3
+ import { StyleProp, View, ViewStyle } from 'react-native';
4
+ import { InView, IOContext } from '../intersection-observer';
5
+ import { noop } from '../utils/noop';
6
+ import { usePreservedCallback } from '../utils/usePreservedCallback';
7
+ import { useVisibility } from '../visibility';
8
+
9
+ interface Props {
10
+ onImpressionStart?: () => void;
11
+ onImpressionEnd?: () => void;
12
+ enabled?: boolean;
13
+ UNSAFE__impressFallbackOnMount?: boolean;
14
+ style?: StyleProp<ViewStyle>;
15
+ areaThreshold?: number;
16
+ timeThreshold?: number;
17
+ children?: ReactNode;
18
+ }
19
+
20
+ /**
21
+ * @public
22
+ * @category Screen Control
23
+ * @name ImpressionArea
24
+ * @description
25
+ * A component that detects whether a specific component is visible on the screen and notifies the outside. Using this component, you can easily implement features like collecting logs or running animations when a specific component becomes visible on the screen.
26
+ * The visibility is detected using the return value of `useVisibility` and the `IOScrollView` and `InView` components that indicate whether the component is displayed within the viewport. When using `ScrollView` in React, even if a view is not visible on the screen, using `ImpressionArea` allows you to trigger events only when the view is actually visible on the screen.
27
+
28
+ ::: info Note
29
+
30
+ `ImpressionArea` must be used inside `IOScrollView`. If you need to use it outside of `IOScrollView`, you can set the `UNSAFE__impressFallbackOnMount` property to `true` to detect based on when the component is mounted. If this property is set to `false` and used outside of `IOScrollView`, an `IOProviderMissingError` will occur.
31
+
32
+ :::
33
+ *
34
+ * @param {() => void} [onImpressionStart] Callback function that is executed when the child component becomes visible on the screen.
35
+ * @param {() => void} [onImpressionEnd] Callback function that is executed when the child component is hidden from the screen.
36
+ * @param {boolean} [enabled=true] Value that directly controls the condition for visibility. Default value is `true`. If passed as `false`, the `onImpressionStart` callback function will not be executed even if the component is visible.
37
+ * @param {number} [areaThreshold=0] Value that sets the ratio of the visible area. If the component appears on the screen with a ratio greater than this value, `onImpressionStart` is called.
38
+ * The value should be set between 0 and 1. Setting it to 0 triggers the event when even 1px of the component is visible. Conversely, setting it to 1 only triggers the event when the component is 100% visible on the screen.
39
+ * @param {number} [timeThreshold=0] Sets the time in milliseconds before `onImpressionStart` is called after this component becomes visible on the screen. Default value is `0` milliseconds (0 seconds).
40
+ * @param {ViewStyle} [style] `style` value to be applied to the `InView` component. Default value is `undefined`, used when you want to specify a style.
41
+ * @param {boolean} [UNSAFE__impressFallbackOnMount=false] Whether to consider the component as visible immediately when mounted. Default value is `false`.
42
+ * Useful when you cannot determine if the component is in the viewport without using `IOScrollView`. For example, a component located outside of `IOScrollView` will be considered visible at mount time if set to `true`.
43
+ *
44
+ * @returns {ReactElement} - Returns a component that can detect visibility on the screen.
45
+ * @example
46
+ *
47
+ * ### Basic Usage Example
48
+ *
49
+ * ```tsx
50
+ * import { useState } from 'react';
51
+ * import { Button, Dimensions, Text, View } from 'react-native';
52
+ * import { ImpressionArea, IOScrollView } from '@granite-js/react-native';
53
+ *
54
+ * export default function ImpressionAreaExample() {
55
+ * const [isImpressionStart, setIsImpressionStart] = useState(false);
56
+ *
57
+ * return (
58
+ * <>
59
+ * <Text>{isImpressionStart ? 'Impression Start' : 'Impression End'}</Text>
60
+ * <IOScrollView
61
+ * style={{
62
+ * flex: 1,
63
+ * margin: 16,
64
+ * backgroundColor: 'white',
65
+ * }}
66
+ * >
67
+ * <View
68
+ * style={{
69
+ * height: Dimensions.get('screen').height,
70
+ * borderWidth: 1,
71
+ * borderColor: 'black',
72
+ * }}
73
+ * >
74
+ * <Text>Scroll to here</Text>
75
+ * </View>
76
+ *
77
+ * <ImpressionArea
78
+ * onImpressionStart={() => setIsImpressionStart(true)}
79
+ * onImpressionEnd={() => setIsImpressionStart(false)}
80
+ * >
81
+ * <Button title="Button" />
82
+ * </ImpressionArea>
83
+ * </IOScrollView>
84
+ * </>
85
+ * );
86
+ * }
87
+ * ```
88
+ *
89
+ * ### Example of Detection at Mount Time
90
+ *
91
+ * When `ImpressionArea` is not located inside a component like `IOScrollView`, setting `UNSAFE__impressFallbackOnMount` to `true` will consider the component as visible when it is mounted.
92
+ *
93
+ * ```tsx
94
+ * import { useState } from 'react';
95
+ * import { Button, Dimensions, ScrollView, Text, View } from 'react-native';
96
+ * import { ImpressionArea } from '@granite-js/react-native';
97
+ *
98
+ * export default function ImpressionArea2Example() {
99
+ * const [isImpressionStart, setIsImpressionStart] = useState(false);
100
+ *
101
+ * return (
102
+ * <>
103
+ * <Text>{isImpressionStart ? 'Impression Start' : 'Impression End'}</Text>
104
+ * <ScrollView
105
+ * style={{
106
+ * flex: 1,
107
+ * margin: 16,
108
+ * backgroundColor: 'white',
109
+ * }}
110
+ * >
111
+ * <View
112
+ * style={{
113
+ * height: Dimensions.get('screen').height,
114
+ * borderWidth: 1,
115
+ * borderColor: 'black',
116
+ * }}
117
+ * >
118
+ * <Text>Scroll to here</Text>
119
+ * </View>
120
+ *
121
+ * <ImpressionArea
122
+ * UNSAFE__impressFallbackOnMount={true}
123
+ * onImpressionStart={() => setIsImpressionStart(true)}
124
+ * onImpressionEnd={() => setIsImpressionStart(false)}
125
+ * >
126
+ * <Button title="Button" />
127
+ * </ImpressionArea>
128
+ * </ScrollView>
129
+ * </>
130
+ * );
131
+ * }
132
+ * ```
133
+ */
134
+ export function ImpressionArea(props: Props): ReactElement {
135
+ const context = useContext(IOContext);
136
+
137
+ if (context?.manager == null && props.UNSAFE__impressFallbackOnMount) {
138
+ return <ImpressionAreaOnMount {...props} />;
139
+ }
140
+
141
+ return <ImpressionAreaWithIntersectionObserver {...props} />;
142
+ }
143
+
144
+ export class IOProviderMissingError extends Error {
145
+ message =
146
+ 'ImpressionArea가 IOContext.Provider 밖에서 사용되었습니다. ' +
147
+ '최상위 ScrollView가 @granite-js/react-native의 IOScrollView인지 확인해주세요.';
148
+ }
149
+
150
+ /**
151
+ * @category Components
152
+ * @kind function
153
+ * @name ImpressionAreaOnMount
154
+ * @description
155
+ * A component that calls `onImpressionStart` and `onImpressionEnd` when the component is mounted on the screen.
156
+ * Used when `UNSAFE__impressFallbackOnMount` is `true` in `ImpressionArea`.
157
+ * This component can be used when `ImpressionArea` is needed outside of `IOScrollView`.
158
+ *
159
+ * @param {() => void} [onImpressionStart] - Callback function that is called when the component is mounted.
160
+ * @param {() => void} [onImpressionEnd] - Callback function that is called when the component is unmounted.
161
+ * @param {StyleProp<ViewStyle>} [style] - Sets the style of the top-level container of the `ImpressionAreaOnMount` component.
162
+ * @param {ReactNode} [children] - Sets the child components to be wrapped by `ImpressionAreaOnMount`.
163
+ * @returns {ReactElement} - Returns a component that detects mount status
164
+ * @example
165
+ * ### Example of Detection at Mount Time
166
+ * ```tsx
167
+ * import { ImpressionAreaOnMount } from '@granite-js/react-native';
168
+ *
169
+ * export default function AlwaysImpressionAreaPage() {
170
+ * // isImpressionStart is set to true when AlwaysImpressionAreaPage is mounted
171
+ * const [isImpressionStart, setIsImpressionStart] = useState(false);
172
+ *
173
+ * return (
174
+ * <ScrollView
175
+ * style={{
176
+ * flex: 1,
177
+ * padding: 16,
178
+ * backgroundColor: 'white',
179
+ * }}
180
+ * >
181
+ * <TestTitle title="ImpressionArea" description="@granite-js/impression-area" />
182
+ * <Button label={'Scroll to origin'} onPress={() => {}} />
183
+ * <Code style={{ width, height }} json={{ isImpressionStart }} />
184
+ * <ImpressionAreaOnMount
185
+ * onImpressionStart={() => setIsImpressionStart(true)}
186
+ * onImpressionEnd={() => setIsImpressionStart(false)}
187
+ * >
188
+ * <Button label={'Scroll to here'} onPress={() => {}} />
189
+ * </ImpressionArea>
190
+ * </ScrollView>
191
+ * );
192
+ * }
193
+ * ```
194
+ */
195
+ export function ImpressionAreaOnMount({
196
+ onImpressionStart: _onImpressionStart = noop,
197
+ onImpressionEnd: _onImpressionEnd = noop,
198
+ style,
199
+ children,
200
+ }: PropsWithChildren<Props>): ReactElement {
201
+ const visible = useVisibility();
202
+
203
+ const isImpressed = visible;
204
+
205
+ const onImpressionStart = usePreservedCallback(_onImpressionStart);
206
+ const onImpressionEnd = usePreservedCallback(_onImpressionEnd);
207
+
208
+ useEffect(() => {
209
+ if (isImpressed) {
210
+ onImpressionStart();
211
+ } else {
212
+ onImpressionEnd();
213
+ }
214
+ }, [isImpressed, onImpressionStart, onImpressionEnd]);
215
+
216
+ return <View style={style}>{children}</View>;
217
+ }
218
+
219
+ /**
220
+ * @category Components
221
+ * @kind function
222
+ * @name ImpressionAreaWithIntersectionObserver
223
+ * @description
224
+ * A component that calls `onImpressionStart` and `onImpressionEnd` based on whether the component is visible on the user's screen.
225
+ * This is a component that can be used in `IOScrollView` and can notify the outside about its visibility status.
226
+ *
227
+ * @param {() => void} [onImpressionStart] - Callback function that is called when the component becomes visible on the screen.
228
+ * @param {() => void} [onImpressionEnd] - Callback function that is called when the component disappears from the screen.
229
+ * @param {boolean} [enabled=true] - Sets whether to enable `ImpressionArea`.
230
+ * @param {StyleProp<ViewStyle>} [style] - Sets the style of the top-level container of the `ImpressionAreaWithIntersectionObserver` component.
231
+ * @param {number} [areaThreshold=0] - Considers the component visible when its visible area exceeds the set ratio.
232
+ * @param {number} [timeThreshold=0] - Sets the time in milliseconds before `onImpressionStart` and `onImpressionEnd` are called after the component becomes visible on the screen. Default value is `0` milliseconds.
233
+ * @param {ReactNode} [children] - Sets the child components to be wrapped by `ImpressionAreaWithIntersectionObserver`.
234
+ * @returns {ReactElement} - Returns a component that can detect visibility on the screen.
235
+ * @example
236
+ * ### Example using IntersectionObserver
237
+ * ```tsx
238
+ * import { ImpressionAreaWithIntersectionObserver, IOScrollView } from '@granite-js/react-native';
239
+ *
240
+ * export default function ImpressionAreaPage() {
241
+ * const [isImpressionStart, setIsImpressionStart] = useState(false);
242
+ *
243
+ * return (
244
+ * <IOScrollView
245
+ * style={{
246
+ * flex: 1,
247
+ * margin: 16,
248
+ * backgroundColor: 'white',
249
+ * }}
250
+ * >
251
+ * <TestTitle title="ImpressionArea" description="@granite-js/impression-area" />
252
+ * <Button label={'Scroll to origin'} onPress={() => {}} />
253
+ * <Code style={{ width, height }} textStyle={{ flex: 1, verticalAlign: 'middle' }} json={{ isImpressionStart }} />
254
+ * <ImpressionAreaWithIntersectionObserver
255
+ * onImpressionStart={() => setIsImpressionStart(true)}
256
+ * onImpressionEnd={() => setIsImpressionStart(false)}
257
+ * >
258
+ * <Button label={'Scroll to here'} onPress={() => {}} />
259
+ * </ImpressionArea>
260
+ * </IOScrollView>
261
+ * );
262
+ * }
263
+ * ```
264
+ */
265
+ export function ImpressionAreaWithIntersectionObserver({
266
+ onImpressionStart: _onImpressionStart = noop,
267
+ onImpressionEnd: _onImpressionEnd = noop,
268
+ timeThreshold = 0,
269
+ ...props
270
+ }: PropsWithChildren<{
271
+ onImpressionStart?: () => void;
272
+ onImpressionEnd?: () => void;
273
+ enabled?: boolean;
274
+ style?: StyleProp<ViewStyle>;
275
+ areaThreshold?: number;
276
+ timeThreshold?: number;
277
+ }>): ReactElement {
278
+ const [isImpressed, setIsImpressed] = useState(false);
279
+
280
+ const onImpressionChange = useMemo(() => {
281
+ if (timeThreshold === 0) {
282
+ return setIsImpressed;
283
+ } else {
284
+ return debounce(setIsImpressed, timeThreshold);
285
+ }
286
+ }, [setIsImpressed, timeThreshold]);
287
+
288
+ const onImpressionStart = usePreservedCallback(_onImpressionStart);
289
+ const onImpressionEnd = usePreservedCallback(_onImpressionEnd);
290
+
291
+ useEffect(() => {
292
+ if (isImpressed) {
293
+ onImpressionStart?.();
294
+ } else {
295
+ onImpressionEnd?.();
296
+ }
297
+ }, [isImpressed, onImpressionStart, onImpressionEnd]);
298
+
299
+ return <ImpressionAreaImpl {...props} onImpressionChange={onImpressionChange} />;
300
+ }
301
+
302
+ function ImpressionAreaImpl({
303
+ children,
304
+ onImpressionChange: _onImpressionChange = noop,
305
+ enabled = true,
306
+ areaThreshold = 0,
307
+ style,
308
+ }: PropsWithChildren<{
309
+ onImpressionChange?: (isImpressed: boolean) => void;
310
+ enabled?: boolean;
311
+ style?: StyleProp<ViewStyle>;
312
+ areaThreshold?: number;
313
+ }>) {
314
+ const visible = useVisibility();
315
+ const [inviewImpressed, setInviewImpressed] = useState(false);
316
+
317
+ const context = useContext(IOContext);
318
+
319
+ if (context?.manager == null) {
320
+ throw new IOProviderMissingError();
321
+ }
322
+
323
+ const impressed = visible && inviewImpressed && enabled;
324
+ const onImpressionChange = usePreservedCallback(_onImpressionChange);
325
+
326
+ useEffect(() => {
327
+ onImpressionChange?.(impressed);
328
+ }, [impressed, onImpressionChange]);
329
+
330
+ return (
331
+ <InView
332
+ onChange={(inView, currentThreshold) => {
333
+ const isVisible = inView && currentThreshold >= areaThreshold;
334
+ setInviewImpressed(isVisible);
335
+ }}
336
+ style={style}
337
+ >
338
+ {children}
339
+ </InView>
340
+ );
341
+ }
@@ -0,0 +1 @@
1
+ export * from './ImpressionArea';
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ import './types/global';
2
+
3
+ export { Granite } from './app';
4
+ export * from '@granite-js/style-utils';
5
+ export * from '@granite-js/image';
6
+ export * from './dev-entrypoint';
7
+ export * from './native-modules/natives';
8
+ export * from './visibility';
9
+ export * from './use-back-event';
10
+ export * from './keyboard';
11
+ export * from './intersection-observer';
12
+ export * from './impression-area';
13
+ export * from './scroll-view-inertial-background';
14
+ export * from './react';
15
+ export * from './router/createRoute';
16
+ export * from './event';
17
+
18
+ export type { InitialProps, ColorPreference } from './initial-props';
19
+ export type { GraniteProps } from './app';
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @public
3
+ * @category UI
4
+ * @name ColorPreference
5
+ * @description
6
+ * Type representing the color mode of the current device. It is a string representing light mode and dark mode.
7
+ *
8
+ * @typedef {'light' | 'dark'} ColorPreference
9
+ */
10
+ export type ColorPreference = 'light' | 'dark';
11
+
12
+ /**
13
+ * @name NetworkStatus
14
+ * @description
15
+ * Type representing the network status.
16
+ *
17
+ * @typedef {'WIFI' | '2G' | '3G' | '4G' | '5G' | 'UNKNOWN' | 'OFFLINE'} NetworkStatus
18
+ */
19
+ type NetworkStatus = 'WIFI' | '2G' | '3G' | '4G' | '5G' | 'UNKNOWN' | 'OFFLINE';
20
+
21
+ /**
22
+ * @category Types
23
+ * @name BaseInitialProps
24
+ * @description
25
+ * Interface representing the base initial properties.
26
+ *
27
+ * @interface
28
+ * @property {'ios' | 'android'} platform - Platform type
29
+ * @property {string} appVersion - App version
30
+ * @property {ColorPreference} initialColorPreference - Initial color
31
+ * @property {NetworkStatus} networkStatus - Network status
32
+ * @property {number} loadingStartTs - Timestamp when ReactNativeView started rendering in native
33
+ * @property {string} [scheme] - Executed scheme
34
+ */
35
+ type BaseInitialProps = {
36
+ platform: 'ios' | 'android';
37
+ appVersion: string;
38
+ initialColorPreference: ColorPreference;
39
+ networkStatus: NetworkStatus;
40
+ loadingStartTs: number;
41
+ scheme?: string;
42
+ };
43
+
44
+ /**
45
+ * @category Types
46
+ * @name AndroidInitialProps
47
+ * @description
48
+ * Values passed from Android to React Native.
49
+ *
50
+ * @interface
51
+ * @augments BaseInitialProps
52
+ * @property {'android'} platform - Platform name (Android)
53
+ * @property {string} initialFontScale - Font scale set on the device
54
+ * @property {string} distributionGroup - Distribution group
55
+ */
56
+ export type AndroidInitialProps = BaseInitialProps & {
57
+ platform: 'android';
58
+ initialFontScale: string;
59
+ distributionGroup: string;
60
+ };
61
+
62
+ /**
63
+ * @category Types
64
+ * @name IOSInitialProps
65
+ * @description
66
+ * Interface representing iOS initial properties.
67
+ *
68
+ * @interface
69
+ * @augments BaseInitialProps
70
+ * @property {'ios'} platform - Platform (iOS)
71
+ * @property {IOSFontSizeType} initialFontSize - Initial font size
72
+ * @property {boolean} isVisible - Visibility status
73
+ */
74
+ export type IOSInitialProps = BaseInitialProps & {
75
+ platform: 'ios';
76
+ initialFontSize: IOSFontSizeType;
77
+ isVisible: boolean;
78
+ };
79
+
80
+ /**
81
+ * @category Types
82
+ * @name IOSFontSizeType
83
+ * @description
84
+ * Type representing iOS font size type.
85
+ *
86
+ * @typedef {'xSmall' | 'Small' | 'Medium' | 'Large' | 'xLarge' | 'xxLarge' | 'xxxLarge' | 'A11y_Medium' | 'A11y_Large' | 'A11y_xLarge' | 'A11y_xxLarge' | 'A11y_xxxLarge'} IOSFontSizeType
87
+ */
88
+ type IOSFontSizeType =
89
+ | 'xSmall'
90
+ | 'Small'
91
+ | 'Medium'
92
+ | 'Large'
93
+ | 'xLarge'
94
+ | 'xxLarge'
95
+ | 'xxxLarge'
96
+ | 'A11y_Medium'
97
+ | 'A11y_Large'
98
+ | 'A11y_xLarge'
99
+ | 'A11y_xxLarge'
100
+ | 'A11y_xxxLarge';
101
+
102
+ /**
103
+ * @public
104
+ * @category Core
105
+ * @name InitialProps
106
+ * @description
107
+ * Provides the initial data type that native platforms (Android/iOS) pass to the app when a user enters a specific screen in a React Native app.
108
+ * The initial data contains important information used for screen initialization, and the required data types differ by native platform.
109
+ *
110
+ * The data type provided by Android is `AndroidInitialProps`, and the data type provided by iOS is `IOSInitialProps`.
111
+ *
112
+ * @property {'ios' | 'android'} platform - The platform on which the app is currently running. Has a value of either `ios` or `android`.
113
+ * @property {ColorPreference} initialColorPreference - The initial color theme. Represents the color theme set by the user.
114
+ * @property {NetworkStatus} networkStatus - The current device's network connection status and connected network.
115
+ * @property {string} [scheme] - The URL scheme used to enter the current screen.
116
+ * @property {`xSmall` | `Small` | `Medium` | `Large` | `xLarge` | `xxLarge` | `xxxLarge` | `A11y_Medium` | `A11y_Large` | `A11y_xLarge` | `A11y_xxLarge` | `A11y_xxxLarge`} initialFontSize (iOS only) iOS system font size. Each value represents a specific font size. Default value is `Large`.
117
+ * @property {boolean} isVisible (iOS only) Whether the screen is currently visible in iOS. Initial value is passed as `true`.
118
+ * @property {string} initialFontScale (Android only) Android system font scale. The font size scale adjusted by the user in Android device's accessibility settings. This value is multiplied by the base font size to determine the final font size.
119
+ *
120
+ * @example
121
+ *
122
+ * ### Example using `InitialProps`
123
+ *
124
+ * ::: code-group
125
+ * ```tsx [_app.tsx]
126
+ * import { PropsWithChildren } from 'react';
127
+ * import { Granite, InitialProps } from '@granite-js/react-native';
128
+ * import { context } from '../require.context';
129
+ *
130
+ * const APP_NAME = 'my-app-name';
131
+ *
132
+ * function AppContainer({ children, ...initialProps }: PropsWithChildren<InitialProps>) {
133
+ * console.log({ initialProps });
134
+ * return <>{children}</>;
135
+ * }
136
+ *
137
+ * export default Granite.registerApp(AppContainer, {
138
+ * appName: APP_NAME,
139
+ * context,
140
+ * });
141
+ * :::
142
+ * ```
143
+ */
144
+ export type InitialProps = AndroidInitialProps | IOSInitialProps;
@@ -0,0 +1 @@
1
+ export * from './InitialProps';
@@ -0,0 +1,16 @@
1
+ import { createContext } from 'react';
2
+ import IOManager from './IOManager';
3
+
4
+ export interface IOContextValue {
5
+ manager: null | IOManager;
6
+ }
7
+
8
+ /**
9
+ * @name IOContext
10
+ * @description Context that shares the IOManager instance.
11
+ */
12
+ const IOContext = createContext<IOContextValue>({
13
+ manager: null,
14
+ });
15
+
16
+ export default IOContext;
@@ -0,0 +1,72 @@
1
+ import { RefAttributes } from 'react';
2
+ import { FlatList, FlatListProps } from 'react-native';
3
+ import withIO, { IOComponentProps } from './withIO';
4
+
5
+ export type IOFlatListController = FlatList;
6
+
7
+ export type IOFlatListProps<ItemT = any> = IOComponentProps & FlatListProps<ItemT>;
8
+
9
+ /**
10
+ * @public
11
+ * @category Screen Control
12
+ * @name IOFlatList
13
+ * @description
14
+ * `IOFlatList` is a `FlatList` component with added Intersection Observer functionality to detect when specific elements become visible or disappear from the screen during scrolling. Using this component, you can easily check and handle whether each item in the list is visible on the screen.
15
+ *
16
+ * When used with `InView`, you can check the exposure status of each element. The [InView](/reference/react-native/Screen%20Control/InView) component included as a child element detects whether the element is visible on the screen through the observation functionality of `IOFlatList` and triggers events based on the exposure status.
17
+ *
18
+ * @example
19
+ *
20
+ * You can check whether each item in the list appears on the screen using `IOFlatList`.
21
+ * When each item in the list appears on the screen, the `InView` component changes to the `visible` state.
22
+ *
23
+ * ```tsx
24
+ * import { ReactNode, useState } from 'react';
25
+ * import { StyleSheet, Text, View } from 'react-native';
26
+ * import { InView, IOFlatList } from '@granite-js/react-native';
27
+ *
28
+ * const mockData = Array.from({ length: 30 }, (_, i) => ({ key: String(i) }));
29
+ *
30
+ * export default function FlatListPage() {
31
+ * return <IOFlatList data={mockData} renderItem={({ item }) => <InViewItem>{item.key}</InViewItem>} />;
32
+ * }
33
+ *
34
+ * function InViewItem({ children }: { children: ReactNode }) {
35
+ * const [visible, setVisible] = useState(false);
36
+ *
37
+ * return (
38
+ * <InView onChange={setVisible}>
39
+ * <View style={styles.item}>
40
+ * <Text>{children}</Text>
41
+ * <Text>{visible ? 'visible' : ''}</Text>
42
+ * </View>
43
+ * </InView>
44
+ * );
45
+ * }
46
+ *
47
+ * const styles = StyleSheet.create({
48
+ * item: {
49
+ * padding: 16,
50
+ * borderBottomWidth: 1,
51
+ * borderBottomColor: '#ddd',
52
+ * },
53
+ * });
54
+ * ```
55
+ */
56
+ const IOFlatList = withIO(FlatList, [
57
+ 'flashScrollIndicators',
58
+ 'getNativeScrollRef',
59
+ 'getScrollResponder',
60
+ 'getScrollableNode',
61
+ 'scrollToEnd',
62
+ 'scrollToIndex',
63
+ 'scrollToItem',
64
+ 'scrollToOffset',
65
+ ]) as unknown as typeof IOFlatListFunction;
66
+
67
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
68
+ declare function IOFlatListFunction<ItemT = any>(
69
+ props: IOFlatListProps<ItemT> & RefAttributes<IOFlatListController>
70
+ ): JSX.Element;
71
+
72
+ export default IOFlatList;
@@ -0,0 +1,73 @@
1
+ import IntersectionObserver, {
2
+ IntersectionObserverOptions,
3
+ IntersectionObserverEntry,
4
+ Element,
5
+ } from './IntersectionObserver';
6
+
7
+ export type ObserverInstanceCallback = (inView: boolean, intersectionRatio: number) => void;
8
+
9
+ export interface ObserverInstance {
10
+ readonly callback: ObserverInstanceCallback;
11
+ readonly element: Element;
12
+ readonly observerId: number;
13
+ readonly observer: IntersectionObserver;
14
+ }
15
+
16
+ /**
17
+ * @kind class
18
+ * @name IOManager
19
+ * @description A class that tracks the visibility of DOM elements using `IntersectionObserver` instances and executes callbacks when elements enter or leave the viewport.
20
+ * This class makes it easy to manage multiple elements and execute custom logic based on the visibility status of each element.
21
+ */
22
+ class IOManager {
23
+ io: IntersectionObserver;
24
+ observerId: number;
25
+ instanceMap: Map<Element, ObserverInstance> = new Map();
26
+
27
+ constructor(options: IntersectionObserverOptions) {
28
+ this.io = new IntersectionObserver(this.handleChange, options);
29
+ this.observerId = 0;
30
+ }
31
+
32
+ handleChange = (entries: IntersectionObserverEntry[]) => {
33
+ for (let index = 0; index < entries.length; index += 1) {
34
+ const entry = entries[index];
35
+
36
+ if (entry == null) {
37
+ continue;
38
+ }
39
+
40
+ const { target, isIntersecting, intersectionRatio } = entry;
41
+ const instance = this.instanceMap.get(target);
42
+ if (instance) {
43
+ instance.callback(isIntersecting, intersectionRatio);
44
+ }
45
+ }
46
+ };
47
+
48
+ observe(element: Element, callback: ObserverInstanceCallback): ObserverInstance {
49
+ const existInstance = this.instanceMap.get(element);
50
+ if (existInstance) {
51
+ return existInstance;
52
+ }
53
+ this.observerId += 1;
54
+ const instance: ObserverInstance = {
55
+ callback,
56
+ element,
57
+ observerId: this.observerId,
58
+ observer: this.io,
59
+ };
60
+ this.instanceMap.set(element, instance);
61
+ this.io.observe(element);
62
+ return instance;
63
+ }
64
+
65
+ unobserve(element: any) {
66
+ if (this.instanceMap.has(element)) {
67
+ this.instanceMap.delete(element);
68
+ this.io.unobserve(element);
69
+ }
70
+ }
71
+ }
72
+
73
+ export default IOManager;