liveBook · Manning

Component offset

As explained in Build a Frontend Web Framework (From Scratch)

A component offset is the number of elements preceding a fragment-based component view in its parent element, equal to the index of the component’s first element there.

A component offset counts the elements before a fragment-based component view in its parent element. It equals the index of the component’s first element there. section 9.3.3

Fragment-relative indices do not include elements outside the component. Adding the offset converts move and add indices into parent-element indices. section 9.3.3 section 9.3.4

Implement the correction in two small steps:

  • Expose the component’s elements, first element, and offset; return zero when no host component exists. section 9.3.3 section 9.3.4
  • Pass the host component into patchDOM() and add its offset only to move and add indices. section 9.3.4
  • Leave remove and noop operations unadjusted because they do not address DOM nodes by index. section 9.3.4

Offset of one

If a component’s first element is the second child of its parent, its offset is 1. A fragment-relative move from index 1 to index 0 becomes a parent-relative move from index 2 to index 1. section 9.3.3 section 9.3.4

Figure 9.10 The offset of the component is 1.
figure

Use the figure to see the component’s first fragment element positioned after one external parent element. section 9.3.3

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Defines the offset, implements component getters, and integrates correction into patchDOM(); it stops short of production completeness.
Examples
Uses a component after a paragraph and demonstrates corrected move and add indices.
What it assumes
Assumes fragment views, parent elements, DOM indices, and array diff operations.
What you can do after
Apply it via patchDOM() to place fragment children correctly within a parent element.
How it connects
Connects fragments, virtual DOM, DOM patching, and array diff operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.