fix(docs-infra): keep the collapsed code state when a code block is reshown

The code block lives inside `@if (showCode())`, so hiding it destroys the DOM and
showing it builds a fresh copy with no hidden lines. Nothing reapplied
`setCodeLinesVisibility()`, so a collapsed block came back showing the whole file.

Reapply it once the block is rendered again. It already branches on `expanded()`,
so a block that was expanded stays expanded.

(cherry picked from commit 90b9e081f5)
This commit is contained in:
Kam
2026-09-01 22:35:52 +03:00
committed by Matthew Beck
parent d880b77967
commit e44904ddda
2 changed files with 10 additions and 1 deletions
@@ -21,7 +21,7 @@
[class.docs-example-code-toggle__show]="!showCode()"
[attr.aria-checked]="showCode()"
[attr.aria-label]="showCode() ? 'Hide code' : 'Show code'"
(click)="showCode.set(!showCode())"
(click)="toggleCodeVisibility()"
[matTooltip]="showCode() ? 'Hide code' : 'Show code'"
matTooltipPosition="above"
>
@@ -116,6 +116,15 @@ export class ExampleViewer {
this.setCodeLinesVisibility();
}
toggleCodeVisibility(): void {
const showCode = !this.showCode();
this.showCode.set(showCode);
if (showCode) {
afterNextRender(() => this.setCodeLinesVisibility(), {injector: this.injector});
}
}
copyLink(): void {
// Reconstruct the URL using `origin + pathname` so we drop any pre-existing hash.
const fullUrl =