react.mdx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ---
  2. title: React (OpenTUI)
  3. description: React provider and hooks for a pre-created OpenTUI keymap
  4. order: 4
  5. navTitle: React (OpenTUI)
  6. ---
  7. # React keymap bindings (OpenTUI)
  8. This page covers `@opentui/keymap/react`.
  9. These bindings are for OpenTUI React apps. They consume a pre-created `Keymap<Renderable, KeyEvent>` from `@opentui/keymap/opentui`; they do not wrap the DOM/HTML adapter used in browser React apps.
  10. If you have not read the shared model yet, start with [Keymap overview](/docs/keymap/overview). For keymap construction, see [Keymap hosts](/docs/keymap/hosts).
  11. ## What React adds
  12. - `KeymapProvider` - React context provider for an existing OpenTUI keymap
  13. - `useKeymap()` - returns the current OpenTUI keymap from context
  14. - `useBindings(createLayer, deps?)` - registers a keymap layer for the component lifecycle
  15. - `useActiveKeys(options?)` - derived active key list
  16. - `usePendingSequence()` - derived pending sequence
  17. - `reactiveMatcherFromStore(subscribe, getSnapshot, predicate?)` - adapts an external store to `ReactiveMatcher`
  18. ## Basic usage
  19. ```tsx
  20. /** @jsxImportSource @opentui/react */
  21. import { createCliRenderer } from "@opentui/core"
  22. import { createDefaultOpenTuiKeymap } from "@opentui/keymap/opentui"
  23. import { KeymapProvider, useBindings } from "@opentui/keymap/react"
  24. import { createRoot } from "@opentui/react"
  25. const renderer = await createCliRenderer()
  26. const keymap = createDefaultOpenTuiKeymap(renderer)
  27. function App() {
  28. useBindings(
  29. () => ({
  30. commands: [
  31. {
  32. name: "quit",
  33. run() {
  34. renderer.destroy()
  35. },
  36. },
  37. ],
  38. bindings: [{ key: "q", cmd: "quit" }],
  39. }),
  40. [],
  41. )
  42. return <text>Press q to quit</text>
  43. }
  44. createRoot(renderer).render(
  45. <KeymapProvider keymap={keymap}>
  46. <App />
  47. </KeymapProvider>,
  48. )
  49. ```
  50. ## Provider
  51. | Prop | Type | Required | Description |
  52. | ---------- | ------------------------------ | -------- | --------------------------- |
  53. | `keymap` | `Keymap<Renderable, KeyEvent>` | yes | The OpenTUI keymap instance |
  54. | `children` | `ReactNode` | no | Descendant React tree |
  55. ## Hooks
  56. | Hook | Description |
  57. | --------------------------------- | --------------------------------------------------------------------------------------- |
  58. | `useKeymap()` | Returns the current keymap or throws if the provider is missing |
  59. | `useBindings(createLayer, deps?)` | Memoizes the layer factory with `deps`, registers the layer, and disposes it on unmount |
  60. | `useActiveKeys(options?)` | Re-renders on batched keymap state changes and returns `getActiveKeys(...)` |
  61. | `usePendingSequence()` | Re-renders on batched keymap state changes and returns `getPendingSequence()` |
  62. | `reactiveMatcherFromStore(...)` | Builds a `ReactiveMatcher` from any subscribe/getSnapshot store |
  63. ## `useBindings`
  64. `useBindings()` registers a layer-like object. The returned layer can include `priority`, `enabled`, `bindings`, `commands` and any custom addon fields.
  65. React memoizes `createLayer` with `useMemo(createLayer, deps)`. If you omit `deps`, the hook treats the layer factory as static after mount because the default is `[]`. If the layer shape depends on props or React state, include those values in `deps`.
  66. React-specific layer shapes:
  67. | Shape | Description |
  68. | -------------------------- | ----------------------------------------- |
  69. | Global layer | Omit `targetRef` |
  70. | Local `focus-within` layer | Set `targetRef` and omit `targetMode` |
  71. | Local exact-focus layer | Set `targetRef` and `targetMode: "focus"` |
  72. - When `targetRef` is present and `targetMode` is omitted, `focus-within` is used.
  73. - If `targetRef.current` is `null`, registration waits until the ref resolves.
  74. - Passing a local `targetMode` without `targetRef` throws.
  75. - `deps` controls when the layer factory is re-evaluated.
  76. - Local layers follow `targetRef.current`, so they can wait for a ref to mount and retarget if that ref later points at a different renderable.
  77. ## `useActiveKeys()` and `usePendingSequence()`
  78. ```tsx
  79. const activeKeys = useActiveKeys({ includeMetadata: true })
  80. const pendingSequence = usePendingSequence()
  81. ```
  82. Use these hooks for command palettes, key hint UIs, leader prompts and status bars.
  83. ## `reactiveMatcherFromStore()`
  84. Use this when an addon field expects a `ReactiveMatcher`, for example the shipped `enabled` field:
  85. ```tsx
  86. const matcher = useMemo(
  87. () => reactiveMatcherFromStore(store.subscribe, store.getSnapshot, (mode) => mode === "normal"),
  88. [store],
  89. )
  90. useBindings(
  91. () => ({
  92. enabled: matcher,
  93. bindings: [{ key: "x", cmd: "delete-line" }],
  94. }),
  95. [matcher],
  96. )
  97. ```
  98. Example: `packages/react/examples/keymap.tsx`