@makolabs/ripple
Version:
Simple Svelte 5 powered component library ✨
24 lines (23 loc) • 797 B
JavaScript
import { page } from '$app/state';
/**
* Check if a URL matches the current route
* @param href - The URL to check
* @param exact - Whether to require an exact match
*/
export function isRouteActive(href, exact = false) {
const currentPath = page.url.pathname;
if (exact) {
return currentPath === href;
}
// Home page special case
if (href === '/' && currentPath === '/') {
return true;
}
// Check if the current path starts with href, but ensure it's a complete segment
if (href !== '/' && currentPath.startsWith(href)) {
// Check if the next character after href is a slash or the end of the string
const nextChar = currentPath.charAt(href.length);
return nextChar === '' || nextChar === '/';
}
return false;
}