react.mdx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ---
  2. title: React
  3. description: React slot components backed by the shared plugin registry
  4. order: 3
  5. ---
  6. # React slots
  7. This page shows React integration for plugin slots.
  8. Use slots when you want external modules to contribute `ReactNode` UI in host-defined regions without forking your app. The host keeps ownership of layout and slot typing; plugins only see the context and props you expose.
  9. If you have not read the shared model yet, start with [Plugin Slots](/docs/plugins/slots).
  10. ## Runtime-loaded external plugins
  11. If your app loads plugins from disk at runtime (for example `await import(fileUrl)`), add this import once in your app entry:
  12. ```ts
  13. import "@opentui/react/runtime-plugin-support"
  14. ```
  15. This installs Bun runtime support so external TS/TSX plugin modules resolve against the host runtime instances (`@opentui/react`, React JSX runtime modules, and core runtime modules).
  16. Use this for plugin systems in both normal Bun runs and standalone compiled executables.
  17. ```ts
  18. import "@opentui/react/runtime-plugin-support"
  19. const mod = await import(pathToFileURL(pluginPath).href)
  20. registry.register(mod.loadExternalPlugin())
  21. ```
  22. If plugins need additional host-resolved modules, configure runtime support before the side-effect import is loaded:
  23. ```ts
  24. import { ensureRuntimePluginSupport } from "@opentui/react/runtime-plugin-support/configure"
  25. ensureRuntimePluginSupport({
  26. additional: {
  27. "my-runtime-module": { marker: "ok" },
  28. },
  29. })
  30. ```
  31. First-party packages expose side-effect-free runtime module maps. For plugins that import `@opentui/three`, install `@opentui/three` in the host app and pass its runtime modules explicitly:
  32. ```ts
  33. import { runtimeModules as threeRuntimeModules } from "@opentui/three/runtime-modules"
  34. import { ensureRuntimePluginSupport } from "@opentui/react/runtime-plugin-support/configure"
  35. ensureRuntimePluginSupport({
  36. additional: threeRuntimeModules,
  37. })
  38. ```
  39. The side-effect import and configurable entrypoint install the same runtime plugin. Late additions throw with a clear error instead of being ignored.
  40. ## What React adds
  41. - `createReactSlotRegistry(renderer, context, options?)` — create a registry typed for `ReactNode`. Accepts the same [`SlotRegistryOptions`](/docs/plugins/slots#registry-options) as `createSlotRegistry`.
  42. - `Slot<TSlots, TContext>` — generic slot component that takes `registry` as a required prop
  43. - `createSlot(registry, options?)` — optional convenience helper that returns a registry-bound `<Slot />` component
  44. - `ReactPlugin<TSlots, TContext>` — convenience type alias for a plugin that returns `ReactNode`
  45. - `@opentui/react/runtime-plugin-support` — one-line runtime support for external plugin/module loading
  46. - `@opentui/react/runtime-plugin-support/configure` — configurable runtime support without import-time side effects
  47. Register plugins directly with `registry.register()` — no wrapper function is needed (unlike core's `registerCorePlugin`).
  48. ## Basic usage
  49. ```tsx
  50. import { createCliRenderer } from "@opentui/core"
  51. import { createReactSlotRegistry, createRoot, Slot } from "@opentui/react"
  52. type Slots = {
  53. statusbar: { user: string }
  54. }
  55. const context = { appName: "react-app", version: "1.0.0" }
  56. const renderer = await createCliRenderer()
  57. const registry = createReactSlotRegistry<Slots, typeof context>(renderer, context)
  58. const unregister = registry.register({
  59. id: "clock-plugin",
  60. slots: {
  61. statusbar(ctx, props) {
  62. return <text>{`${ctx.appName}:${props.user}`}</text>
  63. },
  64. },
  65. })
  66. const AppSlot = Slot<Slots, typeof context>
  67. function App() {
  68. return (
  69. <AppSlot registry={registry} name="statusbar" user="sam" mode="replace">
  70. <text>fallback-statusbar</text>
  71. </AppSlot>
  72. )
  73. }
  74. createRoot(renderer).render(<App />)
  75. ```
  76. ## Optional convenience helper
  77. If you prefer not to pass `registry` each time, you can still bind one once:
  78. ```tsx
  79. const AppSlot = createSlot(registry)
  80. ```
  81. ## `<Slot>` props
  82. | Prop | Type | Required | Description |
  83. | -------------------------- | ------------------------------------------ | -------- | ---------------------------------------------------------------------------------------------------------- |
  84. | `registry` | `SlotRegistry<ReactNode, Slots, Context>` | yes | Registry to resolve plugins from |
  85. | `name` | `keyof Slots` | yes | Which slot to render |
  86. | `mode` | `SlotMode` | no | `"append"` (default), `"replace"`, or `"single_winner"`. See [slot modes](/docs/plugins/slots#slot-modes). |
  87. | `pluginFailurePlaceholder` | `(failure: PluginErrorEvent) => ReactNode` | no | Per-slot placeholder UI when a plugin throws |
  88. | `children` | `ReactNode` | no | Fallback UI |
  89. | _remaining_ | `Slots[name]` | — | Slot-specific props forwarded to plugin renderers |
  90. ## `ReactSlotOptions` (for `createSlot`)
  91. | Option | Type | Required | Description |
  92. | -------------------------- | ------------------------------------------ | -------- | ------------------------------------------- |
  93. | `pluginFailurePlaceholder` | `(failure: PluginErrorEvent) => ReactNode` | no | Creates placeholder UI when a plugin throws |
  94. ## Plugin failure placeholders
  95. ```tsx
  96. const Slot = createSlot(registry, {
  97. pluginFailurePlaceholder(failure) {
  98. return <text>{`plugin-error:${failure.pluginId}:${failure.phase}`}</text>
  99. },
  100. })
  101. ```
  102. If a plugin throws, the slot renders the placeholder. If no placeholder is provided (or it returns `null`), the slot falls back to `children`.
  103. Plugins that throw during the initial render call are caught inline. Plugins that throw during a React re-render are caught by an internal error boundary that resets on registry changes.
  104. ## Observability
  105. ```tsx
  106. registry.onPluginError((event) => {
  107. console.error(event.pluginId, event.phase, event.source, event.error.message)
  108. })
  109. ```
  110. Example: `packages/react/examples/plugin-slots-errors.tsx`