I have a page that has a nav bar with a "Quarters" link. Under the Quarters link, when the user is on the /quarters
route, a list of quarters will be shown, like 2019Q2 etc. The url will be /quarters/2019q2
.
I want to make a component that show shows a hyperlink that will have the selected
class if the current url matches the href of the link. Here's the closest I can get:
<script>
import { afterUpdate } from 'svelte';
export let segment;
export let text = 'text here';
export let link;
let isCurrentPath = false;
console.log(segment, link, text);
afterUpdate(() => {
if (window.location.pathname.includes(link)) {
isCurrentPath = true;
debugger;
}
console.log('HL afterUpdate ', window.location);
});
</script>
<style>
/* omitted */
</style>
<a class:selected={segment && isCurrentPath} href={link}>{text}</a>
That works fine for the first load, but when the user navigates to a different data page the selection is not updated. How do I get some code to only run on the client-side? If I access the window
object outside of afterUpdate
I will get an null ref error from the server-side code.
ETA: Tried this too:
let isCurrentPath = false;
let path = typeof window === 'undefined' ? '' : window.location.pathname;
$: if (path) isCurrentPath = window.location.pathname.includes(link);
That code doesn't fire when the user clicks one of the data links. Tried onMount
as well with no positive result.