Skip to content

Commit 095f766

Browse files
authored
Enable highlight-intrapage-link sidebar highlight in News (#1910)
1 parent ba0a014 commit 095f766

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

sass/_vars.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ $color-white: #fff;
349349
--todo-bg-color: rgba(191, 170, 64, 0.3);
350350
--todo-border-color: rgba(223, 191, 32, 0.5);
351351

352+
--tree-menu-active-color: var(--color-neutral-93);
352353
--tree-menu-chevron-filter: brightness(70%);
353354
--tree-menu-label-active-bg-color: #{rgba($color-neutral-93, 0.1)};
354355
--tree-menu-label-active-color: var(--color-neutral-93);
@@ -476,6 +477,7 @@ $color-white: #fff;
476477
--todo-bg-color: rgba(191, 170, 64, 0.3);
477478
--todo-border-color: rgba(223, 191, 32, 0.5);
478479

480+
--tree-menu-active-color: var(--color-black);
479481
--tree-menu-chevron-filter: invert(65%);
480482
--tree-menu-label-active-bg-color: var(--color-neutral-88);
481483
--tree-menu-label-active-color: var(--color-black);

sass/components/_on-this-page.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
color: var(--on-this-page-hover-color);
3333
}
3434

35-
&[data-active="true"] {
35+
&[data-highlight-intrapage-link-active=true] {
3636
color: var(--on-this-page-active-color);
3737
}
3838
}

sass/components/_tree-menu.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
&, &:focus, &:active, &:hover, &:link, &:visited {
5858
color: var(--tree-menu-label-color);
5959
}
60+
61+
&[data-highlight-intrapage-link-active=true] {
62+
color: var(--tree-menu-active-color);
63+
}
6064
}
6165

6266
&__toggle {
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! in nav.on-this-page, sets data-active=true on the link to the header you're currently looking at
1+
//! For elements with `[data-highlight-intrapage-link]`, sets
2+
//! `data-highlight-intrapage-link-active=true` to the descendant `<a>` elements
3+
//! (for intra-page navigation) that link to the top-most visible heading.
24

35
if(window.location.hash == ""){
46
otp_set_active(document.querySelector("main h2") || document.querySelector("main h3"));
@@ -7,21 +9,26 @@ if(window.location.hash == ""){
79
}
810

911
/**
10-
* remember which elements are on screen. IntersectionObserver only sends us updates.
12+
* Remember which elements are on screen. `IntersectionObserver` only sends us updates.
1113
* @type {Map<Element, boolean>}
1214
*/
1315
let otp_state = new Map();
1416

1517
/**
16-
* give data-active=true to the page nav link which points to the given id; set all others to false.
17-
* accepts an id string or a DOM node which it will automatically retrieve the id of
18-
* @param {string | HTMLElement} id_or_node
18+
* Set `data-highlight-intrapage-link-active=true` to the page links which point
19+
* to the given id; set all others to `false`. Accepts an id string or a DOM
20+
* node which it will automatically retrieve the id of.
1921
*/
2022
function otp_set_active(id_or_node){
2123
let id = `#${id_or_node instanceof HTMLElement ? id_or_node.getAttribute("id") : id_or_node}`;
2224

23-
document.querySelectorAll(".on-this-page a").forEach(a => {
24-
a.setAttribute("data-active", a.getAttribute("href") == id);
25+
document.querySelectorAll("[data-highlight-intrapage-link] a").forEach(a => {
26+
const href = a.getAttribute("href");
27+
28+
if (href?.includes('#')) {
29+
const fragment = href.substring(href.indexOf('#'));
30+
a.setAttribute("data-highlight-intrapage-link-active", String(id === fragment));
31+
}
2532
});
2633
}
2734

templates/docs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{% endfor %}
1616

1717
<script src="/optional-helpers.js"></script>
18-
<script defer src="/on-this-page.js"></script>
18+
<script defer src="/highlight-intrapage-link.js"></script>
1919
{% endblock head_extensions %}
2020

2121
{% block mobile_page_menu %}

templates/macros/docs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
</ul>
108108
{% endmacro %}
109109
{% macro on_this_page(toc, class="") %}
110-
<nav aria-label="On This Page" class="on-this-page {{ class }}">
110+
<nav aria-label="On This Page" class="on-this-page {{ class }}" data-highlight-intrapage-link>
111111
{# screen readers will read this as 'Navigation: On This Page' #}
112112
<ul>
113113
{% for h1 in toc %}

templates/macros/news.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{% endmacro %}
3434
{% macro news_menu(toc, max_levels) %}
3535
{% if toc %}
36-
<ul class="tree-menu">
36+
<ul class="tree-menu" data-highlight-intrapage-link>
3737
{% for header in toc %}{{ self::news_menu_row(header=header, max_levels=max_levels, level=1) }}{% endfor %}
3838
</ul>
3939
{% endif %}

templates/news-page.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
{% block page_name %}Post{% endblock page_name %}
55
{% block mobile_page_menu %}{{ news_macros::news_menu(toc=page.toc, max_levels=1) }}{% endblock mobile_page_menu %}
66
{% block page_menu %}{{ news_macros::news_menu(toc=page.toc, max_levels=2) }}{% endblock page_menu %}
7+
8+
{% block head_extensions %}
9+
<script defer src="/highlight-intrapage-link.js"></script>
10+
{% endblock head_extensions %}
11+
712
{% block page_content %}
813
<h1 class="news-title">{{ page.title }}</h1>
914
<h2 class="news-subtitle">

0 commit comments

Comments
 (0)