Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bc-pay-button web component #157

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ _Use another popular framework? please let us know or feel free to create a PR f
Bitcoin Connect exposes the following web components for allowing users to connect their desired Lightning wallet:

- `<bc-button/>` - launches the Bitcoin Connect Modal on click
- Arguments:
- `title` - (optional) change the title of the button
- `<bc-pay-button/>` - launches the Bitcoin Connect Payment Modal on click
- Arguments:
- `invoice` - BOLT11 invoice
rolznz marked this conversation as resolved.
Show resolved Hide resolved
- `title` - (optional) change the title of the button
- `preimage` - (optional) set this if you received an external payment
- - Events:
- `bc:onpaid` **Experimental** - fires event with WebLN payment response in `event.detail` (contains `preimage`)
- `<bc-connect/>` - render connect wallet UI without modal
- `<bc-payment/>` - render a payment request UI without modal
- Arguments:
Expand Down Expand Up @@ -565,6 +574,7 @@ This project is powered by Lit.
See [Get started](https://lit.dev/docs/getting-started/) on the Lit site for more information.

## BOLT FUN

Bitcoin Connect is a BOLT FUN Legends of Lightning vol.2 finalist. [Follow our project and journey](https://bolt.fun/project/bitcoin-connect).

## License
Expand Down
5 changes: 5 additions & 0 deletions dev/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
document
.getElementById('send-payment')
.setAttribute('invoice', invoice.paymentRequest);
document
.getElementById('pay-button')
.setAttribute('invoice', invoice.paymentRequest);
document
.getElementById('payment-flow')
.setAttribute('invoice', invoice.paymentRequest);
Expand Down Expand Up @@ -200,6 +203,8 @@ <h2>Payment Flow</h2>
<h1>Components</h1>
<h2>Button</h2>
<bc-button></bc-button>
<h2>Pay Button</h2>
<bc-pay-button id="pay-button"></bc-pay-button>

<h2>Modal header</h2>
<bc-modal-header show-help>Example Title</bc-modal-header>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/bitcoin-connect",
"version": "3.0.0",
"version": "3.1.0-beta.0",
"description": "Web components to connect to a lightning wallet and power a website with WebLN",
"type": "module",
"source": "src/index.ts",
Expand Down
67 changes: 67 additions & 0 deletions src/components/bc-pay-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {PropertyValues, html} from 'lit';
import {customElement, property} from 'lit/decorators.js';
import {BitcoinConnectElement} from './BitcoinConnectElement.js';
import {bcIcon} from './icons/bcIcon.js';
import {withTwind} from './twind/withTwind.js';
import {loadingIcon} from './icons/loadingIcon.js';
import {launchPaymentModal} from '../api.js';
import './bc-balance.js';
import {SendPaymentResponse} from '@webbtc/webln-types';

/**
* A button that when clicked launches a modal to pay an invoice.
*/
@customElement('bc-pay-button')
export class PayButton extends withTwind()(BitcoinConnectElement) {
@property()
override title = 'Pay Now';

@property()
invoice?: string;

@property({})
preimage?: string;

private _setPaid?: (response: SendPaymentResponse) => void;

protected override updated(changedProperties: PropertyValues): void {
super.updated(changedProperties);

if (changedProperties.has('preimage') && this.preimage) {
this._setPaid?.({
preimage: this.preimage,
});
}
}

override render() {
const isLoading = this._connecting || (!this._connected && this._modalOpen);

return html` <div class="inline-flex" @click=${this._onClick}>
<bci-button variant="primary">
${isLoading
? html`<span class="ml-1 mr-1">${loadingIcon}</span>`
: html`<span class="-ml-0.5">${bcIcon}</span>`}
<span class="font-semibold">
${isLoading ? html`Waiting...` : html`${this.title}`}
</span>
</bci-button>
</div>`;
}

private _onClick() {
if (!this.invoice) {
throw new Error('No invoice');
}
const {setPaid} = launchPaymentModal({
invoice: this.invoice,
});
this._setPaid = setPaid;
}
}

declare global {
interface HTMLElementTagNameMap {
'bc-pay-button': PayButton;
}
}
1 change: 1 addition & 0 deletions src/components/flows/bc-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class SendPaymentFlow extends withTwind()(BitcoinConnectElement) {
})
invoice?: string;

// TODO: change to preimage
@property({
type: Boolean,
})
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {LnbitsWebLNProvider} from './connectors/LnbitsConnector';
import './state/boot';

export * from './components/bc-button';
export * from './components/bc-pay-button';
export * from './components/bc-modal';
export * from './components/bc-connector-list';
export * from './components/pages/bc-send-payment';
Expand Down