# The framework bindings

> The React, Next.js and Vue bindings for pontkit's web components: Provider, hooks, widgets and their events.

Source: https://www.pontive.com/docs/sdk/bindings

The bindings wrap `@pontive/pontkit-core`'s elements (`pont-*`), which the loader fetches from the
CDN at the release the platform decides.

| Package | What |
|---|---|
| `@pontive/pontkit-react` | `Provider`, the hooks and control components, the widget wrappers |
| `@pontive/pontkit-nextjs` | `.` re-exports `@pontive/pontkit-react`; `./server` is the auth proxy and server sessions |
| `@pontive/pontkit-vue` | the loader plugin, `isPontElement`, `PONT_ELEMENTS` and the element types |
| `@pontive/pontkit-loader` | resolves the release (`/v1/frontend/runtime-config`), loads its module and stylesheet with SRI |

Core never imports the auth SDK: `<pont-provider>` is given one (`sdk`). The React `Provider`
injects `Auth` from `@pontive/oidc-client` (a dependency of `@pontive/pontkit-react`); Vue and
plain pages set it themselves.

## React [#react]

```tsx
import {
  Provider, SignedIn, SignedOut, Signin, AccountMenu,
  OrganizationSwitcher, useAuthState,
} from "@pontive/pontkit-react";

export function App() {
  return (
    <Provider
      appId="app_123"
      domain="/__pontive"                       // the auth proxy, or the auth hostname
      apiBaseUrl="https://api.us.pontive.com"   // the account widgets' gateway
      signinUrl="/sign-in"
      signinRedirectUrl="/app"
      onRedirect={(e) => { e.preventDefault(); router.push(e.detail.url); }}
    >
      <SignedOut><Signin fallback={<Spinner />} /></SignedOut>
      <SignedIn>
        <OrganizationSwitcher onOrganizationChanged={(e) => reload(e.detail.organizationId)} />
        <AccountMenu accountSettingsUrl="/account" />
      </SignedIn>
    </Provider>
  );
}
```

### `Provider` [#provider]

Renders `<pont-provider>` and does the one thing the element can't: it **injects the SDK**.
By default that is `Auth` from `@pontive/oidc-client`, installed with this package, so an app
that imports the same package shares one instance. Pass `sdk` to use another client.

Its props are the element's inputs, as properties: `appId`, `domain` (required), `locales`,
`signinUrl`, `signupUrl`, `invitationUrl`, `signinRedirectUrl`, `continueHereUrl`, `scope`,
`organizationId`, `apiBaseUrl`, `loginHint`, `autoRedirect`, `theme`, `headers`,
`accountClient`, `config` (your own SDK callbacks, chained after the provider's; never call
`Auth.configure()` yourself), `sdk`. See pontkit-core `docs/kit/provider.md`.

| Hook / component | |
|---|---|
| `useAuthState()` | `{ isAuthenticated, resolved }`, from `pont-auth-changed` (seeded from `.auth`) |
| `useSession()` | the last `pont-session-changed` (`{ reason, source, isAuthenticated, organizationId }`), or null |
| `useSdk()` | the injected SDK |
| `useAccountClient()` | the account client the widgets use (`effectiveAccountClient`), or undefined until the SDK is configured; for the calls an app makes beside them, such as `listBillingAccounts()` and `listMyContracts()` to choose between `Checkout` and `BillingPortal` |
| `SignedIn`, `SignedOut`, `AuthResolved` | branch on `useAuthState()`; nothing until `resolved` (or `fallback`) |
| `Authenticated` | children for a signed-in user; others go to `redirectUrl` or the provider's `signinUrl` (`onRedirect` for a router) |

These are React components, not wrappers over `<pont-signed-in>` / `<pont-signed-out>`:
those detach their children, which React must keep owning.

### Widgets and their events [#widgets-and-their-events]

`Signin`, `Signup`, `Invitation`, `Consent`, `AccountMenu`, `OrganizationSwitcher`,
`AccountSettings`, `MemberManagement`, `Checkout`, `BillingPortal`. The two billing widgets are
beta: they need the instance's `billing` capability, the `pontive:billing:read` and
`pontive:billing:pay` scopes and refresh tokens (pontkit-core `docs/kit/checkout.md` and
`docs/kit/billing-portal.md`). Props are assigned as element properties once the
element is defined. `fallback` (a placeholder until the first screen) is offered only by the
flow widgets (`Signin`, `Signup`, `Invitation`), the elements that honour `[part="fallback"]`.

