/
ConnectEmbed.tsx
452 lines (424 loc) · 13 KB
/
ConnectEmbed.tsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import {
SetModalConfigCtx,
WalletUIStatesProvider,
} from "../../../evm/providers/wallet-ui-states-provider";
import {
modalMaxWidthCompact,
defaultTheme,
reservedScreens,
} from "../constants";
import { ConnectModalContent } from "./ConnectModal";
import { useSetupScreen } from "./screen";
import { DynamicHeight } from "../../../components/DynamicHeight";
import {
CustomThemeProvider,
useCustomTheme,
} from "../../../design-system/CustomThemeProvider";
import { ComponentProps, useContext, useEffect } from "react";
import { useTWLocale } from "../../../evm/providers/locale-provider";
import { StyledDiv } from "../../../design-system/elements";
import { Theme, radius } from "../../../design-system";
import {
WalletInstance,
useConnectionStatus,
useThirdwebAuthContext,
useUser,
useWalletContext,
} from "@thirdweb-dev/react-core";
import { Container } from "../../../components/basic";
import { Spinner } from "../../../components/Spinner";
import { canFitWideModal } from "../../../evm/utils/canFitWIdeModal";
export type ConnectEmbedProps = {
/**
* Class name to be added to the root element of ConnectEmbed
*/
className?: string;
/**
* theme for the ConnectEmbed
*
* If a theme is set on the [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider) component, it will be used as the default theme for all thirdweb components, else the default will be "dark"
*
* theme can be set to either "dark" or "light" or a custom theme object.
*
* You can also import `lightTheme` or `darkTheme` functions from `@thirdweb-dev/react` to use the default themes as base and overrides parts of it.
*
* @example
* ```ts
* import { lightTheme } from "@thirdweb-dev/react";
* const customTheme = lightTheme({
* colors: {
* modalBg: 'red'
* }
* })
* ```
*/
theme?: "dark" | "light" | Theme;
/**
* CSS styles to be applied to the root element of ConnectEmbed
*/
style?: React.CSSProperties;
/**
* If provided, Embed will show a Terms of Service message at the bottom with below link
*/
termsOfServiceUrl?: string;
/**
* If provided, Embed will show a Privacy Policy message at the bottom with below link
*/
privacyPolicyUrl?: string;
/**
* Enforce that users must sign in with their wallet using [auth](https://portal.thirdweb.com/auth) after connecting their wallet.
*
* This requires the `authConfig` prop to be set on the [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider) component.
*/
auth?: {
/**
* specify whether signing in is optional or not.
*
* By default it is `false` ( sign in is required ) if `authConfig` is set on [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider)
*/
loginOptional?: boolean;
/**
* Callback to be called after user signs in with their wallet
*/
onLogin?: (token: string) => void;
/**
* Callback to be called after user signs out
*/
onLogout?: () => void;
};
/**
* Callback to be called on successful connection of wallet. The callback is called with the connected wallet instance as the first argument
*
* ```tsx
* <ConnectEmbed
* onConnect={(wallet) => {
* console.log("connected to", wallet)
* }}
* />
* ```
*
* Note that this does not include the sign in, If you want to call a callback after user connects AND signs in with their wallet, use `auth.onLogin` prop instead
*
* ```tsx
* <ConnectEmbed
* auth={{
* onLogin: () => {
* console.log("wallet connected and signed in")
* }
* }}
* />
* ```
*
*/
onConnect?: (wallet: WalletInstance) => void;
/**
* By default, A "Powered by Thirdweb" branding is shown at the bottom of the embed.
*
* If you want to hide it, set this to `false`
*
* @example
* ```tsx
* <ConnectEmbed showThirdwebBranding={false} />
* ```
*/
showThirdwebBranding?: boolean;
};
/**
* @internal
*/
function useSignInRequired(loginOptional?: boolean) {
const connectionStatus = useConnectionStatus();
const { user } = useUser();
const authConfig = useThirdwebAuthContext();
if (loginOptional === true) {
return false;
}
return (
!!authConfig?.authUrl && !user?.address && connectionStatus === "connected"
);
}
/**
* Returns `true` if the `<ConnectEmbed />` should be rendered.
* It returns true if either one of the following conditions are met:
* - the wallet is not connected
* - the wallet is connected but the user is not signed in and `auth` is required ( loginOptional is not set to false )
*
* @example
* ```tsx
* function Example() {
* const loginOptional = false;
* const showConnectEmbed = useShowConnectEmbed(loginOptional);
*
* if (!showConnectEmbed) {
* return <div> Wallet is connected </div>
* }
*
* return (
* <div>
* <ConnectEmbed
* auth={{
* loginOptional,
* }}
* />
* </div>
* );
* }
*```
*
* @param loginOptional -
* Specify whether the `<ConnectEmbed />` you want to render has auth enabled or not.
* If not specified, it is assumed to be `false` ( login is required )
*
*/
export function useShowConnectEmbed(loginOptional?: boolean) {
const connectionStatus = useConnectionStatus();
const signInRequired = useSignInRequired(loginOptional);
return connectionStatus !== "connected" || signInRequired;
}
/**
* A component that allows the user to connect their wallet.
*
* it renders the same UI as the [`ConnectWallet`](https://portal.thirdweb.com/react/v4/components/ConnectWallet) component's modal - but directly on the page instead of being in a modal.
*
* It only renders UI if either one of the following conditions are met:
* - wallet is not connected
* - wallet is connected but the user is not signed in and `auth` is required ( loginOptional is not set to false )
*
* `ConnectEmbed` uses the [`useShowConnectEmbed`](https://portal.thirdweb.com/react/v4/useShowConnectEmbed) hook internally to determine if it should be rendered or not. You can also use this hook to determine if you should render something else instead of `ConnectEmbed`
*
* @example
* ```tsx
* function Example() {
* const loginOptional = false;
* const showConnectEmbed = useShowConnectEmbed(loginOptional);
*
* if (!showConnectEmbed) {
* return <div> Wallet is connected </div>
* }
*
* return (
* <div>
* <ConnectEmbed
* auth={{
* loginOptional,
* }}
* />
* </div>
* );
* }
* ```
*
* @param props -
* The props for the component.
*
* ### className
* Class name to be added to the root element of ConnectEmbed
*
* ### theme
* theme for the ConnectEmbed
*
* If a theme is set on the [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider) component, it will be used as the default theme for all thirdweb components, else the default will be "dark"
*
* theme can be set to either "dark" or "light" or a custom theme object.
*
* You can also import `lightTheme` or `darkTheme` functions from `@thirdweb-dev/react` to use the default themes as base and overrides parts of it.
*
* ```ts
* import { lightTheme } from "@thirdweb-dev/react";
* const customTheme = lightTheme({
* colors: {
* modalBg: 'red'
* }
* })
* ```
*
* ### style
* CSS styles to be applied to the root element of ConnectEmbed
*
* ### termsOfServiceUrl
* If provided, Embed will show a Terms of Service message at the bottom with below link
*
* ### privacyPolicyUrl
* If provided, Embed will show a Privacy Policy message at the bottom with below link
*
* ### auth
* Enforce that users must sign in with their wallet using [auth](https://portal.thirdweb.com/auth) after connecting their wallet.
*
* This requires the `authConfig` prop to be set on the [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider) component.
*
* The `auth` prop accepts an object with the following properties:
* - `loginOptional` - specify whether signing in is optional or not. By default it is `false` ( sign in is required ) if `authConfig` is set on [`ThirdWebProvider`](https://portal.thirdweb.com/react/v4/ThirdwebProvider)
* - `onLogin` - Callback to be called after user signs in with their wallet
* - `onLogout` - Callback to be called after user signs out
*
* ### onConnect
* Callback to be called on successful connection of wallet
*
* ```tsx
* <ConnectEmbed
* onConnect={() => {
* console.log("wallet connected")
* }}
* />
* ```
*
* Note that this does not include the sign in, If you want to call a callback after user connects AND signs in with their wallet, use `auth.onLogin` prop instead
*
* ```tsx
* <ConnectEmbed
* auth={{
* onLogin: () => {
* console.log("wallet connected and signed in")
* }
* }}
* />
* ```
*
* ### showThirdwebBranding
* By default ConnectWallet shows "Powered by Thirdweb" branding at the bottom of the ConnectWallet Modal.
*
* If you want to hide the branding, set this prop to `false`
*
* ```tsx
* <ConnectWallet showThirdwebBranding={false} />
*```
*
*/
export function ConnectEmbed(props: ConnectEmbedProps) {
const loginOptional = props.auth?.loginOptional;
const show = useShowConnectEmbed(loginOptional);
const contextTheme = useCustomTheme();
const walletUIStatesProps = {
theme: props.theme || contextTheme || defaultTheme,
modalSize: "compact" as const,
title: undefined,
termsOfServiceUrl: props.termsOfServiceUrl,
privacyPolicyUrl: props.privacyPolicyUrl,
isEmbed: true,
auth: props.auth,
onConnect: props.onConnect,
showThirdwebBranding: props.showThirdwebBranding,
};
if (show) {
return (
<WalletUIStatesProvider {...walletUIStatesProps}>
<CustomThemeProvider theme={walletUIStatesProps.theme}>
<ConnectEmbedContent {...props} onConnect={props.onConnect} />
<SyncedWalletUIStates {...walletUIStatesProps} />
</CustomThemeProvider>
</WalletUIStatesProvider>
);
}
return <div></div>;
}
const ConnectEmbedContent = (
props: ConnectEmbedProps & {
loginOptional?: boolean;
},
) => {
const requiresSignIn = useSignInRequired(props.loginOptional);
const screenSetup = useSetupScreen();
const { screen, setScreen, initialScreen } = screenSetup;
// if showing main screen but signIn is required, switch to signIn screen
useEffect(() => {
if (requiresSignIn && screen === reservedScreens.main) {
setScreen(reservedScreens.signIn);
}
}, [requiresSignIn, screen, setScreen]);
const connectionStatus = useConnectionStatus();
const { isAutoConnecting } = useWalletContext();
let content = null;
// show spinner on page load and during auto connecting a wallet
if (isAutoConnecting || connectionStatus === "unknown") {
content = (
<Container
flex="column"
center="both"
style={{
minHeight: "300px",
}}
>
<Spinner size="xl" color="accentText" />
</Container>
);
} else {
content = (
<ConnectModalContent
screenSetup={screenSetup}
isOpen={true}
onClose={() => {
setScreen(initialScreen);
}}
onHide={() => {
// no op
}}
onShow={() => {
// no op
}}
/>
);
}
return (
<EmbedContainer
className={props.className}
style={{
height: "auto",
maxWidth: modalMaxWidthCompact,
...props.style,
}}
>
<DynamicHeight> {content} </DynamicHeight>
</EmbedContainer>
);
};
export function SyncedWalletUIStates(
props: ComponentProps<typeof WalletUIStatesProvider>,
) {
const setModalConfig = useContext(SetModalConfigCtx);
const locale = useTWLocale();
// update modalConfig on props change
useEffect(() => {
setModalConfig((c) => ({
...c,
title: props.title || locale.connectWallet.defaultModalTitle,
theme: props.theme || "dark",
modalSize: (!canFitWideModal() ? "compact" : props.modalSize) || "wide",
termsOfServiceUrl: props.termsOfServiceUrl,
privacyPolicyUrl: props.privacyPolicyUrl,
welcomeScreen: props.welcomeScreen,
titleIconUrl: props.titleIconUrl,
showThirdwebBranding: props.showThirdwebBranding,
}));
}, [
props.title,
props.theme,
props.modalSize,
props.termsOfServiceUrl,
props.privacyPolicyUrl,
props.welcomeScreen,
props.titleIconUrl,
setModalConfig,
locale.connectWallet.defaultModalTitle,
props.showThirdwebBranding,
]);
return <WalletUIStatesProvider {...props} />;
}
const EmbedContainer = /* @__PURE__ */ StyledDiv(() => {
const theme = useCustomTheme();
return {
color: theme.colors.primaryText,
background: theme.colors.modalBg,
width: "100%",
boxSizing: "border-box",
position: "relative",
lineHeight: "normal",
borderRadius: radius.xl,
border: `1px solid ${theme.colors.borderColor}`,
overflow: "hidden",
fontFamily: theme.fontFamily,
"& *::selection": {
backgroundColor: theme.colors.primaryText,
color: theme.colors.modalBg,
},
};
});