Skip to content

Commit

Permalink
Merge pull request #508 from MuckRock/sveltekit-navigation
Browse files Browse the repository at this point in the history
[SvelteKit] Global Nav
  • Loading branch information
allanlasser authored Apr 17, 2024
2 parents 5ae7805 + 37bfb69 commit a59e49d
Show file tree
Hide file tree
Showing 29 changed files with 500 additions and 46 deletions.
6 changes: 4 additions & 2 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UserContextDecorator from "./decorators/UserContextDecorator.svelte";
import OrgContextDecorator from "./decorators/OrgContextDecorator.svelte";

import "@/style/kit.css";
import "../src/lib/i18n/index.js";
import "@/lib/i18n/index.js";

// Initialize MSW
initialize({
Expand All @@ -29,7 +29,9 @@ const preview: Preview = {
stores: {
page: {
url: "/",
data: {},
data: {
breadcrumbs: [],
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"test:unit": "vitest run",
"test:watch": "vitest watch",
"test:coverage": "vitest run --coverage",
"test:dev": "vitest watch --coverage",
"format": "prettier --write .",
"format:check": "prettier --check src",
"build-storybook": "storybook build",
Expand Down
2 changes: 1 addition & 1 deletion src/api/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Methods related to the DocumentCloud document API
*/

import session from "./session.js";
import session from "./session";
import { apiUrl } from "./base.js";
import { timeout } from "@/util/timeout.js";
import { queryBuilder } from "@/util/url.js";
Expand Down
33 changes: 11 additions & 22 deletions src/lib/components/MainLayout.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<script lang="ts">
import type { Writable } from "svelte/store";
import { type Org, type User } from "@/api/types";
import { page } from "$app/stores";
import { getContext } from "svelte";
import { SidebarCollapse16, SidebarExpand16 } from "svelte-octicons";
import Button from "./common/Button.svelte";
import Flex from "./common/Flex.svelte";
import Logo from "./common/Logo.svelte";
import SignedIn from "./common/SignedIn.svelte";
import UserMenu from "./accounts/UserMenu.svelte";
import OrgMenu from "./accounts/OrgMenu.svelte";
import UserMenu from "./navigation/UserMenu.svelte";
import OrgMenu from "./navigation/OrgMenu.svelte";
import { SIGN_IN_URL } from "@/config/config";
import Breadcrumbs from "./navigation/Breadcrumbs.svelte";
import LanguageMenu from "./navigation/LanguageMenu.svelte";
import HelpMenu from "./navigation/HelpMenu.svelte";
export let modal: boolean = false;
export let basement: "left" | "right" | null = null;
let panel: "navigation" | "action" | null = null;
Expand All @@ -29,12 +31,6 @@
};
}
function closeBasement() {
if (basement !== null) {
basement = null;
}
}
function closeModal() {
modal = false;
}
Expand All @@ -52,8 +48,9 @@
</Button>
</div>
{/if}
<a href="/" class="logo"><Logo /></a>
<div class="breadcrumbs"></div>
<slot name="breadcrumbs">
<Breadcrumbs trail={$page.data.breadcrumbs} />
</slot>
<SignedIn>
<Flex>
<OrgMenu org={$org} />
Expand All @@ -63,6 +60,8 @@
Sign In
</Button>
</SignedIn>
<LanguageMenu />
<HelpMenu />
{#if $$slots.action}
<div class="small openPane">
<Button mode="ghost" on:click={openPanel("action")}>
Expand Down Expand Up @@ -166,16 +165,6 @@
overflow-y: auto;
}
.logo {
height: 1.5rem;
width: auto;
padding: 0 0.5rem;
}
.breadcrumbs {
flex: 1 0 0;
}
.navigation {
border-right: 1px solid var(--gray-2, #d8dee2);
}
Expand Down
70 changes: 70 additions & 0 deletions src/lib/components/navigation/Breadcrumbs.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<script lang="ts" context="module">
export interface Breadcrumb {
title: string;
href?: string;
}
</script>

<script lang="ts">
import { TriangleRight16 } from "svelte-octicons";
import Logo from "../common/Logo.svelte";
import Flex from "../common/Flex.svelte";
export let trail: Breadcrumb[] = [];
</script>

<div class="breadcrumbs">
<Flex gap={0.375} align="center">
<slot name="root">
<a href="/app" class="logo crumb"><Logo /></a>
</slot>
{#each trail as { href, title }}
<span class="divider"><TriangleRight16 fill="var(--gray-3)" /></span>
{#if href}
<a class="crumb" {href} {title}>{title}</a>
{:else}
<span {title} class="crumb">{title}</span>
{/if}
{/each}
</Flex>
</div>

<style>
.breadcrumbs {
flex: 1 0 0;
max-width: 100%;
}
.divider {
display: flex;
align-items: center;
}
.crumb {
flex: 0 1 auto;
font-size: var(--font-l);
text-decoration: none;
color: var(--gray-4);
padding: 0.25rem 0.5rem;
border-radius: 0.5rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.crumb:last-child {
color: inherit;
flex: 0 0 auto;
}
a.crumb:hover {
background: var(--gray-1);
}
.logo {
flex: 0 0 auto;
height: 1.5rem;
width: auto;
box-sizing: content-box;
}
</style>
58 changes: 58 additions & 0 deletions src/lib/components/navigation/HelpMenu.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script lang="ts">
import { _ } from "svelte-i18n";
import Dropdown from "@/common/Dropdown2.svelte";
import Menu from "@/common/Menu.svelte";
import {
CommentDiscussion16,
Search16,
Code16,
Plug16,
Mail16,
Question24,
ChevronDown16,
} from "svelte-octicons";
import SidebarItem from "../sidebar/SidebarItem.svelte";
import Premium from "@/common/icons/Premium.svelte";
</script>

<!-- Help Menu -->
<Dropdown id="help" position="right">
<SidebarItem slot="title">
<Question24 />
<div class="dropdownArrow"><ChevronDown16 /></div>
</SidebarItem>
<Menu>
<SidebarItem href="/help/faq">
<CommentDiscussion16 />
{$_("authSection.help.faq")}
</SidebarItem>
<SidebarItem href="/help/search">
<Search16 />
{$_("authSection.help.searchDocs")}
</SidebarItem>
<SidebarItem href="/help/api">
<Code16 />
{$_("authSection.help.apiDocs")}
</SidebarItem>
<SidebarItem href="/help/add-ons">
<Plug16 />
{$_("authSection.help.addOns")}
</SidebarItem>
<SidebarItem href="/help/premium">
<Premium />
{$_("authSection.help.premium")}
</SidebarItem>
<SidebarItem href="mailto:[email protected]" target="_blank">
<Mail16 />
{$_("authSection.help.emailUs")}
</SidebarItem>
</Menu>
</Dropdown>

<style>
.dropdownArrow {
display: flex;
align-items: center;
}
</style>
60 changes: 60 additions & 0 deletions src/lib/components/navigation/LanguageMenu.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<script lang="ts">
import { _, locale } from "svelte-i18n";
import langs from "@/langs/langs.json";
import Dropdown, { closeDropdown } from "@/common/Dropdown2.svelte";
import Menu from "@/common/Menu.svelte";
import SidebarItem from "$lib/components/sidebar/SidebarItem.svelte";
import { Check16, ChevronDown16 } from "svelte-octicons";
$: currentLang = langs.find(([_, code]) => code == $locale) ?? langs[0];
function updateLanguage(code) {
$locale = code;
try {
localStorage.setItem("dc-locale", code);
} catch (e) {}
}
</script>

{#if langs.length > 1}
<!-- Language Menu -->
<Dropdown id="language" position="right">
<SidebarItem slot="title">
<span class="flag">{currentLang[2]}</span>
<!-- <span class="lang">{currentLang[0]}</span> -->
<div class="dropdownArrow"><ChevronDown16 /></div>
</SidebarItem>
<Menu>
{#each langs as [name, code, flag]}
<SidebarItem
on:click={() => {
updateLanguage(code);
closeDropdown("language");
}}
hover
active={code === $locale}
>
<span class="flag">{flag}</span>
<span class="lang">{name}</span>
{#if code === $locale}<Check16 />{/if}
</SidebarItem>
{/each}
</Menu>
</Dropdown>
{/if}

<style>
.flag {
font-size: var(--font-l);
height: 1.5rem;
width: 1.5rem;
display: flex;
align-items: center;
justify-content: center;
}
.dropdownArrow {
display: flex;
align-items: center;
}
</style>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ChevronDown16,
Gear16,
Paperclip16,
Person16,
SignOut16,
} from "svelte-octicons";
import Menu from "@/common/Menu.svelte";
Expand All @@ -26,8 +27,14 @@

<Dropdown id={dropdownId} position="right">
<SidebarItem slot="title">
<img src={user.avatar_url} alt="Avatar" class="avatar" />
<span class="name">{user.name}</span>
<div class="avatar">
{#if user.avatar_url}
<img src={user.avatar_url} alt="Avatar" />
{:else}
<Person16 fill="var(--gray-4)" />
{/if}
</div>
<span class="name">{user.name ?? user.username}</span>
<div class="dropdownArrow"><ChevronDown16 /></div>
</SidebarItem>
<Menu>
Expand All @@ -52,6 +59,16 @@
height: 1.5rem;
width: 1.5rem;
border-radius: 0.75rem;
background: var(--gray-2);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.avatar > img {
height: 100%;
width: 100%;
}
.dropdownArrow {
Expand Down
Loading

0 comments on commit a59e49d

Please sign in to comment.