Active Links
What are active links? It's the practice of visually highlighting the current page in the navigation. For example, this page named "Active Links" is highlighted with a gray background in the navigation on the left of this website. The link is said to be "active".
To implement active links:
- Create a new component
<Link>. <Link>usesusePageContext()to accesspageContext.urlPathname.<Link>checks whetherconst isActive = href === pageContext.urlPathnameand sets a CSS class accordingly<a class="is-active">.
Examples:
- React: /packages/create-vike-core/boilerplate-react/renderer/Link.jsx
- Vue: /packages/create-vike-core/boilerplate-vue/renderer/Link.vue
You cannot use
window.location.pathnameif you use SSR, because it isn't available when the page is rendered on the server-side. If you have setssr: false, then you can usewindow.location.pathnameinstead ofpageContext.urlPathname.