![@arcblock/did-connect-react](https://www.arcblock.io/.netlify/functions/badge/?text=did-connect)

> Client side library to work with DID Connect by ArcBlock.

## Usage

```shell
yarn add @arcblock/did-connect-react
```

Then:

```javascript
import axios from 'axios';

// import each module individually
import DidAddress from '@arcblock/did-connect-react/lib/Address';
import DidConnect from '@arcblock/did-connect-react/lib/Connect';
import DidAvatar from '@arcblock/did-connect-react/lib/Avatar';
import DidButton from '@arcblock/did-connect-react/lib/Button';
import DIDLogo from '@arcblock/did-connect-react/lib/Logo';
import { SessionProvider, SessionConsumer } from '@arcblock/did-connect-react/lib/Session';
import SessionManager from '@arcblock/did-connect-react/lib/SessionManager';

// or use ES6 named imports
import { 
  Address as DidAddress, 
  Connect as DidConnect, 
  Avatar as DidAvatar, 
  Button as DidButton, 
  Logo as DIDLogo, 
  Session, 
  SessionManager, 
} from '@arcblock/did-connect-react/lib';
```

### DidConnect

```jsx
<DidConnect
  popup
  open={open}
  action="login"
  checkFn={axios.get}
  onClose={() => handleOnClose()}
  onSuccess={() => (window.location.href = '/profile')}
  messages={{
    title: 'login',
    scan: 'Scan QR code with DID Wallet',
    confirm: 'Confirm login on your DID Wallet',
    success: 'You have successfully signed in!',
  }}
/>
```

**Note**: DidConnect component has a built-in instance of WebWalletSWKeeper that embeds a wallet iframe in the DOM to keep the service worker of the web wallet alive. When the DidConnect component is destroyed, WebWalletSWKeeper will also be destroyed, so avoid using it like the following:  

```jsx
{open && (
  <DidConnect
    popup
    action="login"
    ...
  />
)}
```

### display DidConnect in popup

```jsx
const [open, setOpen] = React.useState(false);
const handleOnClose = () => {
  // ...
  setOpen(false);
};

...

<button type="button" onClick={() => setOpen(true)}>
  Open DidConnect
</button>

<DidConnect
  popup
  open={open}
  action="login"
  checkFn={axios.get}
  onClose={() => handleOnClose()}
  onSuccess={() => (window.location.href = '/profile')}
  messages={{
    title: 'login',
    scan: 'Scan QR code with DID Wallet',
    confirm: 'Confirm login on your DID Wallet',
    success: 'You have successfully signed in!',
  }}
  webWalletUrl={webWalletUrl}
/>
```

### DidAvatar

```jsx
<DidAvatar did={userDid} size={256} />
```

### DidButton

```jsx
<ConnectButton size="large" />
<ConnectButton size="medium" />
<ConnectButton size="small" />
<ConnectButton>Custom Text</ConnectButton>
```

### DIDLogo

```jsx
<DIDLogo size={32} />
```

### DidAddress

```jsx
<DidAddress did={userDid} size={32} />
```

### SessionManager

```jsx
<SessionProvider serviceHost={get(window, 'blocklet.prefix', '/')} webWalletUrl={webWalletUrl}>
  <SessionConsumer>{(value) => <SessionManager session={value.session} />}</SessionConsumer>
  ...
</SessionProvider>
```
