Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions osmium/src/ui/layout/main-navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMemo, createSignal, For, Show } from "solid-js";
import { useBeforeLeave, useLocation, useMatch } from "@solidjs/router";
import { useBeforeLeave, useLocation } from "@solidjs/router";
import { Icon } from "solid-heroicons";
import { chevronDown } from "solid-heroicons/solid";
import { setIsOpen } from "./mobile-navigation";
Expand All @@ -12,6 +12,7 @@ import {
import { Collapsible } from "@kobalte/core/collapsible";
import { Tabs } from "@kobalte/core/tabs";
import VersionSelector from "./version-selector";
import { getNavigationTabForPath } from "./navigation-tab";

interface MainNavigationProps {}

Expand Down Expand Up @@ -89,9 +90,8 @@ function DirList(props: { items: SidebarItem[] }) {
}

export function MainNavigation(_props: MainNavigationProps) {
const isReference = useMatch(() => "*/reference/*?");

const initialTab = () => (isReference() ? "reference" : "learn");
const location = useLocation();
const initialTab = () => getNavigationTabForPath(location.pathname);

const [selectedTab, setSelectedTab] = createSignal(initialTab());

Expand All @@ -110,11 +110,7 @@ export function MainNavigation(_props: MainNavigationProps) {
useBeforeLeave(({ to }) => {
if (typeof to === "number") return;

if (to.includes("/reference/")) {
setSelectedTab("reference");
} else {
setSelectedTab("learn");
}
setSelectedTab(getNavigationTabForPath(to));
});

return (
Expand Down
24 changes: 24 additions & 0 deletions osmium/src/ui/layout/navigation-tab.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from "node:test";
import assert from "node:assert/strict";

import { getNavigationTabForPath } from "./navigation-tab";

test("selects reference tab for direct reference page loads", () => {
assert.equal(
getNavigationTabForPath("/reference/secondary-primitives/create-selector"),
"reference"
);
});

test("selects reference tab for localized direct reference page loads", () => {
assert.equal(
getNavigationTabForPath(
"/fr/reference/secondary-primitives/create-selector"
),
"reference"
);
});

test("selects learn tab for non-reference page loads", () => {
assert.equal(getNavigationTabForPath("/learn/quick-start"), "learn");
});
5 changes: 5 additions & 0 deletions osmium/src/ui/layout/navigation-tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type NavigationTab = "learn" | "reference";

export function getNavigationTabForPath(pathname: string): NavigationTab {
return /(^|\/)reference(\/|$)/.test(pathname) ? "reference" : "learn";
}
Loading