Skip to content

Modern Docker Management, Designed for Everyone

4.2 KB
Raw Blame
<script lang="ts">
	import { tick } from 'svelte';

	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import * as ArcaneTooltip from '#lib/components/arcane-tooltip/index.js';
	import { Input } from '#lib/components/ui/input/index.js';
	import { InfoIcon, EditIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { cn } from '#lib/utils.js';

	let {
		value = $bindable(),
		displayValue,
		ref = $bindable(),
		variant = 'block',
		error,
		originalValue,
		canEdit = true,
		onCommit,
		placeholder = '',
		disabledMessage = m.compose_name_change_not_allowed(),
		class: className = ''
	}: {
		value: string;
		displayValue?: string;
		ref: HTMLInputElement | null;
		variant?: 'block' | 'inline';
		error?: string;
		originalValue: string;
		canEdit?: boolean;
		onCommit?: () => void;
		placeholder?: string;
		disabledMessage?: string;
		class?: string;
	} = $props();

	let shownValue = $derived(displayValue ?? value);

	let isEditing = $state(false);
	let wrapperClass = $derived(cn('group', variant === 'block' ? 'w-full' : 'min-w-0', className));

	async function beginEdit() {
		if (!canEdit) return;
		isEditing = true;
		await tick();
		ref?.focus();
	}
</script>

<div class={wrapperClass}>
	{#if isEditing}
		<Input
			bind:ref
			bind:value
			{placeholder}
			size={variant === 'block' ? 'default' : 'lg'}
			class={variant === 'block' ? 'h-8 max-w-70 min-w-30' : 'h-8 max-w-90'}
			aria-invalid={!!error}
			autofocus
			onkeydown={(e) => {
				if (e.key === 'Enter') {
					e.preventDefault();
					onCommit?.();
					isEditing = false;
				}
				if (e.key === 'Escape') {
					value = originalValue;
					isEditing = false;
				}
			}}
			onblur={() => {
				if (!isEditing) return;
				onCommit?.();
				isEditing = false;
			}}
			disabled={!canEdit}
		/>
	{:else if variant === 'block'}
		<h1 class="m-0 w-full">
			<button
				type="button"
				class="min-h-8 w-full rounded bg-transparent px-1 py-1 text-center text-base font-semibold transition-colors hover:bg-muted/50 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 {error
					? 'border border-destructive'
					: ''}"
				title={canEdit ? `${shownValue || placeholder} (tap to edit)` : shownValue || placeholder}
				onclick={beginEdit}
				disabled={!canEdit}
			>
				<span class="block truncate {!shownValue && placeholder ? 'text-muted-foreground' : ''}">{shownValue || placeholder}</span
				>
			</button>
		</h1>

		{#if canEdit}
			<div class="flex items-center justify-center">
				<span class="flex items-center gap-0.5 text-4xs whitespace-nowrap text-muted-foreground opacity-30">
					<EditIcon class="size-1.5" />
					{m.tap_to_edit()}
				</span>
			</div>
		{:else}
			<div class="flex items-center justify-center">
				<span class="inline-flex cursor-help items-center gap-0.5 text-4xs whitespace-nowrap text-muted-foreground opacity-40">
					<InfoIcon class="size-1.5" />
					{m.cannot_edit()}
				</span>
			</div>
		{/if}
	{:else}
		<div class="flex h-8 min-w-0 items-center gap-1.5">
			<h1 class="m-0 max-w-90 min-w-0">
				<button
					type="button"
					class={cn(
						'w-full truncate bg-transparent px-0 py-0 text-left text-lg font-semibold',
						!shownValue && placeholder && 'text-muted-foreground',
						error && 'rounded border border-destructive px-1'
					)}
					title={shownValue || placeholder}
					onclick={beginEdit}
					disabled={!canEdit}
				>
					{shownValue || placeholder}
				</button>
			</h1>
			{#if canEdit}
				<ArcaneButton
					action="base"
					tone="ghost"
					size="icon"
					class="pointer-events-none size-6 opacity-0 transition-opacity group-hover:pointer-events-auto group-hover:opacity-100"
					customLabel={m.common_edit_name()}
					showLabel={false}
					icon={EditIcon}
					onclick={beginEdit}
				/>
			{:else}
				<ArcaneTooltip.Root>
					<ArcaneTooltip.Trigger>
						<span class="inline-flex cursor-help items-center text-muted-foreground">
							<InfoIcon class="size-4 shrink-0" />
						</span>
					</ArcaneTooltip.Trigger>
					<ArcaneTooltip.Content>
						{disabledMessage}
					</ArcaneTooltip.Content>
				</ArcaneTooltip.Root>
			{/if}
		</div>
	{/if}
</div>

{#if error}
	<p class="mt-1 text-xs text-destructive">{error}</p>
{/if}