Every `pont-*` event an element fires is a callback prop: drop `pont-`, camel-case, prefix
`on`. **The callback gets the event itself**, so `event.detail` is the payload and a cancelable
event can be cancelled with `event.preventDefault()`.

| Element | Event | Prop | Cancel it to |
|---|---|---|---|
| provider | `pont-auth-changed` | `onAuthChanged` | |
| | `pont-locale-changed` | `onLocaleChanged` | |
| | `pont-session-changed` | `onSessionChanged` | |
| | `pont-redirect` | `onRedirect` | navigate with your router |
| signin, signup, invitation | `pont-flow-response` | `onFlowResponse` | |
| account menu | `pont-account-settings` | `onAccountSettings` | navigate yourself |
| | `pont-logout` | `onLogout` | sign out yourself |
| | `pont-menu-item-click` | `onMenuItemClick` | stop the item's own action |
| | `pont-open-change` | `onOpenChange` | keep the menu as it is |
| organization switcher | `pont-organization-changed` | `onOrganizationChanged` | |
| | `pont-organization-switch-failed` | `onOrganizationSwitchFailed` | |
| | `pont-open-change` | `onOpenChange` | |
| account settings | `pont-profile-updated` | `onProfileUpdated` | |
| | `pont-authorized-app-revoked` | `onAuthorizedAppRevoked` | |
| | `pont-open-change` | `onOpenChange` | |
| member management | `pont-members-invited` | `onMembersInvited` | |
| | `pont-member-roles-updated` | `onMemberRolesUpdated` | |
| | `pont-member-removed` | `onMemberRemoved` | |
| | `pont-invitation-resent` | `onInvitationResent` | |
| | `pont-invitation-revoked` | `onInvitationRevoked` | |
| checkout | `pont-checkout-completed` | `onCheckoutCompleted` | stay on the page or route yourself |
| | `pont-checkout-cancelled` | `onCheckoutCancelled` | handle Cancel yourself |
| billing portal | `pont-billing-contract-changed` | `onBillingContractChanged` | |
| | `pont-billing-contract-cancelled` | `onBillingContractCancelled` | |
| | `pont-billing-cancellation-revoked` | `onBillingCancellationRevoked` | |
| | `pont-billing-invoice-paid` | `onBillingInvoicePaid` | |
| | `pont-billing-payment-method-added` | `onBillingPaymentMethodAdded` | |
| | `pont-billing-payment-method-default-changed` | `onBillingPaymentMethodDefaultChanged` | |
| | `pont-billing-payment-method-removed` | `onBillingPaymentMethodRemoved` | |

Note the switcher's prop is `onOrganizationSwitchFailed` (the event's name), called with the
event. The element also has an `onSwitchFailed` / `onOrganizationChanged` *property* taking
the detail; the binding listens for the events instead and never sets those properties.

## Next.js [#nextjs]

`@pontive/pontkit-nextjs` re-exports `@pontive/pontkit-react`. The components use hooks, so mount them from a client component (`"use client"`). They import and
server-render safely: the tags go out empty and upgrade when the bundle loads.

`domain="/__pontive"` goes through the auth proxy (`createAuthProxy`), which forwards
`/v1/frontend`, `/v1/me` and `/oauth2`, so the provider's `runtime-config`, `widget-messages`
and `theme` reads, and the signed-in user's own `/v1/me` calls (profile, sessions,
memberships, billing), go through it too. **Organization management does not**: `apiBaseUrl`
must be the absolute regional API URL (`https://api.us.pontive.com`, whose routes are under
`/v1`; the default account client refuses anything else), read for example from a
`NEXT_PUBLIC_` variable.

## Vue [#vue]

Vue needs no wrappers: it mounts custom elements and passes properties and listens for events
itself. Tell the compiler to leave the tags alone, and set the provider's SDK:

```ts
import { createApp } from "vue";
import PontKit, { isPontElement } from "@pontive/pontkit-vue";

app.config.compilerOptions.isCustomElement = isPontElement; // every pont-* tag
app.use(PontKit);
```

```vue
<pont-provider ref="provider" app-id="app_123" domain="/__pontive">
  <pont-account-menu @pont-logout="onLogout" />
</pont-provider>
<!-- onMounted: provider.value.sdk = Auth   (import { Auth } from "@pontive/oidc-client") -->
```
