fix(ui): keep selects, menus, tooltips and toasts reachable from a dialog (#62913)

This commit is contained in:
Ludvig Rask
2026-11-09 11:11:05 -06:00
committed by GitHub
Unverified
parent 7a2e04b37d
commit d592629d1c
6 changed files with 125 additions and 2 deletions
@@ -0,0 +1,49 @@
import type { Page } from "@playwright/test"
import { expect, story } from "../../stories/playwright/story"
// Role queries skip aria-hidden content, so each popup is only found if assistive tech can reach it too. A modal dialog
// hides content added outside it a task and a frame after it mounts, so each check waits that long before looking.
const settled = (page: Page) =>
page.evaluate(
() => new Promise((resolve) => setTimeout(() => requestAnimationFrame(() => requestAnimationFrame(resolve)))),
)
story.beforeEach(async ({ mount, page }) => {
await mount("ui-dialog--with-popups")
await expect(page.getByRole("dialog", { name: "Popups" })).toBeVisible()
})
story("a select opened from a dialog lists its options", async ({ page }) => {
const trigger = page.getByRole("dialog").locator('[data-component="select-v2"]')
await trigger.click()
await expect(page.locator('[data-slot="select-v2-listbox"]')).toBeVisible()
await settled(page)
await expect(page.getByRole("option")).toHaveCount(3)
await page.getByRole("option", { name: "Banana" }).click()
await expect(trigger).toContainText("Banana")
})
story("a non-modal menu and its submenu opened from a dialog list their items", async ({ page }) => {
await page.getByRole("dialog").getByRole("button", { name: "Actions" }).click()
await expect(page.getByText("Rename", { exact: true })).toBeVisible()
await settled(page)
await expect(page.getByRole("menuitem", { name: "Rename" })).toBeVisible()
await page.getByRole("menuitem", { name: "Move to" }).hover()
await expect(page.getByText("Archive", { exact: true })).toBeVisible()
await settled(page)
await expect(page.getByRole("menuitem", { name: "Archive" })).toBeVisible()
})
story("a tooltip opened from a dialog is announced", async ({ page }) => {
await page.getByRole("dialog").getByRole("button", { name: "Copy" }).hover()
await expect(page.getByText("Copy the link")).toBeVisible()
await settled(page)
await expect(page.getByRole("tooltip", { name: "Copy the link" })).toBeVisible()
})
story("a toast shown from a dialog is announced", async ({ page }) => {
await page.getByRole("dialog").getByRole("button", { name: "Save" }).click()
await expect(page.getByText("Saved", { exact: true })).toBeVisible()
await settled(page)
await expect(page.getByRole("listitem").filter({ hasText: "Saved" })).toBeVisible()
})
+4
View File
@@ -25,6 +25,10 @@ function ToastRegion(props: ToastRegionProps) {
// 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) => {
// A modal dialog hides what's outside it from assistive tech. The region is in the Portal's own div, so that div,
// rather than the toaster, is what the dialog would hide.
container.setAttribute("data-kb-top-layer", "")
const sync = () => {
container.querySelectorAll<HTMLElement>(".toast-v2-region .toast-v2").forEach((element) => {
const hidden = element.dataset.visible === "false"
+7 -1
View File
@@ -255,7 +255,13 @@ export function Select<T>(props: SelectProps<T>) {
</span>
</Trigger>
<Portal>
<Content class={local.contentClass} data-component="menu-v2-content" data-slot="select-v2-content">
{/* A modal dialog hides what's outside it from assistive tech, and this list is portaled outside it. */}
<Content
class={local.contentClass}
data-kb-top-layer=""
data-component="menu-v2-content"
data-slot="select-v2-content"
>
<MenuScroll viewportRef={(element) => (scroller = element)}>
<Listbox data-slot="select-v2-listbox" scrollRef={() => scroller} />
</MenuScroll>
+3
View File
@@ -283,6 +283,7 @@ function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
return (
<DropdownMenu.SubContent
{...r}
data-kb-top-layer=""
data-component="menu-v2-content"
data-appearance={ctx.appearance()}
classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -360,6 +361,8 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
{...r}
onCloseAutoFocus={onCloseAutoFocus}
onInteractOutside={onInteractOutside}
// A modal dialog hides what's outside it from assistive tech, and a non-modal menu is portaled outside it.
data-kb-top-layer=""
data-component="menu-v2-content"
data-appearance={ctx.appearance()}
classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -2,7 +2,11 @@ import { Overlay, Portal, Root, Trigger } from "@kobalte/core/dialog"
import { Dialog, DialogBody, DialogFooter, DialogHeader, DialogTitle, DialogTitleGroup } from "./dialog"
import { Button } from "@opencode/ui/button"
import { useDialog, useDialogLayer } from "@opencode/ui/context/dialog"
import { onMount, type JSX, type ParentProps } from "solid-js"
import { Menu } from "@opencode/ui/menu"
import { Select } from "@opencode/ui/select"
import { Toast, showToast } from "@opencode/ui/toast"
import { Tooltip } from "@opencode/ui/tooltip"
import { createSignal, onMount, type JSX, type ParentProps } from "solid-js"
const docs = `### Overview
Dialog content wrapper built on Kobalte's dialog primitive with v2 styling.
@@ -283,3 +287,58 @@ function StackedDialogs() {
export const Stacked = {
render: () => <StackedDialogs />,
}
// Each of these portals its content outside the dialog, which a modal dialog hides from assistive tech.
function PopupsDialog() {
const [fruit, setFruit] = createSignal<string>()
return (
<Dialog>
{dialogHeader("Popups", "A select, a menu, a tooltip and a toast opened from a dialog")}
<DialogBody>
<div style={{ display: "flex", gap: "8px", "align-items": "center" }}>
<Select
appearance="base"
placeholder="Pick a fruit"
options={["Apple", "Banana", "Cherry"]}
current={fruit()}
onSelect={(value) => setFruit(value ?? undefined)}
/>
<Menu gutter={6} modal={false}>
<Menu.Trigger as={Button} variant="neutral">
Actions
</Menu.Trigger>
<Menu.Portal>
<Menu.Content>
<Menu.Item>Rename</Menu.Item>
<Menu.Sub>
<Menu.SubTrigger>Move to</Menu.SubTrigger>
<Menu.Portal>
<Menu.SubContent>
<Menu.Item>Archive</Menu.Item>
</Menu.SubContent>
</Menu.Portal>
</Menu.Sub>
</Menu.Content>
</Menu.Portal>
</Menu>
<Tooltip value="Copy the link">
<Button variant="neutral">Copy</Button>
</Tooltip>
<Button variant="neutral" onClick={() => showToast({ title: "Saved", persistent: true })}>
Save
</Button>
</div>
</DialogBody>
</Dialog>
)
}
export const WithPopups = {
render: () => (
<>
<Toast.Region />
<OpenStoryDialog render={() => <PopupsDialog />} />
</>
),
}
@@ -214,6 +214,8 @@ function ActiveTooltip(props: TooltipProps) {
/>
<Portal>
<Content
// A modal dialog hides what's outside it from assistive tech, and this tip is portaled outside it.
data-kb-top-layer=""
ref={(el) => {
const theme = ref?.closest("[data-theme]")?.getAttribute("data-theme")