Skip to content

Commit

Permalink
[v3] attach heading anchor id attribute to heading (like Pagefind d…
Browse files Browse the repository at this point in the history
…o) and fix heading anchor styles when `theme.typesetting: 'article'` is set (#3132)

aa
  • Loading branch information
dimaMachina committed Aug 21, 2024
1 parent 49a9627 commit 150184b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changeset/moody-badgers-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nextra-theme-blog': patch
'nextra-theme-docs': patch
'nextra': patch
---

attach heading anchor `id` attribute to heading (like Pagefind do) and fix heading anchor styles when `theme.typesetting: 'article'` is set
2 changes: 1 addition & 1 deletion packages/nextra-theme-blog/src/mdx-theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function HeadingLink({
}: ComponentProps<'h2'> & { tag: `h${2 | 3 | 4 | 5 | 6}` }): ReactElement {
return (
<Tag
id={id}
className={
// can be added by footnotes
className === 'sr-only' ? '_sr-only' : `subheading-${Tag}`
Expand All @@ -46,7 +47,6 @@ function HeadingLink({
{id && (
<a
href={`#${id}`}
id={id}
className="_not-prose subheading-anchor"
aria-label="Permalink for this section"
/>
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra-theme-docs/css/typesetting-article.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ article.nextra-body-typesetting-article {
pre code {
@apply dark:_bg-transparent;
}
.subheading-anchor + a {
@apply _no-underline hover:_no-underline after:_hidden;
.subheading-anchor {
@apply hover:_no-underline;
}
}
2 changes: 1 addition & 1 deletion packages/nextra-theme-docs/src/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const createHeading = (

return (
<Tag
id={id}
className={
// can be added by footnotes
className === 'sr-only'
Expand All @@ -84,7 +85,6 @@ const createHeading = (
{id && (
<a
href={`#${id}`}
id={id}
className="subheading-anchor"
aria-label="Permalink for this section"
ref={obRef}
Expand Down
12 changes: 7 additions & 5 deletions packages/nextra/styles/subheading-anchor.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
.subheading-anchor {
@apply _opacity-0 _transition-opacity ltr:_ml-1 rtl:_mr-1;

:hover > &,
:target > &,
&:hover,
&:focus {
@apply _opacity-100;
}

:target > &:after {
@apply _text-gray-400;
@apply dark:_text-neutral-500;
}

&:after {
@apply _content-['#'] _px-1;
@apply _text-gray-300 dark:_text-neutral-700;
span:target + & {
@apply _text-gray-400;
@apply dark:_text-neutral-500;
}
}
}

0 comments on commit 150184b

Please sign in to comment.