UNPKG

react-navplus

Version:

A flexible, performance-optimized navigation link component for React with multi-router support, prefetching, and advanced active state detection

49 lines 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isActiveWithCustomFn = exports.isActive = exports.cleanUrl = exports.matchers = void 0; /** * A map of matcher functions that determine if a link is active based on the current URL * @type {Map<MatchMode, (pathname: string, url: string, pattern?: RegExp) => boolean>} */ exports.matchers = new Map([ ['exact', (pathname, url) => pathname === url], ['startsWith', (pathname, url) => pathname.startsWith(url)], ['includes', (pathname, url) => pathname.includes(url)], ['pattern', (pathname, _url, pattern) => pattern ? pattern.test(pathname) : false] ]); /** * Cleans up a URL by ensuring it starts with a forward slash * @param {string} url - The URL to clean * @returns {string} - The cleaned URL */ const cleanUrl = (url) => { if (!url) return '/'; return url.startsWith('/') ? url : `/${url}`; }; exports.cleanUrl = cleanUrl; /** * Determine if a link is active based on the location, URL, match mode, and optional pattern * @param {string} pathname - Current pathname * @param {string} url - URL to match against * @param {MatchMode} matchMode - How to match the URL * @param {RegExp} [matchPattern] - Optional regex pattern for matching * @returns {boolean} - Whether the link is active */ const isActive = (pathname, url, matchMode = 'includes', matchPattern) => { const matchFn = exports.matchers.get(matchMode) || exports.matchers.get('includes'); return matchFn(pathname, url, matchPattern); }; exports.isActive = isActive; /** * Determine if a link is active with a custom function * @param {string} pathname - Current pathname * @param {string} url - URL to match against * @param {(pathname: string, url: string) => boolean} isActiveFunc - Custom active detection function * @returns {boolean} - Whether the link is active */ const isActiveWithCustomFn = (pathname, url, isActiveFunc) => { return isActiveFunc(pathname, url); }; exports.isActiveWithCustomFn = isActiveWithCustomFn; //# sourceMappingURL=matchers.js.map