@xrpl-wallet-kit/react 0.1.0

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.
@@ -0,0 +1,34 @@
1
+ import React from "react";
2
+ import type { WalletAccount, WalletCapabilities, WalletManager, WalletMetadata, WalletSession } from "@xrpl-wallet-kit/core";
3
+ import type { WalletButtonOptions, WalletModal, WalletUiConfig } from "@xrpl-wallet-kit/ui";
4
+ export type WalletKitStatus = "disconnected" | "connecting" | "connected";
5
+ export interface WalletKitContextValue {
6
+ manager: WalletManager;
7
+ account: WalletAccount | null;
8
+ session: WalletSession | null;
9
+ status: WalletKitStatus;
10
+ wallets: WalletMetadata[];
11
+ connect: (adapterId: string) => Promise<WalletSession>;
12
+ disconnect: () => Promise<void>;
13
+ openModal: () => void;
14
+ closeModal: () => void;
15
+ modal: WalletModal;
16
+ }
17
+ export interface WalletKitProviderProps {
18
+ manager: WalletManager;
19
+ children: React.ReactNode;
20
+ ui?: WalletUiConfig;
21
+ }
22
+ export type ReactWalletButtonProps = Partial<Omit<WalletButtonOptions, "manager" | "modal" | "target">>;
23
+ export declare function WalletKitProvider(props: WalletKitProviderProps): import("react/jsx-runtime").JSX.Element | null;
24
+ export declare function useWalletKit(): WalletKitContextValue;
25
+ export declare function useWalletSession(): WalletSession | null;
26
+ export declare function useWalletAccount(): WalletAccount | null;
27
+ export declare function useWalletStatus(): WalletKitStatus;
28
+ export declare function useWalletCapabilities(): WalletCapabilities | undefined;
29
+ export declare function WalletButton(props: ReactWalletButtonProps): import("react/jsx-runtime").JSX.Element;
30
+ export type XrplWalletContextValue = WalletKitContextValue;
31
+ export type XrplWalletProviderProps = WalletKitProviderProps;
32
+ export declare const XrplWalletProvider: typeof WalletKitProvider;
33
+ export declare const useXrplWallet: typeof useWalletKit;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":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eAAe,IAAI,eAAe,CAEjD;AAED,wBAAgB,qBAAqB,IAAI,kBAAkB,GAAG,SAAS,CAGtE;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,sBAAsB,2CAwBzD;AAED,MAAM,MAAM,sBAAsB,GAAG,qBAAqB,CAAC;AAC3D,MAAM,MAAM,uBAAuB,GAAG,sBAAsB,CAAC;AAC7D,eAAO,MAAM,kBAAkB,0BAAoB,CAAC;AACpD,eAAO,MAAM,aAAa,qBAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,133 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import { createWalletButton, createWalletModal } from "@xrpl-wallet-kit/ui";
4
+ const WalletKitContext = createContext(null);
5
+ const useClientLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
6
+ export function WalletKitProvider(props) {
7
+ const [session, setSession] = useState(props.manager.getSession());
8
+ const [status, setStatus] = useState(props.manager.getSession() ? "connected" : "disconnected");
9
+ const [modal, setModal] = useState(null);
10
+ const modalRef = useRef(null);
11
+ useClientLayoutEffect(() => {
12
+ if (typeof document === "undefined")
13
+ return;
14
+ const nextModal = createWalletModal({ manager: props.manager, ...props.ui });
15
+ modalRef.current = nextModal;
16
+ setModal(nextModal);
17
+ return () => {
18
+ nextModal.destroy();
19
+ if (modalRef.current === nextModal)
20
+ modalRef.current = null;
21
+ setModal(null);
22
+ };
23
+ }, [props.manager]);
24
+ useEffect(() => {
25
+ modalRef.current?.updateOptions(props.ui ?? {});
26
+ }, [props.ui]);
27
+ useEffect(() => {
28
+ const syncSession = () => setSession(props.manager.getSession());
29
+ const offConnecting = props.manager.on("connecting", () => setStatus("connecting"));
30
+ const offConnected = props.manager.on("connected", (event) => {
31
+ setSession(event.session ?? null);
32
+ setStatus("connected");
33
+ });
34
+ const offDisconnected = props.manager.on("disconnected", () => {
35
+ setSession(null);
36
+ setStatus("disconnected");
37
+ });
38
+ const offRestored = props.manager.on("session_restored", (event) => {
39
+ setSession(event.session);
40
+ setStatus("connected");
41
+ });
42
+ const offAccountChanged = props.manager.on("accountChanged", syncSession);
43
+ const offNetworkChanged = props.manager.on("networkChanged", syncSession);
44
+ const offStale = props.manager.on("session_stale", () => {
45
+ if (!props.manager.getSession())
46
+ setStatus("disconnected");
47
+ syncSession();
48
+ });
49
+ const offExpired = props.manager.on("session_expired", () => {
50
+ setSession(null);
51
+ setStatus("disconnected");
52
+ });
53
+ const offError = props.manager.on("error", () => {
54
+ if (!props.manager.getSession())
55
+ setStatus("disconnected");
56
+ });
57
+ void props.manager.autoReconnect();
58
+ return () => {
59
+ offConnecting();
60
+ offConnected();
61
+ offDisconnected();
62
+ offRestored();
63
+ offAccountChanged();
64
+ offNetworkChanged();
65
+ offStale();
66
+ offExpired();
67
+ offError();
68
+ };
69
+ }, [props.manager]);
70
+ const value = useMemo(() => {
71
+ if (!modal)
72
+ return null;
73
+ return {
74
+ manager: props.manager,
75
+ account: session?.account ?? null,
76
+ session,
77
+ status,
78
+ wallets: props.manager.getWallets(),
79
+ connect: (adapterId) => props.manager.connect(adapterId),
80
+ disconnect: () => props.manager.disconnect(),
81
+ openModal: () => modal.open(),
82
+ closeModal: () => modal.close(),
83
+ modal
84
+ };
85
+ }, [props.manager, session, status, modal]);
86
+ if (!value)
87
+ return null;
88
+ return _jsx(WalletKitContext.Provider, { value: value, children: props.children });
89
+ }
90
+ export function useWalletKit() {
91
+ const value = useContext(WalletKitContext);
92
+ if (!value)
93
+ throw new Error("useWalletKit must be used inside WalletKitProvider");
94
+ return value;
95
+ }
96
+ export function useWalletSession() {
97
+ return useWalletKit().session;
98
+ }
99
+ export function useWalletAccount() {
100
+ return useWalletKit().account;
101
+ }
102
+ export function useWalletStatus() {
103
+ return useWalletKit().status;
104
+ }
105
+ export function useWalletCapabilities() {
106
+ const { manager, session } = useWalletKit();
107
+ return session ? manager.getCapabilities(session.adapterId) : undefined;
108
+ }
109
+ export function WalletButton(props) {
110
+ const { manager, modal } = useWalletKit();
111
+ const targetRef = useRef(null);
112
+ const buttonRef = useRef(null);
113
+ useEffect(() => {
114
+ if (!targetRef.current)
115
+ return;
116
+ buttonRef.current = createWalletButton({
117
+ manager,
118
+ modal,
119
+ target: targetRef.current,
120
+ ...props
121
+ });
122
+ return () => {
123
+ buttonRef.current?.destroy();
124
+ buttonRef.current = null;
125
+ };
126
+ }, [manager, modal]);
127
+ useEffect(() => {
128
+ buttonRef.current?.updateOptions(props);
129
+ }, [props]);
130
+ return _jsx("span", { ref: targetRef });
131
+ }
132
+ export const XrplWalletProvider = WalletKitProvider;
133
+ export const useXrplWallet = useWalletKit;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@xrpl-wallet-kit/react",
3
+ "version": "0.1.0",
4
+ "description": "React bindings and hooks for XRPL Wallet Kit.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.json"
21
+ },
22
+ "dependencies": {
23
+ "@xrpl-wallet-kit/core": "0.1.0",
24
+ "@xrpl-wallet-kit/ui": "0.1.0"
25
+ },
26
+ "peerDependencies": {
27
+ "react": ">=18"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/XRPDomains/xrpl-wallet-kit.git"
35
+ },
36
+ "homepage": "https://github.com/XRPDomains/xrpl-wallet-kit#readme",
37
+ "bugs": {
38
+ "url": "https://github.com/XRPDomains/xrpl-wallet-kit/issues"
39
+ },
40
+ "keywords": [
41
+ "xrpl",
42
+ "wallet",
43
+ "wallet-adapter",
44
+ "wallet-kit",
45
+ "xrp-ledger",
46
+ "web3"
47
+ ]
48
+ }