Skip to content

consolidated react provider#1259

Draft
tenub wants to merge 21 commits into
mainfrom
cole/schy-372
Draft

consolidated react provider#1259
tenub wants to merge 21 commits into
mainfrom
cole/schy-372

Conversation

@tenub
Copy link
Copy Markdown
Contributor

@tenub tenub commented May 13, 2026

Summary

Consolidates the legacy @schematichq/schematic-react (WS-backed flags/entitlements) and @schematichq/schematic-components (REST-backed UI) into a single package with subpath exports, behind one SchematicContext, one SchematicProvider, and one set of hooks — without bloating the bundle for consumers who only need the lightweight surface.

  • Root entry (@schematichq/schematic-react) — flags / entitlements / events / plan info. ~6 KB ESM. Pulls in nothing beyond react and @schematichq/schematic-js.
  • Components entry (@schematichq/schematic-react/components) — same provider plus the REST-backed UI surface (PricingTable, Embed, PaymentMethod, etc.) and embed-side hooks. ~890 KB ESM.

The standalone components/ package continues to ship from this monorepo for existing consumers; new code should prefer the /components subpath.

Architecture

  • src/context.ts — single SchematicContext with shape { client, embed }.
  • src/provider.tsx — bare plugin host. Composes optional ws / embed adapter components around children, filters props per-adapter, holds no state itself.
  • src/core/WsAdapter.tsx — WS client adapter. Auto-bound by both entries; pass ws={null} to opt out.
  • src/components/embed/EmbedAdapter.tsx — heavy REST / reducer / theme / i18n adapter. Only reachable from /components, and even then loaded lazily on first useEmbed (or first lazy UI-component mount) via a useSyncExternalStore-backed loader (src/embed-loader.ts).
  • Three opt-in / opt-out paths for the embed adapter: embed={undefined} (lazy, default), embed={EmbedAdapter} (eager mount), embed={null} (explicit opt-out — useEmbed throws a clear error).
  • scripts/check-tree-shake.mjs runs post-build and fails if @stripe/*, styled-components, i18next, react-i18next, or @schematichq/schematic-icons leak into the root bundle.

Migration

  • Legacy @schematichq/schematic-react (WS-only) users: import surface is unchanged — bump the version.
  • Legacy @schematichq/schematic-components users: swap the package name and append /components to imports. EmbedProvider is gone — use SchematicProvider from /components. The apiKey prop is gone; publishableKey now authenticates both the WS client and the public REST surface.

Test plan

  • yarn build produces the expected dist/ shape (root ~6 KB ESM, components ~890 KB ESM) and check:tree-shake passes
  • yarn test is green (vitest + MSW under jsdom)
  • Root entry: useSchematicFlag / useSchematicEntitlement / useSchematicEvents work against a live WS connection
  • /components entry, default (lazy): provider mounts; useEmbed-using subtree shows fallback until the embed chunk loads, then renders the populated UI
  • /components entry, eager: embed={EmbedAdapter} starts the chunk loading at provider mount (no fallback flash)
  • /components entry, opt-out: embed={null} causes useEmbed callers to throw the explicit disabled error
  • ws={null} on /components renders the UI surface without opening a WebSocket
  • SSR: both entries import safely from server code; embed-using subtrees render fallback on the server and hydrate on the client
  • Standalone components/ package still builds and ships from the monorepo

@tenub tenub self-assigned this May 13, 2026
@tenub tenub force-pushed the cole/schy-372 branch 4 times, most recently from 7756c68 to 576dd3b Compare May 18, 2026 20:00
@github-actions
Copy link
Copy Markdown

🚀 Components Preview Deployed!

📍 Preview URL: https://schematic-next-example-nczm0mri2-schematichq.vercel.app
🔄 Components Version: Built from commit a8b8ab1
📦 Demo App: schematichq/schematic-next-example

@github-actions
Copy link
Copy Markdown

🚀 Components Preview Deployed!

📍 Preview URL: https://schematic-next-example-iva6msjhn-schematichq.vercel.app
🔄 Components Version: Built from commit ec6ca94
📦 Demo App: schematichq/schematic-next-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants