Lutra/ui/src/components/notifications/NotificationStack.vue
danny 750ecf5e51 F1: conflict toast closes when the merge ends, toasts keep off the commit box
The merge store dismisses its conflict warning once the status lists no
conflicted file (resolved, committed or aborted) and replaces it on a new
conflicted merge. The toast stack moves above areas registered with
keepClearOf; the Changes window registers its commit box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 18:53:08 +01:00

219 lines
5.3 KiB
Vue

<script setup lang="ts">
/**
* The toast stack at the bottom right of the window, newest at the bottom.
*
* Shows the toasts of the notifications store: info toasts as
* `role="status"`, warnings and errors as `role="alert"`. Each toast has a
* close button and closes with Escape while focus is inside it; older
* toasts beyond the visible limit collapse into a "+n more" button. The
* colours match the editor's in-place notices (`--lt-notice-bg`, the error
* tint).
*
* The stack never covers an area registered with `keepClearOf` (the commit
* box): it moves above it ({@link stackBottom}) and is placed again when
* toasts come, the window resizes or a kept area changes size.
*/
import { CircleAlert, Info, TriangleAlert, X } from "@lucide/vue";
import { onBeforeUnmount, onMounted, ref, watch } from "vue";
import { t } from "../../i18n";
import { STACK_BOTTOM, stackBottom, useNotificationsStore, type NotificationLevel } from "../../stores/notifications";
const notifications = useNotificationsStore();
/** The stack's CSS `bottom`, in pixels. */
const bottom = ref(STACK_BOTTOM);
/** Watches the kept areas and their parents for size changes; null where unsupported. */
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(place);
/** Moves the stack above every kept area it would cover. */
function place(): void {
const areas = notifications.keepClear.map((element) => element.getBoundingClientRect());
bottom.value = stackBottom(window.innerWidth, window.innerHeight, areas);
}
/** Observes the kept areas and the elements around them (a resized panel moves them). */
function observe(): void {
if (!observer) {
return;
}
observer.disconnect();
for (const element of notifications.keepClear) {
observer.observe(element);
if (element.parentElement) {
observer.observe(element.parentElement);
}
}
}
watch(
() => notifications.keepClear,
() => {
observe();
place();
},
);
watch(() => notifications.toasts.length, place);
onMounted(() => {
window.addEventListener("resize", place);
observe();
place();
});
onBeforeUnmount(() => {
window.removeEventListener("resize", place);
observer?.disconnect();
});
/** Icon of each level. */
const ICONS = { info: Info, warning: TriangleAlert, error: CircleAlert } as const;
/**
* ARIA role of a toast: errors and warnings interrupt, info is polite.
*
* @param level - the toast's level.
*/
function roleOf(level: NotificationLevel): "status" | "alert" {
return level === "info" ? "status" : "alert";
}
</script>
<template>
<section
v-if="notifications.toasts.length > 0"
class="toasts"
:style="{ bottom: `${bottom}px`, maxHeight: `calc(100vh - ${bottom + STACK_BOTTOM}px)` }"
:aria-label="t('notification.stack.label')"
data-test="toasts"
>
<button
v-if="notifications.hiddenCount > 0"
type="button"
class="toasts__more"
data-test="toasts-more"
@click="notifications.expand()"
>
{{ t("notification.stack.more", { count: notifications.hiddenCount }) }}
</button>
<div
v-for="toast in notifications.visible"
:key="toast.id"
class="toast"
:class="`toast--${toast.level}`"
:role="roleOf(toast.level)"
data-test="toast"
:data-level="toast.level"
@keydown.escape.stop="notifications.dismiss(toast.id)"
>
<component
:is="ICONS[toast.level]"
class="toast__icon"
:size="16"
aria-hidden="true"
/>
<span
class="toast__text"
data-test="toast-text"
>{{ toast.text }}</span>
<button
type="button"
class="toast__close"
:aria-label="t('notification.close')"
:title="t('notification.close')"
data-test="toast-close"
@click="notifications.dismiss(toast.id)"
>
<X
:size="14"
aria-hidden="true"
/>
</button>
</div>
</section>
</template>
<style scoped>
.toasts {
position: fixed;
right: 16px;
z-index: 100;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 8px;
width: min(420px, calc(100vw - 32px));
overflow-y: auto;
user-select: text;
}
.toast {
display: flex;
align-items: flex-start;
gap: 8px;
width: 100%;
box-sizing: border-box;
padding: 8px 8px 8px 12px;
color: var(--lt-fg);
background: var(--lt-notice-bg);
border: 1px solid var(--lt-border);
border-left: 3px solid var(--lt-fg-muted);
border-radius: 4px;
}
.toast--warning {
border-left-color: var(--lt-accent);
}
.toast--error {
background: color-mix(in srgb, var(--lt-error) 15%, var(--lt-bg));
border-left-color: var(--lt-error);
}
.toast__icon {
flex-shrink: 0;
margin-top: 2px;
color: var(--lt-fg-muted);
}
.toast--warning .toast__icon {
color: var(--lt-accent);
}
.toast--error .toast__icon {
color: var(--lt-error);
}
.toast__text {
flex: 1;
min-width: 0;
overflow-wrap: anywhere;
}
.toast__close,
.toasts__more {
font: inherit;
color: var(--lt-fg);
background: transparent;
border: 1px solid var(--lt-border);
border-radius: 3px;
}
.toast__close {
display: inline-flex;
flex-shrink: 0;
padding: 2px;
}
.toasts__more {
padding: 2px 8px;
background: var(--lt-notice-bg);
}
.toast__close:focus-visible,
.toasts__more:focus-visible {
outline: 2px solid var(--lt-accent);
outline-offset: 1px;
}
</style>