mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(ui): watch the toast portal instead of the whole page (#60537)
This commit is contained in:
committed by
GitHub
Unverified
parent
ca68fde51c
commit
5ff0edecb8
@@ -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)
|
||||
})
|
||||
@@ -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<HTMLElement>(".toast-v2-region .toast-v2").forEach((element) => {
|
||||
container.querySelectorAll<HTMLElement>(".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 (
|
||||
<Portal>
|
||||
<Portal ref={observe}>
|
||||
<Toaster
|
||||
position={isRTL(i18n.locale()) ? "bottom-left" : "bottom-right"}
|
||||
offset={isRTL(i18n.locale()) ? { left: 32, bottom: 48 } : { right: 32, bottom: 48 }}
|
||||
|
||||
Reference in New Issue
Block a user