Skip to content

Commit 58c0ccc

Browse files
FEATURE: Add expand all button for TOC
This commit adds a button at the top of the TOC headings list called "Expand all". This will expand all of the subheadings in the list for easier searching. Clicking the button again will hide all the subheadings except the currently active one.
1 parent 05d454d commit 58c0ccc

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

common/common.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $padding-basis: 0.75em;
4646
transition: opacity 0.3s ease-in-out, max-height 0.3s ease-in-out;
4747
}
4848
&.active,
49+
&.expand-all,
4950
.d-toc-wrapper.overlay & {
5051
ul {
5152
max-height: 500em;

javascripts/discourse/components/toc-contents.gjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Component from "@glimmer/component";
2+
import DButton from "discourse/components/d-button";
23
import { tracked } from "@glimmer/tracking";
34
import { action } from "@ember/object";
45
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
@@ -21,6 +22,7 @@ export default class TocContents extends Component {
2122
@tracked activeHeadingId = null;
2223
@tracked headingPositions = [];
2324
@tracked activeAncestorIds = [];
25+
@tracked expandAll = false;
2426

2527
willDestroy() {
2628
super.willDestroy(...arguments);
@@ -36,6 +38,10 @@ export default class TocContents extends Component {
3638
return this.mappedTocStructure(this.args.tocStructure);
3739
}
3840

41+
get expandLabel() {
42+
return this.expandAll ? "Reset expand" : "Expand all";
43+
}
44+
3945
@action
4046
setup() {
4147
this.listenForScroll();
@@ -64,6 +70,11 @@ export default class TocContents extends Component {
6470
);
6571
}
6672

73+
@action
74+
toggleExpandAll() {
75+
this.expandAll = !this.expandAll;
76+
}
77+
6778
@debounce(RESIZE_DEBOUNCE)
6879
calculateHeadingPositions() {
6980
this.updateHeadingPositions();
@@ -160,6 +171,12 @@ export default class TocContents extends Component {
160171
{{didInsert this.setup}}
161172
{{didUpdate this.updateHeadingPositions @postID}}
162173
>
174+
<div class="d-toc-top-buttons">
175+
<DButton
176+
@action={{this.toggleExpandAll}}
177+
@translatedLabel={{this.expandLabel}}
178+
/>
179+
</div>
163180

164181
{{#each @tocStructure as |heading|}}
165182
<ul class="d-toc-heading">
@@ -168,6 +185,7 @@ export default class TocContents extends Component {
168185
@activeHeadingId={{this.activeHeadingId}}
169186
@activeAncestorIds={{this.activeAncestorIds}}
170187
@renderTimeline={{@renderTimeline}}
188+
@expandAll={{this.expandAll}}
171189
/>
172190
</ul>
173191
{{/each}}

javascripts/discourse/components/toc-heading.gjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ export default class TocHeading extends Component {
2525
? ` d-toc-${this.args.item.tagName}`
2626
: "";
2727
let activeClass = "";
28+
let expandAllClass = "";
2829
if (this.isActive) {
2930
activeClass = " direct-active active";
3031
} else if (this.isAncestorActive) {
3132
activeClass = " active";
3233
}
33-
return `${baseClass}${typeClass}${activeClass}`;
34+
if (this.args.expandAll) {
35+
expandAllClass = " expand-all";
36+
}
37+
return `${baseClass}${typeClass}${activeClass}${expandAllClass}`;
3438
}
3539

3640
@action

0 commit comments

Comments
 (0)