Skip to content

Commit 7eebb00

Browse files
cscheidclaudecwickham
authored
Add skip-to-content link to Bootstrap-themed HTML output (#14685)
* Add skip-to-content link to bootstrap-themed HTML output (#14684) Inject a "Skip to main content" link as the first element of the body for all bootstrap-themed HTML output (documents, websites, books, dashboards) so keyboard users can bypass navigation blocks (WCAG 2.4.1 Bypass Blocks). The link is visually hidden until focused, targets the main content container (assigning an id and tabindex="-1" where missing), and its text is localizable via the new language key skip-to-content. Dashboards additionally mark their content container with role="main". Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Regenerate build artifacts for the skip-to-content language key Regenerated with `dev-call build-artifacts`, run twice: the schema registry that produces all-schema-definitions.json is partly seeded from the yaml-intelligence files on disk, so a new key reaches that file only on the second build. Part of this diff is pre-existing drift, not caused by this branch. The committed artifacts on main are stale relative to src/resources/schema, and all-schema-definitions.json is nondeterministic in its duplicate description counts. Running build-artifacts on an unmodified checkout of main produces the same four modified files. * Move focus from the dashboard skip link without setting the hash On a dashboard the skip link was a plain anchor jump, so the browser pushed a history entry for "#quarto-document-content". That entry names no page, so Back landed on a dead step: the URL changed but the visible page did not, and the reader had to press Back twice to leave the page they were on. Handle the click instead: move focus to the target and leave the URL alone. Back and Forward then behave as they do without the skip link, and a page hash such as "#sales" survives using the link, so the shareable URL still works. Without JS the link is still an ordinary in-page anchor. --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Charlotte Wickham <cwickham@gmail.com>
1 parent e2b8616 commit 7eebb00

33 files changed

Lines changed: 305 additions & 10 deletions

‎news/changelog-1.11.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ All changes included in 1.11:
1616
- ([#14818](https://github.com/quarto-dev/quarto-cli/issues/14818)): Fix a dashboard with more than one page going blank when the URL hash does not name a page, such as a footnote link or a cross-reference. Such a hash is now left alone, so the current page stays visible.
1717
- ([#14819](https://github.com/quarto-dev/quarto-cli/issues/14819)): Fix keyboard focus starting inside the page content when a dashboard opens at a page hash, such as `dashboard.html#sales`. Tabbing forward could not reach the navbar. Focus now starts at the top of the document.
1818

19+
### `html`
20+
21+
- ([#14684](https://github.com/quarto-dev/quarto-cli/issues/14684)): Add a "Skip to main content" link to Bootstrap-themed HTML output (documents, websites, books, dashboards) so keyboard users can bypass navigation blocks (WCAG 2.4.1). The link is the first tab stop on every page, visually hidden until focused, and its text is customizable via the `language` key `skip-to-content`. Dashboards additionally mark their content container as the `main` landmark.
22+
1923
## Engines
2024

2125
### `knitr`

‎src/config/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ export const kToggleNavigation = "toggle-navigation";
308308
export const kCopyButtonTooltip = "copy-button-tooltip";
309309
export const kCopyButtonTooltipSuccess = "copy-button-tooltip-success";
310310
export const kBackToTop = "back-to-top";
311+
export const kSkipToContent = "skip-to-content";
311312
export const kRepoActionLinksEdit = "repo-action-links-edit";
312313
export const kRepoActionLinksSource = "repo-action-links-source";
313314
export const kRepoActionLinksIssue = "repo-action-links-issue";
@@ -436,6 +437,7 @@ export const kLanguageDefaultsKeys = [
436437
kCopyButtonTooltip,
437438
kCopyButtonTooltipSuccess,
438439
kBackToTop,
440+
kSkipToContent,
439441
kRepoActionLinksEdit,
440442
kRepoActionLinksSource,
441443
kRepoActionLinksIssue,

‎src/config/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ import {
211211
kSelfContainedMath,
212212
kShiftHeadingLevelBy,
213213
kShortcodes,
214+
kSkipToContent,
214215
kSlideLevel,
215216
kSourceNotebookPrefix,
216217
kStandalone,
@@ -696,6 +697,7 @@ export interface FormatLanguage {
696697
[kCopyButtonTooltip]?: string;
697698
[kCopyButtonTooltipSuccess]?: string;
698699
[kBackToTop]?: string;
700+
[kSkipToContent]?: string;
699701
[kToggleDarkMode]?: string;
700702
[kToggleNavigation]?: string;
701703
[kToggleReaderMode]?: string;

‎src/format/dashboard/format-dashboard.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ function dashboardHtmlPostProcessor(
335335
// Mark the page container with layout instructions
336336
const containerEl = doc.querySelector("div.page-layout-custom");
337337
if (containerEl) {
338+
// dashboards have no <main> element, so mark the content container
339+
// as the main landmark for assistive technology
340+
containerEl.setAttribute("role", "main");
338341
const containerClz = [
339342
"quarto-dashboard-content",
340343
"bslib-gap-spacing",

‎src/format/html/format-html-bootstrap.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
kQuartoTemplateParams,
3030
kRelatedFormatsTitle,
3131
kSectionDivs,
32+
kSkipToContent,
3233
kTocDepth,
3334
kTocExpand,
3435
kTocLocation,
@@ -526,6 +527,10 @@ function bootstrapHtmlPostprocessor(
526527
doc.body.insertBefore(beforeBodyScript, doc.body.firstChild);
527528
}
528529

530+
// add a skip-to-content link as the first element of the body so that
531+
// keyboard users can bypass navigation (WCAG 2.4.1 Bypass Blocks)
532+
injectSkipLink(doc, format);
533+
529534
// Process the elements of this document into an appendix
530535
if (
531536
format.metadata[kAppendixStyle] !== false &&
@@ -545,6 +550,37 @@ function bootstrapHtmlPostprocessor(
545550
};
546551
}
547552

553+
function injectSkipLink(doc: Document, format: Format) {
554+
// resolve the link target, from most to least specific: the article-layout
555+
// main content element, a hand-authored main, the custom-layout container
556+
// (standalone custom layouts and dashboards), or the website custom-layout
557+
// content container
558+
const target = doc.querySelector("#quarto-document-content") ||
559+
doc.querySelector("main") ||
560+
doc.querySelector("div.page-layout-custom") ||
561+
doc.querySelector("#quarto-content");
562+
if (!target) {
563+
return;
564+
}
565+
if (!target.id) {
566+
target.id = "quarto-document-content";
567+
}
568+
// tabindex=-1 so that activating the link moves keyboard focus into the
569+
// content (not just scroll position) across browsers and assistive tech
570+
target.setAttribute("tabindex", "-1");
571+
572+
const skipLink = doc.createElement("a");
573+
skipLink.id = "quarto-skip-link";
574+
skipLink.classList.add("visually-hidden-focusable");
575+
skipLink.setAttribute("href", `#${target.id}`);
576+
skipLink.appendChild(
577+
doc.createTextNode(
578+
format.language[kSkipToContent] || "Skip to main content",
579+
),
580+
);
581+
doc.body.insertBefore(skipLink, doc.body.firstChild);
582+
}
583+
548584
function createLinkChild(formatLink: AlternateLink, doc: Document) {
549585
const link = doc.createElement("a");
550586
link.setAttribute("href", formatLink.href);

‎src/resources/editor/tools/vs-code.mjs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10373,6 +10373,7 @@ var require_yaml_intelligence_resources = __commonJS({
1037310373
"search-no-results-text": "string",
1037410374
"copy-button-tooltip": "string",
1037510375
"copy-button-tooltip-success": "string",
10376+
"skip-to-content": "string",
1037610377
"repo-action-links-edit": "string",
1037710378
"repo-action-links-source": "string",
1037810379
"repo-action-links-issue": "string",
@@ -25387,12 +25388,12 @@ var require_yaml_intelligence_resources = __commonJS({
2538725388
mermaid: "%%"
2538825389
},
2538925390
"handlers/mermaid/schema.yml": {
25390-
_internalId: 223031,
25391+
_internalId: 223755,
2539125392
type: "object",
2539225393
description: "be an object",
2539325394
properties: {
2539425395
"mermaid-format": {
25395-
_internalId: 223023,
25396+
_internalId: 223747,
2539625397
type: "enum",
2539725398
enum: [
2539825399
"png",
@@ -25408,7 +25409,7 @@ var require_yaml_intelligence_resources = __commonJS({
2540825409
exhaustiveCompletions: true
2540925410
},
2541025411
theme: {
25411-
_internalId: 223030,
25412+
_internalId: 223754,
2541225413
type: "anyOf",
2541325414
anyOf: [
2541425415
{

‎src/resources/editor/tools/yaml/all-schema-definitions.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎src/resources/editor/tools/yaml/web-worker.js‎

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/resources/editor/tools/yaml/yaml-intelligence-resources.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,7 @@
33413341
"search-no-results-text": "string",
33423342
"copy-button-tooltip": "string",
33433343
"copy-button-tooltip-success": "string",
3344+
"skip-to-content": "string",
33443345
"repo-action-links-edit": "string",
33453346
"repo-action-links-source": "string",
33463347
"repo-action-links-issue": "string",
@@ -18355,12 +18356,12 @@
1835518356
"mermaid": "%%"
1835618357
},
1835718358
"handlers/mermaid/schema.yml": {
18358-
"_internalId": 223031,
18359+
"_internalId": 223755,
1835918360
"type": "object",
1836018361
"description": "be an object",
1836118362
"properties": {
1836218363
"mermaid-format": {
18363-
"_internalId": 223023,
18364+
"_internalId": 223747,
1836418365
"type": "enum",
1836518366
"enum": [
1836618367
"png",
@@ -18376,7 +18377,7 @@
1837618377
"exhaustiveCompletions": true
1837718378
},
1837818379
"theme": {
18379-
"_internalId": 223030,
18380+
"_internalId": 223754,
1838018381
"type": "anyOf",
1838118382
"anyOf": [
1838218383
{

‎src/resources/formats/dashboard/quarto-dashboard.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
144144
}
145145
});
146146

147+
// Move focus for the skip-to-content link without changing the hash.
148+
// A plain anchor jump pushes a history entry that names no page, so Back
149+
// would land on a dead step: the URL changes but the page does not, and
150+
// the reader has to press Back twice. Without JS the link still works as
151+
// an ordinary in-page anchor.
152+
const skipLinkEl = document.getElementById("quarto-skip-link");
153+
if (skipLinkEl) {
154+
skipLinkEl.addEventListener("click", function (e) {
155+
const targetId = QuartoDashboardUtils.urlHash(
156+
skipLinkEl.getAttribute("href") || ""
157+
).substring(1);
158+
const targetEl = targetId ? document.getElementById(targetId) : null;
159+
if (targetEl) {
160+
e.preventDefault();
161+
targetEl.focus();
162+
}
163+
});
164+
}
165+
147166
// Hook tabs and use that to update history / active tabs
148167
const navItems = document.querySelectorAll(".navbar .nav-item .nav-link");
149168
for (const navItem of navItems) {

0 commit comments

Comments
 (0)