105 lines
2.2 KiB
Svelte
105 lines
2.2 KiB
Svelte
<script lang="ts">
|
|
import type { Snippet } from 'svelte';
|
|
import Spinner from './Spinner.svelte';
|
|
|
|
interface Props {
|
|
/** `danger` = outlined (list rows); `danger-solid` = confirm dialogs only. */
|
|
variant?:
|
|
'primary' | 'secondary' | 'ghost' | 'danger' | 'danger-solid' | 'scene';
|
|
size?: 'sm' | 'md' | 'lg';
|
|
/** Stretch to the container width (e.g. a panel's main action). */
|
|
block?: boolean;
|
|
iconOnly?: boolean;
|
|
'aria-label'?: string;
|
|
'aria-pressed'?: boolean;
|
|
disabled?: boolean;
|
|
loading?: boolean;
|
|
type?: 'button' | 'submit' | 'reset';
|
|
title?: string;
|
|
onclick?: (event: MouseEvent) => void;
|
|
children: Snippet;
|
|
}
|
|
|
|
let {
|
|
variant = 'secondary',
|
|
size = 'md',
|
|
block = false,
|
|
iconOnly = false,
|
|
'aria-label': ariaLabel,
|
|
'aria-pressed': ariaPressed,
|
|
disabled = false,
|
|
loading = false,
|
|
type = 'button',
|
|
title,
|
|
onclick,
|
|
children,
|
|
}: Props = $props();
|
|
</script>
|
|
|
|
<button
|
|
{type}
|
|
class="a-btn btn {variant}"
|
|
class:sm={size === 'sm'}
|
|
class:lg={size === 'lg'}
|
|
class:icon-only={iconOnly}
|
|
aria-label={ariaLabel ?? (iconOnly ? title : undefined)}
|
|
aria-pressed={ariaPressed}
|
|
class:block
|
|
class:loading
|
|
disabled={disabled || loading}
|
|
{title}
|
|
{onclick}
|
|
>
|
|
<span class="btn-content" aria-hidden={loading || undefined}
|
|
>{@render children()}</span
|
|
>
|
|
{#if loading}
|
|
<span class="btn-spinner">
|
|
<Spinner size="sm" />
|
|
</span>
|
|
{/if}
|
|
</button>
|
|
|
|
<style>
|
|
/* Look comes from the shared `.a-btn` rules in app.css (§四.1). */
|
|
.btn {
|
|
position: relative;
|
|
}
|
|
.btn.icon-only {
|
|
width: var(--button-height, 34px);
|
|
padding: 0;
|
|
}
|
|
.btn.icon-only.sm {
|
|
width: var(--button-sm-height, 28px);
|
|
}
|
|
.btn:disabled {
|
|
pointer-events: none;
|
|
}
|
|
.btn-content {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: inherit;
|
|
}
|
|
.btn.loading .btn-content {
|
|
visibility: hidden;
|
|
}
|
|
.btn-spinner {
|
|
position: absolute;
|
|
inset: 0;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
.btn.primary .btn-spinner {
|
|
--spinner-track: color-mix(in srgb, var(--accent-ink) 30%, transparent);
|
|
--spinner-arc: var(--accent-ink);
|
|
}
|
|
.btn.danger .btn-spinner {
|
|
--spinner-arc: var(--err);
|
|
}
|
|
.btn.danger-solid .btn-spinner {
|
|
--spinner-track: color-mix(in srgb, var(--on-media) 30%, transparent);
|
|
--spinner-arc: var(--on-media);
|
|
}
|
|
</style>
|