UNPKG

nexora

Version:

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

29 lines (28 loc) 993 B
import { Nexora } from '../dom/nexora'; import { createState } from '../state/reactive-state'; import { RouterProvider } from './router-provider'; /** * Experimental Router component * @experimental * @description - The Router component is used to manage the routing of the application. * @param children - The children of the Router component. * @returns The Router component. */ export function Router({ children }) { console.log('Router'); const [getRouterContext, setRouterContext] = createState({ currentPath: window.location.pathname, params: {}, }); const handlePopState = () => { setRouterContext((prev) => ({ ...prev, currentPath: window.location.pathname, })); }; if (typeof window !== 'undefined') { window.addEventListener('popstate', handlePopState); } return (Nexora(RouterProvider, { value: { getRouterContext, setRouterContext } }, Nexora("div", null, children))); }