[ui] DomiUi: Rosea's buttons and town search on @domi/ui v1.0.4 (CPAL-1.0, vendored by its own tool into packages/domi-ui), the licence credit at the foot of every screen, the README's copy line and public-domain sources; before-and-after pictures by pnpm look:shots
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<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;
|
||||
}
|
||||
|
||||
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,
|
||||
...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
|
||||
{...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) => e.key === 'Escape' && armed && disarm()}
|
||||
onblur={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>
|
||||
Reference in New Issue
Block a user