@granite-js/react-native 0.1.13 → 0.1.15

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # @granite-js/react-native
2
2
 
3
+ ## 0.1.15
4
+
5
+ ### Patch Changes
6
+
7
+ - d16ee87: Add useInitialProps, useInitialSearchParams hook.
8
+ - Updated dependencies [d16ee87]
9
+ - @granite-js/plugin-core@0.1.15
10
+ - @granite-js/style-utils@0.1.15
11
+ - @granite-js/lottie@0.1.15
12
+ - @granite-js/native@0.1.15
13
+ - @granite-js/image@0.1.15
14
+ - @granite-js/mpack@0.1.15
15
+ - @granite-js/jest@0.1.15
16
+ - @granite-js/cli@0.1.15
17
+
18
+ ## 0.1.14
19
+
20
+ ### Patch Changes
21
+
22
+ - 0520d19: Fix type error
23
+ - a06bb7d: Remove useWaitForReturnNavigator
24
+ - 6d38bc5: Clarify error message for missing \_404 Page
25
+ - @granite-js/cli@0.1.14
26
+ - @granite-js/image@0.1.14
27
+ - @granite-js/jest@0.1.14
28
+ - @granite-js/lottie@0.1.14
29
+ - @granite-js/mpack@0.1.14
30
+ - @granite-js/native@0.1.14
31
+ - @granite-js/plugin-core@0.1.14
32
+ - @granite-js/style-utils@0.1.14
33
+
3
34
  ## 0.1.13
4
35
 
5
36
  ### Patch Changes
@@ -0,0 +1,28 @@
1
+ import { type PropsWithChildren } from 'react';
2
+ import { InitialProps } from '../../initial-props';
3
+ export declare const InitialPropsContext: import("react").Context<InitialProps | null>;
4
+ export declare function InitialPropsProvider({ children, initialProps }: PropsWithChildren<{
5
+ initialProps: InitialProps;
6
+ }>): import("react/jsx-runtime").JSX.Element;
7
+ /**
8
+ * @public
9
+ * @name useInitialProps
10
+ * @category Core
11
+ * @description Provides initial data passed from the native platform (Android or iOS) when entering a specific screen in React Native apps. This data can be used to immediately apply themes or user settings right after app launch. For example, you can receive dark mode settings from the native platform and apply dark mode immediately when the React Native app starts.
12
+ * @returns {InitialProps} Initial data for the app
13
+ * @example
14
+ *
15
+ * ### Checking dark mode status with initial data
16
+ *
17
+ * ```tsx
18
+ * import { useInitialProps } from '@granite-js/react-native';
19
+ *
20
+ * function Page() {
21
+ * const initialProps = useInitialProps();
22
+ * // 'light' or 'dark'
23
+ * console.log(initialProps.initialColorPreference);
24
+ * return <></>;
25
+ * }
26
+ * ```
27
+ */
28
+ export declare function useInitialProps<T extends InitialProps>(): T;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @public
3
+ * @name useInitialSearchParams
4
+ * @category EnvironmentCheck
5
+ * @description A hook that returns the query parameters from the URL passed when the app is first launched, directly as an object. This allows immediate application of initial entry handling like login or theme settings, enabling quick customization of user experience. If an invalid URL is provided, it safely returns an empty object. When the native platform (Android or iOS) passes a URL with query parameters to the app on first launch, you can easily read each parameter value using this hook.
6
+ *
7
+ * @returns {Record<string, string>} An object containing key-value pairs of query parameters from the initial URL. Returns an empty object if there are no query parameters or if the URL is invalid.
8
+ * @example
9
+ * ```tsx
10
+ * import { useInitialSearchParams } from '@granite-js/react-native';
11
+ *
12
+ * function Page() {
13
+ * const params = useInitialSearchParams();
14
+ * // Example: if initial URL is myapp://home?userId=42&theme=dark
15
+ * console.log(params.userId); // "42"
16
+ * console.log(params.theme); // "dark"
17
+ * return <></>;
18
+ * }
19
+ * ```
20
+ */
21
+ export declare function useInitialSearchParams(): {
22
+ [k: string]: string;
23
+ };
@@ -1,2 +1,4 @@
1
+ export { useInitialProps } from './context/InitialPropsContext';
2
+ export { useInitialSearchParams } from './context/useInitialSearchParams';
1
3
  export { Granite } from './Granite';
2
4
  export type { GraniteProps } from './Granite';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import './types/global';
