# packages/core/src/lib/layout/define-layout-component.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/a1dcf8ea5a3d3e01f6a79ac298dd3087f25b8b52/packages/core/src/lib/layout/define-layout-component.ts#L29).

[Back to reference](/reference/functions/define-layout-component/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import type { PibblComponent, PibblElement, PibblNodeInput } from '../element/types.js';</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> import { clonePibblElement } from '../element/create-element.js';</span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> import { materializeContainerChildren } from '../element/children.js';</span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> import { getChildPrimitiveReceiverToken, getElementMetadata, setElementMetadata, getPrimitiveDefinition, getPrimitiveReceiverToken, setPrimitiveReceiverToken, isPlacementConsumed, setPlacementConsumed } from '../element/metadata.js';</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a> import { GLOBAL_STATE } from '../global-state.js';</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a> import { withHooksForbidden } from '../hooks/hook-slot.js';</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a> import { withSignalWriteForbidden } from '../signals/graph.js';</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a> import { adoptReceiverInputProvision, withReceiverInputProvision } from '../signals/input-provision.js';</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a> import type { PibblRenderTransaction } from '../types.js';</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a> import { withIsolatedRenderState } from './measure.js';</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a> import { setPreNormalizedStyle, type PreparedStyleInput } from '../style/resolve-dispatch.js';</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a> </span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a> const definitions = new WeakMap&lt;Function, (props: any) =&gt; PibblNodeInput&gt;();</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a> const transactions = new WeakMap&lt;PibblRenderTransaction, WeakMap&lt;object, PibblElement&lt;any&gt;&gt;&gt;();</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a> </span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a> /**</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a>  * Defines pure reusable structure that a parent can inspect before allocation.</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a>  * The description returns one drawing or layout primitive root, optionally inside</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>  * transparent fragments. Forward any authored style explicitly to that root.</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>  * Signal reads are supported; hooks, Canvas/allocation reads, writes and resource</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>  * creation are excluded. Descriptions are shared only within one transaction.</span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a>  *</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>  * @param describe - Pure synchronous description using authored readonly props.</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>  * @returns A component for JSX or createElement, preserving normal type/key identity.</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>  * @throws When called directly, or when its description has no single primitive root.</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>  * @see {@link PibblComponent}</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>  * @see {@link PibblNodeInput}</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>  */</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a> export function defineLayoutComponent&lt;P&gt;(</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a>   describe: (props: Readonly&lt;P&gt;) =&gt; PibblNodeInput,</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a> ): PibblComponent&lt;P&gt; {</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a>   if (typeof describe !== 'function') throw new TypeError('A layout description must be a function.');</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a>   const component: PibblComponent&lt;P&gt; = function LayoutComponent() {</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>     throw new Error('Mount layout components through JSX or createElement; do not call them directly.');</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>   };</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>   definitions.set(component, describe);</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>   return component;</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a> }</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a> </span>
<span id="L40"><a href="#L40" aria-label="Line 40">40</a> export function isLayoutComponent(type: Function): boolean { return definitions.has(type); }</span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a> </span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a> /** One prepared root per layout occurrence, never cached by mutable prop identity. */</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a> export function prepareLayoutComponent(</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>   element: PibblElement&lt;any&gt;, options?: { receiverToken?: object },</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a> ): PibblElement&lt;any&gt; {</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>   const describe = definitions.get(element.type);</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a>   if (!describe) throw new Error('The element has no structural layout description.');</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>   const transaction = GLOBAL_STATE.renderTransaction;</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a>   const receiver = options?.receiverToken ?? getPrimitiveReceiverToken(element) ??</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a>     (GLOBAL_STATE.componentRefs?.element === element ? GLOBAL_STATE.componentRefs : element);</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a>   let prepared = transaction ? transactions.get(transaction) : undefined;</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>   const cached = prepared?.get(receiver);</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>   if (cached) return cached;</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>   const root = withHooksForbidden('Hooks are forbidden in a layout description.', () =&gt;</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a>     withSignalWriteForbidden('Signal writes are forbidden in a layout description.', () =&gt;</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a>       withIsolatedRenderState(() =&gt; withReceiverInputProvision(transaction, receiver, () =&gt; {</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>         const roots = materializeContainerChildren(describe(element.props), element);</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>         if (roots.length !== 1 || !getPrimitiveDefinition(roots[0].type)) {</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a>           throw new Error('A layout description must return exactly one drawing or layout primitive root.');</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a>         }</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>         const root = clonePibblElement(roots[0]);</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>         setElementMetadata(root, {</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a>           ...getElementMetadata(root),</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a>           layoutDescriptionSource: getElementMetadata(roots[0])?.layoutDescriptionSource ?? roots[0],</span>
<span id="L65"><a href="#L65" aria-label="Line 65">65</a>         });</span>
<span id="L66"><a href="#L66" aria-label="Line 66">66</a>         return root;</span>
<span id="L67"><a href="#L67" aria-label="Line 67">67</a>       })),</span>
<span id="L68"><a href="#L68" aria-label="Line 68">68</a>     ),</span>
<span id="L69"><a href="#L69" aria-label="Line 69">69</a>   );</span>
<span id="L70"><a href="#L70" aria-label="Line 70">70</a>   const current = GLOBAL_STATE.componentRefs;</span>
<span id="L71"><a href="#L71" aria-label="Line 71">71</a>   if (current &amp;&amp; (getPrimitiveReceiverToken(current.element) ?? current) === receiver) {</span>
<span id="L72"><a href="#L72" aria-label="Line 72">72</a>     adoptReceiverInputProvision(transaction, receiver);</span>
<span id="L73"><a href="#L73" aria-label="Line 73">73</a>   }</span>
<span id="L74"><a href="#L74" aria-label="Line 74">74</a>   setPrimitiveReceiverToken(root, getChildPrimitiveReceiverToken(transaction, receiver, 0));</span>
<span id="L75"><a href="#L75" aria-label="Line 75">75</a>   if (transaction) {</span>
<span id="L76"><a href="#L76" aria-label="Line 76">76</a>     if (!prepared) { prepared = new WeakMap(); transactions.set(transaction, prepared); }</span>
<span id="L77"><a href="#L77" aria-label="Line 77">77</a>     prepared.set(receiver, root);</span>
<span id="L78"><a href="#L78" aria-label="Line 78">78</a>   }</span>
<span id="L79"><a href="#L79" aria-label="Line 79">79</a>   return root;</span>
<span id="L80"><a href="#L80" aria-label="Line 80">80</a> }</span>
<span id="L81"><a href="#L81" aria-label="Line 81">81</a> </span>
<span id="L82"><a href="#L82" aria-label="Line 82">82</a> /** Carries the parent's settled content size into the same prepared root. */</span>
<span id="L83"><a href="#L83" aria-label="Line 83">83</a> export function placedLayoutComponentRoot(element: PibblElement&lt;any&gt;, style?: PreparedStyleInput): PibblElement&lt;any&gt; {</span>
<span id="L84"><a href="#L84" aria-label="Line 84">84</a>   const root = prepareLayoutComponent(element);</span>
<span id="L85"><a href="#L85" aria-label="Line 85">85</a>   const slot = getElementMetadata(element)?.preparedLayoutSlot;</span>
<span id="L86"><a href="#L86" aria-label="Line 86">86</a>   if (!style &amp;&amp; !slot) return root;</span>
<span id="L87"><a href="#L87" aria-label="Line 87">87</a>   const placed = clonePibblElement(root);</span>
<span id="L88"><a href="#L88" aria-label="Line 88">88</a>   // The description is transparent: its one actual root consumes the already</span>
<span id="L89"><a href="#L89" aria-label="Line 89">89</a>   // allocated placement, including explicit static/relative positioning, once.</span>
<span id="L90"><a href="#L90" aria-label="Line 90">90</a>   if (isPlacementConsumed(element)) setPlacementConsumed(placed);</span>
<span id="L91"><a href="#L91" aria-label="Line 91">91</a>   if (style) setPreNormalizedStyle(placed, style.style, root, getPrimitiveReceiverToken(root), style.query);</span>
<span id="L92"><a href="#L92" aria-label="Line 92">92</a>   const metadata = getElementMetadata(element);</span>
<span id="L93"><a href="#L93" aria-label="Line 93">93</a>   if (slot || metadata?.absoluteContainingBlock) setElementMetadata(placed, {</span>
<span id="L94"><a href="#L94" aria-label="Line 94">94</a>     ...getElementMetadata(placed),</span>
<span id="L95"><a href="#L95" aria-label="Line 95">95</a>     ...(slot ? { preparedLayoutSlot: slot } : undefined),</span>
<span id="L96"><a href="#L96" aria-label="Line 96">96</a>     ...(metadata?.absoluteContainingBlock ? { absoluteContainingBlock: metadata.absoluteContainingBlock } : undefined),</span>
<span id="L97"><a href="#L97" aria-label="Line 97">97</a>   });</span>
<span id="L98"><a href="#L98" aria-label="Line 98">98</a>   return placed;</span>
<span id="L99"><a href="#L99" aria-label="Line 99">99</a> }</span>
<span id="L100"><a href="#L100" aria-label="Line 100">100</a> </span></code></pre>

## Documentation version

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.
