Skip to content

Commit

Permalink
Better loading feedback on button with promise actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 14, 2023
1 parent fb1197e commit c98c55b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/components/app/Spinning.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script lang="ts">
export let label: string;
import { locale } from '@db/Database';
export let label = $locale.ui.widget.loading.message;
export let large = false;
</script>

Expand Down
3 changes: 0 additions & 3 deletions src/components/project/Collaborators.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@
flags={project.flags}
/>
</Dialog>

<style>
</style>
38 changes: 23 additions & 15 deletions src/components/widgets/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<svelte:options immutable={true} />

<script lang="ts">
import Spinning from '../app/Spinning.svelte';
export let tip: string;
export let action: () => void;
export let action: () => any;
export let active = true;
export let stretch = false;
export let submit = false;
Expand All @@ -13,9 +15,12 @@
export let large = false;
export let background = false;
let loading = false;
async function doAction(event: Event) {
if (active) {
action();
const result = action();
if (result instanceof Promise) loading = true;
event?.stopPropagation();
}
}
Expand All @@ -36,19 +41,22 @@
aria-disabled={!active}
bind:this={view}
on:dblclick|stopPropagation
on:pointerdown={(event) =>
event.button === 0 && active ? doAction(event) : undefined}
on:keydown={(event) =>
(event.key === 'Enter' || event.key === ' ') &&
// Only activate with no modifiers down. Enter is used for other shortcuts.
!event.shiftKey &&
!event.ctrlKey &&
!event.altKey &&
!event.metaKey
? doAction(event)
: undefined}
>
<slot />
on:pointerdown={loading
? null
: (event) =>
event.button === 0 && active ? doAction(event) : undefined}
on:keydown={loading
? null
: (event) =>
(event.key === 'Enter' || event.key === ' ') &&
// Only activate with no modifiers down. Enter is used for other shortcuts.
!event.shiftKey &&
!event.ctrlKey &&
!event.altKey &&
!event.metaKey
? doAction(event)
: undefined}
>{#if loading}<Spinning />{:else}<slot />{/if}
</button>

<style>
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/ConfirmButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Button from './Button.svelte';
export let tip: string;
export let action: () => void;
export let action: () => any;
export let enabled = true;
export let prompt: string;
export let background = false;
Expand Down

0 comments on commit c98c55b

Please sign in to comment.