web/dialogs: move buttons to own component & add optional timeout

This commit is contained in:
wukko 2024-07-28 14:49:12 +06:00
parent a2ead8a813
commit 11d3d71937
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
3 changed files with 60 additions and 23 deletions

View file

@ -0,0 +1,55 @@
<script lang="ts">
import { onDestroy } from "svelte";
import type { DialogButton } from "$lib/types/dialog";
export let button: DialogButton;
export let closeFunc: () => void;
let disabled = false;
let seconds = 0;
if (button.timeout) {
disabled = true;
seconds = Math.round(button.timeout / 1000);
let interval = setInterval(() => {
seconds--;
if (seconds <= 0) {
clearInterval(interval);
disabled = false;
}
}, 1000);
onDestroy(() => clearInterval(interval));
}
</script>
<button
class="button elevated popup-button {button.color}"
class:color={button.color}
class:active={button.main}
{disabled}
on:click={async () => {
await button.action();
closeFunc();
}}
>
{button.text}{seconds ? ` (${seconds})` : ""}
</button>
<style>
.popup-button {
width: 100%;
height: 40px;
transition: 0.2s opacity;
}
.popup-button.red {
background-color: var(--red);
color: var(--white);
}
.popup-button[disabled] {
opacity: 0.6;
}
</style>

View file

@ -1,23 +1,14 @@
<script lang="ts"> <script lang="ts">
import type { DialogButton } from "$lib/types/dialog"; import type { DialogButton as DialogButtonType } from "$lib/types/dialog";
import DialogButton from "$components/dialog/DialogButton.svelte";
export let buttons: DialogButton[]; export let buttons: DialogButtonType[];
export let closeFunc: () => void; export let closeFunc: () => void;
</script> </script>
<div class="popup-buttons"> <div class="popup-buttons">
{#each buttons as button} {#each buttons as button}
<button <DialogButton {button} {closeFunc} />
class="button elevated popup-button {button.color}"
class:color={button.color}
class:active={button.main}
on:click={async () => {
await button.action();
closeFunc();
}}
>
{button.text}
</button>
{/each} {/each}
</div> </div>
@ -31,14 +22,4 @@
border-radius: var(--border-radius); border-radius: var(--border-radius);
min-height: 40px; min-height: 40px;
} }
.popup-button {
width: 100%;
height: 40px;
}
.popup-button.red {
background-color: var(--red);
color: var(--white);
}
</style> </style>

View file

@ -4,6 +4,7 @@ export type DialogButton = {
text: string, text: string,
color?: "red", color?: "red",
main: boolean, main: boolean,
timeout?: number, // milliseconds
action: () => unknown | Promise<unknown> action: () => unknown | Promise<unknown>
} }