chore: consolidate platform split checks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState, type ReactNode } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { X } from "lucide-react";
|
||||
import { translateReactNode, usePlatformLanguage } from "../i18n/LanguageContext";
|
||||
|
||||
type AlertTone = "success" | "info" | "warning" | "danger";
|
||||
|
||||
@@ -17,20 +18,24 @@ type DismissibleAlertProps = {
|
||||
|
||||
let floatingAlertRoot: HTMLElement | null = null;
|
||||
|
||||
function getFloatingAlertRoot(): HTMLElement | null {
|
||||
function getFloatingAlertRoot(ariaLabel: string): HTMLElement | null {
|
||||
if (typeof document === "undefined") return null;
|
||||
if (floatingAlertRoot?.isConnected) return floatingAlertRoot;
|
||||
if (floatingAlertRoot?.isConnected) {
|
||||
floatingAlertRoot.setAttribute("aria-label", ariaLabel);
|
||||
return floatingAlertRoot;
|
||||
}
|
||||
|
||||
const existing = document.getElementById("app-floating-alerts");
|
||||
if (existing) {
|
||||
floatingAlertRoot = existing;
|
||||
floatingAlertRoot.setAttribute("aria-label", ariaLabel);
|
||||
return floatingAlertRoot;
|
||||
}
|
||||
|
||||
floatingAlertRoot = document.createElement("div");
|
||||
floatingAlertRoot.id = "app-floating-alerts";
|
||||
floatingAlertRoot.className = "alert-floating-stack";
|
||||
floatingAlertRoot.setAttribute("aria-label", "Application notices");
|
||||
floatingAlertRoot.setAttribute("aria-label", ariaLabel);
|
||||
document.body.appendChild(floatingAlertRoot);
|
||||
return floatingAlertRoot;
|
||||
}
|
||||
@@ -68,6 +73,7 @@ export default function DismissibleAlert({
|
||||
resetKey,
|
||||
dismissStorageKey
|
||||
}: DismissibleAlertProps) {
|
||||
const { translateText } = usePlatformLanguage();
|
||||
const storageKey = resolveDismissStorageKey(dismissStorageKey, resetKey);
|
||||
const [alertState, setAlertState] = useState(() => ({ storageKey, visible: !readDismissed(storageKey) }));
|
||||
const visible = alertState.storageKey === storageKey ? alertState.visible : !readDismissed(storageKey);
|
||||
@@ -77,6 +83,8 @@ export default function DismissibleAlert({
|
||||
}, [storageKey, resetKey, children]);
|
||||
|
||||
if (!visible) return null;
|
||||
const renderedChildren = translateReactNode(children, translateText);
|
||||
const floatingLabel = translateText("i18n:govoplan-core.application_notices");
|
||||
|
||||
function dismiss() {
|
||||
writeDismissed(storageKey);
|
||||
@@ -90,9 +98,9 @@ export default function DismissibleAlert({
|
||||
role={role}
|
||||
aria-live={role === "alert" ? "assertive" : "polite"}
|
||||
>
|
||||
<div className="alert-message">{children}</div>
|
||||
<div className="alert-message">{renderedChildren}</div>
|
||||
{dismissible && (
|
||||
<button type="button" className="alert-dismiss" aria-label="Dismiss notice" onClick={dismiss}>
|
||||
<button type="button" className="alert-dismiss" aria-label={translateText("i18n:govoplan-core.dismiss_notice.ce08c9ad")} onClick={dismiss}>
|
||||
<X size={16} strokeWidth={2.4} aria-hidden="true" />
|
||||
</button>
|
||||
)}
|
||||
@@ -100,6 +108,6 @@ export default function DismissibleAlert({
|
||||
);
|
||||
|
||||
if (!floating) return alert;
|
||||
const root = getFloatingAlertRoot();
|
||||
const root = getFloatingAlertRoot(floatingLabel);
|
||||
return root ? createPortal(alert, root) : alert;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user