UNPKG

nexora

Version:

A lightweight, production-ready JavaScript library for building user interfaces, supporting JSX.

22 lines (21 loc) 691 B
import { Nexora } from '../dom/nexora'; import { useRouter } from './router-provider'; /** * Route component * @param props - The properties for the Route component. * @returns The Route component. */ export function Route({ path, component: Component }) { const { getRouterContext } = useRouter(); const { currentPath } = getRouterContext(); const match = matchPath(path, currentPath); if (!match) { return null; } return Nexora(Component, null); } function matchPath(pattern, path) { const regexPattern = pattern.replace(/:[^\s/]+/g, '([^/]+)').replace(/\*/g, '.*'); const regex = new RegExp(`^${regexPattern}$`); return regex.test(path); }