Skip to content

Commit

Permalink
⌨️ Simplifications to the skip-to-content handler (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Sep 27, 2023
1 parent 3868cc6 commit 8443761
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-onions-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/site': patch
---

Simplifications to skip-handler.
12 changes: 5 additions & 7 deletions packages/site/src/components/SkipToArticle.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { useCallback } from 'react';

function makeSkipClickHander(hash: string) {
function makeSkipClickHandler(hash: string) {
return (e: React.UIEvent<HTMLElement, Event>) => {
e.preventDefault();
const el = document.querySelector(`#${hash}`);
if (!el) return;
el.scrollIntoView({ behavior: 'smooth' });
(el.nextSibling as HTMLElement).focus();
history.replaceState(undefined, '', `#${hash}`);
(el.nextSibling as HTMLElement).focus({ preventScroll: true });
(e.target as HTMLElement).blur();
};
}

Expand All @@ -22,8 +20,8 @@ export function SkipToArticle({
const fm = 'skip-to-frontmatter';
const art = 'skip-to-article';

const frontmatterHander = useCallback(() => makeSkipClickHander(fm), [frontmatter]);
const articleHandler = useCallback(() => makeSkipClickHander(art), [article]);
const frontmatterHandler = useCallback(() => makeSkipClickHandler(fm), [frontmatter]);
const articleHandler = useCallback(() => makeSkipClickHandler(art), [article]);
return (
<div
className="fixed top-1 left-1 h-[0px] w-[0px] focus-within:z-40 focus-within:h-auto focus-within:w-auto bg-white overflow-hidden focus-within:p-2 focus-within:ring-1"
Expand All @@ -33,7 +31,7 @@ export function SkipToArticle({
<a
href={`#${fm}`}
className="block px-2 py-1 text-black underline"
onClick={frontmatterHander}
onClick={frontmatterHandler}
>
Skip to article frontmatter
</a>
Expand Down

0 comments on commit 8443761

Please sign in to comment.