From 5ff0edecb8daaee9c934cacc003d0ba8dd36e7b7 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:16:24 +1000 Subject: [PATCH] fix(ui): watch the toast portal instead of the whole page (#60537) --- packages/app/component-tests/toast.spec.ts | 23 ++++++++++++ packages/ui/src/feedback/toast/toast.tsx | 42 ++++++---------------- 2 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 packages/app/component-tests/toast.spec.ts diff --git a/packages/app/component-tests/toast.spec.ts b/packages/app/component-tests/toast.spec.ts new file mode 100644 index 00000000000..e4681e2ac8c --- /dev/null +++ b/packages/app/component-tests/toast.spec.ts @@ -0,0 +1,23 @@ +import { expect, sourceURL, story } from "../../stories/playwright/story" + +const toastModule = sourceURL(new URL("../../ui/src/feedback/toast/toast.tsx", import.meta.url)) + +story.beforeEach(async ({ mount }) => { + await mount("ui-toast--all-examples") +}) + +story("keeps toasts hidden behind the stack out of reach of the keyboard", async ({ page }) => { + await page.evaluate(async (toastModule) => { + const { showToast } = await import(toastModule) + const titles = ["First", "Second", "Third", "Fourth"] + + titles.forEach((title) => showToast({ title, persistent: true })) + }, toastModule) + await expect(page.locator(".toast-v2-region .toast-v2")).toHaveCount(4) + + const hidden = page.locator('.toast-v2-region .toast-v2[data-visible="false"]') + await expect(hidden).toHaveCount(1) + await expect(hidden).toHaveJSProperty("inert", true) + await expect(hidden).toHaveAttribute("tabindex", "-1") + await expect(page.locator('.toast-v2-region .toast-v2[data-visible="true"][tabindex="0"]')).toHaveCount(3) +}) diff --git a/packages/ui/src/feedback/toast/toast.tsx b/packages/ui/src/feedback/toast/toast.tsx index e794b61a453..5ef9650f7eb 100644 --- a/packages/ui/src/feedback/toast/toast.tsx +++ b/packages/ui/src/feedback/toast/toast.tsx @@ -2,7 +2,7 @@ import { Toaster, toast, type ToasterProps } from "solid-sonner" import { boundToastDescription } from "./description" import { isRTL } from "@kobalte/core/i18n" import type { ComponentProps, JSX } from "solid-js" -import { createContext, onCleanup, onMount, splitProps, useContext } from "solid-js" +import { createContext, onCleanup, splitProps, useContext } from "solid-js" import { Portal } from "solid-js/web" import { useI18n } from "../../context/i18n" import "../../actions/button/button.css" @@ -14,46 +14,26 @@ export interface ToastRegionProps extends ToasterProps {} function ToastRegion(props: ToastRegionProps) { const i18n = useI18n() const [local, rest] = splitProps(props, ["class", "className", "style", "toastOptions", "swipeDirections"]) - onMount(() => { + + // The Portal's container holds the toast region whenever it exists. Watching the container, not the whole body, + // keeps every DOM change elsewhere in the page (each streamed delta, each mounted row) from scanning the document. + const observe = (container: HTMLDivElement) => { const sync = () => { - document.querySelectorAll(".toast-v2-region .toast-v2").forEach((element) => { + container.querySelectorAll(".toast-v2-region .toast-v2").forEach((element) => { const hidden = element.dataset.visible === "false" element.inert = hidden element.tabIndex = hidden ? -1 : 0 }) } - let connected = false - - const connect = () => { - const regions = document.querySelectorAll(".toast-v2-region") - - if (!regions.length) return - observer.disconnect() - regions.forEach((region) => { - observer.observe(region, { - subtree: true, - childList: true, - attributes: true, - attributeFilter: ["data-visible"], - }) - }) - connected = true - sync() - } - - const observer = new MutationObserver(() => { - if (!connected) connect() - else sync() - }) - - observer.observe(document.body, { subtree: true, childList: true }) - queueMicrotask(connect) + const observer = new MutationObserver(sync) + observer.observe(container, { subtree: true, childList: true, attributes: true, attributeFilter: ["data-visible"] }) + sync() onCleanup(() => observer.disconnect()) - }) + } return ( - +