import { useLocation, useNavigate } from '@solidjs/router'; import type { JSX, ParentProps } from 'solid-js'; import { For, createEffect, splitProps } from 'solid-js'; type SidebarProps = ParentProps & { state: Record; base: string; links: Record; }; function Sidebar(props: SidebarProps) { const searchParams = new URLSearchParams(); const navigate = useNavigate(); const location = useLocation(); createEffect(() => { if (props.base.endsWith('/')) throw new Error('Base should not end with a slash'); Object.keys(props.state).forEach((key) => { searchParams.set(key, props.state[key]); }); }); function goto(href: string) { const url = `${props.base}${href}?${searchParams.toString()}`; navigate(url); } function isActive(link: string) { return location.pathname === `${props.base}${link}` || `${location.pathname}/` === `${props.base}${link}`; } return ( ); } Sidebar.Page = function (props: JSX.HTMLAttributes) { const [split, rest] = splitProps(props, ['class', 'children']); return (
{split.children}
); }; export default Sidebar;