mediawiki-skins-Vector/resources/skins.vector.styles/components/TableOfContents.less

149 lines
3.2 KiB
Plaintext
Raw Normal View History

@import '../../common/variables.less';
@subcategory-padding: 8px;
@fade-height: 40px;
@vertical-padding: 20px;
@toggle-icon-size: 1.834em;
.sidebar-toc {
display: none;
width: @width-sidebar;
max-height: 75vh;
padding: @vertical-padding @subcategory-padding @vertical-padding ~'calc( @{subcategory-padding} * 4 )';
.box-sizing( border-box );
overflow: auto;
background-color: @border-color-sidebar;
.sidebar-toc-header {
padding-bottom: 12px;
}
.sidebar-toc-title {
font-weight: bold;
font-size: @font-size-reset;
margin: 0;
padding: 0;
border: 0;
}
.sidebar-toc-link {
word-break: break-word;
> * {
// Prevent click events on the link's contents so that we can use event
// delegation and have the target be the anchor element instead.
pointer-events: none;
}
Add sectionObserver and tableOfContents component JS to respond to intersection changes This commits sets up the Table of Contents to bold the active section when the section is scrolled. Unfortunately, because our content does not have actual sections but instead has a flat list of headings and paragraphs, we can't use IntersectionObserver in the conventional way as it is optimized to find intersections of elements that are *within* the viewport and the callback will not reliably fire during certain scenarios (e.g. with fast scrolling or when the headings are not currently within the viewport). Furthermore, iterating through a list of elements and calling `getBoundingClientRect()` can be expensive and can also cause significant forced synchronous layouts that block the main thread. The best compromise in terms of performance and function that I've found is to use a combination of a throttled scroll event listener and IntersectionObserver's ability to asyncronously find the boundingClientRect of all elements off the main thread when `.observe` is called which is the approach this patch takes. Although this is an unorthodox way to use IntersectionObserver, performance profiles recorded while holding the "down" arrow and scrolling for 10 seconds with a 6x CPU throttle are comparable between master and this patch: master: https://phabricator.wikimedia.org/F34930737 this patch: https://phabricator.wikimedia.org/F34930738 Bug: T297614 Change-Id: I4077d86a1786cc1f4a7d85b20b7cf402960940e7
2022-01-21 20:15:34 +00:00
}
.sidebar-toc-numb {
display: none;
}
.sidebar-toc-text {
padding: ~'calc( @{subcategory-padding} / 2 )' 0;
}
.sidebar-toc-contents,
.sidebar-toc-list {
margin: 0;
list-style: none;
line-height: 18px;
}
.sidebar-toc-contents {
padding-bottom: ~'calc( @{vertical-padding} / 2 )';
}
.sidebar-toc-list-item {
display: block;
position: relative;
list-style-type: none;
padding-left: @subcategory-padding;
a {
color: @color-base;
font-size: @font-size-base;
}
}
.sidebar-toc-list-item.sidebar-toc-level-1 {
padding-left: 0;
& > a {
color: @color-link;
}
}
}
// Hide scrollbar but allow scrolling for Chrome, Safari, and Opera
.sidebar-toc::-webkit-scrollbar {
display: none;
}
// T302076 Add scrollable indicator as fade
.sidebar-toc:after {
content: '';
display: block;
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: @fade-height;
background: linear-gradient( rgba( 255, 255, 255, 0 ), @border-color-sidebar );
pointer-events: none; // Make the link below the fade clickable
margin: 0 @border-width-base;
}
// Highlight active section
.sidebar-toc .sidebar-toc-list-item-active > .sidebar-toc-link {
font-weight: bold;
}
.sidebar-toc .sidebar-toc-toggle {
// For no-js users, toggling is disabled and icon is hidden
display: none;
cursor: pointer;
}
// Collapse ToC sections by default, excluding no-js
.client-js .sidebar-toc {
.sidebar-toc-level-1 .sidebar-toc-list-item {
display: none;
}
.sidebar-toc-level-1.sidebar-toc-list-item-expanded .sidebar-toc-list-item {
display: block;
}
.sidebar-toc-toggle {
position: absolute;
top: 1px; // visually center icon
left: ~'calc( -1 * @{toggle-icon-size} - 1px )'; // leaves 6px between icon + text
display: block;
width: @toggle-icon-size; // ~22px @ 12
height: @toggle-icon-size;
font-size: 0.75em; // reduces size of toggle icon to 12px @ 16
transform: rotate( -90deg );
transition: @transition-duration-base;
}
.sidebar-toc-level-1.sidebar-toc-list-item-expanded .sidebar-toc-toggle {
transform: rotate( 0deg );
}
}
@media ( min-width: @width-breakpoint-desktop ) {
.sidebar-toc {
display: block;
}
}
// T300975 following media query for TOC experiment treatment
// class can be removed once associated A/B test is over.
@media ( max-width: @width-breakpoint-desktop ) {
.skin-vector-toc-experiment-treatment #toc {
display: table;
}
}