-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrouter-link.js
28 lines (24 loc) · 951 Bytes
/
router-link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { routeTo, ROUTERS, getCurrentUrl } from './element-router.js';
export class RouterLink extends HTMLElement {
connectedCallback() {
this.href = this.href || this.getAttribute('href');
this.activeClass = this.activeClass || this.getAttribute('active-class') || 'active';
this.innerHTML = `<a href="${this.href}">${this.innerHTML}</a>`;
setTimeout(_ => {
this.update();
ROUTERS[0].addEventListener('routechange', () => {
this.update();
});
this.children[0].addEventListener('click', event => {
event.stopPropagation();
event.preventDefault();
routeTo(this.href);
return false;
});
}, 0);
}
update() {
this.children[0].className = this.href === getCurrentUrl() ? this.activeClass : '';
}
}
customElements.define('router-link', RouterLink);