Skip to content

Commit 6968a3a

Browse files
author
Nikos Katsikanis
committed
make router links from anywhere
1 parent ca40b41 commit 6968a3a

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

css/button-badge.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ button,
3838
}
3939
&.transparent {
4040
background-color: transparent;
41-
color: var(--default-text);
42-
border: 1px solid var(--default-text);
41+
color: white;
42+
border: 1px solid white;
4343
&:hover {
4444
color: var(--primary-color);
45-
background-color: var(--default-text);
45+
background-color: white;
4646
}
4747
}
4848

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<nav data-component="nav" ></nav>
3030
-->
3131
<main data-component="router" data-use-hash class="p-md">
32-
<!--components in /pages that will be fetched by router when user clicks on a data-nav -->
32+
<!--components in /pages that will be fetched by router when user clicks on a -->
3333
</main>
3434
</div>
3535
<footer class="flex flex-col align-center gap-md">

js/components/nav.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,35 +109,36 @@ export default (hostComponent) => {
109109
// Render the navigation items
110110
hostComponent.innerHTML = `
111111
<style>${navStyles}</style>
112-
<a data-nav href="/" title="Home">
112+
<a href="/" title="Home">
113113
<span class="icon">&#x1F3E0;</span>
114114
<span class="text">Home</span>
115115
</a>
116-
<a data-nav href="/button-badge" title="Button + Badges Design System">
116+
<a href="/button-badge" title="Button + Badges Design System">
117117
<span class="icon">&#x1F518;</span>
118118
<span class="text">Button + Badges</span>
119119
</a>
120-
<a data-nav href="/form" title="Form Design System">
120+
<a href="/form" title="Form Design System">
121121
<span class="icon">&#x270F;</span>
122122
<span class="text">Form</span>
123123
</a>
124-
<a data-nav href="/heros" title="Heros">
124+
<a href="/heros" title="Heros">
125125
<span class="icon">&#x1F9B8;</span> <!-- Superhero icon -->
126126
<span class="text">Heros</span>
127127
</a>
128-
<a data-nav href="/maps" title="Map example">
128+
129+
<a href="/maps" title="Map example">
129130
<span class="icon">&#x1F5FA;</span>
130131
<span class="text">Maps</span>
131132
</a>
132-
<a data-nav href="/calendar" title="Calendar Example">
133+
<a href="/calendar" title="Calendar Example">
133134
<span class="icon">📆</span>
134135
<span class="text">Calendar</span>
135136
</a>
136-
<a data-nav href="/multiple-instances" title="Multiple instances">
137+
<a href="/multiple-instances" title="Multiple instances">
137138
<span class="icon">🧬</span>
138139
<span class="text">Multiple instances</span>
139140
</a>
140-
<a data-nav href="/cookies" title="Elementary cookie popup permissions thing">
141+
<a href="/cookies" title="Elementary cookie popup permissions thing">
141142
<span class="icon">🍪</span>
142143
<span class="text">Cookie popup</span>
143144
</a>

js/components/router.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Router module for handling client-side routing.
33
* @module components/router
4-
* @version 0.1.1
4+
* @version 0.1.2
55
*/
66

77
import { importComponents, runComponents } from '../componentLoader.js';
@@ -54,29 +54,38 @@ export default async (hostComponent) => {
5454
}
5555
};
5656

57-
document.querySelectorAll('a[data-nav]').forEach((link) => {
58-
link.addEventListener('click', async (event) => {
59-
event.preventDefault();
60-
const url = event.currentTarget.getAttribute('href');
57+
// Global click event listener to handle all internal links
58+
document.addEventListener('click', async (event) => {
59+
const link = event.target.closest('a[href]');
6160

62-
if (useHash) {
63-
const baseURL = window.location.pathname.endsWith('/')
64-
? window.location.origin + window.location.pathname
65-
: window.location.origin + window.location.pathname + '/';
66-
location.href = `${baseURL}#${url}`;
67-
} else {
68-
history.pushState(null, null, url);
69-
await loadRoute(url);
70-
}
71-
});
61+
if (!link) return; // Ignore clicks outside links
62+
63+
const url = link.getAttribute('href');
64+
65+
// Ignore links with file extensions or external links
66+
if (/\.\w+$/.test(url) || link.origin !== window.location.origin) return;
67+
68+
event.preventDefault();
69+
70+
if (useHash) {
71+
const baseURL = window.location.pathname.endsWith('/')
72+
? window.location.origin + window.location.pathname
73+
: window.location.origin + window.location.pathname + '/';
74+
location.href = `${baseURL}#${url}`;
75+
} else {
76+
history.pushState(null, null, url);
77+
await loadRoute(url);
78+
}
7279
});
7380

81+
// Handle browser back/forward navigation
7482
addEventListener('popstate', async () => {
7583
if (!useHash) {
7684
await loadRoute(location.pathname);
7785
}
7886
});
7987

88+
// Handle initial load based on useHash
8089
if (useHash) {
8190
addEventListener('hashchange', async () => {
8291
const url = location.hash.substring(1); // Get the URL from the hash, remove the leading '#'

js/routes/button-badge.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default (hostComponent) => {
4646
</div>
4747
4848
<div style="background: linear-gradient(135deg, #0a90e2, #f013fe); padding: 1rem; border-radius: 0.5rem;">
49-
<p>wireframe (hover matches secondary)</p>
49+
<p>wireframe (hover background matches minor text)</p>
5050
<div>
5151
<button class="wireframe">Text</button>
5252
<button class="wireframe">
@@ -56,7 +56,7 @@ export default (hostComponent) => {
5656
</div>
5757
</div>
5858
<div style="background: linear-gradient(135deg, #0a90e2, #f013fe); padding: 1rem; border-radius: 0.5rem;">
59-
<p>Transparent (hover matches primary)</p>
59+
<p>Transparent - Dark and light same - designed for <a href="/heros">heros</a> (hover text matches primary)</p>
6060
<div>
6161
<button class="transparent">Text</button>
6262
<button class="transparent">

0 commit comments

Comments
 (0)