import type { JSX, ParentProps } from 'solid-js'; import { useLocation, useNavigate } from '@solidjs/router'; import { createEffect, For, splitProps } from 'solid-js'; type SidebarProps = ParentProps & { base: string; links: Record; }; function Sidebar(props: SidebarProps) { const navigate = useNavigate(); const location = useLocation(); createEffect(() => { if (props.base.endsWith('/')) throw new Error('Base should not end with a slash'); }); function goto(href: string, params?: URLSearchParams) { const currParams = new URLSearchParams(location.search); if (params) for (const [key, value] of params) currParams.set(key, value); const url = `${props.base}${href}?${currParams.toString()}`; navigate(url); } function isActive(link: string, params: URLSearchParams | undefined) { const pathnameMatch = location.pathname === `${props.base}${link}` || `${location.pathname}/` === `${props.base}${link}`; const paramsMatch = params ? location.search.endsWith(params.toString()) : true; return pathnameMatch && paramsMatch; } return (
{section => (

{section.toUpperCase()}

{(link) => { if (!link) return; return ( goto(link[2], link[3])} > {link[0]} {link[1]} ); }}
)}
); } Sidebar.Page = function (props: JSX.HTMLAttributes) { const [split, rest] = splitProps(props, ['class', 'children']); return (
{split.children}
); }; export default Sidebar;