# packages/core/src/lib/components/html-box.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/272a94aaf62e0bd6ad8726a4c607a76a9ec44ca1/packages/core/src/lib/components/html-box.ts#L63).

[Back to reference](/reference/components/html-box/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import type { HtmlMountCallback } from '../html/types.js';</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> import type { ResolvedDrawingStyle } from '../style/types.js';</span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> import { definePrimitive } from '../define-primitive.js';</span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> import { GLOBAL_STATE } from '../global-state.js';</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a> import { resolveBox } from '../layout/box.js';</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a> import type { LayoutBox } from '../layout/types.js';</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a> import { createGroupTransform, setPrimitivePresentation } from '../primitive-presentation.js';</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a> import { getCurrentWorldTransform } from '../render-target.js';</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a> import { registerOpaquePrimitiveProps } from '../element/metadata.js';</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a> import { requireHtmlService, snapshotClips } from '../html/html-service.js';</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a> import type { HtmlBoxProps, HtmlBoxSlot, HtmlBoxStyle } from '../html/types.js';</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a> import type { PibblRenderResult, PrimitiveInput, RenderingContext2D } from '../types.js';</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a> import { stripLayoutParticipation } from '../style/resolve-built-in.js';</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a> </span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a> type Resolved = ResolvedDrawingStyle&lt;HtmlBoxStyle&gt; &amp; {</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a>   borderBox: Readonly&lt;LayoutBox&gt;; contentBox: Readonly&lt;LayoutBox&gt;; overflow: 'visible' | 'clip';</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a> };</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a> </span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a> function renderHtmlBox(props: HtmlBoxProps&lt;unknown&gt;, style: Readonly&lt;Resolved&gt;, ctx: RenderingContext2D): void {</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>   const refs = GLOBAL_STATE.componentRefs!;</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>   const owner = GLOBAL_STATE.currentPibblInstance!;</span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a>   const service = requireHtmlService(owner);</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>   let slot = refs.refs[0] as HtmlBoxSlot | undefined;</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>   if (!slot || slot.disposed) {</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>     if (typeof props.mount !== 'function') throw new TypeError('HtmlBox mount must be a function.');</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>     slot = { owner, mount: props.mount, disposed: false, region: { identity: {}, owner, content: undefined, rootBounds: undefined, active: true } };</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>     const stable = slot;</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>     stable.region = { ...stable.region, onFocusSnapshot(snapshot) {</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a>       queueMicrotask(() =&gt; { if (!stable.disposed) stable.callback?.(snapshot); });</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a>     } };</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a>     refs.refs[0] = slot;</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a>     refs.teardowns.add(() =&gt; service.release(stable));</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a>   }</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>   const opacity = style.opacity ?? 1;</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>   if (!Number.isFinite(opacity) || opacity &lt; 0 || opacity &gt; 1) throw new RangeError('HtmlBox opacity must be between 0 and 1.');</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>   service.register({ slot, data: props.data, matrix: getCurrentWorldTransform(),</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>     clips: snapshotClips(GLOBAL_STATE.clippingStack), border: style.borderBox, content: style.contentBox,</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a>     opacity: ctx.globalAlpha * opacity, interactive: props.pointerEvents !== 'none', overflow: style.overflow ?? 'clip', onFocusChange: props.onFocusChange });</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> </span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a> const primitive = definePrimitive&lt;HtmlBoxProps&lt;unknown&gt;, HtmlBoxStyle, HtmlBoxStyle, Resolved&gt;(renderHtmlBox, {</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>   resolveStyle(style, context) {</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>     const box = resolveBox({ ...style, width: style.width === undefined || style.width === 'auto' ? context.allocation.width : style.width,</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>       height: style.height === undefined || style.height === 'auto' ? context.allocation.height : style.height }, context.constraints, { component: 'HtmlBox' });</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a>     return { ...stripLayoutParticipation(style), ...box, overflow: style.overflow ?? 'clip' } as Resolved;</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>   },</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a> });</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a> </span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a> registerOpaquePrimitiveProps(primitive, ['data']);</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a> setPrimitivePresentation&lt;Resolved&gt;(primitive, style =&gt; ({ transform: createGroupTransform(style) }));</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a> </span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a> type HtmlBoxInput&lt;Props&gt; = Omit&lt;PrimitiveInput&lt;HtmlBoxProps&lt;Props&gt;, HtmlBoxStyle&gt;, 'data' | 'mount'&gt; &amp; Pick&lt;HtmlBoxProps&lt;Props&gt;, 'data' | 'mount'&gt;;</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a> type HtmlBoxComponent = {</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>   &lt;Props&gt;(props: HtmlBoxInput&lt;Props&gt;): PibblRenderResult;</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a> };</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a> /**</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>  * A persistent native HTML box projected into Pibbl layout, transforms and clips.</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>  *</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a>  * @see {@link HtmlBoxProps}</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a>  * @see {@link HtmlBoxStyle}</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>  * @see {@link HtmlMountCallback}</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>  */</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a> export const HtmlBox = primitive as HtmlBoxComponent;</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a> </span></code></pre>

## Documentation version

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