@geophrase/react 1.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.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @geophrase/react
2
+
3
+ The official React and Next.js hooks for integrating the Geophrase Connect address selector.
4
+
5
+ 👉 **[Read the full documentation and integration guide on GitHub](https://github.com/geophrase/geophrase-js)**
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@geophrase/react",
3
+ "version": "1.0.0",
4
+ "description": "React and Next.js hooks for the Geophrase Connect SDK",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "peerDependencies": {
8
+ "react": ">=16.8.0"
9
+ },
10
+ "dependencies": {
11
+ "@geophrase/core": "*"
12
+ }
13
+ }
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ "use client";
2
+
3
+ export { useGeophrase } from './useGeophrase.js';
@@ -0,0 +1,70 @@
1
+ "use client";
2
+
3
+ import { useEffect, useRef, useCallback } from 'react';
4
+ import Geophrase from '@geophrase/core';
5
+
6
+ export const useGeophrase = (options = {}) => {
7
+ const geoInstance = useRef(null);
8
+
9
+ // 1. Keep a mutable reference to the latest callbacks
10
+ // This prevents React from destroying/recreating the iframe just
11
+ // because the parent component re-rendered and created a new function.
12
+ const savedCallbacks = useRef({
13
+ onSuccess: options.onSuccess,
14
+ onError: options.onError,
15
+ onClose: options.onClose
16
+ });
17
+
18
+ // 2. Update the callbacks on every render without triggering the useEffect
19
+ useEffect(() => {
20
+ savedCallbacks.current = {
21
+ onSuccess: options.onSuccess,
22
+ onError: options.onError,
23
+ onClose: options.onClose
24
+ };
25
+ });
26
+
27
+ // 3. Manage the Vanilla JS Lifecycle safely
28
+ useEffect(() => {
29
+ if (typeof window === 'undefined') return;
30
+
31
+ // Instantiate the vanilla class EXACTLY once per mount
32
+ geoInstance.current = new Geophrase({
33
+ key: options.key,
34
+ order_id: options.order_id,
35
+ phone: options.phone,
36
+ // Pass wrapper functions to always execute the latest React state
37
+ onSuccess: (data) => {
38
+ if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
39
+ },
40
+ onError: (error) => {
41
+ if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
42
+ },
43
+ onClose: () => {
44
+ if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
45
+ }
46
+ });
47
+
48
+ // Cleanup: Destroy the DOM elements if the React component unmounts
49
+ // This perfectly handles React 18's Strict Mode double-mount behavior
50
+ return () => {
51
+ if (geoInstance.current) {
52
+ geoInstance.current.destroy();
53
+ geoInstance.current = null;
54
+ }
55
+ };
56
+
57
+ // We only re-run this effect if the CORE configuration changes
58
+ }, [options.key, options.order_id, options.phone]);
59
+
60
+ // 4. Provide a stable reference to the open method
61
+ const open = useCallback(() => {
62
+ if (geoInstance.current) {
63
+ geoInstance.current.open();
64
+ } else {
65
+ console.warn("Geophrase widget is not ready yet.");
66
+ }
67
+ }, []);
68
+
69
+ return { open };
70
+ };