Lit
Gate features in a LitElement extension popup with the UserController reactive controller from @extensionbill/sdk/lit.
UserController from @extensionbill/sdk/lit is a Lit reactive controller: add it to your element and it re-renders the element whenever the user’s access changes. Shared setup: Pick your stack.
Quick start
-
Create the project.
npm create vite@latest my-extension -- --template lit-ts cd my-extension npm install @extensionbill/sdk npm install -D @crxjs/vite-plugin -
Add the manifest. Create
vite.config.ts; CRXJS builds every file the manifest references:// vite.config.ts import { defineConfig } from 'vite' import { crx } from '@crxjs/vite-plugin' export default defineConfig({ plugins: [ crx({ manifest: { manifest_version: 3, name: 'My Extension', version: '1.0.0', permissions: ['storage', 'alarms'], background: { service_worker: 'src/background.ts', type: 'module' }, action: { default_popup: 'index.html' }, }, }), ], }) -
Create the client and the background.
# .env.development VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_test_…// src/bill.ts import { ExtensionBill } from '@extensionbill/sdk' export const bill = ExtensionBill({ publishableKey: import.meta.env.VITE_EXTENSIONBILL_PUBLISHABLE_KEY, })// src/background.ts import { bill } from './bill' bill.startBackground()Keep
startBackground()at the top level (why). -
Gate your popup. Add
src/popup-app.ts:// src/popup-app.ts import { LitElement, html } from 'lit' import { UserController } from '@extensionbill/sdk/lit' import { bill } from './bill' export class PopupApp extends LitElement { private account = new UserController(this, bill) render() { if (this.account.loading) return html`<p>Loading…</p>` if (this.account.hasAccess('pro')) { return html` <p>Pro feature unlocked.</p> <button @click=${() => bill.openPortal()}>Manage subscription</button> ` } return html` <button @click=${() => bill.openCheckout()}>Upgrade to Pro</button> <button @click=${() => bill.openRestore()}>Already paid? Restore</button> ` } } customElements.define('popup-app', PopupApp)Replace the body of
index.htmlwith:<popup-app></popup-app> <script type="module" src="/src/popup-app.ts"></script> -
Test a purchase. Run
npm run dev, load thedistfolder unpacked and buy Pro with a sandbox buyer (steps).
Advanced
Drop-in paywall
Swap your upgrade buttons for the ready-made Paywall UI. Mount it with Lit’s ref directive, which calls back with the element on render and undefined on removal:
// src/popup-app.ts (additions)
import { ref } from 'lit/directives/ref.js'
import { mountPaywall } from '@extensionbill/sdk/paywall'
// inside PopupApp
private unmountPaywall?: () => void
private paywallRef = (el?: Element) => {
this.unmountPaywall?.()
this.unmountPaywall = el
? mountPaywall(el as HTMLElement, bill, { title: 'Upgrade to Pro', theme: 'auto' })
: undefined
}
// in render(), instead of the upgrade buttons
return html`<div ${ref(this.paywallRef)}></div>`
Every option: Paywall UI.
What UserController exposes
private account = new UserController(this, bill)
account.user, account.loading and account.error are getters that always read the latest state, plus account.hasAccess(feature?) and account.refresh() (meaning of each field). The controller subscribes in hostConnected, unsubscribes in hostDisconnected, and calls requestUpdate() on every change. It declares Lit’s controller interfaces itself and never imports lit, so it works with any Lit version.
Keys per build
npm run build reads .env.production: put VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_live_… there. Live keys work after going live.
Other build tools
Lit needs a bundler to resolve its imports, so use Vite + CRXJS (above) or WXT:
- WXT:
npx wxt@latest init my-extensionwith the Vanilla template, runnpm install lit @extensionbill/sdk, then follow the WXT setup without amodulesentry. Inentrypoints/popup/, putpopup-app.tsand anindex.htmlwhose body is<popup-app></popup-app>and<script type="module" src="./popup-app.ts"></script>.
Troubleshooting
- The element never updates: create the controller as a class field or in the constructor, so Lit calls
hostConnected.
Everything else: shared troubleshooting.
Next steps
- Checking access in content scripts, the options page and the service worker
- Paywall UI labels and theming
- Free trials
- Going live checklist