@methodfi/opal-react 1.4.2 → 1.4.3
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/dist/index.d.ts +6 -2
- package/dist/index.js +24 -6
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -81,6 +81,7 @@ type OpalAccountVerificationEventData = {
|
|
|
81
81
|
entity_id: string;
|
|
82
82
|
accounts: string[];
|
|
83
83
|
};
|
|
84
|
+
type AppearanceMode = 'light' | 'dark' | 'system';
|
|
84
85
|
/**
|
|
85
86
|
* Configuration object for the useOpal hook
|
|
86
87
|
*/
|
|
@@ -100,6 +101,8 @@ interface OpalConfig {
|
|
|
100
101
|
interface OpalOpenOptions {
|
|
101
102
|
/** The Opal token (otkn_*) obtained from POST /opal/token */
|
|
102
103
|
token: string;
|
|
104
|
+
/** Appearance mode: 'light', 'dark', or 'system' (uses OS preference) */
|
|
105
|
+
appearance?: AppearanceMode;
|
|
103
106
|
}
|
|
104
107
|
/**
|
|
105
108
|
* Return type for the useOpal hook
|
|
@@ -161,10 +164,10 @@ declare function OpalProvider({ children }: OpalProviderProps): JSX.Element;
|
|
|
161
164
|
* },
|
|
162
165
|
* });
|
|
163
166
|
*
|
|
164
|
-
* // Open Opal with a token
|
|
167
|
+
* // Open Opal with a token and appearance
|
|
165
168
|
* const handleOpenOpal = async () => {
|
|
166
169
|
* const token = await getOpalToken(); // Your token fetching logic
|
|
167
|
-
* open({ token });
|
|
170
|
+
* open({ token, appearance: 'dark' });
|
|
168
171
|
* };
|
|
169
172
|
* ```
|
|
170
173
|
*
|
|
@@ -174,3 +177,4 @@ declare function OpalProvider({ children }: OpalProviderProps): JSX.Element;
|
|
|
174
177
|
declare function useOpal(config?: OpalConfig): UseOpalReturn;
|
|
175
178
|
|
|
176
179
|
export { OpalEventType, OpalMode, OpalProvider, useOpal };
|
|
180
|
+
export type { AppearanceMode };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import React, { useState, createContext, useEffect,
|
|
2
|
+
import React, { useState, createContext, useEffect, useCallback, useContext } from 'react';
|
|
3
3
|
|
|
4
4
|
// Opal modes supported by the SDK
|
|
5
5
|
const OpalMode = {
|
|
@@ -78,6 +78,7 @@ const OpalContext = /*#__PURE__*/ createContext(null);
|
|
|
78
78
|
const [error, setError] = useState(null);
|
|
79
79
|
const [currentToken, setCurrentToken] = useState(null);
|
|
80
80
|
const [currentConfig, setCurrentConfig] = useState({});
|
|
81
|
+
const [pendingAppearance, setPendingAppearance] = useState(undefined);
|
|
81
82
|
const environment = currentConfig.env || 'production';
|
|
82
83
|
const baseUrl = OPAL_URLS[environment];
|
|
83
84
|
const opalUrl = currentToken ? `${baseUrl}/?token=${currentToken}` : '';
|
|
@@ -100,6 +101,7 @@ const OpalContext = /*#__PURE__*/ createContext(null);
|
|
|
100
101
|
};
|
|
101
102
|
const open = (options)=>{
|
|
102
103
|
setCurrentToken(options.token);
|
|
104
|
+
setPendingAppearance(options.appearance);
|
|
103
105
|
setIsOpen(true);
|
|
104
106
|
setError(null);
|
|
105
107
|
currentConfig.onOpen == null ? void 0 : currentConfig.onOpen.call(currentConfig);
|
|
@@ -126,8 +128,10 @@ const OpalContext = /*#__PURE__*/ createContext(null);
|
|
|
126
128
|
}, children, isOpen && currentToken && /*#__PURE__*/ React.createElement(OpalIframe, {
|
|
127
129
|
token: currentToken,
|
|
128
130
|
opalUrl: opalUrl,
|
|
131
|
+
baseUrl: baseUrl,
|
|
129
132
|
onMessage: handleMessage,
|
|
130
133
|
onClose: close,
|
|
134
|
+
initialAppearance: pendingAppearance,
|
|
131
135
|
style: {
|
|
132
136
|
position: 'fixed',
|
|
133
137
|
inset: 0,
|
|
@@ -174,10 +178,10 @@ function useOpalContext() {
|
|
|
174
178
|
* },
|
|
175
179
|
* });
|
|
176
180
|
*
|
|
177
|
-
* // Open Opal with a token
|
|
181
|
+
* // Open Opal with a token and appearance
|
|
178
182
|
* const handleOpenOpal = async () => {
|
|
179
183
|
* const token = await getOpalToken(); // Your token fetching logic
|
|
180
|
-
* open({ token });
|
|
184
|
+
* open({ token, appearance: 'dark' });
|
|
181
185
|
* };
|
|
182
186
|
* ```
|
|
183
187
|
*
|
|
@@ -196,8 +200,8 @@ function useOpalContext() {
|
|
|
196
200
|
error: context.error
|
|
197
201
|
};
|
|
198
202
|
}
|
|
199
|
-
function OpalIframe({ token, opalUrl, onMessage, style }) {
|
|
200
|
-
const iframeRef = useRef(null);
|
|
203
|
+
function OpalIframe({ token, opalUrl, baseUrl, onMessage, style, initialAppearance }) {
|
|
204
|
+
const iframeRef = React.useRef(null);
|
|
201
205
|
const handleMessage = useCallback((event)=>{
|
|
202
206
|
// Check if the event is from an allowed origin
|
|
203
207
|
const allowedOrigins = [
|
|
@@ -237,6 +241,19 @@ function OpalIframe({ token, opalUrl, onMessage, style }) {
|
|
|
237
241
|
}, [
|
|
238
242
|
handleMessage
|
|
239
243
|
]);
|
|
244
|
+
const handleLoad = useCallback(()=>{
|
|
245
|
+
var _iframeRef_current;
|
|
246
|
+
// Set initial appearance when iframe loads
|
|
247
|
+
if (initialAppearance && ((_iframeRef_current = iframeRef.current) == null ? void 0 : _iframeRef_current.contentWindow)) {
|
|
248
|
+
iframeRef.current.contentWindow.postMessage({
|
|
249
|
+
type: 'OPAL_SET_APPEARANCE',
|
|
250
|
+
appearance: initialAppearance
|
|
251
|
+
}, baseUrl);
|
|
252
|
+
}
|
|
253
|
+
}, [
|
|
254
|
+
initialAppearance,
|
|
255
|
+
baseUrl
|
|
256
|
+
]);
|
|
240
257
|
if (!token || !opalUrl) {
|
|
241
258
|
return null;
|
|
242
259
|
}
|
|
@@ -245,7 +262,8 @@ function OpalIframe({ token, opalUrl, onMessage, style }) {
|
|
|
245
262
|
src: opalUrl,
|
|
246
263
|
style: style,
|
|
247
264
|
sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-top-navigation",
|
|
248
|
-
title: "Opal"
|
|
265
|
+
title: "Opal",
|
|
266
|
+
onLoad: handleLoad
|
|
249
267
|
});
|
|
250
268
|
}
|
|
251
269
|
|