-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathWidgetContext.tsx
More file actions
117 lines (105 loc) · 4.03 KB
/
Copy pathWidgetContext.tsx
File metadata and controls
117 lines (105 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { CONNECTOR_INITIAL_AUTHENTICATION_MODE, WALLET_CONNECTOR_TYPE } from "@web3auth/no-modal";
import { createContext, type FC, type ReactNode, useContext, useEffect, useMemo, useState } from "react";
import { browser, ExternalWalletEventType, LoginModalProps, os, platform, SocialLoginEventType } from "../interfaces";
type WidgetContextType = {
isDark: boolean;
appLogo?: string;
deviceDetails: { platform: platform; browser: browser; os: os };
uiConfig: LoginModalProps;
isConnectAndSignAuthenticationMode: boolean;
handleSocialLoginClick: (params: SocialLoginEventType) => void;
handleExternalWalletClick: (params: ExternalWalletEventType) => void;
handleMobileVerifyConnect: (params: { connector: WALLET_CONNECTOR_TYPE }) => void;
handleShowExternalWallets: (externalWalletsInitialized: boolean) => void;
handleAcceptConsent: () => void | Promise<void>;
handleDeclineConsent: () => void | Promise<void>;
handleChangeWallet: () => void | Promise<void>;
closeModal: () => void;
};
type WidgetProviderProps = {
children: ReactNode;
isDark: boolean;
deviceDetails: { platform: platform; browser: browser; os: os };
uiConfig: LoginModalProps;
handleSocialLoginClick: (params: SocialLoginEventType) => void;
handleExternalWalletClick: (params: ExternalWalletEventType) => void;
handleMobileVerifyConnect: (params: { connector: WALLET_CONNECTOR_TYPE }) => void;
handleShowExternalWallets: (externalWalletsInitialized: boolean) => void;
handleAcceptConsent: () => void | Promise<void>;
handleDeclineConsent: () => void | Promise<void>;
handleChangeWallet: () => void | Promise<void>;
closeModal: () => void;
};
const WidgetContext = createContext<WidgetContextType | undefined>(undefined);
export const WidgetProvider: FC<WidgetProviderProps> = ({
children,
isDark: isDarkProp = true,
deviceDetails,
uiConfig,
handleSocialLoginClick,
handleExternalWalletClick,
handleMobileVerifyConnect,
handleShowExternalWallets,
handleAcceptConsent,
handleDeclineConsent,
handleChangeWallet,
closeModal,
}) => {
const [isDark, setIsDark] = useState(() => {
if (uiConfig.mode === "auto" && typeof window !== "undefined" && typeof window.matchMedia === "function") {
return window.matchMedia("(prefers-color-scheme: dark)").matches;
}
return isDarkProp;
});
// When mode is "auto", follow live OS color-scheme changes so the modal re-themes without reopening.
useEffect(() => {
if (uiConfig.mode !== "auto" || typeof window === "undefined" || typeof window.matchMedia !== "function") {
return undefined;
}
const mql = window.matchMedia("(prefers-color-scheme: dark)");
const applyPreference = () => {
setIsDark(mql.matches);
// Keep the CSS `dark` variant in sync with the React context value.
document.getElementById("w3a-parent-container")?.classList.toggle("w3a--dark", mql.matches);
};
mql.addEventListener("change", applyPreference);
return () => {
mql.removeEventListener("change", applyPreference);
};
}, [uiConfig.mode]);
const appLogo = useMemo(() => {
return isDark ? uiConfig.logoDark : uiConfig.logoLight;
}, [isDark, uiConfig.logoDark, uiConfig.logoLight]);
const isConnectAndSignAuthenticationMode = useMemo(
() => uiConfig.initialAuthenticationMode === CONNECTOR_INITIAL_AUTHENTICATION_MODE.CONNECT_AND_SIGN,
[uiConfig.initialAuthenticationMode]
);
return (
<WidgetContext.Provider
value={{
isDark,
appLogo,
deviceDetails,
uiConfig,
isConnectAndSignAuthenticationMode,
handleSocialLoginClick,
handleExternalWalletClick,
handleMobileVerifyConnect,
handleShowExternalWallets,
handleAcceptConsent,
handleDeclineConsent,
handleChangeWallet,
closeModal,
}}
>
{children}
</WidgetContext.Provider>
);
};
export const useWidget = () => {
const context = useContext(WidgetContext);
if (!context) {
throw new Error("useWidget must be used within a WidgetProvider");
}
return context;
};