Skip to content

Commit d8ad118

Browse files
committed
feat: docs flyout menu
Adds a dropdown menu that has one main a tag and flyout sub a tags on hover. The menu is used for the docs entry Related to #3 / #61
1 parent df643cd commit d8ad118

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

apps/svelte.dev/src/routes/nav.json/+server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const GET = async () => {
1111
async function get_nav_list(): Promise<NavigationLink[]> {
1212
const docs = index.docs.children.map((topic) => ({
1313
title: topic.metadata.title,
14+
path: '/' + topic.slug, // this will make the UI show a flyout menu for the docs nav entry
1415
sections: topic.children.map((section) => ({
1516
title: section.metadata.title,
1617
sections: section.children.map((page) => ({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import type { NavigationLink } from '../types';
4+
5+
let { links: _links, prefix }: { links: string[] | NavigationLink; prefix: string } = $props();
6+
7+
const links = $derived(
8+
Array.isArray(_links)
9+
? _links.map((l) => ({ title: l, path: l }))
10+
: [
11+
{ title: _links.title, path: _links.pathname },
12+
..._links.sections!.map((s) => ({ title: s.title, path: s.path! }))
13+
]
14+
);
15+
</script>
16+
17+
<div class="dropdown">
18+
<a
19+
href={links[0].path}
20+
class="main-action"
21+
aria-current={$page.url.pathname.startsWith(`/${prefix}`) ? 'page' : null}>{links[0].title}</a
22+
>
23+
<nav class="dropdown-content">
24+
{#each links.slice(1) as link}
25+
<a href={link.path}>{link.title}</a>
26+
{/each}
27+
</nav>
28+
</div>
29+
30+
<style>
31+
.dropdown {
32+
position: relative;
33+
display: inline-block;
34+
}
35+
36+
.dropdown-content {
37+
display: none;
38+
position: absolute;
39+
background-color: var(--sk-back-1);
40+
min-width: 10rem;
41+
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
42+
z-index: 1;
43+
animation: flyout 0.3s ease-in-out;
44+
}
45+
46+
@keyframes flyout {
47+
from {
48+
opacity: 0;
49+
transform: translateY(-10px);
50+
}
51+
to {
52+
opacity: 1;
53+
transform: translateY(0);
54+
}
55+
}
56+
57+
.dropdown:hover .dropdown-content {
58+
display: block;
59+
}
60+
61+
.dropdown-content a {
62+
color: var(--sk-text-3);
63+
padding: 12px 16px;
64+
text-decoration: none;
65+
display: block;
66+
margin: 0 !important;
67+
}
68+
69+
.dropdown-content a:hover {
70+
background-color: var(--sk-back-4);
71+
}
72+
</style>

packages/site-kit/src/lib/components/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { default as Icons } from './Icons.svelte';
44
export { default as Section } from './Section.svelte';
55
export { default as Shell } from './Shell.svelte';
66
export { default as ThemeToggle } from './ThemeToggle.svelte';
7+
export { default as LinksDropdown } from './LinksDropdown.svelte';

packages/site-kit/src/lib/nav/Nav.svelte

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Top navigation bar for the application. It provides a slot for the left side, th
1313
import Separator from './Separator.svelte';
1414
import type { NavigationLink } from '../types';
1515
import type { Snippet } from 'svelte';
16+
import LinksDropdown from '../components/LinksDropdown.svelte';
1617
1718
interface Props {
1819
home_title?: string;
@@ -103,12 +104,16 @@ Top navigation bar for the application. It provides a slot for the left side, th
103104

104105
<div class="menu">
105106
{#each links as link}
106-
<a
107-
href={link.pathname}
108-
aria-current={$page.url.pathname.startsWith(`/${link.prefix}`) ? 'page' : null}
109-
>
110-
{link.title}
111-
</a>
107+
{#if link.sections?.[0].path}
108+
<LinksDropdown links={link} prefix={link.prefix} />
109+
{:else}
110+
<a
111+
href={link.pathname}
112+
aria-current={$page.url.pathname.startsWith(`/${link.prefix}`) ? 'page' : null}
113+
>
114+
{link.title}
115+
</a>
116+
{/if}
112117
{/each}
113118

114119
<Separator />

packages/site-kit/src/lib/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ export interface NavigationLink {
44
pathname: string;
55
sections?: {
66
title: string;
7+
path?: string;
78
sections: {
89
title: string;
10+
path?: string;
911
sections: {
1012
title: string;
1113
path: string;

0 commit comments

Comments
 (0)