Skip to content

Modern Docker Management, Designed for Everyone

2.7 KB
Raw Blame
<script lang="ts">
	import type { Snippet } from 'svelte';

	import { CloseIcon, FileTextIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { cn } from '#lib/utils.js';

	interface EditorTab {
		key: string;
		label: string;
		title: string;
		iconClass: string;
		pending: boolean;
	}

	interface Props {
		tabs: EditorTab[];
		activeKey: string;
		onSelect: (key: string) => void;
		onClose: (key: string) => void;
		actions?: Snippet;
	}

	let { tabs, activeKey, onSelect, onClose, actions }: Props = $props();

	// Middle-click anywhere on a tab closes it like browser and IDE tabs; preventing mousedown stops Windows auto-scroll.
	function preventMiddleMouseDown(event: MouseEvent) {
		if (event.button === 1) event.preventDefault();
	}

	function closeOnMiddleClick(event: MouseEvent, key: string) {
		if (event.button !== 1) return;
		event.preventDefault();
		onClose(key);
	}
</script>

<div class="flex h-9 shrink-0 items-center border-b border-border bg-muted/30">
	<div class="scrollbar-hide flex h-full min-w-0 flex-1 items-center overflow-x-auto">
		{#each tabs as tab (tab.key)}
			{@const isActive = activeKey === tab.key}
			<!-- svelte-ignore a11y_no_static_element_interactions -->
			<div
				class={cn(
					'group relative flex h-full shrink-0 items-center border-r border-border',
					isActive ? 'bg-card' : 'hover:bg-accent/50'
				)}
				data-tab-key={tab.key}
				data-active={isActive}
				onmousedown={preventMiddleMouseDown}
				onauxclick={(event) => closeOnMiddleClick(event, tab.key)}
			>
				{#if isActive}
					<span class="absolute inset-x-0 top-0 h-0.5 bg-primary"></span>
				{/if}
				<button
					type="button"
					class={cn(
						'flex h-full items-center gap-1.5 pr-1 pl-3 text-xs-plus',
						isActive ? 'text-foreground' : 'text-muted-foreground'
					)}
					title={tab.title}
					onclick={() => onSelect(tab.key)}
				>
					<FileTextIcon class={cn('size-3.5 shrink-0', tab.iconClass)} />
					<span class="max-w-40 truncate">{tab.label}</span>
					{#if tab.pending}
						<span
							class="size-1.5 shrink-0 rounded-full bg-primary"
							role="img"
							aria-label={m.common_unsaved_changes()}
							title={m.common_unsaved_changes()}
						></span>
					{/if}
				</button>
				<button
					type="button"
					class={cn(
						'mr-1 inline-flex size-5 shrink-0 items-center justify-center rounded opacity-0 group-hover:opacity-100 hover:bg-foreground/10 focus-visible:opacity-100',
						isActive && 'opacity-100'
					)}
					aria-label={m.common_close()}
					onclick={() => onClose(tab.key)}
				>
					<CloseIcon class="size-3" />
				</button>
			</div>
		{/each}
	</div>
	{#if actions}
		<div class="flex shrink-0 items-center gap-0.5 px-1.5">
			{@render actions()}
		</div>
	{/if}
</div>