2
- export { Granite } from './app';
2
+ export { Granite, useInitialSearchParams, useInitialProps } from './app';
3
3
  export * from '@granite-js/style-utils';
4
4
  export * from '@granite-js/image';
5
5
  export * from '@granite-js/lottie';
@@ -11,7 +11,6 @@ export * from './keyboard';
11
11
  export * from './intersection-observer';
12
12
  export * from './impression-area';
13
13
  export * from './scroll-view-inertial-background';
14
- export * from './react';
15
14
  export * from './router/createRoute';
16
15
  export * from './event';
17
16
  export * from './video';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@granite-js/react-native",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "The Granite Framework",
5
5
  "bin": {
6
6
  "granite": "./bin/cli.js"
@@ -74,7 +74,7 @@
74
74
  "@babel/core": "^7.24.9",
75
75
  "@babel/preset-env": "^7.24.8",
76
76
  "@babel/preset-typescript": "^7.24.7",
77
- "@granite-js/native": "0.1.13",
77
+ "@granite-js/native": "0.1.15",
78
78
  "@testing-library/dom": "^10.4.0",
79
79
  "@testing-library/react": "^16.1.0",
80
80
  "@types/babel__core": "^7",
@@ -100,13 +100,13 @@
100
100
  "react-native": "*"
101
101
  },
102
102
  "dependencies": {
103
- "@granite-js/cli": "0.1.13",
104
- "@granite-js/image": "0.1.13",
105
- "@granite-js/jest": "0.1.13",
106
- "@granite-js/lottie": "0.1.13",
107
- "@granite-js/mpack": "0.1.13",
108
- "@granite-js/plugin-core": "0.1.13",
109
- "@granite-js/style-utils": "0.1.13",
103
+ "@granite-js/cli": "0.1.15",
104
+ "@granite-js/image": "0.1.15",
105
+ "@granite-js/jest": "0.1.15",
106
+ "@granite-js/lottie": "0.1.15",
107
+ "@granite-js/mpack": "0.1.15",
108
+ "@granite-js/plugin-core": "0.1.15",
109
+ "@granite-js/style-utils": "0.1.15",
110
110
  "es-toolkit": "^1.39.8",
111
111
  "react-native-url-polyfill": "1.3.0"
112
112
  }
@@ -6,6 +6,7 @@ import { BackEventProvider } from '../use-back-event';
6
6
  import { App } from './App';
7
7
  import type { GraniteProps } from './Granite';
8
8
  import { getSchemePrefix } from '../utils/getSchemePrefix';
9
+ import { InitialPropsProvider } from './context/InitialPropsContext';
9
10
 
