Skip to content

Commit 2863286

Browse files
authored
feat: <AppKitProvider> (#4696)
1 parent 8173296 commit 2863286

7 files changed

Lines changed: 675 additions & 25 deletions

File tree

.changeset/crazy-jobs-run.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
'@reown/appkit': patch
3+
'@reown/appkit-adapter-bitcoin': patch
4+
'@reown/appkit-adapter-ethers': patch
5+
'@reown/appkit-adapter-ethers5': patch
6+
'@reown/appkit-adapter-solana': patch
7+
'@reown/appkit-adapter-wagmi': patch
8+
'@reown/appkit-utils': patch
9+
'@reown/appkit-cdn': patch
10+
'@reown/appkit-cli': patch
11+
'@reown/appkit-codemod': patch
12+
'@reown/appkit-common': patch
13+
'@reown/appkit-controllers': patch
14+
'@reown/appkit-core': patch
15+
'@reown/appkit-experimental': patch
16+
'@reown/appkit-pay': patch
17+
'@reown/appkit-polyfills': patch
18+
'@reown/appkit-scaffold-ui': patch
19+
'@reown/appkit-siwe': patch
20+
'@reown/appkit-siwx': patch
21+
'@reown/appkit-testing': patch
22+
'@reown/appkit-ui': patch
23+
'@reown/appkit-wallet': patch
24+
'@reown/appkit-wallet-button': patch
25+
---
26+
27+
Introduced `AppKitProvider` React component for easy AppKit integration in React apps
28+
29+
**Example usage**
30+
31+
```tsx
32+
import { AppKitProvider } from '@reown/appkit/react';
33+
34+
function App() {
35+
return (
36+
<AppKitProvider projectId="YOUR_PROJECT_ID" networks={[/* Your Networks */]}>
37+
{/* Your App */}
38+
</AppKitProvider>
39+
);
40+
}
41+
```

packages/appkit/exports/react.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,6 @@ export {
7777
AppKitConnectButton,
7878
AppKitAccountButton
7979
} from '../src/library/react/components.js'
80+
81+
export { AppKitProvider } from '../src/library/react/providers.js'
82+
export type { AppKitProviderProps } from '../src/library/react/providers.js'

packages/appkit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@
137137
"dependencies": {
138138
"@reown/appkit-common": "workspace:*",
139139
"@reown/appkit-controllers": "workspace:*",
140+
"@reown/appkit-pay": "workspace:*",
140141
"@reown/appkit-polyfills": "workspace:*",
141142
"@reown/appkit-scaffold-ui": "workspace:*",
142143
"@reown/appkit-ui": "workspace:*",
143144
"@reown/appkit-utils": "workspace:*",
144145
"@reown/appkit-wallet": "workspace:*",
145-
"@reown/appkit-pay": "workspace:*",
146146
"@walletconnect/types": "2.21.5",
147147
"@walletconnect/universal-provider": "2.21.5",
148148
"bs58": "6.0.0",
@@ -152,6 +152,7 @@
152152
},
153153
"devDependencies": {
154154
"@reown/appkit-siwe": "workspace:*",
155+
"@testing-library/react": "16.3.0",
155156
"@types/react": "19.1.3",
156157
"@types/react-dom": "19.1.3",
157158
"@vitest/coverage-v8": "2.1.9",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { type ReactNode } from 'react'
2+
3+
import { type CreateAppKit, createAppKit } from '../../../exports/react.js'
4+
5+
// -- Types --------------------------------------------------------------------------------
6+
export type AppKitProviderProps = CreateAppKit & {
7+
children: ReactNode
8+
}
9+
10+
// -- State --------------------------------------------------------------------------------
11+
let appkit: ReturnType<typeof createAppKit> | null = null
12+
13+
// -- Utils & Others -----------------------------------------------------------------------
14+
export function memoizeCreateAppKit(config: CreateAppKit) {
15+
if (!appkit) {
16+
appkit = createAppKit(config)
17+
}
18+
19+
return appkit
20+
}
21+
22+
// -- Providers ----------------------------------------------------------------------------
23+
export function AppKitProvider({ children, ...props }: AppKitProviderProps) {
24+
memoizeCreateAppKit(props)
25+
26+
return children
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createElement } from 'react'
2+
3+
import { render } from '@testing-library/react'
4+
import { beforeEach, describe, expect, it, vi } from 'vitest'
5+
6+
import { AppKitProvider, type AppKitProviderProps } from '../../src/library/react/providers'
7+
8+
let mod: typeof import('../../exports/react')
9+
10+
describe('AppKitProvider', () => {
11+
beforeEach(async () => {
12+
vi.clearAllMocks()
13+
mod = await import('../../exports/react')
14+
vi.spyOn(mod, 'createAppKit')
15+
})
16+
17+
it('should call createAppKit with the correct parameters', () => {
18+
const props = {
19+
projectId: 'test',
20+
networks: [],
21+
children: 'child'
22+
} as unknown as AppKitProviderProps
23+
24+
const { rerender } = render(
25+
createElement(AppKitProvider, props, createElement('div', null, 'child'))
26+
)
27+
28+
delete props.children
29+
30+
expect(mod.createAppKit).toHaveBeenCalledTimes(1)
31+
expect(mod.createAppKit).toHaveBeenCalledWith(props)
32+
33+
rerender(createElement('div', null, 'child'))
34+
35+
expect(mod.createAppKit).toHaveBeenCalledTimes(1)
36+
})
37+
})

packages/appkit/vitest.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { defineProject } from 'vitest/config'
44
export default defineProject({
55
test: {
66
globals: true,
7-
environmentMatchGlobs: [['**/vue.test.ts', 'jsdom']],
7+
environmentMatchGlobs: [
8+
['**/vue.test.ts', 'jsdom'],
9+
['**/react.test.ts', 'jsdom']
10+
],
811

912
// @ts-ignore
1013
coverage: {

0 commit comments

Comments
 (0)