Skip to content

Commit 09c707a

Browse files
committed
Add global click handler for routing with Shadow DOM support
1 parent f19565d commit 09c707a

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

.roo/rules/03-router-transitions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Apply smooth fade-in/fade-out effects to minimize visual disruption
2424
- Use an initial loading overlay in index.html to prevent flashes of unstyled content
2525
- Move route-specific content to web components to prevent hardcoded content flashes
26+
- Implement a global click handler that works with Shadow DOM to intercept all internal links
2627

2728
## Styling During Transitions
2829
- Maintain consistent theme colors during transitions

public/js/components/pf-hero.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,8 @@ class PfHero extends HTMLElement {
9191
</div>
9292
`;
9393

94-
// Add event listeners to the links to use the router
95-
this.shadowRoot.querySelectorAll('a').forEach(link => {
96-
link.addEventListener('click', (e) => {
97-
e.preventDefault();
98-
const href = link.getAttribute('href');
99-
window.router.navigate(href);
100-
});
101-
});
94+
// No need for specific click handlers anymore
95+
// The global router click handler will handle all links
10296
}
10397
}
10498

public/js/router.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,28 @@ class Router {
4444
this.navigate(window.location.pathname, false);
4545
});
4646

47-
// Intercept link clicks
47+
// Intercept all clicks at the document level (including those from Shadow DOM)
4848
document.addEventListener('click', (e) => {
49-
// Find closest anchor element
50-
const anchor = e.target.closest('a');
49+
// Check if the click path includes an anchor element
50+
// This works for both regular DOM and shadow DOM
51+
let anchor = null;
52+
let path = e.composedPath();
53+
54+
// Find the first anchor element in the event path
55+
for (let i = 0; i < path.length; i++) {
56+
if (path[i].tagName === 'A') {
57+
anchor = path[i];
58+
break;
59+
}
60+
}
5161

5262
// Skip if no anchor or if modifier keys are pressed
5363
if (!anchor || e.metaKey || e.ctrlKey || e.shiftKey) return;
5464

55-
// Skip if it's an external link or has a target
65+
// Get the href attribute
5666
const href = anchor.getAttribute('href');
67+
68+
// Skip if it's an external link or has a target
5769
if (!href || href.startsWith('http') || href.startsWith('//') || anchor.hasAttribute('target')) return;
5870

5971
// Skip if it's a download link
@@ -70,7 +82,9 @@ class Router {
7082

7183
// Navigate to the link
7284
this.navigate(href);
73-
});
85+
86+
console.log('Intercepted click on link:', href);
87+
}, { capture: true }); // Use capture to get events before they reach the shadow DOM
7488
}
7589

7690
/**

0 commit comments

Comments
 (0)