165 lines
5.2 KiB
Svelte
165 lines
5.2 KiB
Svelte
<script lang="ts">
|
|
import type { Snippet } from 'svelte';
|
|
import type { HTMLButtonAttributes } from 'svelte/elements';
|
|
import Icon from './Icon.svelte';
|
|
import Strike from './Strike.svelte';
|
|
import { strike } from '../lib/strike';
|
|
import { failureText, type ButtonState } from '../lib/keys';
|
|
import type { IconName } from '../lib/icons';
|
|
|
|
interface Props extends Omit<HTMLButtonAttributes, 'onclick' | 'class' | 'children'> {
|
|
/** plate: every ordinary action. primary: the one leaf per screen. quiet: toolbars and third actions. danger: deletes. */
|
|
variant?: 'plate' | 'primary' | 'quiet' | 'danger';
|
|
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
icon?: IconName;
|
|
/** Icon only: the label becomes the accessible name and the tooltip. */
|
|
iconOnly?: boolean;
|
|
/** The words on the button. Required for iconOnly (it is the accessible name), else use children. */
|
|
label?: string;
|
|
block?: boolean;
|
|
/** Set to drive the state yourself. Left unset, a Promise returned by onclick drives it. */
|
|
status?: ButtonState;
|
|
/** May return a Promise: the button shows loading, then success (briefly) or error. */
|
|
onclick?: (event: MouseEvent) => unknown;
|
|
/**
|
|
* Two-step confirmation for danger: the first press arms the button and shows this text ("Press again to
|
|
* delete"); a second press within confirmFor ms runs onclick. Escape, blur or the timeout disarms it.
|
|
*/
|
|
confirm?: string;
|
|
confirmFor?: number;
|
|
/** Announced while working, when done, and when it failed (unless the error is a DomiError with its own text). */
|
|
busyText?: string;
|
|
doneText?: string;
|
|
failText?: string;
|
|
/** Showcase and tests only: show hover, press or focus without the pointer. */
|
|
force?: string;
|
|
class?: string;
|
|
children?: Snippet;
|
|
/** The <button> element, for focusing it from outside (bind:element), e.g. to return focus after a dialog. */
|
|
element?: HTMLButtonElement | null;
|
|
}
|
|
|
|
let {
|
|
variant = 'plate',
|
|
size = 'md',
|
|
icon,
|
|
iconOnly = false,
|
|
label,
|
|
block = false,
|
|
status,
|
|
onclick,
|
|
confirm,
|
|
confirmFor = 4000,
|
|
busyText = 'Working',
|
|
doneText = 'Done',
|
|
failText = 'That did not work. Try again.',
|
|
force,
|
|
class: cls = '',
|
|
disabled = false,
|
|
type = 'button',
|
|
children,
|
|
// Taken out of rest, so the button can run them before its own (a spread before the attribute would lose them).
|
|
onkeydown: passedKeydown,
|
|
onblur: passedBlur,
|
|
element = $bindable(null),
|
|
...rest
|
|
}: Props = $props();
|
|
|
|
let auto = $state<ButtonState>('idle');
|
|
let armed = $state(false);
|
|
let said = $state('');
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
let armTimer: ReturnType<typeof setTimeout> | undefined;
|
|
const shown = $derived<ButtonState>(status ?? auto);
|
|
const busy = $derived(shown === 'loading');
|
|
|
|
function hold(): number {
|
|
const v = getComputedStyle(document.documentElement).getPropertyValue('--domi-dur-hold');
|
|
return Math.max(900, parseFloat(v) || 1400);
|
|
}
|
|
|
|
function disarm() {
|
|
armed = false;
|
|
clearTimeout(armTimer);
|
|
}
|
|
|
|
async function press(event: MouseEvent) {
|
|
if (disabled || busy) {
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
if (confirm && !armed) {
|
|
armed = true;
|
|
said = confirm;
|
|
clearTimeout(armTimer);
|
|
armTimer = setTimeout(disarm, confirmFor);
|
|
return;
|
|
}
|
|
disarm();
|
|
const result = onclick?.(event);
|
|
const thenable = result != null && typeof (result as PromiseLike<unknown>).then === 'function';
|
|
if (!thenable || status !== undefined) return;
|
|
clearTimeout(timer);
|
|
auto = 'loading';
|
|
said = busyText;
|
|
try {
|
|
await result;
|
|
auto = 'success';
|
|
said = doneText;
|
|
timer = setTimeout(() => (auto = 'idle'), hold());
|
|
} catch (err) {
|
|
auto = 'error';
|
|
said = failureText(err, failText);
|
|
}
|
|
}
|
|
|
|
$effect(() => () => {
|
|
clearTimeout(timer);
|
|
clearTimeout(armTimer);
|
|
});
|
|
</script>
|
|
|
|
<button
|
|
bind:this={element}
|
|
{...rest}
|
|
{type}
|
|
{disabled}
|
|
class={[
|
|
'domi-btn',
|
|
variant !== 'plate' && `domi-btn--${variant}`,
|
|
size !== 'md' && `domi-btn--${size}`,
|
|
iconOnly && 'domi-btn--icon',
|
|
block && 'domi-btn--block',
|
|
cls,
|
|
]}
|
|
data-state={shown === 'idle' ? undefined : shown}
|
|
data-armed={armed ? '' : undefined}
|
|
data-force={force}
|
|
aria-busy={busy ? 'true' : undefined}
|
|
aria-disabled={busy ? 'true' : undefined}
|
|
aria-label={iconOnly ? (armed ? confirm : label) : undefined}
|
|
title={iconOnly ? (armed ? confirm : label) : undefined}
|
|
onclick={press}
|
|
onkeydown={(e) => {
|
|
passedKeydown?.(e);
|
|
if (e.key === 'Escape' && armed) disarm();
|
|
}}
|
|
onblur={(e) => {
|
|
passedBlur?.(e);
|
|
disarm();
|
|
}}
|
|
use:strike
|
|
>
|
|
<span class="domi-btn__label">
|
|
{#if shown === 'error'}<Icon name="alert" />{:else if icon}<Icon name={icon} />{/if}
|
|
{#if !iconOnly}
|
|
{#if armed}{confirm}{:else if children}{@render children()}{:else}{label}{/if}
|
|
{/if}
|
|
</span>
|
|
<span class="domi-btn__state domi-btn__state--loading"><Strike /></span>
|
|
<span class="domi-btn__state domi-btn__state--success">
|
|
{#if shown === 'success'}<Icon name="check" draw />{/if}
|
|
</span>
|
|
</button>
|
|
<span class="domi-sr" role="status">{said}</span>
|