Quoting @alexhollender_WMF in T317661,
Currently a section is considered active (and therefore bolded in the table of contents) once the top of it's heading begins to intersect with the top of the viewport (or the bottom of the sticky header).
Since the heading top—viewport top relative position doesn't necessarily imply which section is the first visible on the page, this can result in a mismatch between the user's subjective perception of which section is "current" and the highlighted section. This is a prime example:
The whitespace between the sections here subjectively belongs to the "Definitions" section, but technically, since the heading top starts a section, it's a part of the lead section.
I assume there are two possible ways to solve this problem:
- Use a fixed threshold for distance between the top of the viewport and the top of the heading. When the distance is lower than the threshold, the section is assumed active. This solution is proposed in T317661 (100px is proposed as a threshold).
- Use a more refined calculation that could require additional sources of data (other than Intersection Observer API which is used under the hood).
The first solution doesn't take into account heading margins which may vary substantially based on the section level and the skin. For instance, <h2> and <h3> in Vector have different padding-margin values and structure (only padding is considered part of an element). Hence a fixed threshold will work differently for them (the threshold is counted from blue/green areas, leaving out orange ones):
These are the actual boxes used by Intersection Observer API to calculate intersections:
Another question is,
- should the first actually visible section be considered active (so that only whitespace is excluded from the top section),
- or should we make an assumption about what should be the "right" correspondence between what the user sees on the page and which section is highlighted as active. This may mean we will consider a section active once the last line of the previous section stops being fully visible, or something like that.
Britannica, for instance, uses a fixed threshold, although practically the "section step" only happens when the last line stops being fully visible: