-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦶🎵 Add footnotes to bottom of page (#397)
- Loading branch information
Showing
16 changed files
with
188 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'myst-to-react': patch | ||
'@myst-theme/article': patch | ||
'@myst-theme/site': patch | ||
'@myst-theme/book': patch | ||
'@myst-theme/styles': patch | ||
--- | ||
|
||
Add footnotes to bottom of page in addition to hover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useXRefState } from '@myst-theme/providers'; | ||
import classNames from 'classnames'; | ||
|
||
export type HashLinkBehavior = { | ||
/** When scrolling, is this `instant`, `auto` or `scroll`? */ | ||
scrollBehavior?: ScrollBehavior; | ||
/** When updating the URL, do you push state or replace it? */ | ||
historyState?: 'replace' | 'push' | null; | ||
/** Change the keyboard tab-index location to the new element */ | ||
focusTarget?: boolean; | ||
}; | ||
|
||
function openDetails(el: HTMLElement | null) { | ||
if (!el) return; | ||
if (el.nodeName === 'DETAILS') { | ||
(el as HTMLDetailsElement).open = true; | ||
} | ||
openDetails(el.parentElement); | ||
} | ||
|
||
export function scrollToElement( | ||
el: HTMLElement | null, | ||
{ | ||
htmlId = el?.id, | ||
scrollBehavior = 'smooth', | ||
historyState = 'replace', | ||
focusTarget = true, | ||
}: { | ||
/** Update the URL fragment to this ID */ | ||
htmlId?: string; | ||
} & HashLinkBehavior = {}, | ||
) { | ||
if (!el) return; | ||
openDetails(el); | ||
el.scrollIntoView({ behavior: scrollBehavior }); | ||
if (historyState === 'push') { | ||
history.pushState(undefined, '', `#${htmlId}`); | ||
} else if (historyState === 'replace') { | ||
history.replaceState(undefined, '', `#${htmlId}`); | ||
} | ||
if (focusTarget) { | ||
// Changes keyboard tab-index location | ||
if (el.tabIndex === -1) el.tabIndex = -1; | ||
el.focus({ preventScroll: true }); | ||
} | ||
} | ||
|
||
export function HashLink({ | ||
id, | ||
kind, | ||
title = `Link to this ${kind}`, | ||
children = '¶', | ||
hover, | ||
className = 'font-normal', | ||
hideInPopup, | ||
scrollBehavior, | ||
historyState, | ||
focusTarget, | ||
}: { | ||
id?: string; | ||
kind?: string; | ||
title?: string; | ||
hover?: boolean; | ||
children?: '#' | '¶' | React.ReactNode; | ||
className?: string; | ||
hideInPopup?: boolean; | ||
} & HashLinkBehavior) { | ||
const { inCrossRef } = useXRefState(); | ||
if (inCrossRef || !id) { | ||
// If we are in a cross-reference pop-out, either hide hash link | ||
// or return something that is **not** a link | ||
return hideInPopup ? null : ( | ||
<span className={classNames('select-none', className)}>{children}</span> | ||
); | ||
} | ||
const scroll: React.MouseEventHandler<HTMLAnchorElement> = (evt) => { | ||
evt.preventDefault(); | ||
const el = document.getElementById(id); | ||
scrollToElement(el, { scrollBehavior, historyState, focusTarget }); | ||
}; | ||
return ( | ||
<a | ||
className={classNames('select-none no-underline text-inherit hover:text-inherit', className, { | ||
'transition-opacity opacity-0 focus:opacity-100 group-hover:opacity-70': hover, | ||
'hover:underline': !hover, | ||
})} | ||
onClick={scroll} | ||
href={`#${id}`} | ||
title={title} | ||
aria-label={title} | ||
> | ||
{children} | ||
</a> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useGridSystemProvider, useReferences } from '@myst-theme/providers'; | ||
import classNames from 'classnames'; | ||
import type { GenericNode } from 'myst-common'; | ||
import type { FootnoteDefinition, FootnoteReference } from 'myst-spec-ext'; | ||
import { HashLink, MyST } from 'myst-to-react'; | ||
import { selectAll } from 'unist-util-select'; | ||
|
||
export function Footnotes() { | ||
const references = useReferences(); | ||
const grid = useGridSystemProvider(); | ||
const defs = selectAll('footnoteDefinition', references?.article) as FootnoteDefinition[]; | ||
const refs = selectAll('footnoteReference', references?.article) as FootnoteReference[]; | ||
if (defs.length === 0) return null; | ||
return ( | ||
<section id="footnotes" className={classNames(grid, 'subgrid-gap col-screen')}> | ||
<div> | ||
<header className="text-lg font-semibold text-stone-900 dark:text-white group"> | ||
Footnotes | ||
<HashLink id="footnotes" title="Link to Footnotes" hover className="ml-2" /> | ||
</header> | ||
</div> | ||
<div className="pl-3 mb-8 text-xs text-stone-500 dark:text-stone-300"> | ||
<ol> | ||
{defs.map((fn) => { | ||
return ( | ||
<li key={(fn as GenericNode).key} id={`fn-${fn.identifier}`} className="group"> | ||
<div className="flex flex-row"> | ||
<div className="break-words grow"> | ||
<MyST ast={fn.children} /> | ||
</div> | ||
<div className="flex flex-col grow-0"> | ||
{refs | ||
.filter((ref) => ref.identifier === fn.identifier) | ||
.map((ref) => ( | ||
<HashLink | ||
key={(ref as GenericNode).key} | ||
id={`fnref-${(ref as GenericNode).key}`} | ||
title="Link to Content" | ||
hover | ||
className="p-1" | ||
children="↩" | ||
scrollBehavior="instant" | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</li> | ||
); | ||
})} | ||
</ol> | ||
</div> | ||
</section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
@import './hover.css'; | ||
@import './proof.css'; | ||
@import './toc.css'; | ||
@import './backmatter.css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
@layer base { | ||
#footnotes p { | ||
margin: 0.25rem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters