|
| 1 | +--- |
| 2 | +title: Badger React |
| 3 | +icon: react |
| 4 | +ordinal: 6 |
| 5 | +--- |
| 6 | + |
| 7 | +A set of React components and helpers to integrate Bitcoin Cash (BCH) and the Badger wallet into your app with ease. |
| 8 | + |
| 9 | +### Get Started |
| 10 | + |
| 11 | +- [Component Showcase](http://badger-storybook.surge.sh) |
| 12 | +- [NPM page `badger-components-react`](https://www.npmjs.com/package/badger-components-react) |
| 13 | + |
| 14 | +### Install Components |
| 15 | + |
| 16 | +Start by installing the `badger-components-react` library and it's 3 peer depenencies, `react`, `react-dom` and `styled-components` |
| 17 | + |
| 18 | +```bash |
| 19 | +Install library |
| 20 | +$ npm install --save badger-components-react |
| 21 | + |
| 22 | +Install peerDependencies |
| 23 | +$ npm install --save styled-components react react-dom |
| 24 | +``` |
| 25 | + |
| 26 | +### Add to React Application |
| 27 | + |
| 28 | +The library comes with some ready-to-use Badger components, `BadgerBadge` and `BadgerButton`, with more coming soon. These components are can be added quickly to any React projects. If you require a different solution then continue reading to learn how to create custom Badger integrations using `BadgerBase`. |
| 29 | + |
| 30 | +#### Badger Button |
| 31 | + |
| 32 | +Simple Badger Button to display the price in local currency, satoshi amount, and have an optional message. Supports the following props |
| 33 | + |
| 34 | +- `to: BCH Address` - Required - Bitcoin Cash address to send BCH to |
| 35 | +- `price: number` - Required - Price in chosen currency, will be turned into satoshis |
| 36 | +- `currency: string` - Default `USD` - [ISO Country Code](https://en.wikipedia.org/wiki/ISO_4217) to charge in |
| 37 | +- `showSatoshis: boolean` Default `true` - Show the value in BCH Satoshis below the button |
| 38 | +- `border: boolean` - Default `true` - Border around button and text |
| 39 | +- `text: string` - Optional - Text between the button and border |
| 40 | +- `successFn: Function` - Optional -Callback function when payment is successful |
| 41 | +- `failFn: Function` - Optional - Callback function when payment fails or is cancelled |
| 42 | + |
| 43 | +<spacer></spacer> |
| 44 | +<badger-button to="bitcoincash:pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g" price="0.01" currency="CAD" text="Badger Button"></badger-button> |
| 45 | + |
| 46 | +#### Badger Badge |
| 47 | + |
| 48 | +A slightly larger display of payment details. |
| 49 | +Supports the following props. |
| 50 | + |
| 51 | +- `to: BCH Address` - Required - Bitcoin Cash address to send BCH to |
| 52 | +- `price: number` - Required - Price in chosen currency, will be turned into satoshis |
| 53 | +- `currency: string` - Default `USD` - [ISO Country Code](https://en.wikipedia.org/wiki/ISO_4217) to charge in |
| 54 | +- `showSatoshis: boolean` Default `true` - Show the value in BCH Satoshis below the button |
| 55 | +- `showBrand: boolean` Default `true` - Show the branding text |
| 56 | +- `border: boolean` - Default `true` - Border around button and text |
| 57 | +- `text: string` - Default `Payment Total` - Text between the button and border |
| 58 | +- `tag: string` - Default `Badger Pay` - Text on the button |
| 59 | +- `successFn: Function` - Optional - Callback function when payment is successful |
| 60 | +- `failFn: Function` - Optional - Callback function when payment fails or is cancelled |
| 61 | + |
| 62 | +<spacer></spacer> |
| 63 | +<badger-badge to="bitcoincash:pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g" price="0.01" currency="CAD" text="Badger Badger"></badger-button> |
| 64 | + |
| 65 | +#### Code Examples |
| 66 | + |
| 67 | +```js |
| 68 | +import React from 'react' |
| 69 | +import { BadgerButton, BadgerBadge } from 'badger-components-react' |
| 70 | + |
| 71 | +const Example = props => { |
| 72 | + // EatBCH address for example purposes. |
| 73 | + const toAddress = 'bitcoincash:pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g' |
| 74 | + |
| 75 | + return ( |
| 76 | + <> |
| 77 | + {/* Minimal Examples */} |
| 78 | + <BadgerBadge to={toAddress} price={0.5} currency={'USD'} /> |
| 79 | + <BadgerButton to={toAddress} price={1} currency={'JPY'} /> |
| 80 | + |
| 81 | + {/* More Complex Examples */} |
| 82 | + <BadgerBadge |
| 83 | + price={0.01} // Price in currency |
| 84 | + currency={'CAD'} // Currency to convert from |
| 85 | + to={'bitcoincash:pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g'} // Payment address |
| 86 | + tag={'Badger Pay'} // Text on button |
| 87 | + text={'Payment Total'} // Text at top of badge |
| 88 | + showBrand={true} // Show link to badger website |
| 89 | + showSatoshis={true} // Show BCH satoshi amount |
| 90 | + successFn={() => console.log('Payment success callback')} |
| 91 | + failFn={() => console.warn('Payment failed or cancelled callback')} |
| 92 | + /> |
| 93 | + |
| 94 | + <BadgerButton |
| 95 | + price={0.003} |
| 96 | + currency={'USD'} |
| 97 | + to={'bitcoincash:pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g'} |
| 98 | + text={'Badger Pay'} |
| 99 | + showSatoshis={true} |
| 100 | + border={true} |
| 101 | + successFn={() => console.log('success example function called')} |
| 102 | + failFn={() => console.log('fail example function called')} |
| 103 | + /> |
| 104 | + </> |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +export default Example |
| 109 | +``` |
| 110 | + |
| 111 | +### Creating Custom Badger Integrations |
| 112 | + |
| 113 | +The library contains a Higher Order Component (HOC) `BadgerBase` which contains all of the required Badger interaction logic. This allows for any component to integrate with Badger easily. |
| 114 | + |
| 115 | +Components wrapped in `BadgerBase` support the following props |
| 116 | + |
| 117 | +- `to: BCH Address` - Required - Bitcoin Cash address to send BCH to |
| 118 | +- `price: number` - Required - Price in chosen currency, will be turned into satoshis |
| 119 | +- `currency: string` - Default `USD` - [ISO Country Code](https://en.wikipedia.org/wiki/ISO_4217) to charge in |
| 120 | + |
| 121 | +And get the following props added to them for use in custom integration component |
| 122 | + |
| 123 | +- `handleClick: Function` - Call this to start the Badger transaction |
| 124 | +- `step: string` - State of the Badger transaction. One of `fresh`, `pending`, `complete`, `login`, `install` |
| 125 | +- `satoshiDisplay: string` - Transaction value in satoshis, converted from the price and currency every minute |
| 126 | + |
| 127 | +```js |
| 128 | +import React from 'react' |
| 129 | +import { BadgerBase } from 'badger-react-components' |
| 130 | + |
| 131 | +import styled from 'styled-components' |
| 132 | + |
| 133 | +const CoolButton = styled.button` |
| 134 | + background-color: rebeccapurple; |
| 135 | + color: lime; |
| 136 | + border-radius: 24px; |
| 137 | +` |
| 138 | + |
| 139 | +const MyButton extends React.Component { |
| 140 | + render() { |
| 141 | + // Props from higher order component |
| 142 | + const {handleClick, to, price, currency, satoshiDisplay, step} = this.props; |
| 143 | + return ( |
| 144 | + <div> |
| 145 | + <h3>Donate {price}{currency} to {to}</h3> |
| 146 | + <h4>Satoshis: {satoshiDisplay}</h4> |
| 147 | + <CoolButton onClick={handleClick}>Custom looking button with render</CoolButton> |
| 148 | + </div> |
| 149 | + ) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Wrap with BadgerBase higher order component |
| 154 | +export default BadgerBase(MyButton); |
| 155 | +``` |
0 commit comments