Skip to content

@xtandard/entitlements/react

Hooks and small components for gating your product UI on a customer’s plan: EntitlementsProvider, useEntitlement, useEntitlements, useCustomer, <Gate>, <UsageMeter>, <PricingTable>, <UpgradePrompt> and createEntitlementsReact<typeof catalog>(). Plain React (a peer), no other dependencies; optional default styles in @xtandard/entitlements/react/components.css.

Client-side gates are UX only — enforce on the server with check / consume or the adapters’ requireEntitlement guards.

import { … } from "@xtandard/entitlements/react";
Name Summary
computeEntitlement A feature’s client-side state from the provider’s data (null = still loading). amount is what the UI is about to ask for (default 1).
covers Would amount more units fit in this feature’s current state?
createEntitlementsReact Catalog-typed hooks and components: feature keys autocomplete, value features return their declared type, UsageMeter only takes numeric features, plan keys are checked.
customerInfo Summarise the customer for useCustomer.
EntitlementsProvider Loads a customer’s entitlements once and shares them with every hook and component below it. Re-renders only when the data changes.
formatPlanPrice Format a plan’s first display price: "$29 / month", "Free".
Gate Render children when the customer has feature (for amount units), fallback otherwise. UX only — the server must still check.
meterState Used share (0–1, uncapped) and threshold level for a meter.
PricingTable Render a pricing table: [data-xe=pricing] > article[data-xe=plan] per plan (data-current, data-highlight), each with name, price, an optional button and the feature list (li[data-included]). Unstyled.
unlockersFromPricing Plans and add-ons in a pricing table that would allow feature for a total of needed units, excluding the customer’s active plans. Cheapest first, then table order (the client-side twin of the engine’s unlockingPlans).
UpgradePrompt When feature is blocked and some plan would unlock it: “Upgrade to Pro to use PDF export” with a button. Renders nothing when allowed, loading, or no plan would help (a hard forbidden).
UsageMeter An accessible usage meter (role="progressbar") for a numeric feature. data-level = ok | warn | critical | unlimited | disabled; the fill’s width is the only inline style. Parts: [data-part=label|value|track|fill|reset].
useCustomer The customer: profile, current plan + status, trial / period end, link-out availability.
useEntitlement One feature, computed client-side: { allowed, enabled, limit, used, remaining, value, resetsAt, loading, unlockedBy }. amount is what the UI is about to ask for (default 1). UX only — enforce on the server.
useEntitlements The provider’s state: data, loading, error, refresh(), checkout().
Name Summary
CustomerInfo What useCustomer returns.
EntitlementsContextValue What useEntitlements returns.
EntitlementsProviderProps Props for EntitlementsProvider. Give one source: baseUrl, fetcher or initial.
EntitlementState A feature’s state, computed client-side by useEntitlement.
GateProps Props for Gate.
PortalData What GET /api/portal/me returns, and what the provider holds: the customer’s entitlements with usage, profile, public pricing and link-out availability.
PricingTableProps Props for PricingTable. Pass pricing or catalog, or neither to use the provider’s.
TypedEntitlementsReact What createEntitlementsReact returns: the same API, typed by your catalog.
UpgradePromptProps Props for UpgradePrompt.
UsageMeterProps Props for UsageMeter.
Name Summary
EntitlementsData Data a provider can be seeded with: the full portal view, or entitlements alone.
MeterLevel Threshold level of a meter: ok < 75% ≤ warn < 90% ≤ critical.
TokenSource A portal token, or a function that returns a fresh one (re-mint on expiry).