feat: use router for import links in package code#1307
feat: use router for import links in package code#1307danielroe merged 2 commits intonpmx-dev:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
📝 WalkthroughWalkthroughThe change modifies the Code Viewer component to implement client-side navigation for import links. A new Suggested reviewers
🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Comment |
| function handleImportLinkNavigate() { | ||
| if (!codeRef.value) return | ||
|
|
||
| const anchors = codeRef.value.querySelectorAll('a.import-link') | ||
| anchors.forEach(anchor => { | ||
| // NOTE: We do not need to remove previous listeners because we re-create the entire HTML content on each html update | ||
| anchor.addEventListener('click', event => { | ||
| const href = anchor.getAttribute('href') | ||
| if (href) { | ||
| event.preventDefault() | ||
| navigateTo(href) | ||
| } | ||
| }) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat -n app/components/Code/Viewer.vue | sed -n '55,85p'Repository: npmx-dev/npmx.dev
Length of output: 1006
🏁 Script executed:
head -n 61 app/components/Code/Viewer.vue | tail -n 40Repository: npmx-dev/npmx.dev
Length of output: 1141
🏁 Script executed:
# Check if there are any other uses of addEventListener on import-link anchors
rg "import-link" app/components/Code/Viewer.vueRepository: npmx-dev/npmx.dev
Length of output: 204
🏁 Script executed:
head -n 30 app/components/Code/Viewer.vueRepository: npmx-dev/npmx.dev
Length of output: 836
🏁 Script executed:
# Verify the full component structure to check Vue Composition API usage
wc -l app/components/Code/Viewer.vue && echo "---" && tail -n 30 app/components/Code/Viewer.vueRepository: npmx-dev/npmx.dev
Length of output: 911
🏁 Script executed:
head -n 100 app/components/Code/Viewer.vue | grep -E "^import|^const.*useTemplateRef|^const.*navigateTo"Repository: npmx-dev/npmx.dev
Length of output: 102
🏁 Script executed:
sed -n '1,15p' app/components/Code/Viewer.vueRepository: npmx-dev/npmx.dev
Length of output: 399
Preserve modified and auxiliary link behaviour before intercepting clicks.
Currently, all anchor clicks are prevented and routed via navigateTo, which blocks ctrl/cmd‑click, middle‑click, target="_blank", and download behaviours. Add guards so normal browser behaviour is preserved for these cases.
🔧 Suggested fix
anchors.forEach(anchor => {
// NOTE: We do not need to remove previous listeners because we re-create the entire HTML content on each html update
anchor.addEventListener('click', event => {
+ if (
+ event.defaultPrevented ||
+ event.button !== 0 ||
+ event.metaKey ||
+ event.ctrlKey ||
+ event.shiftKey ||
+ event.altKey ||
+ (anchor.getAttribute('target') && anchor.getAttribute('target') !== '_self') ||
+ anchor.hasAttribute('download')
+ ) {
+ return
+ }
const href = anchor.getAttribute('href')
if (href) {
event.preventDefault()
navigateTo(href)
}
})
})There was a problem hiding this comment.
This is sort of true I think, should we handle this? @danielroe
There was a problem hiding this comment.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
There was a problem hiding this comment.
I've made a fix locally. I'm not sure how merge queues work, so I'll wait for this to merge then send a follow-up PR.
Currently the import links in the package code page are raw anchor tags which aren't handled by Nuxt, which means they are handled by the browser and does a full page load when clicked.
I added some client-side code to attach click listeners and call
navigateToso that it uses our router for snappier navigation. I'm not sure if this is the best way but it might be the least complicated.