Integrating the UI SDK into your application

This page guides you through the steps required to integrate the UI SDK into your existing web application.

Install CSPR.click dependencies

Run the following command in a terminal window to install CSPR.click libraries:

npm install --save-dev @make-software/csprclick-ui @make-software/csprclick-core-client @make-software/csprclick-core-types

If you're using Typescript, the command above also installs type definitions for CSPR.click.

ClickProvider context provider

First, define the initialization options for the CSPR.click library:

import { CONTENT_MODE } from '@make-software/csprclick-core-types';

const clickOptions: CsprClickInitOptions = {
    appName: 'Casper dApp',
    appId: 'csprclick-template',
    contentMode: CONTENT_MODE.IFRAME,
    providers: [
        'casper-wallet',
        'ledger',
        'casper-signer',
    ]
};

You can use the default csprclick-template application identifier while you're working locally on your application. But to upload your new project to a server, you'll need to get your own application id.

Update the properties in the example according to your needs. Read more about the CsprClickInitOptions type in the Core JS SDK reference.

Next, wrap your main application component with the <ClickProvider> context provider:

<ClickProvider options={clickOptions}>
  <App />
</ClickProvider>

This component will download the Core JS SDK and initialize it with the options defined.

Add <ClickTopBar> component to your app

All the CSPR.click UI elements are managed from the <ClickTopBar> component. This component must be added to the very beginning of your main UI component and it's responsible for displaying the top bar and all the modal windows and pop-ups needed for connecting with wallets, showing information to the user, etc.

<ClickTopBar
  onThemeSwitch={toggleTheme}
  themeMode={themeMode}
  accountMenuItems={accountMenuItems}
  languageSettings={languageSettings}
  currencySettings={currencySettings}
  networkSettings={networkSettings}
/>

Refer to the Customizing the top bar section in this guide for complete reference on how to work with each of the settings added as props to this component.

Add CSPR.click styles

Option 1: your application uses styled-components

When your application already uses the <ThemeProvider> component from styled-components library, you need to add CSPR.click styles to your themes.

Considering as an example that your application has light and dark themes, you may merge our styles into your themes like this:

import { CsprClickThemes } from '@make-software/csprclick-react';

const YourAppThemes = {
	dark: {
		...CsprClickThemes.dark,
		// your styles for dark theme here
	},
	light: {
		...CsprClickThemes.light,
		// your styles for light theme here
	},
};

Option 2: your application doesn't use styled-components

Since CSPR.click depends on styled-components library, add it to your dependencies by running the command:

npm install --save styled-components

Now, add the theme provider to your application. It must wrap at least the <ClickTopBar> component you added in the previous step:

import { CsprClickThemes } from '@make-software/csprclick-react';

<ThemeProvider theme={CsprClickThemes['dark']}>
  <ClickTopBar
    ...
  />
</ThemeProvider>

Note you can choose between two themes: light and dark.

Bind callbacks to respond to connection events

In your application, you'll need to listen and respond to some events triggered when the user connects an account, switches to a different one, or closes the session.

Refer to the Handling events page for information on how to listen to these events.

Request transaction approvals

At some point, your application will need to interact with the Casper network by sending a transaction (aka deploy). CSPR.click manages this process communicating with the active wallet to request the user to approve or reject the transaction. The UI depends on the wallet.

Refer to the Signing transactions page for information on how to request the user a transaction approval.

Last updated