Skip to content

Modern Docker Management, Designed for Everyone

4.6 KB
Raw Blame
<script lang="ts" module>
	import type { WithChildren, WithoutChildren } from 'bits-ui';
	import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';

	import type { IconType } from '#lib/icons/index.js';
	import type { ShortcutKey } from '#lib/utils/navigation.js';

	import type { ArcaneButtonSize, Action, ArcaneButtonHoverEffect, ActionConfig, ArcaneButtonTone } from './variants';

	export type ArcaneButtonPropsWithoutHTML = WithChildren<{
		ref?: HTMLElement | null;
		action: Action;
		size?: ArcaneButtonSize;
		tone?: ArcaneButtonTone;
		hoverEffect?: ArcaneButtonHoverEffect;
		loading?: boolean;
		showLabel?: boolean;
		customLabel?: string;
		loadingLabel?: string;
		icon?: IconType | null;
		shortcut?: ShortcutKey[];
		onClickPromise?: (
			e: MouseEvent & {
				currentTarget: EventTarget & HTMLButtonElement;
			}
		) => Promise<void>;
	}>;

	export type ArcaneAnchorElementProps = ArcaneButtonPropsWithoutHTML &
		WithoutChildren<Omit<HTMLAnchorAttributes, 'href' | 'type'>> & {
			href: HTMLAnchorAttributes['href'];
			type?: never;
			disabled?: HTMLButtonAttributes['disabled'];
		};

	export type ArcaneButtonElementProps = ArcaneButtonPropsWithoutHTML &
		WithoutChildren<Omit<HTMLButtonAttributes, 'type' | 'href'>> & {
			type?: HTMLButtonAttributes['type'];
			href?: never;
			disabled?: HTMLButtonAttributes['disabled'];
		};

	export type ArcaneButtonProps = ArcaneAnchorElementProps | ArcaneButtonElementProps;
</script>

<script lang="ts">
	import type { Attachment } from 'svelte/attachments';
	import { on } from 'svelte/events';

	import { Spinner } from '#lib/components/ui/spinner/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import userStore from '#lib/stores/user-store.svelte.js';
	import { cn } from '#lib/utils.js';
	import { matchesShortcutEvent } from '#lib/utils/navigation.js';

	import { arcaneButtonVariants, actionConfigs } from './variants';

	void arcaneButtonVariants;

	let {
		ref = $bindable(null),
		action,
		size = 'default',
		tone = undefined,
		hoverEffect = undefined,
		href = undefined,
		type = 'button',
		loading = false,
		disabled = false,
		showLabel = true,
		customLabel = undefined,
		loadingLabel = undefined,
		icon = undefined,
		shortcut = undefined,
		tabindex = 0,
		onclick,
		onClickPromise,
		class: className,
		role,
		'aria-label': ariaLabel,
		children,
		...rest
	}: ArcaneButtonProps = $props();

	let config = $derived(actionConfigs[action] as ActionConfig);
	let displayLabel = $derived(customLabel ?? config.defaultLabel);
	let displayLoadingLabel = $derived(loadingLabel ?? config.loadingLabel ?? m.common_processing());
	let isIconOnlyButton = $derived(size === 'icon' || !showLabel);

	let IconComponent = $derived(icon === null ? null : (icon ?? config.IconComponent));

	// The shortcut clicks the button, so disabled and loading block it and hidden buttons (inactive tabs) ignore it.
	function clickOnShortcut(keys: ShortcutKey[]): Attachment<HTMLElement> {
		return (node) =>
			on(window, 'keydown', (event) => {
				if (event.defaultPrevented || userStore.current?.preferences?.keyboardShortcutsEnabled === false) return;
				if (!matchesShortcutEvent(keys, event) || !node.checkVisibility()) return;
				event.preventDefault();
				node.click();
			});
	}
</script>

<svelte:element
	this={href ? 'a' : 'button'}
	{...rest}
	data-slot="arcane-button"
	data-action={action}
	type={href ? undefined : type}
	href={href && !disabled ? href : undefined}
	disabled={href ? undefined : disabled || loading}
	aria-disabled={href ? disabled : undefined}
	role={href && disabled ? 'link' : role}
	tabindex={href && disabled ? -1 : tabindex}
	class={cn('relative', arcaneButtonVariants({ tone: tone ?? config.tone, size, hoverEffect }), className)}
	aria-label={ariaLabel ?? (children ? undefined : isIconOnlyButton ? displayLabel : undefined)}
	bind:this={ref}
	{@attach shortcut && clickOnShortcut(shortcut)}
	onclick={async (e: any) => {
		onclick?.(e);
		if (type === undefined) return;
		if (onClickPromise) {
			loading = true;
			await onClickPromise(e);
			loading = false;
		}
	}}
>
	{#if type !== undefined && loading}
		<div
			class="absolute inset-0 flex items-center justify-center rounded-inherit bg-background/55 backdrop-blur-sm"
			aria-hidden="true"
		>
			<Spinner class="size-4" />
		</div>
		<span class="sr-only">{m.common_loading_label({ label: displayLoadingLabel })}</span>
	{/if}

	<span class={cn('flex items-center gap-2 transition-opacity duration-150', loading && 'opacity-0')}>
		{#if IconComponent}
			<IconComponent class="size-4" />
		{/if}
		{#if !isIconOnlyButton && displayLabel}
			{displayLabel}
		{/if}
		{@render children?.()}
	</span>
</svelte:element>