https://github.com/daviddarnes/heading-anchors
A Web Component to add anchor links to headings with IDs
https://github.com/daviddarnes/heading-anchors
component components customelement customelements webcomponent webcomponents
Last synced: 10 months ago
JSON representation
A Web Component to add anchor links to headings with IDs
- Host: GitHub
- URL: https://github.com/daviddarnes/heading-anchors
- Owner: daviddarnes
- License: mit
- Created: 2023-12-10T16:01:29.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T10:57:22.000Z (over 1 year ago)
- Last Synced: 2025-04-12T02:05:32.763Z (10 months ago)
- Topics: component, components, customelement, customelements, webcomponent, webcomponents
- Language: HTML
- Homepage: https://daviddarnes.github.io/heading-anchors/demo.html
- Size: 41 KB
- Stars: 28
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `heading-anchors`
A Web Component to add anchor links to headings with IDs.
**[Demo](https://daviddarnes.github.io/heading-anchors/demo.html)** | **[Styling demo](https://daviddarnes.github.io/heading-anchors/demo-styling.html)** | **[Further reading](https://darn.es/heading-anchors-web-component/)**
## Examples
```html
Heading level 2
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Heading level 3
Excepturi eligendi exercitationem, ratione, in delectus vitae veritatis
dolorem porro cupiditate quam eaque voluptates.
```
## Features
This Web Component allows you to:
- Add anchor links to headings which have an ID attribute and value
- Customise which elements will be appended with an anchor link using the `selector` attribute
- Customise where the anchor link is appended using the `position` attribute
## Installation
You have a few options (choose one of these):
1. Install via [npm](https://www.npmjs.com/package/@daviddarnes/heading-anchors): `npm install @daviddarnes/heading-anchors` (also avaialbe on [jsr](https://jsr.io/@daviddarnes/heading-anchors))
1. [Download the source manually from GitHub](https://github.com/daviddarnes/heading-anchors/releases) into your project.
1. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)
### Usage
Make sure you include the `` in your project (choose one of these):
```html
<!-- Host yourself -->
<script type="module" src="heading-anchors.js">
```
```html
```
```html
```
### Using `selector` and `position`
By default `heading-anchors` will select all the `h2[id]`, `h3[id]` and `h4[id]` elements within it to append an anchor link to. However with the `selector` attribute you can pass in a different selector string to select:
```html
```
_Note: It's recommended you use selectors that utilise the `[id]` attribute selector for better reliability_
By default `heading-anchors` will append anchor links right after the element, using the [`insertAdjacentHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML) API. However you can adjust this using the `position` attribute:
```html
```
## Credit
With thanks to the following people:
- [Zach Leatherman](https://zachleat.com) for inspiring this [Web Component repo template](https://github.com/daviddarnes/component-template)
- [Curtis Wilcox](https://github.com/extra808) for the accessibility feedback