-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more accessible heading-anchors w/ anchored-heading wrapper divs (#44)
* more accessible heading-anchors with anchored-heading wrapper divs see conversation on mastodon at: https://tweesecake.social/@weirdwriter/112939207207501581 * add --anchored-heading-offset for mobile safari * post + anchored-heading margin fixes and css cleanup * spaces to tabs
- Loading branch information
Showing
7 changed files
with
128 additions
and
43 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
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
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,53 @@ | ||
/** | ||
* Thanks to @daviddarnes/heading-anchors 2.0.0 | ||
* https://github.com/daviddarnes/heading-anchors | ||
* and 11ty | ||
* https://github.com/11ty/11ty-website/blob/main/src/_includes/components/heading-anchors.js | ||
*/ | ||
class HeadingAnchors extends HTMLElement { | ||
static register(tagName) { | ||
if ("customElements" in window) { | ||
customElements.define(tagName || "heading-anchors", HeadingAnchors); | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
this.headings.forEach((heading) => { | ||
if ( | ||
heading.hasAttribute("data-heading-anchors-optout") === false | ||
) { | ||
heading.insertAdjacentHTML(this.position, this.anchor(heading)); | ||
heading.remove(); | ||
} | ||
}); | ||
} | ||
|
||
anchor(heading) { | ||
// TODO this would be good use case for shadow dom | ||
let anchor = document.createElement("a"); | ||
anchor.setAttribute("data-pagefind-ignore", ""); | ||
anchor.href = `#${heading.id}`; | ||
anchor.classList.add("direct-link"); | ||
anchor.innerHTML = `<span class="sr-only">Jump to heading</span><span aria-hidden="true">#</span>`; | ||
let flexybox = document.createElement("div"); | ||
flexybox.classList.add("anchored-heading"); | ||
flexybox.innerHTML = `${heading.outerHTML}${anchor.outerHTML}`; | ||
return flexybox.outerHTML; | ||
} | ||
|
||
get headings() { | ||
return this.querySelectorAll( | ||
this.selector.split(",").map((entry) => `${entry.trim()}[id]`) | ||
); | ||
} | ||
|
||
get selector() { | ||
return this.getAttribute("selector") || "h1,h2,h3,h4"; | ||
} | ||
|
||
get position() { | ||
return this.getAttribute("position") || "beforebegin"; | ||
} | ||
} | ||
|
||
HeadingAnchors.register(); |