import { Component } from "../component/component"; import { xml } from "../tags"; import { Destination, RouterEnv } from "./router"; type Props = Destination; export class Link extends Component { static template = xml` `; href: string = this.env.router.destToPath(this.props); async willUpdateProps(nextProps) { this.href = this.env.router.destToPath(nextProps); } get isActive() { if (this.env.router.mode === "hash") { return (document.location).hash === this.href; } return (document.location).pathname === this.href; } navigate(ev) { // don't redirect with control keys if (ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) { return; } // don't redirect on right click if (ev.button !== undefined && ev.button !== 0) { return; } // don't redirect if `target="_blank"` if (ev.currentTarget && ev.currentTarget.getAttribute) { const target = ev.currentTarget.getAttribute("target"); if (/\b_blank\b/i.test(target)) { return; } } ev.preventDefault(); this.env.router.navigate(this.props); } }