https://github.com/nullvoxpopuli/should-handle-link
Utility for handling link clicks with event delegation.
https://github.com/nullvoxpopuli/should-handle-link
Last synced: over 1 year ago
JSON representation
Utility for handling link clicks with event delegation.
- Host: GitHub
- URL: https://github.com/nullvoxpopuli/should-handle-link
- Owner: NullVoxPopuli
- License: mit
- Created: 2024-08-15T03:15:11.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-21T15:37:36.000Z (over 1 year ago)
- Last Synced: 2025-04-19T05:14:37.359Z (over 1 year ago)
- Language: JavaScript
- Size: 110 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# should-handle-link
A utility to help libraries and frameworks handle `` clicks, properly handling all the default behavior that comes with clicking links (ctrl+click, cmd+click, etc).
This allows folks to usue a single event listener on the document to handle all the links in their app.
## Installation
```bash
pnpm add should-handle-link
```
## Usage
```js
import { shouldHandle, getAnchor } from 'should-handle-link';
function handler(event) {
let anchor = getAnchor(event);
if (!shouldHandle(location.href, anchor, event)) {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
// Do single-page-app routing,
// or some other manual handling of the clicked anchor element
}
document.body.addEventListener('click', handler);
```
## License
MIT
## Related projects
- [internal-nav-helper](https://github.com/HenrikJoreteg/internal-nav-helper)
This porject has narrower scope and doesn't cover as many use cases (and is also outdated).