Page MenuHomePhabricator

[ToC] Dynamically expand/collapse sub-sections in ToC based on # of headings
Closed, ResolvedPublic3 Estimated Story Points

Description

Description

One of our main open questions when testing the table of contents was: should the sub-sections of the ToC be expanded or collapsed by default? After asking community and staff members, and further considering it ourselves, we've realized that it should depend on the article (more specifically, the height of the fully expanded table of contents). For some articles where the fully expanded table of contents is able to fit within one screen length it seems useful to show it fully expanded. For other articles, with taller ToCs that are not able to fit within one screen length when fully expanded, it seems more useful to have all of the sub-sections collapsed.

We therefore would like to have a rule that dynamically expands/collapses the sub-sections of the table of contents based on whether or not the whole thing can fit on a screen in its fully expanded state. For example:

short (expanded)medium (expanded)long (collapsed)
Screen Shot 2022-02-03 at 7.39.04 PM.png (987×1 px, 625 KB)
Screen Shot 2022-02-03 at 7.38.40 PM.png (987×1 px, 630 KB)
Screen Shot 2022-02-03 at 7.39.53 PM.png (987×1 px, 850 KB)

Expand/collapse logic

@Jdlrobson suggested using the number of headings in the article to determine the height of the fully expanded ToC. Based on font-size and line-height we can do some rough math to figure how many headings can fit into a standard screen height (~780px). Some headings will wrap onto more than one line so this isn't going to be perfect. We might also change the font-size and/or line-height in the future. So it is important that we're able to tweak the number in our logic here so we can fine-tune this until we get it right.

toc headings.jpg (970×792 px, 431 KB)

As a starting point we can say: if there are 19 headings or fewer on the page expand all the sub-sections, otherwise collapse all the sub-sections.

Developer notes

Pending merge of work from T299361, the default state of the TOC can be determined by passing in a config variable (to set the minimum number of sections for TOC to be expanded/collapsed) and comparing it to the total number of sections. See POC patch.

QA

Login to beta and enable vector-2022 skin

When article has < 20 sections
  1. Visit an article with < 20 sections. E.g.: https://en.wikipedia.beta.wmflabs.org/wiki/Dog?tableofcontents=1
  2. Assert that any section in the TOC that has subsections appears expanded
When article has >= 20 sections
  1. Visit an article with >= 20 sections. E.g.: https://en.wikipedia.beta.wmflabs.org/wiki/Cat?tableofcontents=1
  2. Assert that any section in the TOC that has subsections appears contracted

https://phabricator.wikimedia.org/T300973#7740516

QA Results - Beta|Prod

ACStatusDetails
1T300973#7740516
2T300973#7740516

Event Timeline

noting that using 19 headings violates the no magic numbers rule...open to better ideas 🙏

hi @alexhollender_WMF - between this ticket and T299361, can you clarify/confirm the following?

  • When TOC is expanded by default because it contains less than X (19 for now) sections, should scrolling collapse non-active sections or should TOC just remain expanded while scrolling?
  • When TOC is collapsed by default because it contains more than X (19 for now) sections, scrolling should expand active sections only and leave all other sections collapsed.

Also, do you mind pointing me to the most current prototype?

  • When TOC is expanded by default because it contains less than X (19 for now) sections, should scrolling collapse non-active sections or should TOC just remain expanded while scrolling?

All TOC sections should remain expanded while scrolling (also see T299361#7696657 for clarification)

  • When TOC is collapsed by default because it contains more than X (19 for now) sections, scrolling should expand active sections only and leave all other sections collapsed.

This is no longer the case. Scrolling will not automatically expand sections.

Also, do you mind pointing me to the most current prototype?

This is the latest, though note that it does not yet expand/collapse all sections based on # of headings (I will try to add that later today):
https://di-article-tools-da959.web.app/Humpback_whale

Change 761464 had a related patch set uploaded (by Clare Ming; author: Clare Ming):

[mediawiki/skins/Vector@master] POC: Set default expanded/collapsed state of Table of Contents

https://gerrit.wikimedia.org/r/761464

cjming removed cjming as the assignee of this task.Feb 9 2022, 8:52 PM
cjming updated the task description. (Show Details)
cjming subscribed.

Change 763624 had a related patch set uploaded (by Nray; author: Nray):

[mediawiki/skins/Vector@master] Dynamically expand/collapse sub-sections in ToC based on # of headings

https://gerrit.wikimedia.org/r/763624

nray subscribed.

Change 764433 had a related patch set uploaded (by Nray; author: Nray):

[mediawiki/core@master] Add `section-count` to data returned by SkinComponentTableOfContents

https://gerrit.wikimedia.org/r/764433

Change 764433 merged by jenkins-bot:

[mediawiki/core@master] Add `number-section-count` to data returned by SkinComponentTableOfContents

https://gerrit.wikimedia.org/r/764433

Change 763624 merged by jenkins-bot:

[mediawiki/skins/Vector@master] Dynamically expand/collapse sub-sections in ToC based on # of headings

https://gerrit.wikimedia.org/r/763624

nray updated the task description. (Show Details)
Edtadros subscribed.

Test Result - Prod

Status: ✅ PASS
Environment: enwiki
OS: macOS Monterey
Browser: Chrome
Device: MBP
Emulated Device:NA

Test Artifact(s):

QA Steps

Login and enable vector-2022 skin

✅ AC1: When article has < 20 sections
Visit an article with < 20 sections. E.g.: https://en.wikipedia.beta.wmflabs.org/wiki/Dog?tableofcontents=1
Assert that any section in the TOC that has subsections appears expanded

Screen Shot 2022-02-27 at 9.36.50 PM.png (1×868 px, 234 KB)

✅ AC2: When article has >= 20 sections
Visit an article with >= 20 sections. E.g.: https://en.wikipedia.beta.wmflabs.org/wiki/Cat?tableofcontents=1
Assert that any section in the TOC that has subsections appears contracted

Screen Shot 2022-02-27 at 9.37.22 PM.png (1×868 px, 311 KB)

Found one with exactly 20.

Screen Shot 2022-02-27 at 9.41.07 PM.png (1×868 px, 412 KB)

Screen Shot 2022-02-27 at 9.41.17 PM.png (1×868 px, 438 KB)

Change #761464 abandoned by Jdlrobson:

[mediawiki/skins/Vector@master] POC: Set default expanded/collapsed state of Table of Contents

Reason:

Hello this is an automated message.
I am abandoning this patch as it over a year old, and is not currently in a mergeable state. This has nothing to do with the quality of the patch.

If you still care about this patch, please feel free to restore it and rebase it, and we can happily continue the conversation to help you get it merged.

https://gerrit.wikimedia.org/r/761464