import { makeEventListener } from '@solid-primitives/event-listener'; import { isClient } from '@solid-primitives/utils'; import { createEffect, createSignal, For } from 'solid-js'; import { A, useLocation } from '@solidjs/router'; type TabsProps = { items: string[]; }; /** * Tabs organize content into multiple sections and allow users to navigate * between them. */ export function Tabs(props: TabsProps) { // Use location const location = useLocation(); // Create navigation element and indicator style signal const [getNavElement, setNavElement] = createSignal(); const [getIndicatorStyle, setIndicatorStyle] = createSignal<{ left: string; width: string; }>(); /** * Updates the indicator style position. */ const updateIndicatorStyle = () => { // Get active navigation element by pathname and href const activeElement = [...getNavElement()!.children].find((e) => (e as HTMLAnchorElement).href.endsWith(location.pathname) ) as HTMLAnchorElement | undefined; // Update indicator style to active element or reset it to undefined setIndicatorStyle( activeElement ? { left: `${activeElement.offsetLeft || 0}px`, width: `${activeElement.offsetWidth || 0}px`, } : undefined ); }; // Update indicator style when active element changes createEffect(updateIndicatorStyle); // Update indicator style when window size change if (isClient) { makeEventListener(window, 'resize', updateIndicatorStyle); } return (
); }