@@ -21,6 +21,8 @@ export type SetPropHandler = <T>(
2121export interface RenderOptions {
2222 setProp ?: SetPropHandler ;
2323 document ?: Document ;
24+ /** The root cell for auto-wrapping with ct-cell-context on [UI] traversal */
25+ rootCell ?: Cell ;
2426}
2527
2628export const vdomSchema : JSONSchema = {
@@ -63,13 +65,20 @@ export const render = (
6365) : Cancel => {
6466 // Initialize visited set with the original cell for cycle detection
6567 const visited = new Set < object > ( ) ;
68+ let rootCell : Cell | undefined ;
69+
6670 if ( isCell ( view ) ) {
6771 visited . add ( view ) ;
72+ rootCell = view ; // Capture the original cell for ct-cell-context wrapping
6873 view = view . asSchema ( vdomSchema ) ;
6974 }
75+
76+ // Pass rootCell through options if we have one
77+ const optionsWithCell = rootCell ? { ...options , rootCell } : options ;
78+
7079 return effect (
7180 view ,
72- ( view : VNode ) => renderImpl ( parent , view , options , visited ) ,
81+ ( view : VNode ) => renderImpl ( parent , view , optionsWithCell , visited ) ,
7382 ) ;
7483} ;
7584
@@ -141,6 +150,10 @@ const renderNode = (
141150
142151 const document = options . document ?? globalThis . document ;
143152
153+ // Check if we should wrap with ct-cell-context (when traversing [UI] with a rootCell)
154+ const shouldWrapWithContext = node [ UI ] && options . rootCell ;
155+ const cellForContext = shouldWrapWithContext ? options . rootCell : undefined ;
156+
144157 // Follow `[UI]` to actual vdom. Do this before otherwise parsing the vnode,
145158 // so that if there are both, the `[UI]` annotation takes precedence (avoids
146159 // accidental collision with the otherwise quite generic property names)
@@ -190,6 +203,16 @@ const renderNode = (
190203 addCancel ( cancelChildren ) ;
191204 }
192205
206+ // Wrap with ct-cell-context if we traversed [UI] with a rootCell
207+ if ( cellForContext && element ) {
208+ const wrapper = document . createElement (
209+ "ct-cell-context" ,
210+ ) as HTMLElement & { cell ?: Cell } ;
211+ wrapper . cell = cellForContext ;
212+ wrapper . appendChild ( element ) ;
213+ return [ wrapper , cancel ] ;
214+ }
215+
193216 return [ element , cancel ] ;
194217} ;
195218
0 commit comments