Frameworks Lit

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

  1. 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
    
  2. 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' },
          },
        }),
      ],
    })
    
  3. 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).

  4. 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.html with:

    <popup-app></popup-app>
    <script type="module" src="/src/popup-app.ts"></script>
    
  5. Test a purchase. Run npm run dev, load the dist folder 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-extension with the Vanilla template, run npm install lit @extensionbill/sdk, then follow the WXT setup without a modules entry. In entrypoints/popup/, put popup-app.ts and an index.html whose 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