10
11
  /**
11
12
  * @internal
@@ -24,19 +25,21 @@ export function AppRoot({ appName, context, container: Container, initialProps,
24
25
  });
25
26
 
26
27
  return (
27
- <App {...initialProps}>
28
- <SafeAreaProvider>
29
- <BackEventProvider>
30
- <Router
31
- context={context}
32
- initialProps={initialProps}
33
- initialScheme={initialScheme}
34
- container={Container}
35
- prefix={prefix}
36
- {...router}
37
- />
38
- </BackEventProvider>
39
- </SafeAreaProvider>
40
- </App>
28
+ <InitialPropsProvider initialProps={initialProps}>
29
+ <App {...initialProps}>
30
+ <SafeAreaProvider>
31
+ <BackEventProvider>
32
+ <Router
33
+ context={context}
34
+ initialProps={initialProps}
35
+ initialScheme={initialScheme}
36
+ container={Container}
37
+ prefix={prefix}
38
+ {...router}
39
+ />
40
+ </BackEventProvider>
41
+ </SafeAreaProvider>
42
+ </App>
43
+ </InitialPropsProvider>
41
44
  );
42
45
  }
@@ -0,0 +1,39 @@
1
+ import { createContext, useContext, type PropsWithChildren } from 'react';
2
+ import { InitialProps } from '../../initial-props';
3
+
4
+ export const InitialPropsContext = createContext<InitialProps | null>(null);
5
+
6
+ export function InitialPropsProvider({ children, initialProps }: PropsWithChildren<{ initialProps: InitialProps }>) {
7
+ return <InitialPropsContext.Provider value={initialProps}>{children}</InitialPropsContext.Provider>;
8
+ }
9
+
10
+ /**
11
+ * @public
12
+ * @name useInitialProps
13
+ * @category Core
14
+ * @description Provides initial data passed from the native platform (Android or iOS) when entering a specific screen in React Native apps. This data can be used to immediately apply themes or user settings right after app launch. For example, you can receive dark mode settings from the native platform and apply dark mode immediately when the React Native app starts.
15
+ * @returns {InitialProps} Initial data for the app
16
+ * @example
17
+ *
18
+ * ### Checking dark mode status with initial data
19
+ *
20
+ * ```tsx
21
+ * import { useInitialProps } from '@granite-js/react-native';
22
+ *
23
+ * function Page() {
24
+ * const initialProps = useInitialProps();
25
+ * // 'light' or 'dark'
26
+ * console.log(initialProps.initialColorPreference);
27
+ * return <></>;
28
+ * }
29
+ * ```
30
+ */
31
+ export function useInitialProps<T extends InitialProps>() {
32
+ const initialProps = useContext(InitialPropsContext);
33
+
34
+ if (!initialProps) {
35
+ throw new Error('InitialPropsContext not found');
36
+ }
37
+
38
+ return initialProps as T;
39
+ }
@@ -0,0 +1,32 @@
1
+ import { useInitialProps } from './InitialPropsContext';
2
+ import { getSchemeUri } from '../../native-modules';
3
+
4
+ /**
5
+ * @public
6
+ * @name useInitialSearchParams
7
+ * @category EnvironmentCheck
8
+ * @description A hook that returns the query parameters from the URL passed when the app is first launched, directly as an object. This allows immediate application of initial entry handling like login or theme settings, enabling quick customization of user experience. If an invalid URL is provided, it safely returns an empty object. When the native platform (Android or iOS) passes a URL with query parameters to the app on first launch, you can easily read each parameter value using this hook.
9
+ *
10
+ * @returns {Record<string, string>} An object containing key-value pairs of query parameters from the initial URL. Returns an empty object if there are no query parameters or if the URL is invalid.
11
+ * @example
12
+ * ```tsx
13
+ * import { useInitialSearchParams } from '@granite-js/react-native';
14
+ *
15
+ * function Page() {
16
+ * const params = useInitialSearchParams();
17
+ * // Example: if initial URL is myapp://home?userId=42&theme=dark
18
+ * console.log(params.userId); // "42"
19
+ * console.log(params.theme); // "dark"
20
+ * return <></>;
21
+ * }
22
+ * ```
23
+ */
24
+ export function useInitialSearchParams() {
25
+ const scheme = useInitialProps().scheme ?? getSchemeUri();
26
+
27
+ try {
28
+ return Object.fromEntries(new URL(scheme).searchParams);
29
+ } catch {
30
+ return {};
31
+ }
32
+ }
package/src/app/index.ts CHANGED
@@ -1,2 +1,4 @@
1
+ export { useInitialProps } from './context/InitialPropsContext';
2
+ export { useInitialSearchParams } from './context/useInitialSearchParams';
1
3
  export { Granite } from './Granite';
2
4
  export type { GraniteProps } from './Granite';
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import './types/global';
2
2
 
3
- export { Granite } from './app';
3
+ export { Granite, useInitialSearchParams, useInitialProps } from './app';
4
4
  export * from '@granite-js/style-utils';
5
5
  export * from '@granite-js/image';
6
6
  export * from '@granite-js/lottie';
@@ -13,7 +13,6 @@ export * from './keyboard';
13
13
  export * from './intersection-observer';
14
14
  export * from './impression-area';
15
15
  export * from './scroll-view-inertial-background';
16
- export * from './react';
17
16
  export * from './router/createRoute';
18
17
  export * from './event';
19
18
  export * from './video';
@@ -72,6 +72,12 @@ export function getScreenPathMapConfig(routeScreens: RouteScreen[]) {
72
72
  path: '',
73
73
  };
74
74
 
75
+ const notFoundPage = routeScreens.find((screen) => screen.path === '/_404');
76
+
77
+ if (notFoundPage == null) {
78
+ throw new Error('404 page not found. Please create a `_404.ts` or `_404.tsx` file in the `pages/*` folder.');
79
+ }
80
+
75
81
  // https://reactnavigation.org/docs/configuring-links/#handling-unmatched-routes-or-404
