@granite-js/react-native 0.1.14 → 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,20 @@
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
+
3
18
  ## 0.1.14
4
19
 
5
20
  ### 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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@granite-js/react-native",
3
- "version": "0.1.14",
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.14",
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.14",
104
- "@granite-js/image": "0.1.14",
105
- "@granite-js/jest": "0.1.14",
106
- "@granite-js/lottie": "0.1.14",
107
- "@granite-js/mpack": "0.1.14",
108
- "@granite-js/plugin-core": "0.1.14",
109
- "@granite-js/style-utils": "0.1.14",
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';