-
Notifications
You must be signed in to change notification settings - Fork 0
#14: Link click listeners #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -80,7 +80,7 @@ export function isLocalURL(url) { | |||||||||||
| export function bootstrap() { | ||||||||||||
| console.assert( | ||||||||||||
| !!window, | ||||||||||||
| "boostrap error: must be called from within a browser context", | ||||||||||||
| "bootstrap error: must be called from within a browser context", | ||||||||||||
| ); | ||||||||||||
| const link = window.document.querySelector( | ||||||||||||
| 'head link[rel*="alternate"][type="application/vnd.api+json"]', | ||||||||||||
|
|
@@ -93,7 +93,35 @@ export function bootstrap() { | |||||||||||
| ) | ||||||||||||
| : new URL(link.getAttribute("href")); | ||||||||||||
| const client = new Client(initialURL.href); | ||||||||||||
| // Register a global listener for history updates. | ||||||||||||
|
|
||||||||||||
| // Add click event listener to div#app | ||||||||||||
| const appDiv = document.getElementById("app"); | ||||||||||||
| console.assert(!!appDiv, "bootstrap error: missing div#app element"); | ||||||||||||
| appDiv.addEventListener("click", (event) => { | ||||||||||||
| // Check if the target is an anchor element | ||||||||||||
| if (!(event.target instanceof HTMLAnchorElement)) return; | ||||||||||||
|
|
||||||||||||
| const anchor = event.target; | ||||||||||||
|
Comment on lines
+102
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too strict, for example the markup could be something like
Suggested change
|
||||||||||||
| const href = anchor.href; // Safe to access href directly | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the
Suggested change
|
||||||||||||
|
|
||||||||||||
| // Check if href is a relative URL | ||||||||||||
| const isRelative = href && | ||||||||||||
| (href.startsWith("./") || href.startsWith("/") || | ||||||||||||
| !/^(?:[a-z]+:)?\/\//i.test(href)); | ||||||||||||
|
||||||||||||
| if (!isRelative) return; | ||||||||||||
|
|
||||||||||||
| // Check if anchor has a type attribute (opt-out) | ||||||||||||
| if (anchor.hasAttribute("type")) return; | ||||||||||||
gabesullice marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
|
||||||||||||
| // Prevent default behavior and stop propagation | ||||||||||||
| event.preventDefault(); | ||||||||||||
| event.stopPropagation(); | ||||||||||||
|
|
||||||||||||
| // Call the client's follow function | ||||||||||||
| client.follow(href, {}); | ||||||||||||
|
||||||||||||
| client.follow(href, {}); | |
| client.follow(href); |
Uh oh!
There was an error while loading. Please reload this page.