Skip to content

Commit

Permalink
⏭️ add SkipTo and use it in themes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejpurves committed Jul 1, 2024
1 parent f685454 commit e7c7f4b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-dragons-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/site': patch
---

Marking the opinionated SkipToArticle component as deprecated in favor of a configurable SkipTo component
29 changes: 28 additions & 1 deletion packages/site/src/components/SkipToArticle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import React, { useCallback } from 'react';

function makeSkipClickHandler(hash: string) {
return (e: React.UIEvent<HTMLElement, Event>) => {
Expand All @@ -10,6 +10,10 @@ function makeSkipClickHandler(hash: string) {
};
}

/**
* @deprecated use `SkipTo` instead with a list of targets
*
*/
export function SkipToArticle({
frontmatter = true,
article = true,
Expand Down Expand Up @@ -48,3 +52,26 @@ export function SkipToArticle({
</div>
);
}

/**
* Add a skip navigation unit with links based on a list of targets
*/
export const SkipTo = React.memo(({ targets }: { targets: { id: string; title: string }[] }) => {
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"
aria-label="skip to content options"
>
{targets.map(({ id, title }) => (
<a
key={id}
href={`#${id}`}
className="block px-2 py-1 text-black underline"
onClick={makeSkipClickHandler(id)}
>
{title}
</a>
))}
</div>
);
});
4 changes: 2 additions & 2 deletions themes/article/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getMetaTagsForSite,
getThemeSession,
ContentReload,
SkipToArticle,
SkipTo,
} from '@myst-theme/site';
import { Outlet, useLoaderData } from '@remix-run/react';
export { AppCatchBoundary as CatchBoundary } from '@myst-theme/site';
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function AppWithReload() {
baseurl={BASE_URL}
top={0}
>
<SkipToArticle frontmatter={false} />
<SkipTo targets={[{ id: 'skip-to-article', title: 'Skip to article content' }]} />
<Outlet />
</Document>
);
Expand Down
9 changes: 7 additions & 2 deletions themes/book/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getMetaTagsForSite,
getThemeSession,
ContentReload,
SkipToArticle,
SkipTo,
} from '@myst-theme/site';
import { Outlet, useLoaderData } from '@remix-run/react';
export { AppCatchBoundary as CatchBoundary } from '@myst-theme/site';
Expand Down Expand Up @@ -68,7 +68,12 @@ export default function AppWithReload() {
staticBuild={MODE === 'static'}
baseurl={BASE_URL}
>
<SkipToArticle />
<SkipTo
targets={[
{ id: 'skip-to-frontmatter', title: 'Skip to article frontmatter' },
{ id: 'skip-to-article', title: 'Skip to article content' },
]}
/>
<Outlet />
</Document>
);
Expand Down

0 comments on commit e7c7f4b

Please sign in to comment.