Skip to content

Commit bb08034

Browse files
authored
web3-onboard/core:v2.0.9: [enhancement] - Autoselect Disable Modals (#873)
* Add additional options to autoselect * Increment version * Update docs * Update variable name for better readablility
1 parent 8074de7 commit bb08034

10 files changed

+235
-208
lines changed

packages/core/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ if (previouslyConnectedWallets) {
208208
// Connect the most recently connected wallet (first in the array)
209209
await onboard.connectWallet({ autoSelect: previouslyConnectedWallets[0] })
210210

211+
// You can also auto connect "silently" and disable all onboard modals to avoid them flashing on page load
212+
await onboard.connectWallet({
213+
autoSelect: { label: previouslyConnectedWallets[0], disableModals: true }
214+
})
215+
211216
// OR - loop through and initiate connection for all previously connected wallets
212217
// note: This UX might not be great as the user may need to login to each wallet one after the other
213218
// for (walletLabel in previouslyConnectedWallets) {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.0.8",
3+
"version": "2.0.9",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/core/src/connect.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { firstValueFrom } from 'rxjs'
22
import { filter, withLatestFrom, pluck } from 'rxjs/operators'
33
import { state } from './store'
44
import { connectWallet$, wallets$ } from './streams'
5-
import type { ConnectOptions, WalletState } from './types'
5+
import type { ConnectOptions, ConnectOptionsString, WalletState } from './types'
66
import { validateConnectOptions } from './validation'
77

8-
async function connect(options?: ConnectOptions): Promise<WalletState[]> {
8+
async function connect(
9+
options?: ConnectOptions | ConnectOptionsString
10+
): Promise<WalletState[]> {
911
if (options) {
1012
const error = validateConnectOptions(options)
1113
if (error) {
@@ -22,9 +24,17 @@ async function connect(options?: ConnectOptions): Promise<WalletState[]> {
2224
'At least one chain must be set before attempting to connect a wallet'
2325
)
2426

25-
const { autoSelect } = options || { autoSelect: '' }
27+
const { autoSelect } = options || {
28+
autoSelect: { label: '', disableModals: false }
29+
}
2630

27-
connectWallet$.next({ autoSelect, inProgress: true })
31+
connectWallet$.next({
32+
autoSelect:
33+
typeof autoSelect === 'string'
34+
? { label: autoSelect, disableModals: false }
35+
: autoSelect,
36+
inProgress: true
37+
})
2838

2939
const result$ = connectWallet$.pipe(
3040
filter(

packages/core/src/streams.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { resetStore } from './store/actions'
1313
import { state } from './store'
1414

15-
import type { WalletState, InternalState } from './types'
15+
import type { WalletState, InternalState, ConnectOptions } from './types'
1616

1717
export const reset$ = new Subject<void>()
1818
export const disconnectWallet$ = new Subject<WalletState['label']>()
@@ -25,7 +25,7 @@ export const internalState$ = new BehaviorSubject<InternalState>({
2525
})
2626

2727
export const connectWallet$ = new BehaviorSubject<{
28-
autoSelect?: string
28+
autoSelect?: ConnectOptions['autoSelect']
2929
actionRequired?: string
3030
inProgress: boolean
3131
}>({ inProgress: false, actionRequired: '' })

packages/core/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export interface OnboardAPI {
3030
state: typeof state
3131
}
3232
export interface ConnectOptions {
33-
autoSelect?: string // wallet name to autoselect for user
33+
autoSelect?: { label: string; disableModals: boolean }
34+
}
35+
36+
export interface ConnectOptionsString {
37+
autoSelect?: string
3438
}
3539

3640
export interface DisconnectOptions {

packages/core/src/validation.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type {
55
InitOptions,
66
WalletState,
77
ConnectOptions,
8-
DisconnectOptions
8+
DisconnectOptions,
9+
ConnectOptionsString
910
} from './types'
1011

1112
const chainId = Joi.string().pattern(/^0x[0-9a-fA-F]+$/)
@@ -100,7 +101,13 @@ const initOptions = Joi.object({
100101
})
101102

102103
const connectOptions = Joi.object({
103-
autoSelect: Joi.string()
104+
autoSelect: [
105+
Joi.object({
106+
label: Joi.string().required(),
107+
disableModals: Joi.boolean()
108+
}),
109+
Joi.string()
110+
]
104111
})
105112

106113
const disconnectOptions = Joi.object({
@@ -133,7 +140,9 @@ export function validateWalletModule(data: WalletModule): ValidateReturn {
133140
return validate(walletModule, data)
134141
}
135142

136-
export function validateConnectOptions(data: ConnectOptions): ValidateReturn {
143+
export function validateConnectOptions(
144+
data: ConnectOptions | ConnectOptionsString
145+
): ValidateReturn {
137146
return validate(connectOptions, data)
138147
}
139148

packages/core/src/views/connect/ConnectedWallet.svelte

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,17 @@
11
<script lang="ts">
22
import { _ } from 'svelte-i18n'
3-
4-
import { getBalance, getEns } from '../../provider'
5-
import { updateAccount } from '../../store/actions'
6-
import { connectWallet$, internalState$ } from '../../streams'
7-
import { validEnsChain } from '../../utils'
3+
import { internalState$ } from '../../streams'
84
import success from '../../icons/success'
95
import WalletAppBadge from '../shared/WalletAppBadge.svelte'
106
117
import type { WalletState } from '../../types'
128
import defaultAppIcon from '../../icons/default-app-icon'
139
import SuccessStatusIcon from '../shared/SuccessStatusIcon.svelte'
14-
import { state } from '../../store'
1510
import en from '../../i18n/en.json'
1611
1712
export let selectedWallet: WalletState
1813
19-
const { label } = selectedWallet
2014
const { appMetadata } = internalState$.getValue()
21-
22-
async function updateAccountDetails() {
23-
const { accounts, chains: selectedWalletChains } = selectedWallet
24-
const appChains = state.get().chains
25-
const [connectedWalletChain] = selectedWalletChains
26-
27-
const appChain = appChains.find(
28-
({ namespace, id }) =>
29-
namespace === connectedWalletChain.namespace &&
30-
id === connectedWalletChain.id
31-
)
32-
33-
const { address } = accounts[0]
34-
let { balance, ens } = accounts[0]
35-
36-
if (balance === null) {
37-
getBalance(address, appChain).then(balance => {
38-
updateAccount(label, address, {
39-
balance
40-
})
41-
})
42-
}
43-
44-
if (ens === null && validEnsChain(connectedWalletChain.id)) {
45-
getEns(address, appChain).then(ens => {
46-
updateAccount(label, address, {
47-
ens
48-
})
49-
})
50-
}
51-
52-
setTimeout(() => connectWallet$.next({ inProgress: false }), 1500)
53-
}
54-
55-
updateAccountDetails()
5615
</script>
5716

5817
<style>

packages/core/src/views/connect/ConnectingWallet.svelte

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,19 @@
11
<script lang="ts">
22
import { _ } from 'svelte-i18n'
3-
import { createEventDispatcher } from 'svelte'
4-
import { ProviderRpcErrorCode } from '@web3-onboard/common'
5-
6-
import { getChainId, requestAccounts, trackWallet } from '../../provider'
73
import { internalState$ } from '../../streams'
84
import type { WalletState, i18n } from '../../types'
95
106
import WalletAppBadge from '../shared/WalletAppBadge.svelte'
117
import defaultAppIcon from '../../icons/default-app-icon'
128
import en from '../../i18n/en.json'
13-
import { addWallet } from '../../store/actions'
149
10+
export let connectWallet: () => Promise<void>
1511
export let selectedWallet: WalletState
1612
export let deselectWallet: (label: string) => void
17-
export let updateSelectedWallet: (update: Partial<WalletState>) => void
1813
export let setStep: (update: keyof i18n['connect']) => void
19-
20-
let connectionRejected = false
14+
export let connectionRejected: boolean
2115
2216
const { appMetadata } = internalState$.getValue()
23-
24-
const dispatch = createEventDispatcher<{ connectionRejected: boolean }>()
25-
26-
async function connect() {
27-
dispatch('connectionRejected', false)
28-
connectionRejected = false
29-
30-
const { provider, label } = selectedWallet
31-
32-
try {
33-
const [address] = await requestAccounts(provider)
34-
35-
// canceled previous request
36-
if (!address) {
37-
return
38-
}
39-
40-
const chain = await getChainId(provider)
41-
42-
const update: Pick<WalletState, 'accounts' | 'chains'> = {
43-
accounts: [{ address, ens: null, balance: null }],
44-
chains: [{ namespace: 'evm', id: chain }]
45-
}
46-
47-
addWallet({ ...selectedWallet, ...update })
48-
trackWallet(provider, label)
49-
updateSelectedWallet(update)
50-
setStep('connectedWallet')
51-
} catch (error) {
52-
const { code } = error as { code: number; message: string }
53-
54-
// user rejected account access
55-
if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_REJECTED) {
56-
connectionRejected = true
57-
dispatch('connectionRejected', true)
58-
return
59-
}
60-
61-
// account access has already been requested and is awaiting approval
62-
if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_ALREADY_REQUESTED) {
63-
return
64-
}
65-
}
66-
}
67-
68-
connect()
6917
</script>
7018

7119
<style>
@@ -176,7 +124,7 @@
176124
)}
177125
</div>
178126
{#if connectionRejected}
179-
<div class="rejected-cta subtext" on:click={connect}>
127+
<div class="rejected-cta subtext" on:click={connectWallet}>
180128
{$_('connect.connectingWallet.rejectedCTA', {
181129
default: en.connect.connectingWallet.rejectedCTA
182130
})}
@@ -195,7 +143,6 @@
195143
<button
196144
on:click={() => {
197145
deselectWallet(selectedWallet.label)
198-
dispatch('connectionRejected', false)
199146
setStep('selectingWallet')
200147
}}
201148
class="onboard-button-primary"

0 commit comments

Comments
 (0)