Enhancement: add ability to set custom auto-dismissal duration for notifications (#13829)

* Enhancement: add ability to set custom auto-dismissal duration for notifications

* Updated based on feedback.

* Enforce max duration for auto-dismiss to 2 minutes
This commit is contained in:
Conor Webb 2024-06-05 12:29:07 +01:00 committed by GitHub
parent 2497d60431
commit c9fb6e35c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 13 deletions

View File

@ -25,6 +25,8 @@
}, },
] ]
const MAX_DURATION = 120000 // Maximum duration in milliseconds (2 minutes)
onMount(() => { onMount(() => {
if (!parameters.type) { if (!parameters.type) {
parameters.type = "success" parameters.type = "success"
@ -33,6 +35,14 @@
parameters.autoDismiss = true parameters.autoDismiss = true
} }
}) })
function handleDurationChange(event) {
let newDuration = event.detail
if (newDuration > MAX_DURATION) {
newDuration = MAX_DURATION
}
parameters.duration = newDuration
}
</script> </script>
<div class="root"> <div class="root">
@ -47,6 +57,16 @@
/> />
<Label /> <Label />
<Checkbox text="Auto dismiss" bind:value={parameters.autoDismiss} /> <Checkbox text="Auto dismiss" bind:value={parameters.autoDismiss} />
{#if parameters.autoDismiss}
<Label>Duration (ms)</Label>
<DrawerBindableInput
title="Duration"
{bindings}
value={parameters.duration}
placeholder="3000"
on:change={handleDurationChange}
/>
{/if}
</div> </div>
<style> <style>

View File

@ -1,7 +1,7 @@
import { writable, get } from "svelte/store" import { writable, get } from "svelte/store"
import { routeStore } from "./routes" import { routeStore } from "./routes"
const NOTIFICATION_TIMEOUT = 3000 const DEFAULT_NOTIFICATION_TIMEOUT = 3000
const createNotificationStore = () => { const createNotificationStore = () => {
let block = false let block = false
@ -18,13 +18,13 @@ const createNotificationStore = () => {
type = "info", type = "info",
icon, icon,
autoDismiss = true, autoDismiss = true,
duration,
count = 1 count = 1
) => { ) => {
if (block) { if (block) {
return return
} }
// If peeking, pass notifications back to parent window
if (get(routeStore).queryParams?.peek) { if (get(routeStore).queryParams?.peek) {
window.parent.postMessage({ window.parent.postMessage({
type: "notification", type: "notification",
@ -32,11 +32,13 @@ const createNotificationStore = () => {
message, message,
type, type,
icon, icon,
duration,
autoDismiss, autoDismiss,
}, },
}) })
return return
} }
const _id = id() const _id = id()
store.update(state => { store.update(state => {
const duplicateError = state.find(err => err.message === message) const duplicateError = state.find(err => err.message === message)
@ -60,7 +62,7 @@ const createNotificationStore = () => {
if (autoDismiss) { if (autoDismiss) {
setTimeout(() => { setTimeout(() => {
dismiss(_id) dismiss(_id)
}, NOTIFICATION_TIMEOUT) }, duration || DEFAULT_NOTIFICATION_TIMEOUT)
} }
} }
@ -74,14 +76,14 @@ const createNotificationStore = () => {
subscribe: store.subscribe, subscribe: store.subscribe,
actions: { actions: {
send, send,
info: (msg, autoDismiss) => info: (msg, autoDismiss, duration) =>
send(msg, "info", "Info", autoDismiss ?? true), send(msg, "info", "Info", autoDismiss ?? true, duration),
success: (msg, autoDismiss) => success: (msg, autoDismiss, duration) =>
send(msg, "success", "CheckmarkCircle", autoDismiss ?? true), send(msg, "success", "CheckmarkCircle", autoDismiss ?? true, duration),
warning: (msg, autoDismiss) => warning: (msg, autoDismiss, duration) =>
send(msg, "warning", "Alert", autoDismiss ?? true), send(msg, "warning", "Alert", autoDismiss ?? true, duration),
error: (msg, autoDismiss) => error: (msg, autoDismiss, duration) =>
send(msg, "error", "Alert", autoDismiss ?? false), send(msg, "error", "Alert", autoDismiss ?? false, duration),
blockNotifications, blockNotifications,
dismiss, dismiss,
}, },

View File

@ -416,11 +416,11 @@ const continueIfHandler = action => {
} }
const showNotificationHandler = action => { const showNotificationHandler = action => {
const { message, type, autoDismiss } = action.parameters const { message, type, autoDismiss, duration } = action.parameters
if (!message || !type) { if (!message || !type) {
return return
} }
notificationStore.actions[type]?.(message, autoDismiss) notificationStore.actions[type]?.(message, autoDismiss, duration)
} }
const promptUserHandler = () => {} const promptUserHandler = () => {}