OpenTUI 0.9.10

npmGitHub release

Suppose a prompt has a footer, and the footer has a status line. One plugin adds a clock to the status line. Another plugin replaces the whole footer. Where does the clock go? The slot registry cannot answer that question. It gives each slot one flat name, and its slots do not contain each other.

OpenTUI 0.9.10 adds a slot tree to @opentui/core. A slot path is names joined by dots, and a path contains every path that extends it at a dot: prompt.footer contains prompt.footer.status, but not prompt.footerbar. The host mounts paths. Each plugin is an owner that claims a place before, after, or inside a path, or that replaces it. In Solid and React, createTreeSlot binds a tree to a Slot component:

type Slots = { "prompt.footer": { user: string }; "prompt.footer.status": {} }

const tree = new SlotTree<JSX.Element, Slots>()
const Slot = createTreeSlot(tree)

export function Footer(props: { user: string }) {
  return (
    <Slot path="prompt.footer" input={{ user: props.user }}>
      <Slot path="prompt.footer.status">
        <text>ready</text>
      </Slot>
    </Slot>
  )
}

const stage = tree.owner("clock").stage()
stage.claim({ append: "prompt.footer.status", render: () => <text> 12:00</text> })
stage.commit()

Two rules answer the question about the clock. First, the last replace on a path wins and suppresses every claim inside that path, so a plugin that replaces prompt.footer also hides the clock. Second, an added claim on a path that is not mounted goes to the end of the nearest mounted ancestor. If the host never mounts prompt.footer.status, the clock shows at the end of prompt.footer. tree.resolution() lists the suppressed claims, each with the claim that won, and the claims that moved to an ancestor. A host can use that list to explain why a plugin’s content does not show.

A stage holds a plugin’s claims until commit(), so a plugin that fails during setup shows nothing: call stage.dispose() and its claims never show. Each claim renders in its own error boundary. A claim that throws calls the tree’s onError and shows the placeholder. The other claims in the slot still render. The slot registry, SlotRenderable, and the existing Slot components do not change. Rust apps get the same tree as opentui::slot. The Rust crate and Core check the same table of expected results.

Added

  • core: SlotTree holds plugin claims on nested slot paths, such as prompt.footer and prompt.footer.status. resolveSlotClaims() resolves claims without a tree, and slotPathContains() tells if one path contains another. See Load plugins and modules at runtime. (#2204)
  • react: createTreeSlot(tree, options) returns a Slot component for a SlotTree. Each claim renders in its own error boundary, and options.placeholder shows instead of a claim that fails. See Load plugins and modules at runtime. (#2204)
  • solid: createTreeSlot(tree, options) returns a Slot component for a SlotTree. Each claim renders in its own error boundary, and options.placeholder shows instead of a claim that fails. See Load plugins and modules at runtime. (#2204)
  • native: The Rust crate adds opentui::slot, a slot tree that resolves claims as Core does. Tree::mount returns a Slot widget, and a plugin claim with an empty name in its path returns MountError::InvalidPath. See Widgets and layout. (#2205)
  • native: The Rust crate adds Fragment, a container with no box of its own. Its children lay out, draw, take focus, and receive clicks as children of the fragment’s parent. See Widgets and layout. (#2201)

Fixed

  • native: With the Kitty keyboard protocol, the parser now reads a key report that a slow link splits before its first ;. Before, ESC [97, a pause, and then ;2u typed ;2u instead of Shift+A. See Keyboard input. (#2207)
  • core: If a renderBlock function renders code blocks, MarkdownRenderable no longer shows a code block in a list item twice. The extra copy appeared after a theme change or after a change to fg, bg, conceal, the table options, or streaming. See Markdown. (#2208)
  • native: In the Rust crate, Markdown quotes now draw as in Core. Code and table text in a quote use the text color, an empty quote (>) draws its bar, and list markers use only the list style. See Code, Markdown, and highlighting. (#2210)
  • native: On Linux, the native library no longer adds a 256 kB Zig signal stack to each thread of the process. An idle thread now uses 12 kB of RSS instead of 268 kB, and a thread with a stack of 256 kB or less can start. See Native rendering. (#2218)

API changes#

@opentui/core#

Added

  • class SlotTree<TNode, TSlots extends object>
  • constructor SlotTree(options?: SlotTreeOptions<TNode, TSlots>)
  • function resolveSlotClaims<T extends SlotClaimPlacement>(input: { readonly paths: ReadonlySet<string>; readonly claims: ReadonlyArray<T>; }): SlotResolution<T>
  • function slotPathContains(ancestor: string, path: string): boolean
  • interface ResolvedSlotClaim<TNode = unknown, TSlots extends object = any> extends SlotClaimPlacement
  • interface SlotClaimPlacement
  • interface SlotOwner<TNode, TSlots extends object>
  • interface SlotPlaced<T>
  • interface SlotResolution<T>
  • interface SlotStage<TNode, TSlots extends object>
  • interface SlotTreeErrorEvent<TNode = unknown, TSlots extends object = any>
  • interface SlotTreeOptions<TNode = unknown, TSlots extends object = any>
  • type SlotClaim<TNode, TSlots extends object> = { [Path in keyof TSlots]: { readonly render: SlotClaimRender<TNode, TSlots[Path]>; } & SlotPlacementOf<Path>; }[keyof TSlots]
  • type SlotClaimRender<TNode, TInput> = (input: TInput) => TNode
  • type SlotPlacementKind = "before" | "prepend" | "append" | "after" | "replace"
  • type SlotPlacementOf<Path> = Readonly<ExactlyOne<Record<SlotPlacementKind, Path>>>

@opentui/react#

Added

  • function createTreeSlot<TSlots extends TreeSlots<TSlots>>(tree: SlotTree<ReactNode, TSlots>, options?: TreeSlotOptions<TSlots>): <Path extends keyof TSlots>(props: TreeSlotProps<TSlots, Path>) => ReactNode
  • interface TreeSlotOptions<TSlots extends TreeSlots<TSlots> = any>
  • type TreeSlotProps<TSlots extends TreeSlots<TSlots>, Path extends keyof TSlots> = { path: Path; children?: ReactNode; } & InputProp<TSlots[Path]>

@opentui/solid#

Added

  • function createTreeSlot<TSlots extends TreeSlots<TSlots>>(tree: SlotTree<JSX.Element, TSlots>, options?: TreeSlotOptions<TSlots>): <Path extends keyof TSlots>(props: TreeSlotProps<TSlots, Path>) => JSX.Element
  • interface TreeSlotOptions<TSlots extends TreeSlots<TSlots> = any>
  • type TreeSlotProps<TSlots extends TreeSlots<TSlots>, Path extends keyof TSlots> = { path: Path; children?: JSX.Element; } & InputProp<TSlots[Path]>