Add the `hoc` slot kind so plugins can wrap owner content in layers
新增 `hoc` 插槽类型,让插件层层包裹宿主内容
The home whale plays when blank space is clicked. Its canvas must paint below the content, and `.scrollBody` fills the whole body, so the canvas itself never receives clicks; only an ancestor of the content does. No existing slot kind lets a plugin become an ancestor of the content: | Kind | Behavior | Why it is not enough | |---|---|---| | `single` / `list` / `keyed` | Render the occupant at an owner-chosen position
English
Problem
The home whale plays when blank space is clicked. Its canvas must paint below the content, and .scrollBody fills the whole body, so the canvas itself never receives clicks; only an ancestor of the content does.
No existing slot kind lets a plugin become an ancestor of the content:
| Kind | Behavior | Why it is not enough |
|---|---|---|
single / list / keyed | Render the occupant at an owner-chosen position | The occupant is a sibling of the content |
chain | The elected entry replaces the fallback; with overlay it renders beside the fallback | It does not wrap |
| Factory local slot | The caller replaces a local Component per occurrence | Only the caller chooses; plugins cannot stack |
The previous approach had the Conversation classify blank clicks on its body and notify the background plugin through onBlankClick(listener) => dispose. The Conversation thereby maintained click classification and subscription forwarding that served only a decorative plugin.
Decision
The Markdown fence keyed hoc slot note records why hoc layers take an optional dispatch key, may omit children, share compiled chains per key, and stay registered after a crash.
Add a fifth kind: hoc.
| Item | Contract |
|---|---|
| Declaration | The parent entry's children lists { kind: 'hoc', scope }; SlotMap declares kind: 'hoc' |
| Rendering | renderSlot(key, owner, { children, entryKey }); only / fallback / hookContext are unavailable; omitted children renders the layers around nothing; omitted entryKey applies only wildcard layers |
| Registration | Required id, optional key, order, and priority; a layer without key is a wildcard serving every dispatch, a layer with key serves only that entryKey; the cell is (key or wildcard, id), and within one id an exact-key layer replaces the wildcard layer for that key; the same cell at the same priority throws, a different priority shadows |
| Nesting order | Ascending order, then priority, then registration order; lowest outermost |
| Layer props | Every layer receives the same owner props plus the framework-supplied children (the next inner layer; the innermost is the owner content); children is not an owner prop, HocShare types it, and owner props must not use that name |
| Layer obligation | Render children at most once; a layer that omits it replaces everything inside it; a layer that wraps children creates no box of its own unless its slot documents otherwise; do not change the props passed inward |
| No entry | children renders unchanged |
| Layer crash | Each layer has its own error boundary; a crashed layer reports its error and renders its children at the occurrence that failed for the rest of that occurrence's mount, even when its owner props change; it does not abdicate, so other occurrences, including those dispatching the same key, keep it, and a remounted occurrence renders it again |
| Content error | An error thrown by the wrapped content passes every layer as its original error and is reported against no layer |
| Remounting | Adding or removing a layer that applies to a dispatch remounts the content inside it, while a registration for another key leaves it mounted; a session or session-maybe layer also remounts it when its Session binding changes |
Implementation
| Package | Realization |
|---|---|
ui-slots | SlotKind includes 'hoc'; HocRenderOpts (optional children and entryKey), HocKeysOf, HocShare; RenderSlotFn has a hoc-key overload taking optional { children, entryKey }; KindOptions has a hoc branch with optional key; SlotCore.register / entriesOfSlot key hoc cells by (key or wildcard, id) |
ui-renderer | HocOutlet takes its render function from hocChainFor, which caches one chain per host, spec object, slot name, and entryKey across outlet instances. Once per ledger version it reselects the key's layers with hocLayersFor (wildcard winners plus exact-key winners, an exact-key layer replacing the wildcard layer with its id, sorted by order) and calls composeHocLayers to nest them into one render function only when that ordered list changes by entry identity. Each layer is one Component that fixes its scope-specific Entry (StrictSessionEntry / SessionMaybeEntry / RootEntry), entry, slot inject, and crash reporting at composition, and receives only owner and the next inner layer when rendering. With no layer the outlet returns the owner content directly. HocContentBoundary wraps each layer's inner content and rethrows its errors as HocContentError, which layer boundaries pass through and HocOutletBoundary unwraps to the original error. The Entry chain passes children to the registered Component as JSX children; non-hoc entries receive undefined. SlotErrorBoundary takes a crashed replacement node, which a layer's boundary keeps rendering for the rest of its mount; layer crash reports pass abdicate: false |
scripts/gen-client-catalog.ts | Register options, a children-rendering example, and the wraps-owner-content replace risk for hoc slots |
First consumer
| Location | Realization |
|---|---|
ui-conversation | conversation.hero.frame (hoc, root) with owner props { hero: boolean } wraps .scrollBody in every phase, so the composer does not remount when the home ends. On the home (.root[data-phase='hero']), the Conversation root, which paints the background, sets isolation: isolate as part of the frame contract, so layer decorations at negative z-index paint above that background and below all Conversation content. The Conversation performs no click classification for its layers |
ui-brand-official | PixelWhale is a frame layer: a React onClick on a display: contents frame plays the whale for primary clicks on blank space, excluding portaled overlays (currentTarget.contains(target)), controls (a selector), and clicks that end a text selection inside the frame. While hero is true it paints a canvas at negative z-index. The frame atlas loads through the package asset route; the renderer returns { play, dispose } |
Alternatives considered
onBlankClicksubscription forwarding: the Conversation owns click classification that serves only a decorative plugin.- Canvas below with
pointer-events: noneon.scrollBody: blank space cannot scroll when the home overflows. - A canvas with no
z-index: it needs no stacking context, but it paints over every unpositioned Conversation content outside thez-index: 1home composer stack, such as composer takeovers, and depends on that unrelated rule. - A layer box that establishes the stacking context itself: the layer has to mirror the body's flex column layout.
- A separate
renderNestedSlotor a JSX<HocSlot>component: one more set of bindings;hookContextalready shows thatrenderSlotcan constrain its third argument per key. - Precompiling every slot kind per ledger version: no measurable end-to-end time difference, see #7838.
Consequences
- Adding or removing a layer remounts everything inside it, including the composer on the home.
- Every non-hoc registered Component receives
children: undefined, which overrides any owner prop of that name. - Each layer adds one Component and two error boundaries to the tree; each hoc slot with layers adds one outlet boundary.
- The renderer marks the slot and where each layer renders
children, not the layer's own markup. Plugins need no wrapper of their own, but structural selectors must account for those DOM elements. - React 18 recollects the component stack where an error is rethrown, so a content error reaching boundaries above a hoc slot with layers carries a component stack that starts at
HocOutletBoundary. - A layer that wraps its
childrenin its own error boundary catches the internalHocContentErrorrather than the original content error. - The frame contract holds only on the home: other phases leave the Conversation root out of the stacking order, so unportaled Conversation popovers still rise above AppFrame chrome that floats over the column, while on the home they stack only within the root.
HocOutletkeeps its layer Components in a shared compile cache, not in outlet-instance state, so they do not depend on React retaining memoized values; these Components are the remount boundary of the content inside them. Different slot names use separate chains even when they share a spec object.- The slot system standard remains authoritative for the other kinds; this note adds the fifth.
中文
问题
首页鲸鱼需要在空白处被点击时播放。canvas 必须画在内容下面,.scrollBody 铺满整个 body,所以 canvas 自己收不到点击;能收到点击的只有内容的祖先节点。
现有插槽类型都不能让插件成为内容的祖先:
| 类型 | 行为 | 为什么不够 |
|---|---|---|
single / list / keyed | 在宿主指定位置渲染 occupant | occupant 和内容是兄弟节点 |
chain | 选中的 entry 替换 fallback;overlay 时与 fallback 并排 | 不包裹 |
| Factory 局部 slot | 调用方按次替换局部组件 | 只有调用方能选,插件无法叠加 |
此前的做法是由 Conversation 在 body 上判定空白点击,再通过 onBlankClick(listener) => dispose 通知背景插件。这让 Conversation 维护一套只服务于装饰插件的点击分类和订阅转发。
Decision
Markdown fence keyed hoc slot 的 Note 记录了 hoc 层为何接受可选的分派 key、可以不渲染 children、为何按 key 共享编译好的层链,以及为何崩溃后仍保持注册。
新增第五种 kind:hoc。
| 项 | 约定 |
|---|---|
| 声明 | 父 entry 的 children 写 { kind: 'hoc', scope };SlotMap 中 kind: 'hoc' |
| 渲染 | renderSlot(key, owner, { children, entryKey });only / fallback / hookContext 不可用;省略 children 时各层包裹空内容;省略 entryKey 时只叠加通配层 |
| 注册 | 必填 id,可选 key、order、priority;不带 key 的层是通配层,服务所有分派,带 key 的层只服务该 entryKey;cell 为 (key 或通配, id),同一 id 下精确 key 的层替换该 key 的通配层;同 cell 同 priority 抛错,不同 priority 遮蔽 |
| 嵌套顺序 | 依次按 order、priority、注册顺序升序,最小者在最外层 |
| 层的 props | 每层收到同一份 owner props,加上框架注入的 children(内一层,最内层是宿主内容);children 不属于 owner props,类型由 HocShare 提供,owner props 不得使用这个名字 |
| 层的义务 | 至多渲染一次 children;不渲染 children 的层替换其内部全部内容;包裹 children 的层自身不产生盒子(其 slot 另有说明的除外);不能修改传给内层的 props |
| 无 entry | 原样渲染 children |
| 层崩溃 | 每层有独立 error boundary;崩溃时错误照常上报,该层在出错的那一处渲染自己的 children,直到该处本次挂载结束,owner props 变化也不恢复;该层不 abdicate,因此其他出现处(包括分派同一 key 的出现处)仍保留它,重新挂载的出现处会再次渲染它 |
| 内容出错 | 被包裹内容抛出的错误以原始错误穿过所有层,不记到任何层名下 |
| 重新挂载 | 增减一个对本次分派生效的层会让其内部内容重新挂载,为其他 key 注册的层不会;session 或 session-maybe 作用域的层在 Session binding 变化时也会让内部内容重新挂载 |
实现
| 包 | 实现 |
|---|---|
ui-slots | SlotKind 包含 'hoc';HocRenderOpts(可选 children 与 entryKey)、HocKeysOf、HocShare;RenderSlotFn 为 hoc key 提供接受可选 { children, entryKey } 的重载;KindOptions 的 hoc 分支含可选 key;SlotCore.register / entriesOfSlot 按 (key 或通配, id) 划分 hoc cell |
ui-renderer | HocOutlet 从 hocChainFor 取渲染函数,后者按 host、spec 对象、slot 名和 entryKey 缓存层链,同一组合的 outlet 实例共享一条层链。每个 ledger 版本它只用 hocLayersFor 重新选出一次该 key 的层(通配胜者加精确 key 胜者,精确 key 的层替换同 id 的通配层,按 order 排序),仅当这一有序列表按 entry 身份比较发生变化时才调用 composeHocLayers 把各层组合成一个嵌套渲染函数。每层是一个组件,在组合时固定按 scope 选定的 Entry(StrictSessionEntry / SessionMaybeEntry / RootEntry)、entry、slot inject 与崩溃上报,渲染时只接收 owner 与内一层。没有层时 outlet 直接返回宿主内容。HocContentBoundary 包住每层的内层内容,把其中的错误包装为 HocContentError 重新抛出;层的边界放行这种错误,HocOutletBoundary 再解开为原始错误。Entry 链以 JSX children 把 children 交给注册组件;非 hoc entry 收到 undefined。SlotErrorBoundary 接受 crashed 替换节点,层的边界在本次挂载内一直渲染该节点;层的崩溃上报传 abdicate: false |
scripts/gen-client-catalog.ts | hoc 插槽的注册选项、渲染 children 的示例,以及 wraps-owner-content 替换风险 |
首个使用者
| 位置 | 实现 |
|---|---|
ui-conversation | conversation.hero.frame(hoc,root),owner props 为 { hero: boolean },在所有阶段包裹 .scrollBody,首页结束时输入框不重新挂载。首页阶段(.root[data-phase='hero']),绘制背景的 Conversation 根元素设置 isolation: isolate,这是 frame 契约的一部分:层中负 z-index 的装饰画在该背景之上、所有 Conversation 内容之下。Conversation 不为层做任何点击判定 |
ui-brand-official | PixelWhale 是 frame 层:display: contents 外框上的 React onClick 在主按钮点击留白时播放,排除 portal 浮层(currentTarget.contains(target))、控件(选择器)和结束外框内文字选择的点击。hero 为 true 时以负 z-index 绘制 canvas。帧图经包 asset 路由加载;渲染器返回 { play, dispose } |
Alternatives considered
onBlankClick订阅转发:Conversation 承担只为装饰插件服务的点击分类。- canvas 放底下、
.scrollBody设pointer-events: none:首页溢出时空白处无法滚动。 - 不设
z-index的 canvas:不需要层叠上下文,但会画在z-index: 1的首页输入区以外的所有非定位 Conversation 内容之上(例如输入区接管面板),并依赖那条无关的规则。 - 由层自己的盒子建立层叠上下文:层必须复刻 body 的 flex 列布局。
- 单独的
renderNestedSlot/ JSX 组件<HocSlot>:多一套绑定;hookContext已证明可以按 key 约束renderSlot的第三个参数。 - 按 ledger 版本预编译所有插槽类型:端到端测不出耗时差异,见 #7838。
Consequences
- 增减一层会让其内部所有内容重新挂载,包括首页的输入框。
- 所有非 hoc 的注册组件都会收到
children: undefined,会覆盖同名的 owner prop。 - 每层在组件树中多一个组件和两个 error boundary;有层的 hoc 插槽另多一个 outlet 边界。
- 渲染器标记的是 slot 和各层渲染
children的位置,而不是层组件自己的元素。插件不必添加自己的包装元素,但结构选择器必须考虑这些 DOM 元素。 - React 18 会在错误被重新抛出的位置重新采集组件栈,因此有层的 hoc 插槽之上的边界收到的内容错误,组件栈从
HocOutletBoundary开始。 - 层如果自己在
children外包错误边界,捕获到的是内部的HocContentError,而不是原始的内容错误。 - frame 契约只在首页阶段成立:其他阶段根元素不参与层叠隔离,未经 portal 的 Conversation 浮层仍能盖住悬浮在中栏上方的 AppFrame 元素;首页阶段它们只在根元素内部比较层级。
HocOutlet把层组件保存在共享的编译缓存中,而不是 outlet 实例状态里,因此不依赖 React 保留 memo 缓存;这些组件是其内部内容的重新挂载边界。不同名称的 slot 即使复用同一个 spec 对象,也不共享层链。- 其他 kind 仍以 slot 体系标准 为准;本 Note 新增第五种。