Skip to content

Commit

Permalink
🎹 Improve keyboard accessibility (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Sep 30, 2024
1 parent 23e9f9a commit d175a30
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-steaks-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/site': patch
---

Change focus when selecting titles or skip-to-article
5 changes: 4 additions & 1 deletion packages/site/src/components/DocumentOutline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ const Headings = ({ headings, activeId }: Props) => (
href={`#${heading.id}`}
onClick={(e) => {
e.preventDefault();
const el = document.querySelector(`#${heading.id}`);
const el = document.querySelector(`#${heading.id}`) as HTMLElement | undefined;
if (!el) return;

el.scrollIntoView({ behavior: 'smooth' });
history.replaceState(undefined, '', `#${heading.id}`);
// Changes keyboard tab-index location
if (el.tabIndex === -1) el.tabIndex = -1;
el.focus({ preventScroll: true });
}}
// Note that the title can have math in it!
dangerouslySetInnerHTML={{ __html: heading.titleHTML }}
Expand Down
5 changes: 4 additions & 1 deletion packages/site/src/components/SkipToArticle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import React, { useCallback } from 'react';
function makeSkipClickHandler(hash: string) {
return (e: React.UIEvent<HTMLElement, Event>) => {
e.preventDefault();
const el = document.querySelector(`#${hash}`);
const el = document.querySelector(`#${hash}`) as HTMLElement;
if (!el) return;
(el.nextSibling as HTMLElement).focus();
history.replaceState(undefined, '', `#${hash}`);
// Changes keyboard tab-index location
if (el.tabIndex === -1) el.tabIndex = -1;
el.focus({ preventScroll: true });
};
}

Expand Down

0 comments on commit d175a30

Please sign in to comment.