76
82
  screensConfig['/_404'] = {
77
83
  path: '*',
@@ -1 +0,0 @@
1
- export { useWaitForReturnNavigator } from './useWaitForReturnNavigator';
@@ -1,39 +0,0 @@
1
- /**
2
- * @public
3
- * @category Screen Control
4
- * @name useWaitForReturnNavigator
5
- * @description
6
- * A Hook that helps execute the next code synchronously when returning from a screen transition.
7
- * Screen navigation uses [@react-navigation/native `useNavigation`'s `navigate`](https://reactnavigation.org/docs/6.x/navigation-prop#navigate).
8
- *
9
- * For example, it can be used when you want to log that a user has navigated to another screen and returned.
10
- *
11
- * @example
12
- * ### Example of code execution when returning from screen navigation
13
- *
14
- * When the **"Navigate"** button is pressed, it navigates to another screen, and logs are created when returning.
15
- *
16
- * ```tsx
17
- * import { Button } from 'react-native';
18
- * import { useWaitForReturnNavigator } from '@granite-js/react-native';
19
- *
20
- * export function UseWaitForReturnNavigator() {
21
- * const navigate = useWaitForReturnNavigator();
22
- *
23
- * return (
24
- * <>
25
- * <Button
26
- * title="Navigate"
27
- * onPress={async () => {
28
- * console.log(1);
29
- * await navigate('/examples/use-visibility');
30
- * // This code executes when returning to the screen
31
- * console.log(2);
32
- * }}
33
- * />
34
- * </>
35
- * );
36
- * }
37
- * ```
38
- */
39
- export declare function useWaitForReturnNavigator<T extends Record<string, object | undefined>>(): <RouteName extends keyof T>(route: RouteName, params?: T[RouteName]) => Promise<void>;
@@ -1 +0,0 @@
1
- export { useWaitForReturnNavigator } from './useWaitForReturnNavigator';
@@ -1,75 +0,0 @@
1
- import { useNavigation } from '@granite-js/native/@react-navigation/native';
2
- import { NativeStackNavigationProp } from '@granite-js/native/@react-navigation/native-stack';
3
- import { useCallback, useRef } from 'react';
4
- import { useVisibilityChange, type VisibilityState } from '../visibility';
5
-
6
- /**
7
- * @public
8
- * @category Screen Control
9
- * @name useWaitForReturnNavigator
10
- * @description
11
- * A Hook that helps execute the next code synchronously when returning from a screen transition.
12
- * Screen navigation uses [@react-navigation/native `useNavigation`'s `navigate`](https://reactnavigation.org/docs/6.x/navigation-prop#navigate).
13
- *
14
- * For example, it can be used when you want to log that a user has navigated to another screen and returned.
15
- *
16
- * @example
17
- * ### Example of code execution when returning from screen navigation
18
- *
19
- * When the **"Navigate"** button is pressed, it navigates to another screen, and logs are created when returning.
20
- *
21
- * ```tsx
22
- * import { Button } from 'react-native';
23
- * import { useWaitForReturnNavigator } from '@granite-js/react-native';
24
- *
25
- * export function UseWaitForReturnNavigator() {
26
- * const navigate = useWaitForReturnNavigator();
27
- *
28
- * return (
29
- * <>
30
- * <Button
31
- * title="Navigate"
32
- * onPress={async () => {
33
- * console.log(1);
34
- * await navigate('/examples/use-visibility');
35
- * // This code executes when returning to the screen
36
- * console.log(2);
37
- * }}
38
- * />
39
- * </>
40
- * );
41
- * }
42
- * ```
43
- */
44
-
45
- export function useWaitForReturnNavigator<T extends Record<string, object | undefined>>() {
46
- const callbacks = useRef<Array<() => void>>([]).current;
47
- const navigation = useNavigation<NativeStackNavigationProp<T>>();
48
-
49
- const startNavigating = useCallback(
50
- <RouteName extends keyof T>(route: RouteName, params?: T[RouteName]): Promise<void> => {
51
- return new Promise<void>((resolve) => {
52
- callbacks.push(resolve);
53
- navigation.navigate(route as any, params as any);
54
- });
55
- },
56
- [callbacks, navigation]
57
- );
58
-
59
- const handleVisibilityChange = useCallback(
60
- (state: VisibilityState) => {
61
- if (state === 'visible' && callbacks.length > 0) {
62
- for (const callback of callbacks) {
63
- callback();
64
- }
65
-
66
- callbacks.splice(0, callbacks.length);
67
- }
68
- },
69
- [callbacks]
70
- );
71
-
72
- useVisibilityChange(handleVisibilityChange);
73
-
74
- return startNavigating;
75
- }