staging-tester / ocean-landing-serve / src/ routes/ (app)/ +layout.svelte

<script lang="ts">
	import { flushSync, onMount, tick, untrack } from 'svelte';
	import { fade, fly } from 'svelte/transition';
	import { cubicIn, quartOut } from 'svelte/easing';
	import { afterNavigate } from '$app/navigation';
	import CommandMenu from '#lib/components/app/CommandMenu.svelte';
	import Sidebar from '#lib/components/app/Sidebar.svelte';
	import { Panes, setPanes, shell } from '#lib/components/app/shell.svelte.js';
	import { loadVisits, visit } from '#lib/components/app/visits.svelte.js';
	import * as Tooltip from '#lib/components/ui/tooltip/index.js';
	import type { LayoutProps } from './$types';

	let { children, data }: LayoutProps = $props();

	const panes = new Panes(untrack(() => data.panes));
	setPanes(panes);

	let side = $state<HTMLElement | null>(null);
	let main = $state<HTMLElement | null>(null);

	// FLIP: the sidebar width changes once and the page lays out once; only a transform and a clip animate after that.
	const animateSidebar = (apply: () => void) => {
		if (!side || !main || matchMedia('(prefers-reduced-motion: reduce)').matches) return apply();
		const collapsing = !panes.sidebarCollapsed;
		const from = main.getBoundingClientRect().left;
		apply();
		flushSync();
		const dx = from - main.getBoundingClientRect().left;
		const timing = collapsing
			? { duration: 150, easing: 'cubic-bezier(0.4, 0, 1, 1)' }
			: { duration: 200, easing: 'cubic-bezier(0.2, 0, 0, 1)' };
		if (collapsing) {
			main.animate([{ transform: `translateX(${dx}px)` }, { transform: 'translateX(0)' }], timing);
			side.animate([{ borderRightColor: 'transparent' }, { borderRightColor: 'transparent' }], timing);
		} else {
			// The clip stops 1px short so the page's shadow can draw the sidebar edge while it moves.
			const edge = `-1px 0 0 0 ${getComputedStyle(side).borderRightColor}`;
			main.animate([{ transform: `translateX(${dx}px)`, boxShadow: edge }, { transform: 'translateX(0)', boxShadow: edge }], timing);
			side.animate([{ clipPath: `inset(0 ${1 - dx}px 0 0)` }, { clipPath: 'inset(0 1px 0 0)' }], timing);
		}
	};

	onMount(() => {
		loadVisits();
		panes.animateSidebar = animateSidebar;
		return () => (panes.animateSidebar = null);
	});

	afterNavigate(({ to }) => {
		shell.drawer = false;
		panes.sheet = false;
		if (to) void tick().then(() => visit(to.url.pathname + to.url.search, document.title.replace(/ on Sailfork$/, '')));
	});

	// Opaque, so the strip that follows the page edge matches the sidebar exactly.
	const sidebarFill = 'bg-[color-mix(in_oklab,var(--color-muted)_40%,var(--color-background))]';

	const typing = (t: EventTarget | null) =>
		t instanceof HTMLElement && (t.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(t.tagName));

	const find = (query: string) => {
		if (matchMedia('(min-width: 64rem)').matches && !panes.sidebarCollapsed && panes.find) panes.find(query);
		else {
			shell.query = query;
			shell.searching = true;
		}
	};

	const onkeydown = (e: KeyboardEvent) => {
		if (e.key === 'Escape' && (shell.drawer || panes.sheet)) {
			shell.drawer = false;
			panes.sheet = false;
			return;
		}
		if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
			e.preventDefault();
			shell.searching = !shell.searching;
			return;
		}
		if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.altKey || shell.searching || typing(e.target)) return;
		if (e.key === '[') {
			if (matchMedia('(min-width: 64rem)').matches) panes.toggleSidebar();
			else shell.drawer = !shell.drawer;
		} else if (e.key === ']') {
			if (matchMedia('(min-width: 80rem)').matches) {
				if (panes.rails > 0) panes.toggleRail();
			} else if (panes.sheets > 0) panes.sheet = !panes.sheet;
		} else if (e.key.length === 1 && e.key !== ' ') {
			// Deferred so a page's own shortcut (j and k on the pulls list) can claim the key first.
			const key = e.key;
			setTimeout(() => !e.defaultPrevented && !typing(document.activeElement) && find(key));
		}
	};
</script>

<Tooltip.Provider delayDuration={200}>
	<div class="flex h-svh overflow-hidden bg-background print:block print:h-auto print:overflow-visible">
		<aside
			bind:this={side}
			class={['relative z-10 hidden shrink-0 overflow-hidden border-r border-border bg-clip-padding lg:block print:hidden', sidebarFill, panes.sidebarCollapsed ? 'w-14' : 'w-64']}
		>
			<div class={['h-full', panes.sidebarCollapsed ? 'w-14' : 'w-64']}>
				<Sidebar collapsed={panes.sidebarCollapsed} onsearch={() => (shell.searching = true)} />
			</div>
		</aside>

		{#if shell.drawer}
			<button
				type="button"
				tabindex="-1"
				aria-label="Close menu"
				class="fixed inset-0 z-30 bg-foreground/20 lg:hidden"
				onclick={() => (shell.drawer = false)}
				in:fade={{ duration: 150, easing: quartOut }}
				out:fade={{ duration: 100, easing: cubicIn }}
			></button>
			<aside
				class="fixed inset-y-0 left-0 z-40 w-72 border-r border-border bg-background lg:hidden"
				in:fly={{ x: -16, duration: 200, easing: quartOut }}
				out:fly={{ x: -12, duration: 150, easing: cubicIn }}
			>
				<Sidebar collapsed={false} drawer onsearch={() => (shell.searching = true)} />
			</aside>
		{/if}

		<div bind:this={main} class="relative flex min-w-0 flex-1 flex-col">
			<!-- Sits under the sidebar at rest; while the page slides it fills the gap, so the sidebar edge moves with the page. -->
			<div aria-hidden="true" class={['pointer-events-none absolute inset-y-0 right-full hidden w-64 border-r border-border lg:block print:hidden', sidebarFill]}>
				<div class="h-12 border-b border-border"></div>
			</div>
			{@render children()}
		</div>
	</div>
</Tooltip.Provider>

<svelte:window {onkeydown} />

<CommandMenu bind:open={shell.searching} />