Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329<script lang="ts"> import { resolve } from '$app/paths'; import { MediaQuery } from 'svelte/reactivity'; import { tick } from 'svelte'; import { SURFACES } from '#lib/surfaces.ts';
/** * The one way between a project's surfaces. A destination keeps its name and * its position on every page, so the writer learns one row. * * On a narrow window the chrome carries its own disclosure, so a nested panel * would be a second layer; the items sit flat instead. */ let { projectId, current, canWrite = true, canManage = false, onversions, versionsOpen = false, versionsCount = 0 }: { projectId: string; /** The surface this header belongs to, marked as the current page. */ current: 'write' | 'read' | 'grid' | 'recovery' | 'types' | 'share' | 'feedback'; canWrite?: boolean; canManage?: boolean; /** Present only where scene versions open as a panel beside the prose. */ onversions?: () => void; versionsOpen?: boolean; versionsCount?: number; } = $props();
const narrow = new MediaQuery('(max-width: 56rem)', false);
let open = $state(false); let button: HTMLButtonElement | undefined = $state(); let panel: HTMLElement | undefined = $state();
const items = $derived.by(() => { const list: { id: string; text: string; href?: string; count?: number }[] = [];
if (canWrite) list.push({ id: 'write', text: 'Write', href: resolve('/p/[id]', { id: projectId }) }); list.push({ id: 'read', text: 'Read-through', href: resolve('/p/[id]/read', { id: projectId }) }); list.push({ id: 'feedback', text: 'Feedback', href: resolve('/p/[id]/feedback', { id: projectId }) }); if (canManage) list.push({ id: 'share', text: 'Sharing', href: resolve('/p/[id]/share', { id: projectId }) }); if (canWrite) { list.push({ id: 'grid', text: 'Plot grid', href: resolve('/p/[id]/grid', { id: projectId }) }); } if (onversions) { list.push({ id: 'versions', text: SURFACES.history.label, count: versionsCount }); } /* * Recovery and export need write access: the route asks for it, and the * archive holds history a reader cannot see. For a reader, both are a 404. */ if (canWrite) { list.push({ id: 'recovery', text: 'Recovery and deleted work', href: resolve('/p/[id]/recovery', { id: projectId }) }); list.push({ id: 'export', text: 'Export project', href: resolve('/api/projects/[id]/export', { id: projectId }) }); }
return list; });
async function toggle() { open = !open;
if (open) { await tick(); panel?.querySelector<HTMLElement>('a, button')?.focus(); return; }
button?.focus(); }
function close() { if (!open) return;
open = false; button?.focus(); }
function onkeydown(event: KeyboardEvent) { if (!open) return;
if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); close(); return; }
/* * `role="menu"` promises arrow keys; a menu that answers only Tab is the shape * a screen reader cannot predict. */ if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
const items = Array.from(panel?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? []); if (items.length === 0) return;
event.preventDefault();
const at = items.indexOf(document.activeElement as HTMLElement); const next = event.key === 'Home' ? 0 : event.key === 'End' ? items.length - 1 : event.key === 'ArrowDown' ? at < 0 ? 0 : (at + 1) % items.length : at < 0 ? items.length - 1 : (at - 1 + items.length) % items.length;
items[next].focus(); }
function onpointerdown(event: PointerEvent) { if (!open) return;
const target = event.target as Node | null;
if (panel?.contains(target) || button?.contains(target)) return;
open = false; }</script>
<svelte:window {onpointerdown} />
<!-- Every href in `items` is built by resolve(), and the rule cannot follow a value through a prop. --><!-- eslint-disable svelte/no-navigation-without-resolve -->{#if narrow.current} {#each items as item (item.id)} {#if item.href} <a class="btn btn-sm btn-ghost narrow-item" href={item.href} aria-current={current === item.id ? 'page' : undefined} > {item.text} </a> {:else} <button class="btn btn-sm btn-ghost narrow-item" type="button" aria-pressed={versionsOpen} onclick={() => onversions?.()} > {item.text} {#if item.count} <span class="count tnum">{item.count}</span> {/if} </button> {/if} {/each}{:else} <div class="menu" onfocusout={(event) => { // An open menu covers the page it no longer describes. if (!event.currentTarget.contains(event.relatedTarget as Node | null)) open = false; }} > <button class="btn btn-sm btn-ghost" type="button" bind:this={button} aria-expanded={open} aria-controls={open ? 'project-menu' : undefined} onclick={toggle} > Project <span class="caret" aria-hidden="true">▾</span> </button>
{#if open} <div class="menu-popover" id="project-menu" bind:this={panel} role="menu" tabindex="-1" aria-label="Project" {onkeydown} > {#each items as item (item.id)} {#if item.href} <a class="item" role="menuitem" href={item.href} aria-current={current === item.id ? 'page' : undefined} > {item.text} </a> {:else} <button class="item" role="menuitem" type="button" onclick={() => { open = false; onversions?.(); }} > {item.text} {#if item.count} <span class="count tnum">{item.count}</span> {/if} </button> {/if} {/each} </div> {/if} </div>{/if}
<!-- eslint-enable svelte/no-navigation-without-resolve -->
<style> .menu { position: relative; }
.menu-popover { position: absolute; top: calc(100% + 0.375rem); right: 0; z-index: var(--fw-z-menu); display: flex; flex-direction: column; gap: 2px; min-width: 15rem; padding: var(--dr-space-2xs); border: 2px solid var(--dr-border-color-container); border-radius: var(--dr-radius); background: var(--dr-surface-elevated); box-shadow: 4px 4px 0 0 var(--dr-shadow-color), var(--dr-shadow-inner-highlight); }
.item { display: flex; align-items: center; gap: var(--dr-space-sm); width: 100%; padding: var(--dr-space-sm); border: 0; background: transparent; font-family: var(--dr-font-heading); font-weight: 600; font-size: var(--dr-text-xs); letter-spacing: 0.08em; text-transform: uppercase; text-align: left; text-decoration: none; color: var(--dr-text-secondary); cursor: pointer; }
.item:hover { background: var(--dr-surface-highlight); color: var(--dr-text-primary); }
.item:focus-visible { outline: var(--dr-focus-ring); outline-offset: -2px; }
/* Where you are, without spending the warm accent. */ .item[aria-current='page'], .narrow-item[aria-current='page'], .narrow-item[aria-pressed='true'] { background: var(--dr-surface-highlight); color: var(--dr-text-primary); box-shadow: inset 2px 0 0 var(--dr-text-secondary); }
.count { margin-left: auto; font-size: var(--fw-text-2xs); color: var(--dr-text-muted); }
.caret { font-size: var(--fw-text-2xs); }
/* Above 56rem this is the dropdown shape, so its rows need a touch height. */ @media (pointer: coarse) { .item { min-height: 44px; } }</style>