Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(app): render toasts in the window that raised them

Hampus5d5ddc3a5d83
diff --git a/fluxer_app/src/features/ui/state/Toast.ts b/fluxer_app/src/features/ui/state/Toast.ts
index 5f9fbc5de..578a6fd9a 100644
--- a/fluxer_app/src/features/ui/state/Toast.ts
+++ b/fluxer_app/src/features/ui/state/Toast.ts
@@ -2,6 +2,7 @@
import {Logger} from '@app/features/platform/utils/AppLogger';
import {randomUuid} from '@app/features/platform/utils/RandomUuid';
+import {getActivePortalHost} from '@app/features/ui/overlay/PortalHostContext';
import type {ToastProps} from '@app/features/ui/toast';
import {makeAutoObservable, observableRef} from 'mobx';
@@ -10,6 +11,7 @@ const logger = new Logger('Toast');
interface ToastEntry {
id: string;
data: ToastProps;
+ ownerDocument: Document;
}
class Toast {
@@ -28,7 +30,7 @@ class Toast {
createToast(data: ToastProps): string {
const id = randomUuid();
logger.debug(`Creating toast: ${id}, type: ${data.type}`);
- this.currentToast = {id, data};
+ this.currentToast = {id, data, ownerDocument: getActivePortalHost()?.ownerDocument ?? document};
return id;
}
diff --git a/fluxer_app/src/features/ui/toast/ToastAnchor.ts b/fluxer_app/src/features/ui/toast/ToastAnchor.ts
new file mode 100644
index 000000000..a6efedde5
--- /dev/null
+++ b/fluxer_app/src/features/ui/toast/ToastAnchor.ts
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {type RefObject, useLayoutEffect} from 'react';
+
+const anchors = new Set<HTMLElement>();
+
+export function useToastAnchor(ref: RefObject<HTMLElement | null>): void {
+ useLayoutEffect(() => {
+ const element = ref.current;
+ if (!element) return;
+ anchors.add(element);
+ return () => {
+ anchors.delete(element);
+ };
+ }, [ref]);
+}
+
+export function getToastAnchorBottom(): number | null {
+ let bottom: number | null = null;
+ for (const element of anchors) {
+ if (!element.isConnected) continue;
+ const rect = element.getBoundingClientRect();
+ if (rect.height === 0) continue;
+ bottom = Math.max(bottom ?? 0, rect.bottom);
+ }
+ return bottom;
+}
diff --git a/fluxer_app/src/features/ui/toast/Toasts.module.css b/fluxer_app/src/features/ui/toast/Toasts.module.css
index 2490a9ec9..dcf538221 100644
--- a/fluxer_app/src/features/ui/toast/Toasts.module.css
+++ b/fluxer_app/src/features/ui/toast/Toasts.module.css
@@ -7,25 +7,31 @@
}
.containerMobile {
- right: 1rem;
- left: 1rem;
- top: calc(1rem + env(safe-area-inset-top, 0px));
+ inset-inline-end: 1rem;
+ inset-inline-start: 1rem;
+ top: max(calc(1rem + env(safe-area-inset-top, 0px)), calc(var(--toast-anchor-bottom, 0px) + 0.75rem));
display: flex;
justify-content: center;
}
.containerDesktop {
transform: translateX(-50%);
- top: calc(1.5rem + env(safe-area-inset-top, 0px));
+ top: max(calc(1.5rem + env(safe-area-inset-top, 0px)), calc(var(--toast-anchor-bottom, 0px) + 0.75rem));
left: 50%;
}
:global(html.platform-native #toast-portal-root) .containerDesktop {
- top: calc(1.5rem + env(safe-area-inset-top, 0px) + var(--native-titlebar-height, 0px));
+ top: max(
+ calc(1.5rem + env(safe-area-inset-top, 0px) + var(--native-titlebar-height, 0px)),
+ calc(var(--toast-anchor-bottom, 0px) + 0.75rem)
+ );
}
:global(html.platform-native #toast-portal-root) .containerMobile {
- top: calc(1rem + env(safe-area-inset-top, 0px) + var(--native-titlebar-height, 0px));
+ top: max(
+ calc(1rem + env(safe-area-inset-top, 0px) + var(--native-titlebar-height, 0px)),
+ calc(var(--toast-anchor-bottom, 0px) + 0.75rem)
+ );
}
.toastWrapper {
diff --git a/fluxer_app/src/features/ui/toast/Toasts.tsx b/fluxer_app/src/features/ui/toast/Toasts.tsx
index eb9663d7a..e011d84c0 100644
--- a/fluxer_app/src/features/ui/toast/Toasts.tsx
+++ b/fluxer_app/src/features/ui/toast/Toasts.tsx
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import * as ToastCommands from '@app/features/ui/commands/ToastCommands';
-import {usePortalHost} from '@app/features/ui/overlay/PortalHostContext';
+import {scopePortalHostToDocument, usePortalHost} from '@app/features/ui/overlay/PortalHostContext';
import ToastState from '@app/features/ui/state/Toast';
import {Toast} from '@app/features/ui/toast/Toast';
+import {getToastAnchorBottom} from '@app/features/ui/toast/ToastAnchor';
import styles from '@app/features/ui/toast/Toasts.module.css';
import {isMobileExperienceEnabled} from '@app/features/ui/utils/MobileExperience';
import {clsx} from 'clsx';
@@ -12,41 +13,44 @@ import {observer} from 'mobx-react-lite';
import {useCallback, useEffect, useState} from 'react';
import {createPortal} from 'react-dom';
-export const Toasts: React.FC = observer(() => {
+interface ToastsProps {
+ ownerDocument?: Document;
+}
+
+export const Toasts: React.FC<ToastsProps> = observer(({ownerDocument = document}) => {
const [bodyPortalRoot, setBodyPortalRoot] = useState<HTMLElement | null>(null);
- const overrideHost = usePortalHost();
+ const overrideHost = scopePortalHostToDocument(usePortalHost(), ownerDocument);
useEffect(() => {
- const container = document.createElement('div');
+ const container = ownerDocument.createElement('div');
container.id = 'toast-portal-root';
- document.body.appendChild(container);
+ ownerDocument.body.appendChild(container);
setBodyPortalRoot(container);
return () => {
- document.body.removeChild(container);
+ ownerDocument.body.removeChild(container);
};
- }, []);
+ }, [ownerDocument]);
const isMobileExperience = isMobileExperienceEnabled();
const closeToast = useCallback((id: string) => {
ToastCommands.destroyToast(id);
}, []);
const portalRoot = overrideHost ?? bodyPortalRoot;
if (!portalRoot) return null;
+ const currentToast = ToastState.currentToast?.ownerDocument === ownerDocument ? ToastState.currentToast : null;
+ const anchorBottom = currentToast ? getToastAnchorBottom() : null;
return createPortal(
<div
className={clsx(styles.container, isMobileExperience ? styles.containerMobile : styles.containerDesktop)}
+ style={anchorBottom == null ? undefined : ({'--toast-anchor-bottom': `${anchorBottom}px`} as React.CSSProperties)}
data-flx="ui.toast.toasts.container"
>
<AnimatePresence mode="wait" data-flx="ui.toast.toasts.animate-presence">
- {ToastState.currentToast && (
- <div
- key={ToastState.currentToast.id}
- className={styles.toastWrapper}
- data-flx="ui.toast.toasts.toast-wrapper"
- >
+ {currentToast && (
+ <div key={currentToast.id} className={styles.toastWrapper} data-flx="ui.toast.toasts.toast-wrapper">
<Toast
- id={ToastState.currentToast.id}
+ id={currentToast.id}
closeToast={closeToast}
data-flx="ui.toast.toasts.toast"
- {...ToastState.currentToast.data}
+ {...currentToast.data}
/>
</div>
)}